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_zlib.h>
47 #include <Main/options.hxx>
48 #include <Main/views.hxx>
49 #include <Time/fg_time.hxx>
52 #include "planets.hxx"
56 #define EpochStart (631065600)
57 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
60 /* Define four structures, each with varying amounts of stars */
61 /* static */ GLint stars[FG_STAR_LEVELS];
64 /* Initialize the Star Management Subsystem */
65 int fgStarsInit( void ) {
68 /* struct CelestialCoord pltPos; */
69 char path[256], gzpath[256];
70 char line[256], name[256];
72 double right_ascension, declination, magnitude;
73 /* double ra_save, decl_save; */
74 /* double ra_save1, decl_save1; */
75 int count, i, max_stars;
77 fgPrintf( FG_ASTRO, FG_INFO, "Initializing stars\n");
81 /* build the full path name to the stars data base file */
83 strcat(path, o->fg_root);
84 strcat(path, "/Scenery/");
85 strcat(path, "Stars");
87 max_stars = FG_MAX_STARS;
89 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
90 fgPrintf( FG_ASTRO, FG_INFO,
91 " Loading %d Stars: %s\n", max_stars, path);
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);
103 stars[i] = xglGenLists(1);
104 xglNewList( stars[i], GL_COMPILE );
105 xglBegin( GL_POINTS );
107 /* read in each line of the file */
109 while ( (fggets(fd, line, 256) != NULL) && (count < max_stars) ) {
112 /* printf(" Read line = %s", front); */
114 /* advance to first non-whitespace character */
115 while ( (front[0] == ' ') || (front[0] == '\t') ) {
119 /* printf(" Line length (after trimming) = %d\n", strlen(front));*/
121 if ( front[0] == '#' ) {
123 } else if ( strlen(front) <= 1 ) {
130 while ( end[0] != ',' ) {
138 sscanf(front, "%lf,%lf,%lf\n",
139 &right_ascension, &declination, &magnitude);
142 if ( strcmp(name, "Betelgeuse") == 0 ) {
143 printf(" *** Marking %s\n", name);
144 ra_save = right_ascension;
145 decl_save = declination;
150 if ( strcmp(name, "Alnilam") == 0 ) {
151 printf(" *** Marking %s\n", name);
152 ra_save1 = right_ascension;
153 decl_save1 = declination;
157 /* scale magnitudes to (0.0 - 1.0) */
158 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
160 /* scale magnitudes again so they look ok */
161 if ( magnitude > 1.0 ) { magnitude = 1.0; }
162 if ( magnitude < 0.0 ) { magnitude = 0.0; }
164 magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.1);
165 /* printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
166 name, right_ascension, declination, magnitude); */
168 xglColor3f( magnitude, magnitude, magnitude );
169 /*xglColor3f(0,0,0);*/
170 xglVertex3f( 50000.0 * cos(right_ascension) * cos(declination),
171 50000.0 * sin(right_ascension) * cos(declination),
172 50000.0 * sin(declination) );
184 xglBegin(GL_LINE_LOOP);
185 xglColor3f(1.0, 0.0, 0.0);
186 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
187 50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
188 50000.0 * sin(decl_save-0.2) );
189 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
190 50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
191 50000.0 * sin(decl_save-0.2) );
192 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
193 50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
194 50000.0 * sin(decl_save+0.2) );
195 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
196 50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
197 50000.0 * sin(decl_save+0.2) );
202 xglBegin(GL_LINE_LOOP);
203 xglColor3f(0.0, 1.0, 0.0);
204 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
205 50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
206 50000.0 * sin(decl_save1-0.2) );
207 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
208 50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
209 50000.0 * sin(decl_save1-0.2) );
210 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
211 50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
212 50000.0 * sin(decl_save1+0.2) );
213 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
214 50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
215 50000.0 * sin(decl_save1+0.2) );
224 return 1; // OK, we got here because initialization worked.
229 void fgStarsRender( void ) {
236 f = current_aircraft.flight;
237 l = &cur_light_params;
238 t = &cur_time_params;
241 /* FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise */
243 /* t->sun_angle = 3.0; */ /* to force stars to be drawn (for testing) */
245 /* render the stars */
246 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
247 /* determine which star structure to draw */
248 if ( l->sun_angle > (FG_PI_2 + 7.25 * DEG_TO_RAD ) ) {
250 } else if ( l->sun_angle > (FG_PI_2 + 6.50 * DEG_TO_RAD ) ) {
252 } else if ( l->sun_angle > (FG_PI_2 + 5.75 * DEG_TO_RAD ) ) {
258 /* printf("RENDERING STARS = %d (night)\n", i); */
260 xglCallList(stars[i]);
262 /* printf("not RENDERING STARS (day)\n"); */
268 /* Revision 1.7 1998/05/29 20:35:42 curt
269 /* Added zlib support for reading in compressed data files.
271 * Revision 1.6 1998/05/13 18:25:35 curt
272 * Root path info moved to fgOPTIONS.
274 * Revision 1.5 1998/04/28 01:19:03 curt
275 * Type-ified fgTIME and fgVIEW
277 * Revision 1.4 1998/04/26 05:10:02 curt
278 * "struct fgLIGHT" -> "fgLIGHT" because fgLIGHT is typedef'd.
280 * Revision 1.3 1998/04/25 22:06:26 curt
281 * Edited cvs log messages in source files ... bad bad bad!
283 * Revision 1.2 1998/04/24 00:45:03 curt
284 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
285 * Fixed a bug when generating sky colors.
287 * Revision 1.1 1998/04/22 13:21:34 curt
288 * C++ - ifing the code a bit.
290 * Revision 1.11 1998/04/18 04:13:58 curt
291 * Moved fg_debug.c to it's own library.
293 * Revision 1.10 1998/04/03 21:52:51 curt
294 * Converting to Gnu autoconf system.
296 * Revision 1.9 1998/03/14 00:27:12 curt
297 * Updated fgGENERAL to a "type" of struct.
299 * Revision 1.8 1998/02/12 21:59:38 curt
300 * Incorporated code changes contributed by Charlie Hotchkiss
301 * <chotchkiss@namg.us.anritsu.com>
303 * Revision 1.7 1998/02/09 15:07:48 curt
306 * Revision 1.6 1998/02/02 20:53:23 curt
309 * Revision 1.5 1998/01/27 18:35:53 curt
312 * Revision 1.4 1998/01/27 00:47:49 curt
313 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
314 * system and commandline/config file processing code.
316 * Revision 1.3 1998/01/19 19:26:59 curt
317 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
318 * This should simplify things tremendously.
320 * Revision 1.2 1998/01/19 18:40:18 curt
321 * Tons of little changes to clean up the code and to remove fatal errors
322 * when building with the c++ compiler.
324 * Revision 1.1 1998/01/07 03:16:20 curt
325 * Moved from .../Src/Scenery/ to .../Src/Astro/
327 * Revision 1.24 1997/12/30 22:22:39 curt
328 * Further integration of event manager.
330 * Revision 1.23 1997/12/30 20:47:53 curt
331 * Integrated new event manager with subsystem initializations.
333 * Revision 1.22 1997/12/30 16:36:53 curt
334 * Merged in Durk's changes ...
336 * Revision 1.21 1997/12/19 23:35:00 curt
337 * Lot's of tweaking with sky rendering and lighting.
339 * Revision 1.20 1997/12/15 23:55:03 curt
340 * Add xgl wrappers for debugging.
341 * Generate terrain normals on the fly.
343 * Revision 1.19 1997/12/12 19:53:00 curt
344 * Working on lightling and material properties.
346 * Revision 1.18 1997/12/10 22:37:52 curt
347 * Prepended "fg" on the name of all global structures that didn't have it yet.
348 * i.e. "struct WEATHER {}" became "struct fgWEATHER {}"
350 * Revision 1.17 1997/12/09 04:25:33 curt
351 * Working on adding a global lighting params structure.
353 * Revision 1.16 1997/11/25 19:25:38 curt
354 * Changes to integrate Durk's moon/sun code updates + clean up.
356 * Revision 1.15 1997/10/30 12:38:45 curt
357 * Working on new scenery subsystem.
359 * Revision 1.14 1997/10/28 21:00:22 curt
360 * Changing to new terrain format.
362 * Revision 1.13 1997/10/25 03:18:28 curt
363 * Incorporated sun, moon, and planet position and rendering code contributed
366 * Revision 1.12 1997/09/23 00:29:43 curt
367 * Tweaks to get things to compile with gcc-win32.
369 * Revision 1.11 1997/09/22 14:44:21 curt
370 * Continuing to try to align stars correctly.
372 * Revision 1.10 1997/09/20 03:34:32 curt
373 * Still trying to get those durned stars aligned properly.
375 * Revision 1.9 1997/09/18 16:20:09 curt
376 * At dusk/dawn add/remove stars in stages.
378 * Revision 1.8 1997/09/16 22:14:52 curt
379 * Tweaked time of day lighting equations. Don't draw stars during the day.
381 * Revision 1.7 1997/09/16 15:50:31 curt
382 * Working on star alignment and time issues.
384 * Revision 1.6 1997/09/05 14:17:31 curt
385 * More tweaking with stars.
387 * Revision 1.5 1997/09/05 01:35:59 curt
388 * Working on getting stars right.
390 * Revision 1.4 1997/09/04 02:17:38 curt
393 * Revision 1.3 1997/08/29 17:55:28 curt
394 * Worked on properly aligning the stars.
396 * Revision 1.2 1997/08/27 21:32:30 curt
397 * Restructured view calculation code. Added stars.
399 * Revision 1.1 1997/08/27 03:34:48 curt