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