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