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