1 /**************************************************************************
2 * sky.c -- model sky with an upside down "bowl"
4 * Written by Curtis Olson, started December 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
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>
53 #include <Include/general.h>
56 /* in meters of course */
57 #define CENTER_ELEV 25000.0
59 #define INNER_RADIUS 50000.0
60 #define INNER_ELEV 20000.0
62 #define MIDDLE_RADIUS 70000.0
63 #define MIDDLE_ELEV 8000.0
65 #define OUTER_RADIUS 80000.0
66 #define OUTER_ELEV 0.0
68 #define BOTTOM_RADIUS 50000.0
69 #define BOTTOM_ELEV -2000.0
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];
77 static float inner_color[12][4];
78 static float middle_color[12][4];
79 static float outer_color[12][4];
82 /* Calculate the sky structure vertices */
83 void fgSkyVerticesInit( void ) {
87 fgPrintf(FG_ASTRO, FG_INFO, " Generating the sky dome vertices.\n");
89 for ( i = 0; i < 12; i++ ) {
90 theta = (i * 30.0) * DEG_TO_RAD;
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;
96 /* printf(" %.2f %.2f\n", cos(theta) * INNER_RADIUS,
97 sin(theta) * INNER_RADIUS); */
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;
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;
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;
114 /* (Re)calculate the sky colors at each vertex */
115 void fgSkyColorsInit( void ) {
117 double sun_angle, diff;
118 double outer_param[3], outer_amt[3], outer_diff[3];
119 double middle_param[3], middle_amt[3], middle_diff[3];
122 l = &cur_light_params;
124 fgPrintf( FG_ASTRO, FG_INFO,
125 " Generating the sky colors for each vertex.\n" );
127 /* setup for the possibility of sunset effects */
128 sun_angle = l->sun_angle * RAD_TO_DEG;
129 // fgPrintf( FG_ASTRO, FG_INFO,
130 // " Sun angle in degrees = %.2f\n", sun_angle);
132 if ( (sun_angle > 80.0) && (sun_angle < 100.0) ) {
134 outer_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 25.0;
135 outer_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 35.0;
136 outer_param[2] = 0.0;
138 middle_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 20.0;
139 middle_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 40.0;
140 middle_param[2] = 0.0;
142 outer_diff[0] = outer_param[0] / 6.0;
143 outer_diff[1] = outer_param[1] / 6.0;
144 outer_diff[2] = outer_param[2] / 6.0;
146 middle_diff[0] = middle_param[0] / 6.0;
147 middle_diff[1] = middle_param[1] / 6.0;
148 middle_diff[2] = middle_param[2] / 6.0;
150 outer_param[0] = outer_param[1] = outer_param[2] = 0.0;
151 middle_param[0] = middle_param[1] = middle_param[2] = 0.0;
153 outer_diff[0] = outer_diff[1] = outer_diff[2] = 0.0;
154 middle_diff[0] = middle_diff[1] = middle_diff[2] = 0.0;
156 /* printf(" outer_red_param = %.2f outer_red_diff = %.2f\n",
157 outer_red_param, outer_red_diff); */
159 /* calculate transition colors between sky and fog */
160 for ( j = 0; j < 3; j++ ) {
161 outer_amt[j] = outer_param[j];
162 middle_amt[j] = middle_param[j];
165 for ( i = 0; i < 6; i++ ) {
166 for ( j = 0; j < 3; j++ ) {
167 diff = l->sky_color[j] - l->fog_color[j];
169 /* printf("sky = %.2f fog = %.2f diff = %.2f\n",
170 l->sky_color[j], l->fog_color[j], diff); */
172 inner_color[i][j] = l->sky_color[j] - diff * 0.3;
173 middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j];
174 outer_color[i][j] = l->fog_color[j] + outer_amt[j];
176 if ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
177 if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
178 if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
179 if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
180 if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
181 if ( outer_color[i][j] < 0.10 ) { outer_color[i][j] = 0.10; }
183 inner_color[i][3] = middle_color[i][3] = outer_color[i][3] =
186 for ( j = 0; j < 3; j++ ) {
187 outer_amt[j] -= outer_diff[j];
188 middle_amt[j] -= middle_diff[j];
192 printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
193 inner_color[i][1], inner_color[i][2], inner_color[i][3]);
194 printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i,
195 middle_color[i][0], middle_color[i][1], middle_color[i][2],
197 printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i,
198 outer_color[i][0], outer_color[i][1], outer_color[i][2],
203 for ( j = 0; j < 3; j++ ) {
208 for ( i = 6; i < 12; i++ ) {
210 for ( j = 0; j < 3; j++ ) {
211 diff = l->sky_color[j] - l->fog_color[j];
213 /* printf("sky = %.2f fog = %.2f diff = %.2f\n",
214 l->sky_color[j], l->fog_color[j], diff); */
216 inner_color[i][j] = l->sky_color[j] - diff * 0.3;
217 middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j];
218 outer_color[i][j] = l->fog_color[j] + outer_amt[j];
220 if ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
221 if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
222 if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
223 if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
224 if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
225 if ( outer_color[i][j] < 0.15 ) { outer_color[i][j] = 0.15; }
227 inner_color[i][3] = middle_color[i][3] = outer_color[i][3] =
230 for ( j = 0; j < 3; j++ ) {
231 outer_amt[j] += outer_diff[j];
232 middle_amt[j] += middle_diff[j];
236 printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
237 inner_color[i][1], inner_color[i][2], inner_color[i][3]);
238 printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i,
239 middle_color[i][0], middle_color[i][1], middle_color[i][2],
241 printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i,
242 outer_color[i][0], outer_color[i][1], outer_color[i][2],
249 /* Initialize the sky structure and colors */
250 void fgSkyInit( void ) {
251 fgPrintf(FG_ASTRO, FG_INFO, "Initializing the sky\n");
255 /* regester fgSkyColorsInit() as an event to be run periodically */
256 global_events.Register( "fgSkyColorsInit()", fgSkyColorsInit,
257 fgEVENT::FG_EVENT_READY, 30000);
262 void fgSkyRender( void ) {
266 float inner_color[4];
267 float middle_color[4];
268 float outer_color[4];
272 f = current_aircraft.flight;
273 l = &cur_light_params;
276 /* printf("Rendering the sky.\n"); */
278 // calculate the proper colors
279 for ( i = 0; i < 3; i++ ) {
280 diff = l->sky_color[i] - l->adj_fog_color[i];
282 /* printf("sky = %.2f fog = %.2f diff = %.2f\n",
283 l->sky_color[j], l->adj_fog_color[j], diff); */
285 inner_color[i] = l->sky_color[i] - diff * 0.3;
286 middle_color[i] = l->sky_color[i] - diff * 0.9;
287 outer_color[i] = l->adj_fog_color[i];
289 inner_color[3] = middle_color[3] = outer_color[3] = l->adj_fog_color[3];
293 /* Translate to view position */
294 xglTranslatef( v->cur_zero_elev.x(),
295 v->cur_zero_elev.y(),
296 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 ); */
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( l->sun_rotation * RAD_TO_DEG, 0.0, 0.0, 1.0 );
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 );
313 xglVertex3fv( inner_vertex[i] );
315 xglColor4fv( inner_color );
316 xglVertex3fv( inner_vertex[0] );
319 // Draw the middle ring
320 xglBegin( GL_TRIANGLE_STRIP );
321 for ( i = 0; i < 12; i++ ) {
322 xglColor4fv( middle_color );
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 );
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] );
335 xglColor4fv( middle_color );
336 // xglColor4f(1.0, 0.0, 0.0, 1.0);
337 xglVertex3fv( middle_vertex[0] );
338 xglColor4fv( inner_color );
339 // xglColor4f(0.0, 0.0, 1.0, 1.0);
340 xglVertex3fv( inner_vertex[0] );
343 /* Draw the outer ring */
344 xglBegin( GL_TRIANGLE_STRIP );
345 for ( i = 0; i < 12; i++ ) {
346 xglColor4fv( outer_color );
347 xglVertex3fv( outer_vertex[i] );
348 xglColor4fv( middle_color );
349 xglVertex3fv( middle_vertex[i] );
351 xglColor4fv( outer_color );
352 xglVertex3fv( outer_vertex[0] );
353 xglColor4fv( middle_color );
354 xglVertex3fv( middle_vertex[0] );
357 /* Draw the bottom skirt */
358 xglBegin( GL_TRIANGLE_STRIP );
359 xglColor4fv( outer_color );
360 for ( i = 0; i < 12; i++ ) {
361 xglVertex3fv( bottom_vertex[i] );
362 xglVertex3fv( outer_vertex[i] );
364 xglVertex3fv( bottom_vertex[0] );
365 xglVertex3fv( outer_vertex[0] );
373 /* Revision 1.11 1998/10/16 00:52:19 curt
374 /* Converted to Point3D class.
376 * Revision 1.10 1998/08/29 13:07:16 curt
377 * Rewrite of event manager thanks to Bernie Bright.
379 * Revision 1.9 1998/08/22 01:18:59 curt
380 * Minor tweaks to avoid using unitialized memory.
382 * Revision 1.8 1998/08/12 21:40:44 curt
383 * Sky now tracks adjusted fog color so it blends well with terrain.
385 * Revision 1.7 1998/07/22 21:39:21 curt
386 * Lower skirt tracks adjusted fog color, not fog color.
388 * Revision 1.6 1998/05/23 14:07:14 curt
389 * Use new C++ events class.
391 * Revision 1.5 1998/04/28 01:19:02 curt
392 * Type-ified fgTIME and fgVIEW
394 * Revision 1.4 1998/04/26 05:10:01 curt
395 * "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
397 * Revision 1.3 1998/04/25 22:06:25 curt
398 * Edited cvs log messages in source files ... bad bad bad!
400 * Revision 1.2 1998/04/24 00:45:03 curt
401 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
402 * Fixed a bug when generating sky colors.
404 * Revision 1.1 1998/04/22 13:21:32 curt
405 * C++ - ifing the code a bit.
407 * Revision 1.9 1998/04/03 21:52:50 curt
408 * Converting to Gnu autoconf system.
410 * Revision 1.8 1998/03/09 22:47:25 curt
411 * Incorporated Durk's updates.
413 * Revision 1.7 1998/02/19 13:05:49 curt
414 * Incorporated some HUD tweaks from Michelle America.
415 * Tweaked the sky's sunset/rise colors.
416 * Other misc. tweaks.
418 * Revision 1.6 1998/02/07 15:29:32 curt
419 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
420 * <chotchkiss@namg.us.anritsu.com>
422 * Revision 1.5 1998/01/27 00:47:48 curt
423 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
424 * system and commandline/config file processing code.
426 * Revision 1.4 1998/01/26 15:54:28 curt
427 * Added a "skirt" to try to help hide gaps between scenery and sky. This will
428 * have to be revisited in the future.
430 * Revision 1.3 1998/01/19 19:26:59 curt
431 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
432 * This should simplify things tremendously.
434 * Revision 1.2 1998/01/19 18:40:17 curt
435 * Tons of little changes to clean up the code and to remove fatal errors
436 * when building with the c++ compiler.
438 * Revision 1.1 1998/01/07 03:16:19 curt
439 * Moved from .../Src/Scenery/ to .../Src/Astro/
441 * Revision 1.11 1997/12/30 22:22:38 curt
442 * Further integration of event manager.
444 * Revision 1.10 1997/12/30 20:47:53 curt
445 * Integrated new event manager with subsystem initializations.
447 * Revision 1.9 1997/12/30 13:06:57 curt
448 * A couple lighting tweaks ...
450 * Revision 1.8 1997/12/23 04:58:38 curt
451 * Tweaked the sky coloring a bit to build in structures to allow finer rgb
454 * Revision 1.7 1997/12/22 23:45:48 curt
455 * First stab at sunset/sunrise sky glow effects.
457 * Revision 1.6 1997/12/22 04:14:34 curt
458 * Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
460 * Revision 1.5 1997/12/19 23:34:59 curt
461 * Lot's of tweaking with sky rendering and lighting.
463 * Revision 1.4 1997/12/19 16:45:02 curt
464 * Working on scene rendering order and options.
466 * Revision 1.3 1997/12/18 23:32:36 curt
467 * First stab at sky dome actually starting to look reasonable. :-)
469 * Revision 1.2 1997/12/18 04:07:03 curt
470 * Worked on properly translating and positioning the sky dome.
472 * Revision 1.1 1997/12/17 23:14:30 curt
474 * Begin work on rendering the sky. (Rather than just using a clear screen.)