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