1 // stars.cxx -- data structures and routines for managing and rendering stars.
3 // Written by Curtis Olson, started August 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@me.umn.edu
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //*************************************************************************/
33 #include "Include/compiler.h"
34 #ifdef FG_HAVE_STD_INCLUDES
51 #include <Aircraft/aircraft.hxx>
52 #include <Debug/logstream.hxx>
53 #include <Include/fg_constants.h>
54 #include <Misc/fgstream.hxx>
55 #include <Main/options.hxx>
56 #include <Main/views.hxx>
57 #include <Misc/stopwatch.hxx>
58 #include <Time/fg_time.hxx>
59 #include "Misc/stopwatch.hxx"
63 FG_USING_STD(getline);
65 #define EpochStart (631065600)
66 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
67 #define FG_MAX_STARS 3500
69 // Define four structures, each with varying amounts of stars
70 static GLint stars[FG_STAR_LEVELS];
73 // Initialize the Star Management Subsystem
74 int fgStarsInit( void ) {
75 Point3D starlist[FG_MAX_STARS];
76 // struct CelestialCoord pltPos;
77 double right_ascension, declination, magnitude;
78 double min_magnitude[FG_STAR_LEVELS];
79 // double ra_save, decl_save;
80 // double ra_save1, decl_save1;
81 int i, j, starcount, count;
83 FG_LOG( FG_ASTRO, FG_INFO, "Initializing stars" );
85 if ( FG_STAR_LEVELS < 4 ) {
86 FG_LOG( FG_ASTRO, FG_ALERT, "Big whups in stars.cxx" );
90 // build the full path name to the stars data base file
91 string path = current_options.get_fg_root() + "/Astro/stars" + ".gz";
93 FG_LOG( FG_ASTRO, FG_INFO, " Loading stars from " << path );
95 fg_gzifstream in( path );
97 FG_LOG( FG_ASTRO, FG_ALERT, "Cannot open star file: " << path );
106 // read in each line of the file
107 while ( ! in.eof() && starcount < FG_MAX_STARS )
111 getline( in, name, ',' );
112 in >> starlist[starcount];
117 FG_LOG( FG_ASTRO, FG_INFO,
118 "Loaded " << starcount << " stars in "
119 << timer.elapsedSeconds() << " seconds" );
121 min_magnitude[0] = 4.2;
122 min_magnitude[1] = 3.6;
123 min_magnitude[2] = 3.0;
124 min_magnitude[3] = 2.4;
125 min_magnitude[4] = 1.8;
126 min_magnitude[5] = 1.2;
127 min_magnitude[6] = 0.6;
128 min_magnitude[7] = 0.0;
130 // build the various star display lists
131 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
133 stars[i] = xglGenLists(1);
134 xglNewList( stars[i], GL_COMPILE );
135 xglBegin( GL_POINTS );
139 for ( j = 0; j < starcount; j++ ) {
140 magnitude = starlist[j].z();
141 // printf("magnitude = %.2f\n", magnitude);
143 if ( magnitude < min_magnitude[i] ) {
144 right_ascension = starlist[j].x();
145 declination = starlist[j].y();
149 // scale magnitudes to (0.0 - 1.0)
150 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
152 // scale magnitudes again so they look ok
153 if ( magnitude > 1.0 ) { magnitude = 1.0; }
154 if ( magnitude < 0.0 ) { magnitude = 0.0; }
156 // magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
158 magnitude = magnitude * 0.9 +
159 (((FG_STAR_LEVELS - 1) - i) * 0.014);
160 // printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
161 // name, right_ascension, declination, magnitude);
163 xglColor3f( magnitude, magnitude, magnitude );
164 //xglColor3f(0,0,0);*/
165 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
166 50000.0*sin(right_ascension)*cos(declination),
167 50000.0*sin(declination) );
174 xglBegin(GL_LINE_LOOP);
175 xglColor3f(1.0, 0.0, 0.0);
176 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
177 50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
178 50000.0 * sin(decl_save-0.2) );
179 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
180 50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
181 50000.0 * sin(decl_save-0.2) );
182 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save+0.2),
183 50000.0 * sin(ra_save+0.2) * cos(decl_save+0.2),
184 50000.0 * sin(decl_save+0.2) );
185 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
186 50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
187 50000.0 * sin(decl_save+0.2) );
192 xglBegin(GL_LINE_LOOP);
193 xglColor3f(0.0, 1.0, 0.0);
194 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
195 50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
196 50000.0 * sin(decl_save1-0.2) );
197 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
198 50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
199 50000.0 * sin(decl_save1-0.2) );
200 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1+0.2),
201 50000.0 * sin(ra_save1+0.2) * cos(decl_save1+0.2),
202 50000.0 * sin(decl_save1+0.2) );
203 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
204 50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
205 50000.0 * sin(decl_save1+0.2) );
211 FG_LOG( FG_ASTRO, FG_INFO,
212 " Loading " << count << " stars brighter than "
213 << min_magnitude[i] );
216 return 1; // OK, we got here because initialization worked.
221 void fgStarsRender( void ) {
227 f = current_aircraft.fdm_state;
228 l = &cur_light_params;
229 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)
236 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
237 // determine which star structure to draw
238 if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
240 } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
242 } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
244 } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
246 } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
248 } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
250 } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
256 // printf("RENDERING STARS = %d (night)\n", i);
258 xglCallList(stars[i]);
260 // printf("not RENDERING STARS (day)\n");