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