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