]> git.mxchange.org Git - flightgear.git/blob - src/Astro/stars.cxx
d26f71a4837237e7cd90611c63b2c98deb88138b
[flightgear.git] / src / Astro / stars.cxx
1 // stars.cxx -- data structures and routines for managing and rendering stars.
2 //
3 // Written by Curtis Olson, started August 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@me.umn.edu
6 //
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.
11 //
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.
16 //
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.
20 //
21 // $Id$
22 //*************************************************************************/
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include "Include/compiler.h"
34 #ifdef FG_HAVE_STD_INCLUDES
35 #  include <cmath>
36 #  include <cstdio>
37 #  include <cstring>
38 #  include <ctime>
39 #else
40 #  include <math.h>
41 #  include <stdio.h>
42 #  include <string.h>
43 #  include <time.h>
44 #endif
45
46 #include <string>
47
48 #include <GL/glut.h>
49 #include <XGL/xgl.h>
50
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"
61
62 #include "stars.hxx"
63
64 FG_USING_STD(getline);
65
66 #define EpochStart           (631065600)
67 #define DaysSinceEpoch(secs) (((secs)-EpochStart)*(1.0/(24*3600)))
68 #define FG_MAX_STARS 3500
69
70 // Define four structures, each with varying amounts of stars
71 static GLint stars[FG_STAR_LEVELS];
72
73
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;
85
86     FG_LOG( FG_ASTRO, FG_INFO, "Initializing stars" );
87
88     if ( FG_STAR_LEVELS < 4 ) {
89         FG_LOG( FG_ASTRO, FG_ALERT, "Big whups in stars.cxx" );
90         exit(-1);
91     }
92
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() );
97
98     fg_gzifstream in( path.str() );
99     if ( ! in ) {
100         FG_LOG( FG_ASTRO, FG_ALERT, "Cannot open star file: " << path.str() );
101         exit(-1);
102     }
103
104     starcount = 0;
105
106     StopWatch timer;
107     timer.start();
108
109     // read in each line of the file
110     while ( ! in.eof() && starcount < FG_MAX_STARS )
111     {
112         in >> skipcomment;
113         string name;
114         getline( in, name, ',' );
115         in >> starlist[starcount];
116         ++starcount;
117     }
118
119     timer.stop();
120     FG_LOG( FG_ASTRO, FG_INFO, 
121             "Loaded " << starcount << " stars in "
122             << timer.elapsedSeconds() << " seconds" );
123
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;
132
133     // build the various star display lists
134     for ( i = 0; i < FG_STAR_LEVELS; i++ ) {
135
136         stars[i] = xglGenLists(1);
137         xglNewList( stars[i], GL_COMPILE );
138         xglBegin( GL_POINTS );
139
140         count = 0;
141
142         for ( j = 0; j < starcount; j++ ) {
143             magnitude = starlist[j].z();
144             // printf("magnitude = %.2f\n", magnitude);
145
146             if ( magnitude < min_magnitude[i] ) {
147                 right_ascension = starlist[j].x();
148                 declination = starlist[j].y();
149
150                 count++;
151
152                 // scale magnitudes to (0.0 - 1.0)
153                 magnitude = (0.0 - magnitude) / 5.0 + 1.0;
154                 
155                 // scale magnitudes again so they look ok
156                 if ( magnitude > 1.0 ) { magnitude = 1.0; }
157                 if ( magnitude < 0.0 ) { magnitude = 0.0; }
158                 // magnitude =
159                 //     magnitude * 0.7 + (((FG_STAR_LEVELS - 1) - i) * 0.042);
160                   
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);
165                     
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) );
171             }
172         } // while
173
174         xglEnd();
175
176         /*
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) );
191         xglEnd();
192         */
193
194         /*
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) );
209         xglEnd();
210         */
211
212         xglEndList();
213             
214         FG_LOG( FG_ASTRO, FG_INFO,
215                 "  Loading " << count << " stars brighter than " 
216                 << min_magnitude[i] );
217     }
218
219     return 1;  // OK, we got here because initialization worked.
220 }
221
222
223 // Draw the Stars
224 void fgStarsRender( void ) {
225     FGInterface *f;
226     fgLIGHT *l;
227     FGTime *t;
228     int i;
229
230     f = current_aircraft.fdm_state;
231     l = &cur_light_params;
232     t = FGTime::cur_time_params;
233
234     // FG_PI_2 + 0.1 is about 6 degrees after sundown and before sunrise
235
236     // t->sun_angle = 3.0; // to force stars to be drawn (for testing)
237
238     // render the stars
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 ) ) {
242             i = 0;
243         } else if ( l->sun_angle > (FG_PI_2 + 8.8 * DEG_TO_RAD ) ) {
244             i = 1;
245         } else if ( l->sun_angle > (FG_PI_2 + 7.5 * DEG_TO_RAD ) ) {
246             i = 2;
247         } else if ( l->sun_angle > (FG_PI_2 + 7.0 * DEG_TO_RAD ) ) {
248             i = 3;
249         } else if ( l->sun_angle > (FG_PI_2 + 6.5 * DEG_TO_RAD ) ) {
250             i = 4;
251         } else if ( l->sun_angle > (FG_PI_2 + 6.0 * DEG_TO_RAD ) ) {
252             i = 5;
253         } else if ( l->sun_angle > (FG_PI_2 + 5.5 * DEG_TO_RAD ) ) {
254             i = 6;
255         } else {
256             i = 7;
257         }
258
259         // printf("RENDERING STARS = %d (night)\n", i);
260
261         xglCallList(stars[i]);
262     } else {
263         // printf("not RENDERING STARS (day)\n");
264     }
265 }
266
267