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