]> git.mxchange.org Git - simgear.git/blob - simgear/ephemeris/solarsystem.cxx
Separating out the Sky rendering as a separate unit and ssg-ifying it.
[simgear.git] / simgear / ephemeris / solarsystem.cxx
1 /**************************************************************************
2  * solarsystem.cxx
3  * Written by Durk Talsma. Originally started October 1997, for distribution  
4  * with the FlightGear project. Version 2 was written in August and 
5  * September 1998. This code is based upon algorithms and data kindly 
6  * provided by Mr. Paul Schlyter. (pausch@saaf.se). 
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * $Id$
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 #ifdef __BORLANDC__
34 #  define exception c_exception
35 #endif
36 #include <math.h>
37
38 #include <GL/glut.h>
39 #include <simgear/xgl/xgl.h>
40
41 #include <simgear/debug/logstream.hxx>
42
43 #include <Time/sunpos.hxx>
44 #include <Time/moonpos.hxx>
45 #include "solarsystem.hxx"
46
47 /***************************************************************************
48  * default constructor for class  SolarSystem:   
49  * or course there can only be one way to create an entire solar system -:) )
50  * the FGTime argument is needed to properly initialize the the current orbital
51  * elements
52  *************************************************************************/
53 SolarSystem::SolarSystem(FGTime *t)
54 {
55   if (theSolarSystem)
56     {
57       FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
58       exit(-1);
59     }
60   theSolarSystem = this;
61   ourSun     = new Star(t);   
62   earthsMoon = new Moon(t);
63   mercury    = new Mercury(t);
64   venus      = new Venus(t);
65   mars       = new Mars(t);
66   jupiter    = new Jupiter(t);
67   saturn     = new Saturn(t);
68   uranus     = new Uranus(t);
69   neptune    = new Neptune(t);
70
71   displayList = 0;
72 };
73
74 /**************************************************************************
75  * the destructor for class SolarSystem;
76  **************************************************************************/
77 SolarSystem::~SolarSystem()
78 {
79   delete ourSun;
80   delete earthsMoon;
81   delete mercury;
82   delete venus;
83   delete mars;
84   delete jupiter;
85   delete saturn;
86   delete uranus;
87   delete neptune;
88 }
89 /****************************************************************************
90  * void SolarSystem::rebuild()
91  *
92  * this member function updates the positions for the sun, moon, and planets,
93  * and then rebuilds the display list. 
94  *
95  * arguments: none
96  * return value: none
97  ***************************************************************************/
98 void SolarSystem::rebuild()
99 {
100   //fgLIGHT *l = &cur_light_params;
101   FGTime *t = FGTime::cur_time_params;  
102   //float x, y, z;
103   //double sun_angle;
104   double ra, dec;
105   //double x_2, x_4, x_8, x_10;*/
106   double magnitude;
107   //GLfloat ambient;
108   //GLfloat amb[4];
109   
110   glDisable(GL_LIGHTING);
111
112   // Step 1: update all the positions
113   ourSun->updatePosition(t);
114   earthsMoon->updatePosition(t, ourSun);
115   mercury->updatePosition(t, ourSun);
116   venus->updatePosition(t, ourSun);
117   mars->updatePosition(t, ourSun);
118   jupiter->updatePosition(t, ourSun);
119   saturn->updatePosition(t, ourSun);
120   uranus->updatePosition(t, ourSun);
121   neptune->updatePosition(t, ourSun);
122   
123   fgUpdateSunPos();   // get the right sun angle (especially important when 
124                       // running for the first time).
125   fgUpdateMoonPos();
126
127   if (displayList)
128     xglDeleteLists(displayList, 1);
129
130   displayList = xglGenLists(1);
131
132   FG_LOG( FG_ASTRO, FG_INFO, "Rebuilding astro display list" );
133
134   // Step 2: rebuild the display list
135   xglNewList( displayList, GL_COMPILE);
136   {
137     // Step 2a: Add the moon...
138     // Not that it is preferred to draw the moon first, and the sun next, in order to mime a
139     // solar eclipse. This is yet untested though...
140     // Euhh, actually the ecplise doesn't work...
141
142     earthsMoon->newImage();
143     // Step 2b:  Add the sun
144     ourSun->newImage();
145     // Step 2c: Add the planets
146     xglBegin(GL_POINTS);
147     mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
148     venus  ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
149     mars   ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
150     jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
151     saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
152     uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
153     neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
154     xglEnd();
155     xglEnable(GL_LIGHTING);
156   }
157   xglEndList();
158 }
159
160 /*****************************************************************************
161  * double SolarSystem::scaleMagnitude(double magn)
162  * This private member function rescales the original magnitude, as used in the
163  * astronomical sense of the word, into a value used by OpenGL to draw a 
164  * convincing Star or planet
165  * 
166  * Argument: the astronomical magnitude
167  *
168  * return value: the rescaled magnitude
169  ****************************************************************************/
170 double SolarSystem::scaleMagnitude(double magn)
171 {
172   double magnitude = (0.0 - magn) / 5.0 + 1.0;
173   magnitude = magnitude * 0.7 + (3 * 0.1);
174   if (magnitude > 1.0) magnitude = 1.0;
175   if (magnitude < 0.0) magnitude = 0.0;
176   return magnitude;
177 }
178
179 /***************************************************************************
180  * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
181  *
182  * This private member function first causes the magnitude to be properly
183  * rescaled, and then adds the planet to the display list.
184  * 
185  * arguments: Right Ascension, declination, and magnitude
186  *
187  * return value: none
188  **************************************************************************/
189 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
190 {
191   double
192     magnitude = scaleMagnitude ( magn );
193
194   fgLIGHT *l = &cur_light_params;
195
196   if ((double) (l->sun_angle - FG_PI_2) > 
197       ((magnitude - 1.0) * - 20 * DEG_TO_RAD)) 
198     {
199       xglColor3f (magnitude, magnitude, magnitude);
200       xglVertex3f( 50000.0 * cos (ra) * cos (dec),
201                    50000.0 * sin (ra) * cos (dec),
202                    50000.0 * sin (dec));
203     }
204 }
205
206
207 SolarSystem* SolarSystem::theSolarSystem = 0;
208
209 /******************************************************************************
210  * void solarSystemRebuild()
211  * this a just a wrapper function, provided for use as an interface to the 
212  * event manager
213  *****************************************************************************/
214 void solarSystemRebuild()
215 {
216   SolarSystem::theSolarSystem->rebuild();
217 }