1 /**************************************************************************
2 * stars.c -- data structures and routines for managing and rendering stars.
4 * Written by Curtis Olson, started August 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
42 #include "../constants.h"
43 #include "../general.h"
45 #include "../Aircraft/aircraft.h"
46 #include "../Main/views.h"
47 #include "../Time/fg_time.h"
50 #define EpochStart (631065600)
51 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
54 /* Define four structures, each with varying amounts of stars */
55 static GLint stars[FG_STAR_LEVELS];
58 /* Initialize the Star Management Subsystem */
62 struct CelestialCoord pltPos;
64 char line[256], name[256];
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;
73 /* build the full path name to the stars data base file */
75 strcat(path, g->root_dir);
76 strcat(path, "/Scenery/");
77 strcat(path, "Stars.dat");
79 max_stars = FG_MAX_STARS;
81 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
82 printf("Loading %d Stars: %s\n", max_stars, path);
84 if ( (fd = fopen(path, "r")) == NULL ) {
85 printf("Cannot open star file: '%s'\n", path);
89 stars[i] = glGenLists(1);
90 glNewList( stars[i], GL_COMPILE );
93 /* read in each line of the file */
95 while ( (fgets(line, 256, fd) != NULL) && (count < max_stars) ) {
98 /* printf("Read line = %s", front); */
100 /* advance to first non-whitespace character */
101 while ( (front[0] == ' ') || (front[0] == '\t') ) {
105 /* printf("Line length (after trimming) = %d\n", strlen(front)); */
107 if ( front[0] == '#' ) {
109 } else if ( strlen(front) <= 1 ) {
116 while ( end[0] != ',' ) {
124 sscanf(front, "%lf,%lf,%lf\n",
125 &right_ascension, &declination, &magnitude);
127 if ( strcmp(name, "Betelgeuse") == 0 ) {
128 printf(" *** Marking %s\n", name);
129 ra_save = right_ascension;
130 decl_save = declination;
133 if ( strcmp(name, "Alnilam") == 0 ) {
134 printf(" *** Marking %s\n", name);
135 ra_save1 = right_ascension;
136 decl_save1 = declination;
139 /* scale magnitudes to (0.0 - 1.0) */
140 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
142 /* scale magnitudes again so they look ok */
143 if ( magnitude > 1.0 ) { magnitude = 1.0; }
144 if ( magnitude < 0.0 ) { magnitude = 0.0; }
146 magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1);
147 /* printf("Found star: %d %s, %.3f %.3f %.3f\n", count,
148 name, right_ascension, declination, magnitude); */
150 glColor3f( magnitude, magnitude, magnitude );
151 /*glColor3f(0,0,0);*/
152 glVertex3f( 190000.0 * cos(right_ascension) * cos(declination),
153 190000.0 * sin(right_ascension) * cos(declination),
154 190000.0 * sin(declination) );
157 } /* if valid line */
163 /* Add the planets to all four display lists */
164 for ( j = 2; j < 9; j++ ) {
165 pltPos = fgCalculatePlanet(pltOrbElements[j],
166 pltOrbElements[0], cur_time_params);
167 printf("Planet found at %f (ra), %f (dec)\n",
168 pltPos.RightAscension, pltPos.Declination);
169 /* give the planets a temporary color, for testing purposes */
170 glColor3f( 1.0, 0.0, 0.0);
171 glVertex3f( 190000.0 * cos(pltPos.RightAscension) *
172 cos(pltPos.Declination),
173 190000.0 * sin(pltPos.RightAscension) *
174 cos(pltPos.Declination),
175 190000.0 * sin(pltPos.Declination) );
179 glBegin(GL_LINE_LOOP);
180 glColor3f(1.0, 0.0, 0.0);
181 glVertex3f( 190000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
182 190000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
183 190000.0 * sin(decl_save-0.2) );
184 glVertex3f( 190000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
185 190000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
186 190000.0 * sin(decl_save-0.2) );
187 glVertex3f( 190000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
188 190000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
189 190000.0 * sin(decl_save+0.2) );
190 glVertex3f( 190000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
191 190000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
192 190000.0 * sin(decl_save+0.2) );
195 glBegin(GL_LINE_LOOP);
196 glColor3f(0.0, 1.0, 0.0);
197 glVertex3f( 190000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
198 190000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
199 190000.0 * sin(decl_save1-0.2) );
200 glVertex3f( 190000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
201 190000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
202 190000.0 * sin(decl_save1-0.2) );
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) );
219 void fgStarsRender() {
224 static double warp = 0;
227 f = ¤t_aircraft.flight;
228 t = &cur_time_params;
231 /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
233 /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
235 if ( t->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
236 /* determine which star structure to draw */
237 if ( t->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
239 } else if ( t->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
241 } else if ( t->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
247 printf("RENDERING STARS = %d (night)\n", i);
250 glDisable( GL_LIGHTING );
253 glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
255 angle = t->gst * 15.0; /* 15 degrees per hour rotation */
259 glRotatef( (angle+warp), 0.0, 0.0, -1.0 );
260 printf("Rotating stars by %.2f degrees + %.2f degrees\n",angle,warp);
262 glCallList(stars[i]);
265 glEnable( GL_LIGHTING );
268 printf("not RENDERING STARS (day)\n");
274 /* Revision 1.14 1997/10/28 21:00:22 curt
275 /* Changing to new terrain format.
277 * Revision 1.13 1997/10/25 03:18:28 curt
278 * Incorporated sun, moon, and planet position and rendering code contributed
281 * Revision 1.12 1997/09/23 00:29:43 curt
282 * Tweaks to get things to compile with gcc-win32.
284 * Revision 1.11 1997/09/22 14:44:21 curt
285 * Continuing to try to align stars correctly.
287 * Revision 1.10 1997/09/20 03:34:32 curt
288 * Still trying to get those durned stars aligned properly.
290 * Revision 1.9 1997/09/18 16:20:09 curt
291 * At dusk/dawn add/remove stars in stages.
293 * Revision 1.8 1997/09/16 22:14:52 curt
294 * Tweaked time of day lighting equations. Don't draw stars during the day.
296 * Revision 1.7 1997/09/16 15:50:31 curt
297 * Working on star alignment and time issues.
299 * Revision 1.6 1997/09/05 14:17:31 curt
300 * More tweaking with stars.
302 * Revision 1.5 1997/09/05 01:35:59 curt
303 * Working on getting stars right.
305 * Revision 1.4 1997/09/04 02:17:38 curt
308 * Revision 1.3 1997/08/29 17:55:28 curt
309 * Worked on properly aligning the stars.
311 * Revision 1.2 1997/08/27 21:32:30 curt
312 * Restructured view calculation code. Added stars.
314 * Revision 1.1 1997/08/27 03:34:48 curt