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