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