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