]> git.mxchange.org Git - flightgear.git/blob - Scenery/stars.c
At dusk/dawn add/remove stars in stages.
[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, "Hamal") == 0 ) {
125                     printf("  *** Marking %s\n", name);
126                     ra_save = right_ascension;
127                     decl_save = declination;
128                 }
129
130                 if ( strcmp(name, "Algenib") == 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 * sin(right_ascension) * cos(declination),
149                             -190000.0 * cos(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 * sin(ra_save-0.2) * cos(decl_save-0.2),
165                     -190000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
166                     190000.0 * sin(decl_save-0.2) );
167         glVertex3f( 190000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
168                     -190000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
169                     190000.0 * sin(decl_save-0.2) );
170         glVertex3f( 190000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
171                     -190000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
172                     190000.0 * sin(decl_save+0.2) );
173         glVertex3f( 190000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
174                     -190000.0 * cos(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 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
181                     -190000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
182                     190000.0 * sin(decl_save1-0.2) );
183         glVertex3f( 190000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
184                     -190000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
185                     190000.0 * sin(decl_save1-0.2) );
186         glVertex3f( 190000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
187                     -190000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
188                     190000.0 * sin(decl_save1+0.2) );
189         glVertex3f( 190000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
190                     -190000.0 * cos(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     if ( t->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
217         /* determine which star structure to draw */
218         if ( t->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
219             i = 0;
220         } else if ( t->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
221             i = 1;
222         } else if ( t->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
223             i = 2;
224         } else {
225             i = 3;
226         }
227
228         printf("RENDERING STARS = %d (night)\n", i);
229
230         glDisable( GL_FOG );
231         glDisable( GL_LIGHTING );
232         glPushMatrix();
233
234         glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
235
236         angle = FG_2PI * t->lst / 24.0;
237         /* warp += 1.0 * DEG_TO_RAD; */
238         warp = 15.0 * DEG_TO_RAD;
239         glRotatef( -(angle+warp) * RAD_TO_DEG, 0.0, 0.0, 1.0 );
240         printf("Rotating stars by %.2f + %.2f\n", -angle * RAD_TO_DEG,
241                -warp * RAD_TO_DEG);
242
243         glCallList(stars[i]);
244
245         glPopMatrix();
246         glEnable( GL_LIGHTING );
247         glEnable( GL_FOG );
248     } else {
249         printf("not RENDERING STARS (day)\n");
250     }
251 }
252
253
254 /* $Log$
255 /* Revision 1.9  1997/09/18 16:20:09  curt
256 /* At dusk/dawn add/remove stars in stages.
257 /*
258  * Revision 1.8  1997/09/16 22:14:52  curt
259  * Tweaked time of day lighting equations.  Don't draw stars during the day.
260  *
261  * Revision 1.7  1997/09/16 15:50:31  curt
262  * Working on star alignment and time issues.
263  *
264  * Revision 1.6  1997/09/05 14:17:31  curt
265  * More tweaking with stars.
266  *
267  * Revision 1.5  1997/09/05 01:35:59  curt
268  * Working on getting stars right.
269  *
270  * Revision 1.4  1997/09/04 02:17:38  curt
271  * Shufflin' stuff.
272  *
273  * Revision 1.3  1997/08/29 17:55:28  curt
274  * Worked on properly aligning the stars.
275  *
276  * Revision 1.2  1997/08/27 21:32:30  curt
277  * Restructured view calculation code.  Added stars.
278  *
279  * Revision 1.1  1997/08/27 03:34:48  curt
280  * Initial revision.
281  *
282  */