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