]> git.mxchange.org Git - flightgear.git/blob - Astro/sky.cxx
Edited cvs log messages in source files ... bad bad bad!
[flightgear.git] / Astro / sky.cxx
1 /**************************************************************************
2  * sky.c -- model sky with an upside down "bowl"
3  *
4  * Written by Curtis Olson, started December 1997.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #ifdef HAVE_WINDOWS_H
32 #  include <windows.h>
33 #endif
34
35 #include <math.h>
36
37 #include <GL/glut.h>
38 #include <XGL/xgl.h>
39
40 #include <Aircraft/aircraft.h>
41 #include <Debug/fg_debug.h>
42 #include <Flight/flight.h>
43 #include <Include/fg_constants.h>
44 #include <Main/views.hxx>
45 #include <Math/fg_random.h>
46 #include <Time/event.hxx>
47 #include <Time/fg_time.hxx>
48
49 #include "sky.hxx"
50
51
52 /*
53 #include <Include/general.h>
54 */
55
56 /* in meters of course */
57 #define CENTER_ELEV   25000.0
58
59 #define INNER_RADIUS  50000.0
60 #define INNER_ELEV    20000.0
61
62 #define MIDDLE_RADIUS 70000.0
63 #define MIDDLE_ELEV    8000.0
64
65 #define OUTER_RADIUS  80000.0
66 #define OUTER_ELEV        0.0
67
68 #define BOTTOM_RADIUS 50000.0
69 #define BOTTOM_ELEV   -2000.0
70
71
72 static float inner_vertex[12][3];
73 static float middle_vertex[12][3];
74 static float outer_vertex[12][3];
75 static float bottom_vertex[12][3];
76
77 static float inner_color[12][4];
78 static float middle_color[12][4];
79 static float outer_color[12][4];
80
81
82 /* Calculate the sky structure vertices */
83 void fgSkyVerticesInit( void ) {
84     float theta;
85     int i;
86
87     fgPrintf(FG_ASTRO, FG_INFO, "  Generating the sky dome vertices.\n");
88
89     for ( i = 0; i < 12; i++ ) {
90         theta = (i * 30.0) * DEG_TO_RAD;
91         
92         inner_vertex[i][0] = cos(theta) * INNER_RADIUS;
93         inner_vertex[i][1] = sin(theta) * INNER_RADIUS;
94         inner_vertex[i][2] = INNER_ELEV;
95         
96         /* printf("    %.2f %.2f\n", cos(theta) * INNER_RADIUS, 
97                sin(theta) * INNER_RADIUS); */
98
99         middle_vertex[i][0] = cos((double)theta) * MIDDLE_RADIUS;
100         middle_vertex[i][1] = sin((double)theta) * MIDDLE_RADIUS;
101         middle_vertex[i][2] = MIDDLE_ELEV;
102             
103         outer_vertex[i][0] = cos((double)theta) * OUTER_RADIUS;
104         outer_vertex[i][1] = sin((double)theta) * OUTER_RADIUS;
105         outer_vertex[i][2] = OUTER_ELEV;
106             
107         bottom_vertex[i][0] = cos((double)theta) * BOTTOM_RADIUS;
108         bottom_vertex[i][1] = sin((double)theta) * BOTTOM_RADIUS;
109         bottom_vertex[i][2] = BOTTOM_ELEV;
110     }
111 }
112
113
114 /* (Re)calculate the sky colors at each vertex */
115 void fgSkyColorsInit( void ) {
116     struct fgLIGHT *l;
117     float sun_angle, diff;
118     float outer_param[3], outer_amt[3], outer_diff[3];
119     float middle_param[3], middle_amt[3], middle_diff[3];
120     int i, j;
121
122     l = &cur_light_params;
123
124     fgPrintf( FG_ASTRO, FG_INFO, 
125               "  Generating the sky colors for each vertex.\n" );
126
127     /* setup for the possibility of sunset effects */
128     sun_angle = l->sun_angle * RAD_TO_DEG;
129     fgPrintf( FG_ASTRO, FG_INFO, "  Sun angle in degrees = %.2f\n", sun_angle);
130
131     if ( (sun_angle > 80.0) && (sun_angle < 100.0) ) {
132         /* 0.0 - 0.4 */
133         outer_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 25.0;
134         outer_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 35.0;
135         outer_param[2] = 0.0;
136
137         middle_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 20.0;
138         middle_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 40.0;
139         middle_param[2] = 0.0;
140
141         outer_diff[0] = outer_param[0] / 6.0;
142         outer_diff[1] = outer_param[1] / 6.0;
143         outer_diff[2] = outer_param[2] / 6.0;
144
145         middle_diff[0] = middle_param[0] / 6.0;
146         middle_diff[1] = middle_param[1] / 6.0;
147         middle_diff[2] = middle_param[2] / 6.0;
148     } else {
149         outer_param[0] = outer_param[1] = outer_param[2] = 0.0;
150         middle_param[0] = middle_param[1] = middle_param[2] = 0.0;
151
152         outer_diff[0] = outer_diff[1] = outer_diff[2] = 0.0;
153         middle_diff[0] = middle_diff[1] = middle_diff[2] = 0.0;
154     }
155     /* printf("  outer_red_param = %.2f  outer_red_diff = %.2f\n", 
156            outer_red_param, outer_red_diff); */
157
158     /* calculate transition colors between sky and fog */
159     for ( j = 0; j < 3; j++ ) {
160         outer_amt[j] = outer_param[j];
161         middle_amt[j] = middle_param[j];
162     }
163
164     for ( i = 0; i < 6; i++ ) {
165         for ( j = 0; j < 3; j++ ) {
166             diff = l->sky_color[j] - l->fog_color[j];
167
168             /* printf("sky = %.2f  fog = %.2f  diff = %.2f\n", 
169                    l->sky_color[j], l->fog_color[j], diff); */
170
171             inner_color[i][j] = l->sky_color[j] - diff * 0.3;
172             middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j];
173             outer_color[i][j] = l->fog_color[j] + outer_amt[j];
174
175             if ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
176             if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
177             if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
178             if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
179             if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
180             if ( outer_color[i][j] < 0.10 ) { outer_color[i][j] = 0.10; }
181         }
182         inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = 
183             l->sky_color[3];
184
185         for ( j = 0; j < 3; j++ ) {
186             outer_amt[j] -= outer_diff[j];
187             middle_amt[j] -= middle_diff[j];
188         }
189
190         /*
191         printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
192                inner_color[i][1], inner_color[i][2], inner_color[i][3]);
193         printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
194                middle_color[i][0], middle_color[i][1], middle_color[i][2], 
195                middle_color[i][3]);
196         printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
197                outer_color[i][0], outer_color[i][1], outer_color[i][2], 
198                outer_color[i][3]);
199         */
200     }
201
202     for ( j = 0; j < 3; j++ ) {
203         outer_amt[j] = 0.0;
204         middle_amt[j] = 0.0;
205     }
206
207     for ( i = 6; i < 12; i++ ) {
208
209         for ( j = 0; j < 3; j++ ) {
210             diff = l->sky_color[j] - l->fog_color[j];
211
212             /* printf("sky = %.2f  fog = %.2f  diff = %.2f\n", 
213                    l->sky_color[j], l->fog_color[j], diff); */
214
215             inner_color[i][j] = l->sky_color[j] - diff * 0.3;
216             middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j];
217             outer_color[i][j] = l->fog_color[j] + outer_amt[j];
218
219             if ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
220             if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
221             if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
222             if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
223             if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
224             if ( outer_color[i][j] < 0.15 ) { outer_color[i][j] = 0.15; }
225         }
226         inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = 
227             l->sky_color[3];
228
229         for ( j = 0; j < 3; j++ ) {
230             outer_amt[j] += outer_diff[j];
231             middle_amt[j] += middle_diff[j];
232         }
233
234         /*
235         printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
236                inner_color[i][1], inner_color[i][2], inner_color[i][3]);
237         printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
238                middle_color[i][0], middle_color[i][1], middle_color[i][2], 
239                middle_color[i][3]);
240         printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
241                outer_color[i][0], outer_color[i][1], outer_color[i][2], 
242                outer_color[i][3]);
243         */
244     }
245 }
246
247
248 /* Initialize the sky structure and colors */
249 void fgSkyInit( void ) {
250     fgPrintf(FG_ASTRO, FG_INFO, "Initializing the sky\n");
251
252     fgSkyVerticesInit();
253
254     /* regester fgSkyColorsInit() as an event to be run periodically */
255     fgEventRegister("fgSkyColorsInit()", fgSkyColorsInit, 
256                     FG_EVENT_READY, 30000);
257 }
258
259
260 /* Draw the Sky */
261 void fgSkyRender( void ) {
262     fgFLIGHT *f;
263     struct fgLIGHT *l;
264     struct fgVIEW *v;
265     float /* inner_color[4], middle_color[4], diff, */ east_dot, dot, angle;
266     int i;
267
268     f = current_aircraft.flight;
269     l = &cur_light_params;
270     v = &current_view;
271
272     /* printf("Rendering the sky.\n"); */
273
274     xglPushMatrix();
275
276     /* calculate the angle between v->surface_to_sun and
277      * v->surface_east.  We do this so we can sort out the acos()
278      * ambiguity.  I wish I could think of a more efficient way ... :-( */
279     east_dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_east);
280     /* printf("  East dot product = %.2f\n", east_dot); */
281
282     /* calculate the angle between v->surface_to_sun and
283      * v->surface_south.  this is how much we have to rotate the sky
284      * for it to align with the sun */
285     dot = MAT3_DOT_PRODUCT(v->surface_to_sun, v->surface_south);
286     /* printf("  Dot product = %.2f\n", dot); */
287     if ( east_dot >= 0 ) {
288         angle = acos(dot);
289     } else {
290         angle = -acos(dot);
291     }
292     /* printf("  Sky needs to rotate = %.3f rads = %.1f degrees.\n", 
293            angle, angle * RAD_TO_DEG); */
294
295     /* Translate to view position */
296     xglTranslatef( v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z );
297     /* printf("  Translated to %.2f %.2f %.2f\n", 
298            v->cur_zero_elev.x, v->cur_zero_elev.y, v->cur_zero_elev.z ); */
299
300     /* Rotate to proper orientation */
301     /* printf("  lon = %.2f  lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
302            FG_Latitude * RAD_TO_DEG); */
303     xglRotatef( FG_Longitude * RAD_TO_DEG, 0.0, 0.0, 1.0 );
304     xglRotatef( 90.0 - FG_Latitude * RAD_TO_DEG, 0.0, 1.0, 0.0 );
305     xglRotatef( angle * RAD_TO_DEG, 0.0, 0.0, 1.0 );
306
307     /* Draw inner/center section of sky*/
308     xglBegin( GL_TRIANGLE_FAN );
309     xglColor4fv(l->sky_color);
310     xglVertex3f(0.0, 0.0, CENTER_ELEV);
311     for ( i = 0; i < 12; i++ ) {
312         xglColor4fv( inner_color[i] );
313         xglVertex3fv( inner_vertex[i] );
314     }
315     xglColor4fv( inner_color[0] );
316     xglVertex3fv( inner_vertex[0] );
317     xglEnd();
318
319     // Draw the middle ring
320     xglBegin( GL_TRIANGLE_STRIP );
321     for ( i = 0; i < 12; i++ ) {
322         xglColor4fv( middle_color[i] );
323         /* printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
324                middle_color[i][0], middle_color[i][1], middle_color[i][2], 
325                middle_color[i][3]); */
326         // xglColor4f(1.0, 0.0, 0.0, 1.0);
327         xglVertex3fv( middle_vertex[i] );
328         xglColor4fv( inner_color[i] );
329         /* printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
330                inner_color[i][0], inner_color[i][1], inner_color[i][2], 
331                inner_color[i][3]); */
332         // xglColor4f(0.0, 0.0, 1.0, 1.0);
333         xglVertex3fv( inner_vertex[i] );
334     }
335     xglColor4fv( middle_color[0] );
336     // xglColor4f(1.0, 0.0, 0.0, 1.0);
337     xglVertex3fv( middle_vertex[0] );
338     xglColor4fv( inner_color[0] );
339     // xglColor4f(0.0, 0.0, 1.0, 1.0);
340     xglVertex3fv( inner_vertex[0] );
341     xglEnd();
342
343     /* Draw the outer ring */
344     xglBegin( GL_TRIANGLE_STRIP );
345     for ( i = 0; i < 12; i++ ) {
346         xglColor4fv( outer_color[i] );
347         xglVertex3fv( outer_vertex[i] );
348         xglColor4fv( middle_color[i] );
349         xglVertex3fv( middle_vertex[i] );
350     }
351     xglColor4fv( outer_color[0] );
352     xglVertex3fv( outer_vertex[0] );
353     xglColor4fv( middle_color[0] );
354     xglVertex3fv( middle_vertex[0] );
355     xglEnd();
356
357     /* Draw the bottom skirt */
358     xglBegin( GL_TRIANGLE_STRIP );
359     for ( i = 0; i < 12; i++ ) {
360         xglColor4fv( l->fog_color );
361         xglVertex3fv( bottom_vertex[i] );
362         xglColor4fv( outer_color[i] );
363         xglVertex3fv( outer_vertex[i] );
364     }
365     xglColor4fv( l->fog_color );
366     xglVertex3fv( bottom_vertex[0] );
367     xglColor4fv( outer_color[0] );
368     xglVertex3fv( outer_vertex[0] );
369     xglEnd();
370
371     xglPopMatrix();
372 }
373
374
375 /* $Log$
376 /* Revision 1.3  1998/04/25 22:06:25  curt
377 /* Edited cvs log messages in source files ... bad bad bad!
378 /*
379  * Revision 1.2  1998/04/24 00:45:03  curt
380  * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
381  * Fixed a bug when generating sky colors.
382  *
383  * Revision 1.1  1998/04/22 13:21:32  curt
384  * C++ - ifing the code a bit.
385  *
386  * Revision 1.9  1998/04/03 21:52:50  curt
387  * Converting to Gnu autoconf system.
388  *
389  * Revision 1.8  1998/03/09 22:47:25  curt
390  * Incorporated Durk's updates.
391  *
392  * Revision 1.7  1998/02/19 13:05:49  curt
393  * Incorporated some HUD tweaks from Michelle America.
394  * Tweaked the sky's sunset/rise colors.
395  * Other misc. tweaks.
396  *
397  * Revision 1.6  1998/02/07 15:29:32  curt
398  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
399  * <chotchkiss@namg.us.anritsu.com>
400  *
401  * Revision 1.5  1998/01/27 00:47:48  curt
402  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
403  * system and commandline/config file processing code.
404  *
405  * Revision 1.4  1998/01/26 15:54:28  curt
406  * Added a "skirt" to try to help hide gaps between scenery and sky.  This will
407  * have to be revisited in the future.
408  *
409  * Revision 1.3  1998/01/19 19:26:59  curt
410  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
411  * This should simplify things tremendously.
412  *
413  * Revision 1.2  1998/01/19 18:40:17  curt
414  * Tons of little changes to clean up the code and to remove fatal errors
415  * when building with the c++ compiler.
416  *
417  * Revision 1.1  1998/01/07 03:16:19  curt
418  * Moved from .../Src/Scenery/ to .../Src/Astro/
419  *
420  * Revision 1.11  1997/12/30 22:22:38  curt
421  * Further integration of event manager.
422  *
423  * Revision 1.10  1997/12/30 20:47:53  curt
424  * Integrated new event manager with subsystem initializations.
425  *
426  * Revision 1.9  1997/12/30 13:06:57  curt
427  * A couple lighting tweaks ...
428  *
429  * Revision 1.8  1997/12/23 04:58:38  curt
430  * Tweaked the sky coloring a bit to build in structures to allow finer rgb
431  * control.
432  *
433  * Revision 1.7  1997/12/22 23:45:48  curt
434  * First stab at sunset/sunrise sky glow effects.
435  *
436  * Revision 1.6  1997/12/22 04:14:34  curt
437  * Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
438  *
439  * Revision 1.5  1997/12/19 23:34:59  curt
440  * Lot's of tweaking with sky rendering and lighting.
441  *
442  * Revision 1.4  1997/12/19 16:45:02  curt
443  * Working on scene rendering order and options.
444  *
445  * Revision 1.3  1997/12/18 23:32:36  curt
446  * First stab at sky dome actually starting to look reasonable. :-)
447  *
448  * Revision 1.2  1997/12/18 04:07:03  curt
449  * Worked on properly translating and positioning the sky dome.
450  *
451  * Revision 1.1  1997/12/17 23:14:30  curt
452  * Initial revision.
453  * Begin work on rendering the sky. (Rather than just using a clear screen.)
454  *
455  */