]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
692ceaba10827b29c90e6439ed2f9e122fce251b
[flightgear.git] / Scenery / 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
38 #include "orbits.h"
39 #include "planets.h"
40 #include "stars.h"
41
42 #include "../constants.h"
43 #include "../general.h"
44 #include "../Aircraft/aircraft.h"
45 #include "../Main/views.h"
46 #include "../Time/fg_time.h"
47
48
49 #define EpochStart           (631065600)
50 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
51
52
53 /* Define four structures, each with varying amounts of stars */
54 /* static */  GLint stars[FG_STAR_LEVELS];
55
56
57 /* Initialize the Star Management Subsystem */
58 void fgStarsInit() {
59     FILE *fd;
60     struct fgGENERAL *g;
61     struct CelestialCoord pltPos;
62     char path[1024];
63     char line[256], name[256];
64     char *front, *end;
65     double right_ascension, declination, magnitude;
66     double ra_save, decl_save;
67     double ra_save1, decl_save1;
68     int count, i, j, max_stars;
69
70     g = &general;
71
72     /* build the full path name to the stars data base file */
73     path[0] = '\0';
74     strcat(path, g->root_dir);
75     strcat(path, "/Scenery/");
76     strcat(path, "Stars.dat");
77
78     max_stars = FG_MAX_STARS;
79
80     for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
81         printf("Loading %d Stars: %s\n", max_stars, path);
82
83         if ( (fd = fopen(path, "r")) == NULL ) {
84             printf("Cannot open star file: '%s'\n", path);
85             return;
86         }
87         
88         stars[i] = glGenLists(1);
89         glNewList( stars[i], GL_COMPILE );
90         glBegin( GL_POINTS );
91
92         /* read in each line of the file */
93         count = 0;
94         while ( (fgets(line, 256, fd) != NULL) && (count < max_stars) ) {
95             front = line;
96
97             /* printf("Read line = %s", front); */
98
99             /* advance to first non-whitespace character */
100             while ( (front[0] == ' ') || (front[0] == '\t') ) {
101                 front++;
102             }
103
104             /* printf("Line length (after trimming) = %d\n", strlen(front)); */
105
106             if ( front[0] == '#' ) {
107                 /* comment */
108             } else if ( strlen(front) <= 1 ) {
109                 /* blank line */
110             } else {
111                 /* star data line */
112                 
113                 /* get name */
114                 end = front;
115                 while ( end[0] != ',' ) {
116                     end++;
117                 }
118                 end[0] = '\0';
119                 strcpy(name, front);
120                 front = end;
121                 front++;
122
123                 sscanf(front, "%lf,%lf,%lf\n", 
124                        &right_ascension, &declination, &magnitude);
125
126                 /*
127                 if ( strcmp(name, "Betelgeuse") == 0 ) {
128                     printf("  *** Marking %s\n", name);
129                     ra_save = right_ascension;
130                     decl_save = declination;
131                 }
132                 */
133
134                 /*
135                 if ( strcmp(name, "Alnilam") == 0 ) {
136                     printf("  *** Marking %s\n", name);
137                     ra_save1 = right_ascension;
138                     decl_save1 = declination;
139                 }
140                 */
141
142                 /* scale magnitudes to (0.0 - 1.0) */
143                 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
144
145                 /* scale magnitudes again so they look ok */
146                 if ( magnitude > 1.0 ) { magnitude = 1.0; }
147                 if ( magnitude < 0.0 ) { magnitude = 0.0; }
148                 magnitude = 
149                     magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1);
150                 /* printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
151                        name, right_ascension, declination, magnitude); */
152
153                 glColor3f( magnitude, magnitude, magnitude );
154                 /*glColor3f(0,0,0);*/
155                 glVertex3f( 190000.0 * cos(right_ascension) * cos(declination),
156                             190000.0 * sin(right_ascension) * cos(declination),
157                             190000.0 * sin(declination) );
158                 
159                 count++;
160             } /* if valid line */
161
162         } /* while */
163
164         fclose(fd);
165
166         /* Add the planets to all four display lists */
167         for ( j = 2; j < 9; j++ ) {
168             pltPos = fgCalculatePlanet(pltOrbElements[j], 
169                                        pltOrbElements[0], cur_time_params);
170             printf("Planet found at %f (ra), %f (dec)\n", 
171                    pltPos.RightAscension, pltPos.Declination);
172             /* give the planets a temporary color, for testing purposes */
173             glColor3f( 1.0, 0.0, 0.0);
174             glVertex3f( 190000.0 * cos(pltPos.RightAscension) * 
175                         cos(pltPos.Declination),
176                         190000.0 * sin(pltPos.RightAscension) * 
177                         cos(pltPos.Declination),
178                         190000.0 * sin(pltPos.Declination) );
179         }
180         glEnd();
181
182         /*
183         glBegin(GL_LINE_LOOP);
184         glColor3f(1.0, 0.0, 0.0);
185         glVertex3f( 190000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
186                     190000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
187                     190000.0 * sin(decl_save-0.2) );
188         glVertex3f( 190000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
189                     190000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
190                     190000.0 * sin(decl_save-0.2) );
191         glVertex3f( 190000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
192                     190000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
193                     190000.0 * sin(decl_save+0.2) );
194         glVertex3f( 190000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
195                     190000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
196                     190000.0 * sin(decl_save+0.2) );
197         glEnd();
198         */
199
200         /*
201         glBegin(GL_LINE_LOOP);
202         glColor3f(0.0, 1.0, 0.0);
203         glVertex3f( 190000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
204                     190000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
205                     190000.0 * sin(decl_save1-0.2) );
206         glVertex3f( 190000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
207                     190000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
208                     190000.0 * sin(decl_save1-0.2) );
209         glVertex3f( 190000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
210                     190000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
211                     190000.0 * sin(decl_save1+0.2) );
212         glVertex3f( 190000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
213                     190000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
214                     190000.0 * sin(decl_save1+0.2) );
215         glEnd();
216         */
217        
218         glEndList();
219
220         max_stars /= 2;
221     }
222 }
223
224
225 /* Draw the Stars */
226 void fgStarsRender() {
227     struct fgFLIGHT *f;
228     struct fgVIEW *v;
229     struct fgLIGHT *l;
230     struct fgTIME *t;
231     int i;
232
233     f = &current_aircraft.flight;
234     l = &cur_light_params;
235     t = &cur_time_params;
236     v = &current_view;
237
238     /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
239
240     /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
241
242     /* render the stars */
243     if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
244         /* determine which star structure to draw */
245         if ( l->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
246             i = 0;
247         } else if ( l->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
248             i = 1;
249         } else if ( l->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
250             i = 2;
251         } else {
252             i = 3;
253         }
254
255         printf("RENDERING STARS = %d (night)\n", i);
256
257         glDisable( GL_LIGHTING );
258         glCallList(stars[i]);
259         glEnable( GL_LIGHTING );
260     } else {
261         printf("not RENDERING STARS (day)\n");
262     }
263 }
264
265
266 /* $Log$
267 /* Revision 1.19  1997/12/12 19:53:00  curt
268 /* Working on lightling and material properties.
269 /*
270  * Revision 1.18  1997/12/10 22:37:52  curt
271  * Prepended "fg" on the name of all global structures that didn't have it yet.
272  * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
273  *
274  * Revision 1.17  1997/12/09 04:25:33  curt
275  * Working on adding a global lighting params structure.
276  *
277  * Revision 1.16  1997/11/25 19:25:38  curt
278  * Changes to integrate Durk's moon/sun code updates + clean up.
279  *
280  * Revision 1.15  1997/10/30 12:38:45  curt
281  * Working on new scenery subsystem.
282  *
283  * Revision 1.14  1997/10/28 21:00:22  curt
284  * Changing to new terrain format.
285  *
286  * Revision 1.13  1997/10/25 03:18:28  curt
287  * Incorporated sun, moon, and planet position and rendering code contributed
288  * by Durk Talsma.
289  *
290  * Revision 1.12  1997/09/23 00:29:43  curt
291  * Tweaks to get things to compile with gcc-win32.
292  *
293  * Revision 1.11  1997/09/22 14:44:21  curt
294  * Continuing to try to align stars correctly.
295  *
296  * Revision 1.10  1997/09/20 03:34:32  curt
297  * Still trying to get those durned stars aligned properly.
298  *
299  * Revision 1.9  1997/09/18 16:20:09  curt
300  * At dusk/dawn add/remove stars in stages.
301  *
302  * Revision 1.8  1997/09/16 22:14:52  curt
303  * Tweaked time of day lighting equations.  Don't draw stars during the day.
304  *
305  * Revision 1.7  1997/09/16 15:50:31  curt
306  * Working on star alignment and time issues.
307  *
308  * Revision 1.6  1997/09/05 14:17:31  curt
309  * More tweaking with stars.
310  *
311  * Revision 1.5  1997/09/05 01:35:59  curt
312  * Working on getting stars right.
313  *
314  * Revision 1.4  1997/09/04 02:17:38  curt
315  * Shufflin' stuff.
316  *
317  * Revision 1.3  1997/08/29 17:55:28  curt
318  * Worked on properly aligning the stars.
319  *
320  * Revision 1.2  1997/08/27 21:32:30  curt
321  * Restructured view calculation code.  Added stars.
322  *
323  * Revision 1.1  1997/08/27 03:34:48  curt
324  * Initial revision.
325  *
326  */