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/fgpath.hxx>
55 #include <Misc/fgstream.hxx>
56 #include <Misc/stopwatch.hxx>
57 #include <Main/options.hxx>
58 #include <Main/views.hxx>
59 #include <Time/fg_time.hxx>
60 #include "Misc/stopwatch.hxx"
64 // FG_USING_STD(getline);
66 #define EpochStart (631065600)
67 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
68 #define FG_MAX_STARS 3500
70 // Define four structures, each with varying amounts of stars
71 static GLint stars[FG_STAR_LEVELS];
74 // Initialize the Star Management Subsystem
75 int fgStarsInit( void ) {
76 // -dw- avoid local data > 32k error by dynamic allocation of the
77 // array, problem for some compilers
78 Point3D *starlist = new Point3D[FG_MAX_STARS];
79 // struct CelestialCoord pltPos;
80 double right_ascension, declination, magnitude;
81 double min_magnitude[FG_STAR_LEVELS];
82 // double ra_save, decl_save;
83 // double ra_save1, decl_save1;
84 int i, j, starcount, count;
86 FG_LOG( FG_ASTRO, FG_INFO, "Initializing stars" );
88 if ( FG_STAR_LEVELS < 4 ) {
89 FG_LOG( FG_ASTRO, FG_ALERT, "Big whups in stars.cxx" );
93 // build the full path name to the stars data base file
94 FGPath path ( current_options.get_fg_root() );
95 path.append( "Astro/stars" );
96 FG_LOG( FG_ASTRO, FG_INFO, " Loading stars from " << path.str() );
98 fg_gzifstream in( path.str() );
100 FG_LOG( FG_ASTRO, FG_ALERT, "Cannot open star file: " << path.str() );
109 // read in each line of the file
110 while ( ! in.eof() && starcount < FG_MAX_STARS )
114 getline( in, name, ',' );
115 in >> starlist[starcount];
120 FG_LOG( FG_ASTRO, FG_INFO,
121 "Loaded " << starcount << " stars in "
122 << timer.elapsedSeconds() << " seconds" );
124 min_magnitude[0] = 4.2;
125 min_magnitude[1] = 3.6;
126 min_magnitude[2] = 3.0;
127 min_magnitude[3] = 2.4;
128 min_magnitude[4] = 1.8;
129 min_magnitude[5] = 1.2;
130 min_magnitude[6] = 0.6;
131 min_magnitude[7] = 0.0;
133 // build the various star display lists
134 for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
136 stars[i] = xglGenLists(1);
137 xglNewList( stars[i], GL_COMPILE );
138 xglBegin( GL_POINTS );
142 for ( j = 0; j < starcount; j++ ) {
143 magnitude = starlist[j].z();
144 // printf("magnitude = %.2f\n", magnitude);
146 if ( magnitude < min_magnitude[i] ) {
147 right_ascension = starlist[j].x();
148 declination = starlist[j].y();
152 // scale magnitudes to (0.0 - 1.0)
153 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
155 // scale magnitudes again so they look ok
156 if ( magnitude > 1.0 ) { magnitude = 1.0; }
157 if ( magnitude < 0.0 ) { magnitude = 0.0; }
159 // magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
161 magnitude = magnitude * 0.9 +
162 (((FG_STAR_LEVELS - 1) - i) * 0.014);
163 // printf(" Found star: %d %s, %.3f %.3f %.3f\n", count,
164 // name, right_ascension, declination, magnitude);
166 xglColor3f( magnitude, magnitude, magnitude );
167 //xglColor3f(0,0,0);*/
168 xglVertex3f( 50000.0*cos(right_ascension)*cos(declination),
169 50000.0*sin(right_ascension)*cos(declination),
170 50000.0*sin(declination) );
177 xglBegin(GL_LINE_LOOP);
178 xglColor3f(1.0, 0.0, 0.0);
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) );
188 xglVertex3f( 50000.0 * cos(ra_save-0.2) * cos(decl_save+0.2),
189 50000.0 * sin(ra_save-0.2) * cos(decl_save+0.2),
190 50000.0 * sin(decl_save+0.2) );
195 xglBegin(GL_LINE_LOOP);
196 xglColor3f(0.0, 1.0, 0.0);
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) );
206 xglVertex3f( 50000.0 * cos(ra_save1-0.2) * cos(decl_save1+0.2),
207 50000.0 * sin(ra_save1-0.2) * cos(decl_save1+0.2),
208 50000.0 * sin(decl_save1+0.2) );
214 FG_LOG( FG_ASTRO, FG_INFO,
215 " Loading " << count << " stars brighter than "
216 << min_magnitude[i] );
219 return 1; // OK, we got here because initialization worked.
224 void fgStarsRender( void ) {
230 f = current_aircraft.fdm_state;
231 l = &cur_light_params;
232 t = FGTime::cur_time_params;
234 // FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise
236 // t->sun_angle = 3.0; // to force stars to be drawn (for testing)
239 if ( l->sun_angle > (FG_PI_2 + 5 * DEG_TO_RAD ) ) {
240 // determine which star structure to draw
241 if ( l->sun_angle > (FG_PI_2 + 10.0 * DEG_TO_RAD ) ) {
243 } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
245 } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
247 } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
249 } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
251 } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
253 } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
259 // printf("RENDERING STARS = %d (night)\n", i);
261 xglCallList(stars[i]);
263 // printf("not RENDERING STARS (day)\n");