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 **************************************************************************/
37 #include "../XGL/xgl.h"
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"
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 */
59 void fgStarsInit( void ) {
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;
71 printf("Initializing stars\n");
75 /* build the full path name to the stars data base file */
77 strcat(path, g->root_dir);
78 strcat(path, "/Scenery/");
79 strcat(path, "Stars.dat");
81 max_stars = FG_MAX_STARS;
83 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
84 printf(" Loading %d Stars: %s\n", max_stars, path);
86 if ( (fd = fopen(path, "r")) == NULL ) {
87 printf("Cannot open star file: '%s'\n", path);
91 stars[i] = xglGenLists(1);
92 xglNewList( stars[i], GL_COMPILE );
93 xglBegin( GL_POINTS );
95 /* read in each line of the file */
97 while ( (fgets(line, 256, fd) != NULL) && (count < max_stars) ) {
100 /* printf(" Read line = %s", front); */
102 /* advance to first non-whitespace character */
103 while ( (front[0] == ' ') || (front[0] == '\t') ) {
107 /* printf(" Line length (after trimming) = %d\n", strlen(front));*/
109 if ( front[0] == '#' ) {
111 } else if ( strlen(front) <= 1 ) {
118 while ( end[0] != ',' ) {
126 sscanf(front, "%lf,%lf,%lf\n",
127 &right_ascension, &declination, &magnitude);
130 if ( strcmp(name, "Betelgeuse") == 0 ) {
131 printf(" *** Marking %s\n", name);
132 ra_save = right_ascension;
133 decl_save = declination;
138 if ( strcmp(name, "Alnilam") == 0 ) {
139 printf(" *** Marking %s\n", name);
140 ra_save1 = right_ascension;
141 decl_save1 = declination;
145 /* scale magnitudes to (0.0 - 1.0) */
146 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
148 /* scale magnitudes again so they look ok */
149 if ( magnitude > 1.0 ) { magnitude = 1.0; }
150 if ( magnitude < 0.0 ) { magnitude = 0.0; }
152 magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1);
153 /* printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
154 name, right_ascension, declination, magnitude); */
156 xglColor3f( magnitude, magnitude, magnitude );
157 /*xglColor3f(0,0,0);*/
158 xglVertex3f( 50000.0 * cos(right_ascension) * cos(declination),
159 50000.0 * sin(right_ascension) * cos(declination),
160 50000.0 * sin(declination) );
163 } /* if valid line */
169 /* Add the planets to all four display lists */
170 for ( j = 2; j < 9; j++ ) {
171 pltPos = fgCalculatePlanet(pltOrbElements[j],
172 pltOrbElements[0], cur_time_params, j);
173 /* give the planets a temporary color, for testing purposes */
174 /* xglColor3f( 1.0, 0.0, 0.0); */
176 /* scale magnitudes to (0.0 - 1.0) */
177 magnitude = (0.0 - pltPos.magnitude) / 5.0 + 1.0;
179 /* scale magnitudes again so they look ok */
180 if ( magnitude > 1.0 ) { magnitude = 1.0; }
181 if ( magnitude < 0.0 ) { magnitude = 0.0; }
183 magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1);
186 xglColor3f(magnitude, magnitude, magnitude);
188 xglVertex3f( 50000.0 * cos(pltPos.RightAscension) *
189 cos(pltPos.Declination),
190 50000.0 * sin(pltPos.RightAscension) *
191 cos(pltPos.Declination),
192 50000.0 * sin(pltPos.Declination) );
197 xglBegin(GL_LINE_LOOP);
198 xglColor3f(1.0, 0.0, 0.0);
199 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
200 50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
201 50000.0 * sin(decl_save-0.2) );
202 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
203 50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
204 50000.0 * sin(decl_save-0.2) );
205 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
206 50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
207 50000.0 * sin(decl_save+0.2) );
208 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
209 50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
210 50000.0 * sin(decl_save+0.2) );
215 xglBegin(GL_LINE_LOOP);
216 xglColor3f(0.0, 1.0, 0.0);
217 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
218 50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
219 50000.0 * sin(decl_save1-0.2) );
220 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
221 50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
222 50000.0 * sin(decl_save1-0.2) );
223 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
224 50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
225 50000.0 * sin(decl_save1+0.2) );
226 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
227 50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
228 50000.0 * sin(decl_save1+0.2) );
240 void fgStarsRender( void ) {
247 f = ¤t_aircraft.flight;
248 l = &cur_light_params;
249 t = &cur_time_params;
252 /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
254 /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
256 /* render the stars */
257 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
258 /* determine which star structure to draw */
259 if ( l->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
261 } else if ( l->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
263 } else if ( l->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
269 /* printf("RENDERING STARS = %d (night)\n", i); */
271 xglCallList(stars[i]);
273 /* printf("not RENDERING STARS (day)\n"); */
279 /* Revision 1.2 1998/01/19 18:40:18 curt
280 /* Tons of little changes to clean up the code and to remove fatal errors
281 /* when building with the c++ compiler.
283 * Revision 1.1 1998/01/07 03:16:20 curt
284 * Moved from .../Src/Scenery/ to .../Src/Astro/
286 * Revision 1.24 1997/12/30 22:22:39 curt
287 * Further integration of event manager.
289 * Revision 1.23 1997/12/30 20:47:53 curt
290 * Integrated new event manager with subsystem initializations.
292 * Revision 1.22 1997/12/30 16:36:53 curt
293 * Merged in Durk's changes ...
295 * Revision 1.21 1997/12/19 23:35:00 curt
296 * Lot's of tweaking with sky rendering and lighting.
298 * Revision 1.20 1997/12/15 23:55:03 curt
299 * Add xgl wrappers for debugging.
300 * Generate terrain normals on the fly.
302 * Revision 1.19 1997/12/12 19:53:00 curt
303 * Working on lightling and material properties.
305 * Revision 1.18 1997/12/10 22:37:52 curt
306 * Prepended "fg" on the name of all global structures that didn't have it yet.
307 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
309 * Revision 1.17 1997/12/09 04:25:33 curt
310 * Working on adding a global lighting params structure.
312 * Revision 1.16 1997/11/25 19:25:38 curt
313 * Changes to integrate Durk's moon/sun code updates + clean up.
315 * Revision 1.15 1997/10/30 12:38:45 curt
316 * Working on new scenery subsystem.
318 * Revision 1.14 1997/10/28 21:00:22 curt
319 * Changing to new terrain format.
321 * Revision 1.13 1997/10/25 03:18:28 curt
322 * Incorporated sun, moon, and planet position and rendering code contributed
325 * Revision 1.12 1997/09/23 00:29:43 curt
326 * Tweaks to get things to compile with gcc-win32.
328 * Revision 1.11 1997/09/22 14:44:21 curt
329 * Continuing to try to align stars correctly.
331 * Revision 1.10 1997/09/20 03:34:32 curt
332 * Still trying to get those durned stars aligned properly.
334 * Revision 1.9 1997/09/18 16:20:09 curt
335 * At dusk/dawn add/remove stars in stages.
337 * Revision 1.8 1997/09/16 22:14:52 curt
338 * Tweaked time of day lighting equations. Don't draw stars during the day.
340 * Revision 1.7 1997/09/16 15:50:31 curt
341 * Working on star alignment and time issues.
343 * Revision 1.6 1997/09/05 14:17:31 curt
344 * More tweaking with stars.
346 * Revision 1.5 1997/09/05 01:35:59 curt
347 * Working on getting stars right.
349 * Revision 1.4 1997/09/04 02:17:38 curt
352 * Revision 1.3 1997/08/29 17:55:28 curt
353 * Worked on properly aligning the stars.
355 * Revision 1.2 1997/08/27 21:32:30 curt
356 * Restructured view calculation code. Added stars.
358 * Revision 1.1 1997/08/27 03:34:48 curt