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