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