]> git.mxchange.org Git - flightgear.git/blob - Astro/stars.cxx
Contributions from 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 <Include/fg_zlib.h>
49 #include <Main/options.hxx>
50 #include <Main/views.hxx>
51 #include <Time/fg_time.hxx>
52
53 #include "orbits.hxx"
54 #include "planets.hxx"
55 #include "stars.hxx"
56
57
58 #define EpochStart           (631065600)
59 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
60 #define FG_MAX_STARS 3500
61
62 /* Define four structures, each with varying amounts of stars */
63 static GLint stars[FG_STAR_LEVELS];
64
65
66 /* Initialize the Star Management Subsystem */
67 int fgStarsInit( void ) {
68     fgPoint3d starlist[FG_MAX_STARS];
69     fgFile fd;
70     /* struct CelestialCoord pltPos; */
71     string path, gzpath;
72     char line[256], name[256];
73     char *front, *end;
74     double right_ascension, declination, magnitude;
75     double min_magnitude[FG_STAR_LEVELS];
76     /* double ra_save, decl_save; */
77     /* double ra_save1, decl_save1; */
78     int i, j, starcount, count;
79
80     fgPrintf( FG_ASTRO, FG_INFO, "Initializing stars\n");
81
82     /* build the full path name to the stars data base file */
83     path = current_options.get_fg_root() + "/Astro/stars";
84     gzpath = path + ".gz";
85
86     if ( FG_STAR_LEVELS < 4 ) {
87         fgPrintf( FG_ASTRO, FG_EXIT, "Big whups in stars.cxx\n");
88     }
89
90     fgPrintf( FG_ASTRO, FG_INFO, "  Loading stars from %s\n", path.c_str() );
91
92     // load star data file
93     if ( (fd = fgopen(path.c_str(), "rb")) == NULL ) {
94         if ( (fd = fgopen(gzpath.c_str(), "rb")) == NULL ) {
95             // Oops, lets not even try to continue. This is critical.
96             fgPrintf( FG_ASTRO, FG_EXIT,
97                       "Cannot open star file: '%s'\n", path.c_str() );
98         }
99     }
100
101     starcount = 0;
102
103     // read in each line of the file
104     while ( (fggets(fd, line, 256) != NULL) && (starcount < FG_MAX_STARS) ) {
105         front = line;
106
107         // printf("  Read line = %s", front);
108
109         // advance to first non-whitespace character
110         while ( (front[0] == ' ') || (front[0] == '\t') ) {
111             front++;
112         }
113
114         // printf("  Line length (after trimming) = %d\n", strlen(front));
115
116         if ( front[0] == '#' ) {
117             // comment
118         } else if ( strlen(front) <= 1 ) {
119             // blank line
120         } else {
121             // star data line
122
123             // get name
124             end = front;
125             while ( end[0] != ',' ) {
126                 end++;
127             }
128             end[0] = '\0';
129             strcpy(name, front);
130             front = end;
131             front++;
132
133             sscanf(front, "%lf,%lf,%lf\n",
134                    &right_ascension, &declination, &magnitude);
135             starlist[starcount].x = right_ascension;
136             starlist[starcount].y = declination;
137             starlist[starcount].z = magnitude;
138             starcount++;
139         }
140     }
141
142     min_magnitude[0] = 4.2;
143     min_magnitude[1] = 3.6;
144     min_magnitude[2] = 3.0;
145     min_magnitude[3] = 2.4;
146     min_magnitude[4] = 1.8;
147     min_magnitude[5] = 1.2;
148     min_magnitude[6] = 0.6;
149     min_magnitude[7] = 0.0;
150
151     // build the various star display lists
152     for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
153
154         stars[i] = xglGenLists(1);
155         xglNewList( stars[i], GL_COMPILE );
156         xglBegin( GL_POINTS );
157
158         count = 0;
159
160         for ( j = 0; j < starcount; j++ ) {
161             magnitude = starlist[j].z;
162             // printf("magnitude = %.2f\n", magnitude);
163
164             if ( magnitude < min_magnitude[i] ) {
165                 right_ascension = starlist[j].x;
166                 declination = starlist[j].y;
167
168                 count++;
169
170                 /* scale magnitudes to (0.0 - 1.0) */
171                 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
172                 
173                 /* scale magnitudes again so they look ok */
174                 if ( magnitude > 1.0 ) { magnitude = 1.0; }
175                 if ( magnitude < 0.0 ) { magnitude = 0.0; }
176                 /* magnitude =
177                    magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
178                    */
179                 magnitude = magnitude * 0.9 + 
180                     (((FG_STAR_LEVELS - 1) - i) * 0.014);
181                 /* printf("  Found star: %d %s, %.3f %.3f %.3f\n", count,
182                    name, right_ascension, declination, magnitude); */
183                     
184                 xglColor3f( magnitude, magnitude, magnitude );
185                 /*xglColor3f(0,0,0);*/
186                 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
187                              50000.0*sin(right_ascension)*cos(declination),
188                              50000.0*sin(declination) );
189             }
190         } /* while */
191
192         xglEnd();
193
194         /*
195         xglBegin(GL_LINE_LOOP);
196         xglColor3f(1.0, 0.0, 0.0);
197         xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
198                     50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
199                     50000.0 * sin(decl_save-0.2) );
200         xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
201                     50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
202                     50000.0 * sin(decl_save-0.2) );
203         xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
204                     50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
205                     50000.0 * sin(decl_save+0.2) );
206         xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
207                     50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
208                     50000.0 * sin(decl_save+0.2) );
209         xglEnd();
210         */
211
212         /*
213         xglBegin(GL_LINE_LOOP);
214         xglColor3f(0.0, 1.0, 0.0);
215         xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
216                     50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
217                     50000.0 * sin(decl_save1-0.2) );
218         xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
219                     50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
220                     50000.0 * sin(decl_save1-0.2) );
221         xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
222                     50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
223                     50000.0 * sin(decl_save1+0.2) );
224         xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
225                     50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
226                     50000.0 * sin(decl_save1+0.2) );
227         xglEnd();
228         */
229
230         xglEndList();
231             
232         fgPrintf( FG_ASTRO, FG_INFO,
233                   "  Loading %d stars brighter than %.2f\n", 
234                   count, min_magnitude[i]);
235     }
236
237     return 1;  // OK, we got here because initialization worked.
238 }
239
240
241 /* Draw the Stars */
242 void fgStarsRender( void ) {
243     fgFLIGHT *f;
244     fgVIEW *v;
245     fgLIGHT *l;
246     fgTIME *t;
247     int i;
248
249     f = current_aircraft.flight;
250     l = &cur_light_params;
251     t = &cur_time_params;
252     v = &current_view;
253
254     /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
255
256     /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
257
258     /* render the stars */
259     if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
260         /* determine which star structure to draw */
261         if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
262             i = 0;
263         } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
264             i = 1;
265         } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
266             i = 2;
267         } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
268             i = 3;
269         } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
270             i = 4;
271         } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
272             i = 5;
273         } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
274             i = 6;
275         } else {
276             i = 7;
277         }
278
279         /* printf("RENDERING STARS = %d (night)\n", i); */
280
281         xglCallList(stars[i]);
282     } else {
283         /* printf("not RENDERING STARS (day)\n"); */
284     }
285 }
286
287
288 /* $Log$
289 /* Revision 1.12  1998/08/27 17:02:01  curt
290 /* Contributions from Bernie Bright <bbright@c031.aone.net.au>
291 /* - use strings for fg_root and airport_id and added methods to return
292 /*   them as strings,
293 /* - inlined all access methods,
294 /* - made the parsing functions private methods,
295 /* - deleted some unused functions.
296 /* - propogated some of these changes out a bit further.
297 /*
298  * Revision 1.11  1998/08/25 20:53:29  curt
299  * Shuffled $FG_ROOT file layout.
300  *
301  * Revision 1.10  1998/08/10 20:33:09  curt
302  * Rewrote star loading and rendering to:
303  *   1. significantly improve load speed
304  *   2. transition from no stars to stars through eight stages.
305  *
306  * Revision 1.9  1998/08/06 12:45:20  curt
307  * Modified to bring in stars in 8 increments based on magnitude, not number
308  * of stars.
309  *
310  * Revision 1.8  1998/07/13 21:00:10  curt
311  * Wrote access functions for current fgOPTIONS.
312  *
313  * Revision 1.7  1998/05/29 20:35:42  curt
314  * Added zlib support for reading in compressed data files.
315  *
316  * Revision 1.6  1998/05/13 18:25:35  curt
317  * Root path info moved to fgOPTIONS.
318  *
319  * Revision 1.5  1998/04/28 01:19:03  curt
320  * Type-ified fgTIME and fgVIEW
321  *
322  * Revision 1.4  1998/04/26 05:10:02  curt
323  * "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
324  *
325  * Revision 1.3  1998/04/25 22:06:26  curt
326  * Edited cvs log messages in source files ... bad bad bad!
327  *
328  * Revision 1.2  1998/04/24 00:45:03  curt
329  * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
330  * Fixed a bug when generating sky colors.
331  *
332  * Revision 1.1  1998/04/22 13:21:34  curt
333  * C++ - ifing the code a bit.
334  *
335  * Revision 1.11  1998/04/18 04:13:58  curt
336  * Moved fg_debug.c to it's own library.
337  *
338  * Revision 1.10  1998/04/03 21:52:51  curt
339  * Converting to Gnu autoconf system.
340  *
341  * Revision 1.9  1998/03/14 00:27:12  curt
342  * Updated fgGENERAL to a "type" of struct.
343  *
344  * Revision 1.8  1998/02/12 21:59:38  curt
345  * Incorporated code changes contributed by Charlie Hotchkiss
346  * <chotchkiss@namg.us.anritsu.com>
347  *
348  * Revision 1.7  1998/02/09 15:07:48  curt
349  * Minor tweaks.
350  *
351  * Revision 1.6  1998/02/02 20:53:23  curt
352  * To version 0.29
353  *
354  * Revision 1.5  1998/01/27 18:35:53  curt
355  * Minor tweaks.
356  *
357  * Revision 1.4  1998/01/27 00:47:49  curt
358  * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
359  * system and commandline/config file processing code.
360  *
361  * Revision 1.3  1998/01/19 19:26:59  curt
362  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
363  * This should simplify things tremendously.
364  *
365  * Revision 1.2  1998/01/19 18:40:18  curt
366  * Tons of little changes to clean up the code and to remove fatal errors
367  * when building with the c++ compiler.
368  *
369  * Revision 1.1  1998/01/07 03:16:20  curt
370  * Moved from .../Src/Scenery/ to .../Src/Astro/
371  *
372  * Revision 1.24  1997/12/30 22:22:39  curt
373  * Further integration of event manager.
374  *
375  * Revision 1.23  1997/12/30 20:47:53  curt
376  * Integrated new event manager with subsystem initializations.
377  *
378  * Revision 1.22  1997/12/30 16:36:53  curt
379  * Merged in Durk's changes ...
380  *
381  * Revision 1.21  1997/12/19 23:35:00  curt
382  * Lot's of tweaking with sky rendering and lighting.
383  *
384  * Revision 1.20  1997/12/15 23:55:03  curt
385  * Add xgl wrappers for debugging.
386  * Generate terrain normals on the fly.
387  *
388  * Revision 1.19  1997/12/12  19:53:00  curt
389  * Working on lightling and material properties.
390  *
391  * Revision 1.18  1997/12/10 22:37:52  curt
392  * Prepended "fg" on the name of all global structures that didn't have it yet.
393  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
394  *
395  * Revision 1.17  1997/12/09 04:25:33  curt
396  * Working on adding a global lighting params structure.
397  *
398  * Revision 1.16  1997/11/25 19:25:38  curt
399  * Changes to integrate Durk's moon/sun code updates + clean up.
400  *
401  * Revision 1.15  1997/10/30 12:38:45  curt
402  * Working on new scenery subsystem.
403  *
404  * Revision 1.14  1997/10/28 21:00:22  curt
405  * Changing to new terrain format.
406  *
407  * Revision 1.13  1997/10/25 03:18:28  curt
408  * Incorporated sun, moon, and planet position and rendering code contributed
409  * by Durk Talsma.
410  *
411  * Revision 1.12  1997/09/23 00:29:43  curt
412  * Tweaks to get things to compile with gcc-win32.
413  *
414  * Revision 1.11  1997/09/22 14:44:21  curt
415  * Continuing to try to align stars correctly.
416  *
417  * Revision 1.10  1997/09/20 03:34:32  curt
418  * Still trying to get those durned stars aligned properly.
419  *
420  * Revision 1.9  1997/09/18 16:20:09  curt
421  * At dusk/dawn add/remove stars in stages.
422  *
423  * Revision 1.8  1997/09/16 22:14:52  curt
424  * Tweaked time of day lighting equations.  Don't draw stars during the day.
425  *
426  * Revision 1.7  1997/09/16 15:50:31  curt
427  * Working on star alignment and time issues.
428  *
429  * Revision 1.6  1997/09/05 14:17:31  curt
430  * More tweaking with stars.
431  *
432  * Revision 1.5  1997/09/05 01:35:59  curt
433  * Working on getting stars right.
434  *
435  * Revision 1.4  1997/09/04 02:17:38  curt
436  * Shufflin' stuff.
437  *
438  * Revision 1.3  1997/08/29 17:55:28  curt
439  * Worked on properly aligning the stars.
440  *
441  * Revision 1.2  1997/08/27 21:32:30  curt
442  * Restructured view calculation code.  Added stars.
443  *
444  * Revision 1.1  1997/08/27 03:34:48  curt
445  * Initial revision.
446  *
447  */