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