]> git.mxchange.org Git - simgear.git/blob - Astro/stars.c
Updated fgGENERAL to a "type" of struct.
[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 #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 int fgStarsInit( void ) {
60     FILE *fd;
61     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 0; // Oops, lets not even try to continue. This is critical.
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             } //  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     return 1;  // OK, we got here because initialization worked.
215 }
216
217
218 /* Draw the Stars */
219 void fgStarsRender( void ) {
220     fgFLIGHT *f;
221     struct fgVIEW *v;
222     struct fgLIGHT *l;
223     struct fgTIME *t;
224     int i;
225
226     f = current_aircraft.flight;
227     l = &cur_light_params;
228     t = &cur_time_params;
229     v = &current_view;
230
231     /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
232
233     /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
234
235     /* render the stars */
236     if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
237         /* determine which star structure to draw */
238         if ( l->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
239             i = 0;
240         } else if ( l->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
241             i = 1;
242         } else if ( l->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
243             i = 2;
244         } else {
245             i = 3;
246         }
247
248         /* printf("RENDERING STARS = %d (night)\n", i); */
249
250         xglCallList(stars[i]);
251     } else {
252         /* printf("not RENDERING STARS (day)\n"); */
253     }
254 }
255
256
257 /* $Log$
258 /* Revision 1.9  1998/03/14 00:27:12  curt
259 /* Updated fgGENERAL to a "type" of struct.
260 /*
261  * Revision 1.8  1998/02/12 21:59:38  curt
262  * Incorporated code changes contributed by Charlie Hotchkiss
263  * <chotchkiss@namg.us.anritsu.com>
264  *
265  * Revision 1.7  1998/02/09 15:07:48  curt
266  * Minor tweaks.
267  *
268  * Revision 1.6  1998/02/02 20:53:23  curt
269  * To version 0.29
270  *
271  * Revision 1.5  1998/01/27 18:35:53  curt
272  * Minor tweaks.
273  *
274  * Revision 1.4  1998/01/27 00:47:49  curt
275  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
276  * system and commandline/config file processing code.
277  *
278  * Revision 1.3  1998/01/19 19:26:59  curt
279  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
280  * This should simplify things tremendously.
281  *
282  * Revision 1.2  1998/01/19 18:40:18  curt
283  * Tons of little changes to clean up the code and to remove fatal errors
284  * when building with the c++ compiler.
285  *
286  * Revision 1.1  1998/01/07 03:16:20  curt
287  * Moved from .../Src/Scenery/ to .../Src/Astro/
288  *
289  * Revision 1.24  1997/12/30 22:22:39  curt
290  * Further integration of event manager.
291  *
292  * Revision 1.23  1997/12/30 20:47:53  curt
293  * Integrated new event manager with subsystem initializations.
294  *
295  * Revision 1.22  1997/12/30 16:36:53  curt
296  * Merged in Durk's changes ...
297  *
298  * Revision 1.21  1997/12/19 23:35:00  curt
299  * Lot's of tweaking with sky rendering and lighting.
300  *
301  * Revision 1.20  1997/12/15 23:55:03  curt
302  * Add xgl wrappers for debugging.
303  * Generate terrain normals on the fly.
304  *
305  * Revision 1.19  1997/12/12  19:53:00  curt
306  * Working on lightling and material properties.
307  *
308  * Revision 1.18  1997/12/10 22:37:52  curt
309  * Prepended "fg" on the name of all global structures that didn't have it yet.
310  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
311  *
312  * Revision 1.17  1997/12/09 04:25:33  curt
313  * Working on adding a global lighting params structure.
314  *
315  * Revision 1.16  1997/11/25 19:25:38  curt
316  * Changes to integrate Durk's moon/sun code updates + clean up.
317  *
318  * Revision 1.15  1997/10/30 12:38:45  curt
319  * Working on new scenery subsystem.
320  *
321  * Revision 1.14  1997/10/28 21:00:22  curt
322  * Changing to new terrain format.
323  *
324  * Revision 1.13  1997/10/25 03:18:28  curt
325  * Incorporated sun, moon, and planet position and rendering code contributed
326  * by Durk Talsma.
327  *
328  * Revision 1.12  1997/09/23 00:29:43  curt
329  * Tweaks to get things to compile with gcc-win32.
330  *
331  * Revision 1.11  1997/09/22 14:44:21  curt
332  * Continuing to try to align stars correctly.
333  *
334  * Revision 1.10  1997/09/20 03:34:32  curt
335  * Still trying to get those durned stars aligned properly.
336  *
337  * Revision 1.9  1997/09/18 16:20:09  curt
338  * At dusk/dawn add/remove stars in stages.
339  *
340  * Revision 1.8  1997/09/16 22:14:52  curt
341  * Tweaked time of day lighting equations.  Don't draw stars during the day.
342  *
343  * Revision 1.7  1997/09/16 15:50:31  curt
344  * Working on star alignment and time issues.
345  *
346  * Revision 1.6  1997/09/05 14:17:31  curt
347  * More tweaking with stars.
348  *
349  * Revision 1.5  1997/09/05 01:35:59  curt
350  * Working on getting stars right.
351  *
352  * Revision 1.4  1997/09/04 02:17:38  curt
353  * Shufflin' stuff.
354  *
355  * Revision 1.3  1997/08/29 17:55:28  curt
356  * Worked on properly aligning the stars.
357  *
358  * Revision 1.2  1997/08/27 21:32:30  curt
359  * Restructured view calculation code.  Added stars.
360  *
361  * Revision 1.1  1997/08/27 03:34:48  curt
362  * Initial revision.
363  *
364  */