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 **************************************************************************/
40 #include "../constants.h"
41 #include "../general.h"
43 #include "../GLUT/views.h"
44 #include "../Aircraft/aircraft.h"
45 #include "../Time/fg_time.h"
48 #define EpochStart (631065600)
49 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
52 /* Define four structures, each with varying amounts of stars */
53 static GLint stars[FG_STAR_LEVELS];
56 /* Initialize the Star Management Subsystem */
61 char line[256], name[256];
63 double right_ascension, declination, magnitude;
64 double ra_save, decl_save;
65 double ra_save1, decl_save1;
66 int count, i, max_stars;
70 /* build the full path name to the stars data base file */
72 strcat(path, g->root_dir);
73 strcat(path, "/Scenery/");
74 strcat(path, "Stars.dat");
76 max_stars = FG_MAX_STARS;
78 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
79 printf("Loading %d Stars: %s\n", max_stars, path);
81 if ( (fd = fopen(path, "r")) == NULL ) {
82 printf("Cannot open star file: '%s'\n", path);
86 stars[i] = glGenLists(1);
87 glNewList( stars[i], GL_COMPILE );
90 /* read in each line of the file */
92 while ( (fgets(line, 256, fd) != NULL) && (count < max_stars) ) {
95 /* printf("Read line = %s", front); */
97 /* advance to first non-whitespace character */
98 while ( (front[0] == ' ') || (front[0] == '\t') ) {
102 /* printf("Line length (after trimming) = %d\n", strlen(front)); */
104 if ( front[0] == '#' ) {
106 } else if ( strlen(front) <= 1 ) {
113 while ( end[0] != ',' ) {
121 sscanf(front, "%lf,%lf,%lf\n",
122 &right_ascension, &declination, &magnitude);
124 if ( strcmp(name, "Hamal") == 0 ) {
125 printf(" *** Marking %s\n", name);
126 ra_save = right_ascension;
127 decl_save = declination;
130 if ( strcmp(name, "Algenib") == 0 ) {
131 printf(" *** Marking %s\n", name);
132 ra_save1 = right_ascension;
133 decl_save1 = declination;
136 /* scale magnitudes to (0.0 - 1.0) */
137 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
139 /* scale magnitudes again so they look ok */
140 if ( magnitude > 1.0 ) { magnitude = 1.0; }
141 if ( magnitude < 0.0 ) { magnitude = 0.0; }
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);
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) );
153 } /* if valid line */
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) );
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) );
202 void fgStarsRender() {
207 static double warp = 0;
210 f = ¤t_aircraft.flight;
211 t = &cur_time_params;
214 /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
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 ) ) {
220 } else if ( t->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
222 } else if ( t->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
228 printf("RENDERING STARS = %d (night)\n", i);
231 glDisable( GL_LIGHTING );
234 glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
236 angle = t->gst * 15.0; /* 15 degrees per hour rotation */
240 glRotatef( -(angle+warp), 0.0, 0.0, 1.0 );
241 printf("Rotating stars by %.2f degrees + %.2f degrees\n",-angle,-warp);
243 glCallList(stars[i]);
246 glEnable( GL_LIGHTING );
249 printf("not RENDERING STARS (day)\n");
255 /* Revision 1.10 1997/09/20 03:34:32 curt
256 /* Still trying to get those durned stars aligned properly.
258 * Revision 1.9 1997/09/18 16:20:09 curt
259 * At dusk/dawn add/remove stars in stages.
261 * Revision 1.8 1997/09/16 22:14:52 curt
262 * Tweaked time of day lighting equations. Don't draw stars during the day.
264 * Revision 1.7 1997/09/16 15:50:31 curt
265 * Working on star alignment and time issues.
267 * Revision 1.6 1997/09/05 14:17:31 curt
268 * More tweaking with stars.
270 * Revision 1.5 1997/09/05 01:35:59 curt
271 * Working on getting stars right.
273 * Revision 1.4 1997/09/04 02:17:38 curt
276 * Revision 1.3 1997/08/29 17:55:28 curt
277 * Worked on properly aligning the stars.
279 * Revision 1.2 1997/08/27 21:32:30 curt
280 * Restructured view calculation code. Added stars.
282 * Revision 1.1 1997/08/27 03:34:48 curt