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