]> git.mxchange.org Git - flightgear.git/blob - Astro/stars.cxx
Renamed class fgFLIGHT to class FGState as per request by JSB.
[flightgear.git] / Astro / stars.cxx
1 // stars.cxx -- data structures and routines for managing and rendering stars.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@me.umn.edu
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
26 #ifdef HAVE_CONFIG_H
27 #  include <config.h>
28 #endif
29
30 #ifdef HAVE_WINDOWS_H
31 #  include <windows.h>
32 #endif
33
34
35 #include <math.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <string>
39 #include <time.h>
40
41 #include <GL/glut.h>
42 #include <XGL/xgl.h>
43
44 #include <Aircraft/aircraft.hxx>
45 #include <Debug/logstream.hxx>
46 #include <Include/fg_constants.h>
47 #include <Misc/fgstream.hxx>
48 #include <Main/options.hxx>
49 #include <Main/views.hxx>
50 #include <Misc/stopwatch.hxx>
51 #include <Time/fg_time.hxx>
52
53 #include "stars.hxx"
54
55
56 #define EpochStart           (631065600)
57 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
58 #define FG_MAX_STARS 3500
59
60 // Define four structures, each with varying amounts of stars
61 static GLint stars[FG_STAR_LEVELS];
62
63
64 // Initialize the Star Management Subsystem
65 int fgStarsInit( void ) {
66     Point3D starlist[FG_MAX_STARS];
67     // struct CelestialCoord pltPos;
68     double right_ascension, declination, magnitude;
69     double min_magnitude[FG_STAR_LEVELS];
70     // double ra_save, decl_save;
71     // double ra_save1, decl_save1;
72     int i, j, starcount, count;
73
74     FG_LOG( FG_ASTRO, FG_INFO, "Initializing stars" );
75
76     if ( FG_STAR_LEVELS < 4 ) {
77         FG_LOG( FG_ASTRO, FG_ALERT, "Big whups in stars.cxx" );
78         exit(-1);
79     }
80
81     // build the full path name to the stars data base file
82     string path = current_options.get_fg_root() + "/Astro/stars" + ".gz";
83
84     FG_LOG( FG_ASTRO, FG_INFO, "  Loading stars from " << path );
85
86     fg_gzifstream in( path );
87     if ( ! in ) {
88         FG_LOG( FG_ASTRO, FG_ALERT, "Cannot open star file: " << path );
89         exit(-1);
90     }
91
92     starcount = 0;
93
94     StopWatch timer;
95     timer.start();
96
97     // read in each line of the file
98     while ( ! in.eof() && starcount < FG_MAX_STARS )
99     {
100         in >> skipcomment;
101         string name;
102         getline( in, name, ',' );
103         in >> starlist[starcount];
104         ++starcount;
105     }
106
107     timer.stop();
108     FG_LOG( FG_ASTRO, FG_INFO, 
109             "Loaded " << starcount << " stars in "
110             << timer.elapsedSeconds() << " seconds" );
111
112     min_magnitude[0] = 4.2;
113     min_magnitude[1] = 3.6;
114     min_magnitude[2] = 3.0;
115     min_magnitude[3] = 2.4;
116     min_magnitude[4] = 1.8;
117     min_magnitude[5] = 1.2;
118     min_magnitude[6] = 0.6;
119     min_magnitude[7] = 0.0;
120
121     // build the various star display lists
122     for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
123
124         stars[i] = xglGenLists(1);
125         xglNewList( stars[i], GL_COMPILE );
126         xglBegin( GL_POINTS );
127
128         count = 0;
129
130         for ( j = 0; j < starcount; j++ ) {
131             magnitude = starlist[j].z();
132             // printf("magnitude = %.2f\n", magnitude);
133
134             if ( magnitude < min_magnitude[i] ) {
135                 right_ascension = starlist[j].x();
136                 declination = starlist[j].y();
137
138                 count++;
139
140                 // scale magnitudes to (0.0 - 1.0)
141                 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
142                 
143                 // scale magnitudes again so they look ok
144                 if ( magnitude > 1.0 ) { magnitude = 1.0; }
145                 if ( magnitude < 0.0 ) { magnitude = 0.0; }
146                 // magnitude =
147                 //     magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
148                   
149                 magnitude = magnitude * 0.9 + 
150                     (((FG_STAR_LEVELS - 1) - i) * 0.014);
151                 // printf("  Found star: %d %s, %.3f %.3f %.3f\n", count,
152                 //        name, right_ascension, declination, magnitude);
153                     
154                 xglColor3f( magnitude, magnitude, magnitude );
155                 //xglColor3f(0,0,0);*/
156                 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
157                              50000.0*sin(right_ascension)*cos(declination),
158                              50000.0*sin(declination) );
159             }
160         } // while
161
162         xglEnd();
163
164         /*
165         xglBegin(GL_LINE_LOOP);
166         xglColor3f(1.0, 0.0, 0.0);
167         xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
168                     50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
169                     50000.0 * sin(decl_save-0.2) );
170         xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
171                     50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
172                     50000.0 * sin(decl_save-0.2) );
173         xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
174                     50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
175                     50000.0 * sin(decl_save+0.2) );
176         xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
177                     50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
178                     50000.0 * sin(decl_save+0.2) );
179         xglEnd();
180         */
181
182         /*
183         xglBegin(GL_LINE_LOOP);
184         xglColor3f(0.0, 1.0, 0.0);
185         xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
186                     50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
187                     50000.0 * sin(decl_save1-0.2) );
188         xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
189                     50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
190                     50000.0 * sin(decl_save1-0.2) );
191         xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
192                     50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
193                     50000.0 * sin(decl_save1+0.2) );
194         xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
195                     50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
196                     50000.0 * sin(decl_save1+0.2) );
197         xglEnd();
198         */
199
200         xglEndList();
201             
202         FG_LOG( FG_ASTRO, FG_INFO,
203                 "  Loading " << count << " stars brighter than " 
204                 << min_magnitude[i] );
205     }
206
207     return 1;  // OK, we got here because initialization worked.
208 }
209
210
211 // Draw the Stars
212 void fgStarsRender( void ) {
213     FGState *f;
214     fgVIEW *v;
215     fgLIGHT *l;
216     fgTIME *t;
217     int i;
218
219     f = current_aircraft.fdm_state;
220     l = &cur_light_params;
221     t = &cur_time_params;
222     v = &current_view;
223
224     // FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise
225
226     // t->sun_angle = 3.0; // to force stars to be drawn (for testing)
227
228     // render the stars
229     if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
230         // determine which star structure to draw
231         if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
232             i = 0;
233         } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
234             i = 1;
235         } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
236             i = 2;
237         } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
238             i = 3;
239         } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
240             i = 4;
241         } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
242             i = 5;
243         } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
244             i = 6;
245         } else {
246             i = 7;
247         }
248
249         // printf("RENDERING STARS = %d (night)\n", i);
250
251         xglCallList(stars[i]);
252     } else {
253         // printf("not RENDERING STARS (day)\n");
254     }
255 }
256
257
258 // $Log$
259 // Revision 1.24  1998/12/05 15:54:04  curt
260 // Renamed class fgFLIGHT to class FGState as per request by JSB.
261 //
262 // Revision 1.23  1998/11/23 21:48:28  curt
263 // Borland portability tweaks.
264 //
265 // Revision 1.22  1998/11/07 19:07:07  curt
266 // Enable release builds using the --without-logging option to the configure
267 // script.  Also a couple log message cleanups, plus some C to C++ comment
268 // conversion.
269 //
270 // Revision 1.21  1998/11/06 21:17:42  curt
271 // Converted to new logstream debugging facility.  This allows release
272 // builds with no messages at all (and no performance impact) by using
273 // the -DFG_NDEBUG flag.
274 //
275 // Revision 1.20  1998/11/06 14:47:02  curt
276 // Changes to track Bernie's updates to fgstream.
277 //
278 // Revision 1.19  1998/10/16 23:27:21  curt
279 // C++-ifying.
280 //
281 // Revision 1.18  1998/10/16 00:52:20  curt
282 // Converted to Point3D class.
283 //
284 // Revision 1.17  1998/09/24 15:36:19  curt
285 // Converted to c++ style comments.
286 //
287 // Revision 1.16  1998/09/24 15:25:24  curt
288 // Miscellaneous tweaks.
289 //
290 //
291 // Revision 1.15  1998/09/17 18:25:12  curt
292 // Fixed output message.
293 //
294 // Revision 1.14  1998/09/15 04:26:22  curt
295 // New textured moon and rewritten/restructured Astro code contributed by Durk
296 // Talsma.
297 //
298 // Revision 1.13  1998/09/01 19:03:04  curt
299 // Changes contributed by Bernie Bright <bbright@c031.aone.net.au>
300 //  - The new classes in libmisc.tgz define a stream interface into zlib.
301 //    I've put these in a new directory, Lib/Misc.  Feel free to rename it
302 //    to something more appropriate.  However you'll have to change the
303 //    include directives in all the other files.  Additionally you'll have
304 //    add the library to Lib/Makefile.am and Simulator/Main/Makefile.am.
305 //
306 //    The StopWatch class in Lib/Misc requires a HAVE_GETRUSAGE autoconf
307 //    test so I've included the required changes in config.tgz.
308 //
309 //    There are a fair few changes to Simulator/Objects as I've moved
310 //    things around.  Loading tiles is quicker but thats not where the delay
311 //    is.  Tile loading takes a few tenths of a second per file on a P200
312 //    but it seems to be the post-processing that leads to a noticeable
313 //    blip in framerate.  I suppose its time to start profiling to see where
314 //    the delays are.
315 //
316 //    I've included a brief description of each archives contents.
317 //
318 // Lib/Misc/
319 //   zfstream.cxx
320 //   zfstream.hxx
321 //     C++ stream interface into zlib.
322 //     Taken from zlib-1.1.3/contrib/iostream/.
323 //     Minor mods for STL compatibility.
324 //     There's no copyright associated with these so I assume they're
325 //     covered by zlib's.
326 //
327 //   fgstream.cxx
328 //   fgstream.hxx
329 //     FlightGear input stream using gz_ifstream.  Tries to open the
330 //     given filename.  If that fails then filename is examined and a
331 //     ".gz" suffix is removed or appended and that file is opened.
332 //
333 //   stopwatch.hxx
334 //     A simple timer for benchmarking.  Not used in production code.
335 //     Taken from the Blitz++ project.  Covered by GPL.
336 //
337 //   strutils.cxx
338 //   strutils.hxx
339 //     Some simple string manipulation routines.
340 //
341 // Simulator/Airports/
342 //   Load airports database using fgstream.
343 //   Changed fgAIRPORTS to use set<> instead of map<>.
344 //   Added bool fgAIRPORTS::search() as a neater way doing the lookup.
345 //   Returns true if found.
346 //
347 // Simulator/Astro/
348 //   Modified fgStarsInit() to load stars database using fgstream.
349 //
350 // Simulator/Objects/
351 //   Modified fgObjLoad() to use fgstream.
352 //   Modified fgMATERIAL_MGR::load_lib() to use fgstream.
353 //   Many changes to fgMATERIAL.
354 //   Some changes to fgFRAGMENT but I forget what!
355 //
356 // Revision 1.12  1998/08/27 17:02:01  curt
357 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
358 // - use strings for fg_root and airport_id and added methods to return
359 //   them as strings,
360 // - inlined all access methods,
361 // - made the parsing functions private methods,
362 // - deleted some unused functions.
363 // - propogated some of these changes out a bit further.
364 //
365 // Revision 1.11  1998/08/25 20:53:29  curt
366 // Shuffled $FG_ROOT file layout.
367 //
368 // Revision 1.10  1998/08/10 20:33:09  curt
369 // Rewrote star loading and rendering to:
370 //   1. significantly improve load speed
371 //   2. transition from no stars to stars through eight stages.
372 //
373 // Revision 1.9  1998/08/06 12:45:20  curt
374 // Modified to bring in stars in 8 increments based on magnitude, not number
375 // of stars.
376 //
377 // Revision 1.8  1998/07/13 21:00:10  curt
378 // Wrote access functions for current fgOPTIONS.
379 //
380 // Revision 1.7  1998/05/29 20:35:42  curt
381 // Added zlib support for reading in compressed data files.
382 //
383 // Revision 1.6  1998/05/13 18:25:35  curt
384 // Root path info moved to fgOPTIONS.
385 //
386 // Revision 1.5  1998/04/28 01:19:03  curt
387 // Type-ified fgTIME and fgVIEW
388 //
389 // Revision 1.4  1998/04/26 05:10:02  curt
390 // "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
391 //
392 // Revision 1.3  1998/04/25 22:06:26  curt
393 // Edited cvs log messages in source files ... bad bad bad!
394 //
395 // Revision 1.2  1998/04/24 00:45:03  curt
396 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
397 // Fixed a bug when generating sky colors.
398 //
399 // Revision 1.1  1998/04/22 13:21:34  curt
400 // C++ - ifing the code a bit.
401 //
402 // Revision 1.11  1998/04/18 04:13:58  curt
403 // Moved fg_debug.c to it's own library.
404 //
405 // Revision 1.10  1998/04/03 21:52:51  curt
406 // Converting to Gnu autoconf system.
407 //
408 // Revision 1.9  1998/03/14 00:27:12  curt
409 // Updated fgGENERAL to a "type" of struct.
410 //
411 // Revision 1.8  1998/02/12 21:59:38  curt
412 // Incorporated code changes contributed by Charlie Hotchkiss
413 // <chotchkiss@namg.us.anritsu.com>
414 //
415 // Revision 1.7  1998/02/09 15:07:48  curt
416 // Minor tweaks.
417 //
418 // Revision 1.6  1998/02/02 20:53:23  curt
419 // To version 0.29
420 //
421 // Revision 1.5  1998/01/27 18:35:53  curt
422 // Minor tweaks.
423 //
424 // Revision 1.4  1998/01/27 00:47:49  curt
425 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
426 // system and commandline/config file processing code.
427 //
428 // Revision 1.3  1998/01/19 19:26:59  curt
429 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
430 // This should simplify things tremendously.
431 //
432 // Revision 1.2  1998/01/19 18:40:18  curt
433 // Tons of little changes to clean up the code and to remove fatal errors
434 // when building with the c++ compiler.
435 //
436 // Revision 1.1  1998/01/07 03:16:20  curt
437 // Moved from .../Src/Scenery/ to .../Src/Astro/
438 //
439 // Revision 1.24  1997/12/30 22:22:39  curt
440 // Further integration of event manager.
441 //
442 // Revision 1.23  1997/12/30 20:47:53  curt
443 // Integrated new event manager with subsystem initializations.
444 //
445 // Revision 1.22  1997/12/30 16:36:53  curt
446 // Merged in Durk's changes ...
447 //
448 // Revision 1.21  1997/12/19 23:35:00  curt
449 // Lot's of tweaking with sky rendering and lighting.
450 //
451 // Revision 1.20  1997/12/15 23:55:03  curt
452 // Add xgl wrappers for debugging.
453 // Generate terrain normals on the fly.
454 //
455 // Revision 1.19  1997/12/12  19:53:00  curt
456 // Working on lightling and material properties.
457 //
458 // Revision 1.18  1997/12/10 22:37:52  curt
459 // Prepended "fg" on the name of all global structures that didn't have it yet.
460 // i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
461 //
462 // Revision 1.17  1997/12/09 04:25:33  curt
463 // Working on adding a global lighting params structure.
464 //
465 // Revision 1.16  1997/11/25 19:25:38  curt
466 // Changes to integrate Durk's moon/sun code updates + clean up.
467 //
468 // Revision 1.15  1997/10/30 12:38:45  curt
469 // Working on new scenery subsystem.
470 //
471 // Revision 1.14  1997/10/28 21:00:22  curt
472 // Changing to new terrain format.
473 //
474 // Revision 1.13  1997/10/25 03:18:28  curt
475 // Incorporated sun, moon, and planet position and rendering code contributed
476 // by Durk Talsma.
477 //
478 // Revision 1.12  1997/09/23 00:29:43  curt
479 // Tweaks to get things to compile with gcc-win32.
480 //
481 // Revision 1.11  1997/09/22 14:44:21  curt
482 // Continuing to try to align stars correctly.
483 //
484 // Revision 1.10  1997/09/20 03:34:32  curt
485 // Still trying to get those durned stars aligned properly.
486 //
487 // Revision 1.9  1997/09/18 16:20:09  curt
488 // At dusk/dawn add/remove stars in stages.
489 //
490 // Revision 1.8  1997/09/16 22:14:52  curt
491 // Tweaked time of day lighting equations.  Don't draw stars during the day.
492 //
493 // Revision 1.7  1997/09/16 15:50:31  curt
494 // Working on star alignment and time issues.
495 //
496 // Revision 1.6  1997/09/05 14:17:31  curt
497 // More tweaking with stars.
498 //
499 // Revision 1.5  1997/09/05 01:35:59  curt
500 // Working on getting stars right.
501 //
502 // Revision 1.4  1997/09/04 02:17:38  curt
503 // Shufflin' stuff.
504 //
505 // Revision 1.3  1997/08/29 17:55:28  curt
506 // Worked on properly aligning the stars.
507 //
508 // Revision 1.2  1997/08/27 21:32:30  curt
509 // Restructured view calculation code.  Added stars.
510 //
511 // Revision 1.1  1997/08/27 03:34:48  curt
512 // Initial revision.
513
514