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 **************************************************************************/
43 #include <Aircraft/aircraft.h>
44 #include <Debug/fg_debug.h>
45 #include <Include/fg_constants.h>
46 #include <Include/fg_types.h>
47 #include <Include/fg_zlib.h>
48 #include <Main/options.hxx>
49 #include <Main/views.hxx>
50 #include <Time/fg_time.hxx>
53 #include "planets.hxx"
57 #define EpochStart (631065600)
58 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
59 #define FG_MAX_STARS 3500
61 /* Define four structures, each with varying amounts of stars */
62 static GLint stars[FG_STAR_LEVELS];
65 /* Initialize the Star Management Subsystem */
66 int fgStarsInit( void ) {
67 fgPoint3d starlist[FG_MAX_STARS];
69 /* struct CelestialCoord pltPos; */
70 char path[256], gzpath[256];
71 char line[256], name[256];
73 double right_ascension, declination, magnitude;
74 double min_magnitude[FG_STAR_LEVELS];
75 /* double ra_save, decl_save; */
76 /* double ra_save1, decl_save1; */
77 int i, j, starcount, count;
79 fgPrintf( FG_ASTRO, FG_INFO, "Initializing stars\n");
81 /* build the full path name to the stars data base file */
82 current_options.get_fg_root(path);
83 strcat(path, "/Astro/");
84 strcat(path, "stars");
86 if ( FG_STAR_LEVELS < 4 ) {
87 fgPrintf( FG_ASTRO, FG_EXIT, "Big whups in stars.cxx\n");
90 fgPrintf( FG_ASTRO, FG_INFO, " Loading stars from %s\n", path);
92 // load star data file
93 if ( (fd = fgopen(path, "rb")) == NULL ) {
95 strcat(gzpath, ".gz");
96 if ( (fd = fgopen(gzpath, "rb")) == NULL ) {
97 // Oops, lets not even try to continue. This is critical.
98 fgPrintf( FG_ASTRO, FG_EXIT,
99 "Cannot open star file: '%s'\n", path);
105 // read in each line of the file
106 while ( (fggets(fd, line, 256) != NULL) && (starcount < FG_MAX_STARS) ) {
109 // printf(" Read line = %s", front);
111 // advance to first non-whitespace character
112 while ( (front[0] == ' ') || (front[0] == '\t') ) {
116 // printf(" Line length (after trimming) = %d\n", strlen(front));
118 if ( front[0] == '#' ) {
120 } else if ( strlen(front) <= 1 ) {
127 while ( end[0] != ',' ) {
135 sscanf(front, "%lf,%lf,%lf\n",
136 &right_ascension, &declination, &magnitude);
137 starlist[starcount].x = right_ascension;
138 starlist[starcount].y = declination;
139 starlist[starcount].z = magnitude;
144 min_magnitude[0] = 4.2;
145 min_magnitude[1] = 3.6;
146 min_magnitude[2] = 3.0;
147 min_magnitude[3] = 2.4;
148 min_magnitude[4] = 1.8;
149 min_magnitude[5] = 1.2;
150 min_magnitude[6] = 0.6;
151 min_magnitude[7] = 0.0;
153 // build the various star display lists
154 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
156 stars[i] = xglGenLists(1);
157 xglNewList( stars[i], GL_COMPILE );
158 xglBegin( GL_POINTS );
162 for ( j = 0; j < starcount; j++ ) {
163 magnitude = starlist[j].z;
164 // printf("magnitude = %.2f\n", magnitude);
166 if ( magnitude < min_magnitude[i] ) {
167 right_ascension = starlist[j].x;
168 declination = starlist[j].y;
172 /* scale magnitudes to (0.0 - 1.0) */
173 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
175 /* scale magnitudes again so they look ok */
176 if ( magnitude > 1.0 ) { magnitude = 1.0; }
177 if ( magnitude < 0.0 ) { magnitude = 0.0; }
179 magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
181 magnitude = magnitude * 0.9 +
182 (((FG_STAR_LEVELS - 1) - i) * 0.014);
183 /* printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
184 name, right_ascension, declination, magnitude); */
186 xglColor3f( magnitude, magnitude, magnitude );
187 /*xglColor3f(0,0,0);*/
188 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
189 50000.0*sin(right_ascension)*cos(declination),
190 50000.0*sin(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) );
234 fgPrintf( FG_ASTRO, FG_INFO,
235 " Loading %d stars brighter than %.2f\n",
236 count, min_magnitude[i]);
239 return 1; // OK, we got here because initialization worked.
244 void fgStarsRender( void ) {
251 f = current_aircraft.flight;
252 l = &cur_light_params;
253 t = &cur_time_params;
256 /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
258 /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
260 /* render the stars */
261 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
262 /* determine which star structure to draw */
263 if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
265 } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
267 } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
269 } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
271 } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
273 } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
275 } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
281 /* printf("RENDERING STARS = %d (night)\n", i); */
283 xglCallList(stars[i]);
285 /* printf("not RENDERING STARS (day)\n"); */
291 /* Revision 1.11 1998/08/25 20:53:29 curt
292 /* Shuffled $FG_ROOT file layout.
294 * Revision 1.10 1998/08/10 20:33:09 curt
295 * Rewrote star loading and rendering to:
296 * 1. significantly improve load speed
297 * 2. transition from no stars to stars through eight stages.
299 * Revision 1.9 1998/08/06 12:45:20 curt
300 * Modified to bring in stars in 8 increments based on magnitude, not number
303 * Revision 1.8 1998/07/13 21:00:10 curt
304 * Wrote access functions for current fgOPTIONS.
306 * Revision 1.7 1998/05/29 20:35:42 curt
307 * Added zlib support for reading in compressed data files.
309 * Revision 1.6 1998/05/13 18:25:35 curt
310 * Root path info moved to fgOPTIONS.
312 * Revision 1.5 1998/04/28 01:19:03 curt
313 * Type-ified fgTIME and fgVIEW
315 * Revision 1.4 1998/04/26 05:10:02 curt
316 * "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
318 * Revision 1.3 1998/04/25 22:06:26 curt
319 * Edited cvs log messages in source files ... bad bad bad!
321 * Revision 1.2 1998/04/24 00:45:03 curt
322 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
323 * Fixed a bug when generating sky colors.
325 * Revision 1.1 1998/04/22 13:21:34 curt
326 * C++ - ifing the code a bit.
328 * Revision 1.11 1998/04/18 04:13:58 curt
329 * Moved fg_debug.c to it's own library.
331 * Revision 1.10 1998/04/03 21:52:51 curt
332 * Converting to Gnu autoconf system.
334 * Revision 1.9 1998/03/14 00:27:12 curt
335 * Updated fgGENERAL to a "type" of struct.
337 * Revision 1.8 1998/02/12 21:59:38 curt
338 * Incorporated code changes contributed by Charlie Hotchkiss
339 * <chotchkiss@namg.us.anritsu.com>
341 * Revision 1.7 1998/02/09 15:07:48 curt
344 * Revision 1.6 1998/02/02 20:53:23 curt
347 * Revision 1.5 1998/01/27 18:35:53 curt
350 * Revision 1.4 1998/01/27 00:47:49 curt
351 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
352 * system and commandline/config file processing code.
354 * Revision 1.3 1998/01/19 19:26:59 curt
355 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
356 * This should simplify things tremendously.
358 * Revision 1.2 1998/01/19 18:40:18 curt
359 * Tons of little changes to clean up the code and to remove fatal errors
360 * when building with the c++ compiler.
362 * Revision 1.1 1998/01/07 03:16:20 curt
363 * Moved from .../Src/Scenery/ to .../Src/Astro/
365 * Revision 1.24 1997/12/30 22:22:39 curt
366 * Further integration of event manager.
368 * Revision 1.23 1997/12/30 20:47:53 curt
369 * Integrated new event manager with subsystem initializations.
371 * Revision 1.22 1997/12/30 16:36:53 curt
372 * Merged in Durk's changes ...
374 * Revision 1.21 1997/12/19 23:35:00 curt
375 * Lot's of tweaking with sky rendering and lighting.
377 * Revision 1.20 1997/12/15 23:55:03 curt
378 * Add xgl wrappers for debugging.
379 * Generate terrain normals on the fly.
381 * Revision 1.19 1997/12/12 19:53:00 curt
382 * Working on lightling and material properties.
384 * Revision 1.18 1997/12/10 22:37:52 curt
385 * Prepended "fg" on the name of all global structures that didn't have it yet.
386 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
388 * Revision 1.17 1997/12/09 04:25:33 curt
389 * Working on adding a global lighting params structure.
391 * Revision 1.16 1997/11/25 19:25:38 curt
392 * Changes to integrate Durk's moon/sun code updates + clean up.
394 * Revision 1.15 1997/10/30 12:38:45 curt
395 * Working on new scenery subsystem.
397 * Revision 1.14 1997/10/28 21:00:22 curt
398 * Changing to new terrain format.
400 * Revision 1.13 1997/10/25 03:18:28 curt
401 * Incorporated sun, moon, and planet position and rendering code contributed
404 * Revision 1.12 1997/09/23 00:29:43 curt
405 * Tweaks to get things to compile with gcc-win32.
407 * Revision 1.11 1997/09/22 14:44:21 curt
408 * Continuing to try to align stars correctly.
410 * Revision 1.10 1997/09/20 03:34:32 curt
411 * Still trying to get those durned stars aligned properly.
413 * Revision 1.9 1997/09/18 16:20:09 curt
414 * At dusk/dawn add/remove stars in stages.
416 * Revision 1.8 1997/09/16 22:14:52 curt
417 * Tweaked time of day lighting equations. Don't draw stars during the day.
419 * Revision 1.7 1997/09/16 15:50:31 curt
420 * Working on star alignment and time issues.
422 * Revision 1.6 1997/09/05 14:17:31 curt
423 * More tweaking with stars.
425 * Revision 1.5 1997/09/05 01:35:59 curt
426 * Working on getting stars right.
428 * Revision 1.4 1997/09/04 02:17:38 curt
431 * Revision 1.3 1997/08/29 17:55:28 curt
432 * Worked on properly aligning the stars.
434 * Revision 1.2 1997/08/27 21:32:30 curt
435 * Restructured view calculation code. Added stars.
437 * Revision 1.1 1997/08/27 03:34:48 curt