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