]> git.mxchange.org Git - flightgear.git/blob - Astro/sky.cxx
Modifications to incorporate Jon S. Berndts flight model code.
[flightgear.git] / Astro / sky.cxx
1 // sky.cxx -- model sky with an upside down "bowl"
2 //
3 // Written by Curtis Olson, started December 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
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 <Aircraft/aircraft.hxx>
39 #include <Debug/logstream.hxx>
40 #include <FDM/flight.hxx>
41 #include <Include/fg_constants.h>
42 #include <Main/views.hxx>
43 #include <Math/fg_random.h>
44 #include <Time/event.hxx>
45 #include <Time/fg_time.hxx>
46
47 #include "sky.hxx"
48
49
50 // in meters of course
51 #define CENTER_ELEV   25000.0
52
53 #define INNER_RADIUS  50000.0
54 #define INNER_ELEV    20000.0
55
56 #define MIDDLE_RADIUS 70000.0
57 #define MIDDLE_ELEV    8000.0
58
59 #define OUTER_RADIUS  80000.0
60 #define OUTER_ELEV        0.0
61
62 #define BOTTOM_RADIUS 50000.0
63 #define BOTTOM_ELEV   -2000.0
64
65
66 static float inner_vertex[12][3];
67 static float middle_vertex[12][3];
68 static float outer_vertex[12][3];
69 static float bottom_vertex[12][3];
70
71 static float inner_color[12][4];
72 static float middle_color[12][4];
73 static float outer_color[12][4];
74
75
76 // Calculate the sky structure vertices
77 void fgSkyVerticesInit( void ) {
78     float theta;
79     int i;
80
81     FG_LOG(FG_ASTRO, FG_INFO, "  Generating the sky dome vertices.");
82
83     for ( i = 0; i < 12; i++ ) {
84         theta = (i * 30.0) * DEG_TO_RAD;
85         
86         inner_vertex[i][0] = cos(theta) * INNER_RADIUS;
87         inner_vertex[i][1] = sin(theta) * INNER_RADIUS;
88         inner_vertex[i][2] = INNER_ELEV;
89         
90         // printf("    %.2f %.2f\n", cos(theta) * INNER_RADIUS, 
91         //        sin(theta) * INNER_RADIUS);
92
93         middle_vertex[i][0] = cos((double)theta) * MIDDLE_RADIUS;
94         middle_vertex[i][1] = sin((double)theta) * MIDDLE_RADIUS;
95         middle_vertex[i][2] = MIDDLE_ELEV;
96             
97         outer_vertex[i][0] = cos((double)theta) * OUTER_RADIUS;
98         outer_vertex[i][1] = sin((double)theta) * OUTER_RADIUS;
99         outer_vertex[i][2] = OUTER_ELEV;
100             
101         bottom_vertex[i][0] = cos((double)theta) * BOTTOM_RADIUS;
102         bottom_vertex[i][1] = sin((double)theta) * BOTTOM_RADIUS;
103         bottom_vertex[i][2] = BOTTOM_ELEV;
104     }
105 }
106
107
108 // (Re)calculate the sky colors at each vertex
109 void fgSkyColorsInit( void ) {
110     fgLIGHT *l;
111     double sun_angle, diff;
112     double outer_param[3], outer_amt[3], outer_diff[3];
113     double middle_param[3], middle_amt[3], middle_diff[3];
114     int i, j;
115
116     l = &cur_light_params;
117
118     FG_LOG( FG_ASTRO, FG_INFO, 
119             "  Generating the sky colors for each vertex." );
120
121     // setup for the possibility of sunset effects
122     sun_angle = l->sun_angle * RAD_TO_DEG;
123     // fgPrintf( FG_ASTRO, FG_INFO, 
124     //           "  Sun angle in degrees = %.2f\n", sun_angle);
125
126     if ( (sun_angle > 80.0) && (sun_angle < 100.0) ) {
127         // 0.0 - 0.4
128         outer_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 20.0;
129         outer_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 40.0;
130         outer_param[2] = -(10.0 - fabs(90.0 - sun_angle)) / 30.0;
131         // outer_param[2] = 0.0;
132
133         middle_param[0] = (10.0 - fabs(90.0 - sun_angle)) / 40.0;
134         middle_param[1] = (10.0 - fabs(90.0 - sun_angle)) / 80.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             // printf("sky = %.2f  fog = %.2f  diff = %.2f\n", 
165             //        l->sky_color[j], l->fog_color[j], diff);
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 ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
172             if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
173             if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
174             if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
175             if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
176             if ( outer_color[i][j] < 0.10 ) { outer_color[i][j] = 0.10; }
177         }
178         inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = 
179             l->sky_color[3];
180
181         for ( j = 0; j < 3; j++ ) {
182             outer_amt[j] -= outer_diff[j];
183             middle_amt[j] -= middle_diff[j];
184         }
185
186         /*
187         printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
188                inner_color[i][1], inner_color[i][2], inner_color[i][3]);
189         printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
190                middle_color[i][0], middle_color[i][1], middle_color[i][2], 
191                middle_color[i][3]);
192         printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
193                outer_color[i][0], outer_color[i][1], outer_color[i][2], 
194                outer_color[i][3]);
195         */
196     }
197
198     for ( j = 0; j < 3; j++ ) {
199         outer_amt[j] = 0.0;
200         middle_amt[j] = 0.0;
201     }
202
203     for ( i = 6; i < 12; i++ ) {
204
205         for ( j = 0; j < 3; j++ ) {
206             diff = l->sky_color[j] - l->fog_color[j];
207
208             // printf("sky = %.2f  fog = %.2f  diff = %.2f\n", 
209             //        l->sky_color[j], l->fog_color[j], diff);
210
211             inner_color[i][j] = l->sky_color[j] - diff * 0.3;
212             middle_color[i][j] = l->sky_color[j] - diff * 0.9 + middle_amt[j];
213             outer_color[i][j] = l->fog_color[j] + outer_amt[j];
214
215             if ( inner_color[i][j] > 1.00 ) { inner_color[i][j] = 1.00; }
216             if ( inner_color[i][j] < 0.10 ) { inner_color[i][j] = 0.10; }
217             if ( middle_color[i][j] > 1.00 ) { middle_color[i][j] = 1.00; }
218             if ( middle_color[i][j] < 0.10 ) { middle_color[i][j] = 0.10; }
219             if ( outer_color[i][j] > 1.00 ) { outer_color[i][j] = 1.00; }
220             if ( outer_color[i][j] < 0.15 ) { outer_color[i][j] = 0.15; }
221         }
222         inner_color[i][3] = middle_color[i][3] = outer_color[i][3] = 
223             l->sky_color[3];
224
225         for ( j = 0; j < 3; j++ ) {
226             outer_amt[j] += outer_diff[j];
227             middle_amt[j] += middle_diff[j];
228         }
229
230         /*
231         printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, inner_color[i][0],
232                inner_color[i][1], inner_color[i][2], inner_color[i][3]);
233         printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
234                middle_color[i][0], middle_color[i][1], middle_color[i][2], 
235                middle_color[i][3]);
236         printf("outer_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
237                outer_color[i][0], outer_color[i][1], outer_color[i][2], 
238                outer_color[i][3]);
239         */
240     }
241 }
242
243
244 // Initialize the sky structure and colors
245 void fgSkyInit( void ) {
246     FG_LOG( FG_ASTRO, FG_INFO, "Initializing the sky" );
247
248     fgSkyVerticesInit();
249
250     // regester fgSkyColorsInit() as an event to be run periodically
251     global_events.Register( "fgSkyColorsInit()", fgSkyColorsInit, 
252                             fgEVENT::FG_EVENT_READY, 30000);
253 }
254
255
256 // Draw the Sky
257 void fgSkyRender( void ) {
258     FGInterface *f;
259     fgLIGHT *l;
260     float inner_color[4];
261     float middle_color[4];
262     float outer_color[4];
263     double diff;
264     int i;
265
266     f = current_aircraft.fdm_state;
267     l = &cur_light_params;
268
269     // printf("Rendering the sky.\n");
270
271     // calculate the proper colors
272     for ( i = 0; i < 3; i++ ) {
273         diff = l->sky_color[i] - l->adj_fog_color[i];
274
275         // printf("sky = %.2f  fog = %.2f  diff = %.2f\n", 
276         //        l->sky_color[j], l->adj_fog_color[j], diff);
277
278         inner_color[i] = l->sky_color[i] - diff * 0.3;
279         middle_color[i] = l->sky_color[i] - diff * 0.9;
280         outer_color[i] = l->adj_fog_color[i];
281     }
282     inner_color[3] = middle_color[3] = outer_color[3] = l->adj_fog_color[3];
283
284     xglPushMatrix();
285
286     // Translate to view position
287     Point3D zero_elev = current_view.get_cur_zero_elev();
288     xglTranslatef( zero_elev.x(), zero_elev.y(), zero_elev.z() );
289     // printf("  Translated to %.2f %.2f %.2f\n", 
290     //        zero_elev.x, zero_elev.y, zero_elev.z );
291
292     // Rotate to proper orientation
293     // printf("  lon = %.2f  lat = %.2f\n", FG_Longitude * RAD_TO_DEG,
294     //        FG_Latitude * RAD_TO_DEG);
295     xglRotatef( f->get_Longitude() * RAD_TO_DEG, 0.0, 0.0, 1.0 );
296     xglRotatef( 90.0 - f->get_Latitude() * RAD_TO_DEG, 0.0, 1.0, 0.0 );
297     xglRotatef( l->sun_rotation * RAD_TO_DEG, 0.0, 0.0, 1.0 );
298
299     // Draw inner/center section of sky*/
300     xglBegin( GL_TRIANGLE_FAN );
301     xglColor4fv(l->sky_color);
302     xglVertex3f(0.0, 0.0, CENTER_ELEV);
303     for ( i = 11; i >= 0; i-- ) {
304         xglColor4fv( inner_color );
305         xglVertex3fv( inner_vertex[i] );
306     }
307     xglColor4fv( inner_color );
308     xglVertex3fv( inner_vertex[11] );
309     xglEnd();
310
311     // Draw the middle ring
312     xglBegin( GL_TRIANGLE_STRIP );
313     for ( i = 0; i < 12; i++ ) {
314         xglColor4fv( middle_color );
315         // printf("middle_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
316         //        middle_color[i][0], middle_color[i][1], middle_color[i][2], 
317         //        middle_color[i][3]);
318         // xglColor4f(1.0, 0.0, 0.0, 1.0);
319         xglVertex3fv( middle_vertex[i] );
320         xglColor4fv( inner_color );
321         // printf("inner_color[%d] = %.2f %.2f %.2f %.2f\n", i, 
322         //        inner_color[i][0], inner_color[i][1], inner_color[i][2], 
323         //        inner_color[i][3]);
324         // xglColor4f(0.0, 0.0, 1.0, 1.0);
325         xglVertex3fv( inner_vertex[i] );
326     }
327     xglColor4fv( middle_color );
328     // xglColor4f(1.0, 0.0, 0.0, 1.0);
329     xglVertex3fv( middle_vertex[0] );
330     xglColor4fv( inner_color );
331     // xglColor4f(0.0, 0.0, 1.0, 1.0);
332     xglVertex3fv( inner_vertex[0] );
333     xglEnd();
334
335     // Draw the outer ring
336     xglBegin( GL_TRIANGLE_STRIP );
337     for ( i = 0; i < 12; i++ ) {
338         xglColor4fv( outer_color );
339         xglVertex3fv( outer_vertex[i] );
340         xglColor4fv( middle_color );
341         xglVertex3fv( middle_vertex[i] );
342     }
343     xglColor4fv( outer_color );
344     xglVertex3fv( outer_vertex[0] );
345     xglColor4fv( middle_color );
346     xglVertex3fv( middle_vertex[0] );
347     xglEnd();
348
349     // Draw the bottom skirt
350     xglBegin( GL_TRIANGLE_STRIP );
351     xglColor4fv( outer_color );
352     for ( i = 0; i < 12; i++ ) {
353         xglVertex3fv( bottom_vertex[i] );
354         xglVertex3fv( outer_vertex[i] );
355     }
356     xglVertex3fv( bottom_vertex[0] );
357     xglVertex3fv( outer_vertex[0] );
358     xglEnd();
359
360     xglPopMatrix();
361 }
362
363
364 // $Log$
365 // Revision 1.21  1999/02/05 21:28:50  curt
366 // Modifications to incorporate Jon S. Berndts flight model code.
367 //
368 // Revision 1.20  1999/02/02 20:13:29  curt
369 // MSVC++ portability changes by Bernie Bright:
370 //
371 // Lib/Serial/serial.[ch]xx: Initial Windows support - incomplete.
372 // Simulator/Astro/stars.cxx: typo? included <stdio> instead of <cstdio>
373 // Simulator/Cockpit/hud.cxx: Added Standard headers
374 // Simulator/Cockpit/panel.cxx: Redefinition of default parameter
375 // Simulator/Flight/flight.cxx: Replaced cout with FG_LOG.  Deleted <stdio.h>
376 // Simulator/Main/fg_init.cxx:
377 // Simulator/Main/GLUTmain.cxx:
378 // Simulator/Main/options.hxx: Shuffled <fg_serial.hxx> dependency
379 // Simulator/Objects/material.hxx:
380 // Simulator/Time/timestamp.hxx: VC++ friend kludge
381 // Simulator/Scenery/tile.[ch]xx: Fixed using std::X declarations
382 // Simulator/Main/views.hxx: Added a constant
383 //
384 // Revision 1.19  1999/02/01 21:33:26  curt
385 // Renamed FlightGear/Simulator/Flight to FlightGear/Simulator/FDM since
386 // Jon accepted my offer to do this and thought it was a good idea.
387 //
388 // Revision 1.18  1999/02/01 21:09:00  curt
389 // Bug fix in vertex order of inner disk (fan) of the sky dome.
390 //
391 // Revision 1.17  1998/12/09 18:50:12  curt
392 // Converted "class fgVIEW" to "class FGView" and updated to make data
393 // members private and make required accessor functions.
394 //
395 // Revision 1.16  1998/12/05 15:54:03  curt
396 // Renamed class fgFLIGHT to class FGState as per request by JSB.
397 //
398 // Revision 1.15  1998/12/03 01:15:36  curt
399 // Converted fgFLIGHT to a class.
400 // Tweaks for Sun portability.
401 //
402 // Revision 1.14  1998/11/06 21:17:39  curt
403 // Converted to new logstream debugging facility.  This allows release
404 // builds with no messages at all (and no performance impact) by using
405 // the -DFG_NDEBUG flag.
406 //
407 // Revision 1.13  1998/10/20 18:28:30  curt
408 // Tweaked sunset/sunrise colors.
409 //
410 // Revision 1.12  1998/10/16 23:27:18  curt
411 // C++-ifying.
412 //
413 // Revision 1.11  1998/10/16 00:52:19  curt
414 // Converted to Point3D class.
415 //
416 // Revision 1.10  1998/08/29 13:07:16  curt
417 // Rewrite of event manager thanks to Bernie Bright.
418 //
419 // Revision 1.9  1998/08/22 01:18:59  curt
420 // Minor tweaks to avoid using unitialized memory.
421 //
422 // Revision 1.8  1998/08/12 21:40:44  curt
423 // Sky now tracks adjusted fog color so it blends well with terrain.
424 //
425 // Revision 1.7  1998/07/22 21:39:21  curt
426 // Lower skirt tracks adjusted fog color, not fog color.
427 //
428 // Revision 1.6  1998/05/23 14:07:14  curt
429 // Use new C++ events class.
430 //
431 // Revision 1.5  1998/04/28 01:19:02  curt
432 // Type-ified fgTIME and fgVIEW
433 //
434 // Revision 1.4  1998/04/26 05:10:01  curt
435 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
436 //
437 // Revision 1.3  1998/04/25 22:06:25  curt
438 // Edited cvs log messages in source files ... bad bad bad!
439 //
440 // Revision 1.2  1998/04/24 00:45:03  curt
441 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
442 // Fixed a bug when generating sky colors.
443 //
444 // Revision 1.1  1998/04/22 13:21:32  curt
445 // C++ - ifing the code a bit.
446 //
447 // Revision 1.9  1998/04/03 21:52:50  curt
448 // Converting to Gnu autoconf system.
449 //
450 // Revision 1.8  1998/03/09 22:47:25  curt
451 // Incorporated Durk's updates.
452 //
453 // Revision 1.7  1998/02/19 13:05:49  curt
454 // Incorporated some HUD tweaks from Michelle America.
455 // Tweaked the sky's sunset/rise colors.
456 // Other misc. tweaks.
457 //
458 // Revision 1.6  1998/02/07 15:29:32  curt
459 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
460 // <chotchkiss@namg.us.anritsu.com>
461 //
462 // Revision 1.5  1998/01/27 00:47:48  curt
463 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
464 // system and commandline/config file processing code.
465 //
466 // Revision 1.4  1998/01/26 15:54:28  curt
467 // Added a "skirt" to try to help hide gaps between scenery and sky.  This will
468 // have to be revisited in the future.
469 //
470 // Revision 1.3  1998/01/19 19:26:59  curt
471 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
472 // This should simplify things tremendously.
473 //
474 // Revision 1.2  1998/01/19 18:40:17  curt
475 // Tons of little changes to clean up the code and to remove fatal errors
476 // when building with the c++ compiler.
477 //
478 // Revision 1.1  1998/01/07 03:16:19  curt
479 // Moved from .../Src/Scenery/ to .../Src/Astro/
480 //
481 // Revision 1.11  1997/12/30 22:22:38  curt
482 // Further integration of event manager.
483 //
484 // Revision 1.10  1997/12/30 20:47:53  curt
485 // Integrated new event manager with subsystem initializations.
486 //
487 // Revision 1.9  1997/12/30 13:06:57  curt
488 // A couple lighting tweaks ...
489 //
490 // Revision 1.8  1997/12/23 04:58:38  curt
491 // Tweaked the sky coloring a bit to build in structures to allow finer rgb
492 // control.
493 //
494 // Revision 1.7  1997/12/22 23:45:48  curt
495 // First stab at sunset/sunrise sky glow effects.
496 //
497 // Revision 1.6  1997/12/22 04:14:34  curt
498 // Aligned sky with sun so dusk/dawn effects can be correct relative to the sun.
499 //
500 // Revision 1.5  1997/12/19 23:34:59  curt
501 // Lot's of tweaking with sky rendering and lighting.
502 //
503 // Revision 1.4  1997/12/19 16:45:02  curt
504 // Working on scene rendering order and options.
505 //
506 // Revision 1.3  1997/12/18 23:32:36  curt
507 // First stab at sky dome actually starting to look reasonable. :-)
508 //
509 // Revision 1.2  1997/12/18 04:07:03  curt
510 // Worked on properly translating and positioning the sky dome.
511 //
512 // Revision 1.1  1997/12/17 23:14:30  curt
513 // Initial revision.
514 // Begin work on rendering the sky. (Rather than just using a clear screen.)
515 //
516