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 <simgear/compiler.h>
35 #ifdef FG_HAVE_STD_INCLUDES
50 #include <simgear/xgl/xgl.h>
52 #include <simgear/constants.h>
53 #include <simgear/debug/logstream.hxx>
54 #include <simgear/misc/fgpath.hxx>
55 #include <simgear/misc/fgstream.hxx>
56 #include <simgear/misc/stopwatch.hxx>
58 #include <Aircraft/aircraft.hxx>
59 #include <Main/options.hxx>
60 #include <Main/views.hxx>
61 #include <Time/fg_time.hxx>
65 // FG_USING_STD(getline);
67 #define EpochStart (631065600)
68 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
69 #define FG_MAX_STARS 3500
71 // Define four structures, each with varying amounts of stars
72 static GLint stars[FG_STAR_LEVELS];
75 // Initialize the Star Management Subsystem
76 int fgStarsInit( void ) {
77 // -dw- avoid local data > 32k error by dynamic allocation of the
78 // array, problem for some compilers
79 Point3D *starlist = new Point3D[FG_MAX_STARS];
80 // struct CelestialCoord pltPos;
81 double right_ascension, declination, magnitude;
82 double min_magnitude[FG_STAR_LEVELS];
83 // double ra_save, decl_save;
84 // double ra_save1, decl_save1;
85 int i, j, starcount, count;
87 FG_LOG( FG_ASTRO, FG_INFO, "Initializing stars" );
89 if ( FG_STAR_LEVELS < 4 ) {
90 FG_LOG( FG_ASTRO, FG_ALERT, "Big whups in stars.cxx" );
94 // build the full path name to the stars data base file
95 FGPath path ( current_options.get_fg_root() );
96 path.append( "Astro/stars" );
97 FG_LOG( FG_ASTRO, FG_INFO, " Loading stars from " << path.str() );
99 fg_gzifstream in( path.str() );
101 FG_LOG( FG_ASTRO, FG_ALERT, "Cannot open star file: " << path.str() );
110 // read in each line of the file
111 while ( ! in.eof() && starcount < FG_MAX_STARS )
115 getline( in, name, ',' );
116 in >> starlist[starcount];
121 FG_LOG( FG_ASTRO, FG_INFO,
122 "Loaded " << starcount << " stars in "
123 << timer.elapsedSeconds() << " seconds" );
125 min_magnitude[0] = 4.2;
126 min_magnitude[1] = 3.6;
127 min_magnitude[2] = 3.0;
128 min_magnitude[3] = 2.4;
129 min_magnitude[4] = 1.8;
130 min_magnitude[5] = 1.2;
131 min_magnitude[6] = 0.6;
132 min_magnitude[7] = 0.0;
134 // build the various star display lists
135 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
137 stars[i] = xglGenLists(1);
138 xglNewList( stars[i], GL_COMPILE );
139 xglBegin( GL_POINTS );
143 for ( j = 0; j < starcount; j++ ) {
144 magnitude = starlist[j].z();
145 // printf("magnitude = %.2f\n", magnitude);
147 if ( magnitude < min_magnitude[i] ) {
148 right_ascension = starlist[j].x();
149 declination = starlist[j].y();
153 // scale magnitudes to (0.0 - 1.0)
154 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
156 // scale magnitudes again so they look ok
157 if ( magnitude > 1.0 ) { magnitude = 1.0; }
158 if ( magnitude < 0.0 ) { magnitude = 0.0; }
160 // magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
162 magnitude = magnitude * 0.9 +
163 (((FG_STAR_LEVELS - 1) - i) * 0.014);
164 // printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
165 // name, right_ascension, declination, magnitude);
167 xglColor3f( magnitude, magnitude, magnitude );
168 //xglColor3f(0,0,0);*/
169 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
170 50000.0*sin(right_ascension)*cos(declination),
171 50000.0*sin(declination) );
178 xglBegin(GL_LINE_LOOP);
179 xglColor3f(1.0, 0.0, 0.0);
180 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save-0.2),
181 50000.0 * sin(ra_save-0.2) * cos(decl_save-0.2),
182 50000.0 * sin(decl_save-0.2) );
183 xglVertex3f( 50000.0 * cos(ra_save+0.2) * cos(decl_save-0.2),
184 50000.0 * sin(ra_save+0.2) * cos(decl_save-0.2),
185 50000.0 * sin(decl_save-0.2) );
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) );
196 xglBegin(GL_LINE_LOOP);
197 xglColor3f(0.0, 1.0, 0.0);
198 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1-0.2),
199 50000.0 * sin(ra_save1-0.2) * cos(decl_save1-0.2),
200 50000.0 * sin(decl_save1-0.2) );
201 xglVertex3f( 50000.0 * cos(ra_save1+0.2) * cos(decl_save1-0.2),
202 50000.0 * sin(ra_save1+0.2) * cos(decl_save1-0.2),
203 50000.0 * sin(decl_save1-0.2) );
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) );
215 FG_LOG( FG_ASTRO, FG_INFO,
216 " Loading " << count << " stars brighter than "
217 << min_magnitude[i] );
220 return 1; // OK, we got here because initialization worked.
225 void fgStarsRender( void ) {
231 f = current_aircraft.fdm_state;
232 l = &cur_light_params;
233 t = FGTime::cur_time_params;
235 // FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise
237 // t->sun_angle = 3.0; // to force stars to be drawn (for testing)
240 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
241 // determine which star structure to draw
242 if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
244 } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
246 } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
248 } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
250 } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
252 } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
254 } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
260 // printf("RENDERING STARS = %d (night)\n", i);
262 xglCallList(stars[i]);
264 // printf("not RENDERING STARS (day)\n");