]> git.mxchange.org Git - flightgear.git/blob - src/Astro/solarsystem.cxx
source tree reorganization prior to flightgear 0.7
[flightgear.git] / src / Astro / 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 <XGL/xgl.h>
40 #include <Debug/logstream.hxx>
41 #include <Time/sunpos.hxx>
42 #include <Time/moonpos.hxx>
43 #include "solarsystem.hxx"
44
45 /***************************************************************************
46  * default constructor for class  SolarSystem:   
47  * or course there can only be one way to create an entire solar system -:) )
48  * the FGTime argument is needed to properly initialize the the current orbital
49  * elements
50  *************************************************************************/
51 SolarSystem::SolarSystem(FGTime *t)
52 {
53   if (theSolarSystem)
54     {
55       FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
56       exit(-1);
57     }
58   theSolarSystem = this;
59   ourSun     = new Star(t);   
60   earthsMoon = new Moon(t);
61   mercury    = new Mercury(t);
62   venus      = new Venus(t);
63   mars       = new Mars(t);
64   jupiter    = new Jupiter(t);
65   saturn     = new Saturn(t);
66   uranus     = new Uranus(t);
67   neptune    = new Neptune(t);
68
69   displayList = 0;
70 };
71
72 /**************************************************************************
73  * the destructor for class SolarSystem;
74  **************************************************************************/
75 SolarSystem::~SolarSystem()
76 {
77   delete ourSun;
78   delete earthsMoon;
79   delete mercury;
80   delete venus;
81   delete mars;
82   delete jupiter;
83   delete saturn;
84   delete uranus;
85   delete neptune;
86 }
87 /****************************************************************************
88  * void SolarSystem::rebuild()
89  *
90  * this member function updates the positions for the sun, moon, and planets,
91  * and then rebuilds the display list. 
92  *
93  * arguments: none
94  * return value: none
95  ***************************************************************************/
96 void SolarSystem::rebuild()
97 {
98   //fgLIGHT *l = &cur_light_params;
99   FGTime *t = FGTime::cur_time_params;  
100   //float x, y, z;
101   //double sun_angle;
102   double ra, dec;
103   //double x_2, x_4, x_8, x_10;*/
104   double magnitude;
105   //GLfloat ambient;
106   //GLfloat amb[4];
107   
108   glDisable(GL_LIGHTING);
109
110   // Step 1: update all the positions
111   ourSun->updatePosition(t);
112   earthsMoon->updatePosition(t, ourSun);
113   mercury->updatePosition(t, ourSun);
114   venus->updatePosition(t, ourSun);
115   mars->updatePosition(t, ourSun);
116   jupiter->updatePosition(t, ourSun);
117   saturn->updatePosition(t, ourSun);
118   uranus->updatePosition(t, ourSun);
119   neptune->updatePosition(t, ourSun);
120   
121   fgUpdateSunPos();   // get the right sun angle (especially important when 
122                       // running for the first time).
123   fgUpdateMoonPos();
124
125   if (displayList)
126     xglDeleteLists(displayList, 1);
127
128   displayList = xglGenLists(1);
129
130   FG_LOG( FG_ASTRO, FG_INFO, "Rebuilding astro display list" );
131
132   // Step 2: rebuild the display list
133   xglNewList( displayList, GL_COMPILE);
134   {
135     // Step 2a: Add the moon...
136     // Not that it is preferred to draw the moon first, and the sun next, in order to mime a
137     // solar eclipse. This is yet untested though...
138     // Euhh, actually the ecplise doesn't work...
139
140     earthsMoon->newImage();
141     // Step 2b:  Add the sun
142     ourSun->newImage();
143     // Step 2c: Add the planets
144     xglBegin(GL_POINTS);
145     mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
146     venus  ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
147     mars   ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
148     jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
149     saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
150     uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
151     neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
152     xglEnd();
153     xglEnable(GL_LIGHTING);
154   }
155   xglEndList();
156 }
157
158 /*****************************************************************************
159  * double SolarSystem::scaleMagnitude(double magn)
160  * This private member function rescales the original magnitude, as used in the
161  * astronomical sense of the word, into a value used by OpenGL to draw a 
162  * convincing Star or planet
163  * 
164  * Argument: the astronomical magnitude
165  *
166  * return value: the rescaled magnitude
167  ****************************************************************************/
168 double SolarSystem::scaleMagnitude(double magn)
169 {
170   double magnitude = (0.0 - magn) / 5.0 + 1.0;
171   magnitude = magnitude * 0.7 + (3 * 0.1);
172   if (magnitude > 1.0) magnitude = 1.0;
173   if (magnitude < 0.0) magnitude = 0.0;
174   return magnitude;
175 }
176
177 /***************************************************************************
178  * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
179  *
180  * This private member function first causes the magnitude to be properly
181  * rescaled, and then adds the planet to the display list.
182  * 
183  * arguments: Right Ascension, declination, and magnitude
184  *
185  * return value: none
186  **************************************************************************/
187 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
188 {
189   double
190     magnitude = scaleMagnitude ( magn );
191
192   fgLIGHT *l = &cur_light_params;
193
194   if ((double) (l->sun_angle - FG_PI_2) > 
195       ((magnitude - 1.0) * - 20 * DEG_TO_RAD)) 
196     {
197       xglColor3f (magnitude, magnitude, magnitude);
198       xglVertex3f( 50000.0 * cos (ra) * cos (dec),
199                    50000.0 * sin (ra) * cos (dec),
200                    50000.0 * sin (dec));
201     }
202 }
203
204
205 SolarSystem* SolarSystem::theSolarSystem = 0;
206
207 /******************************************************************************
208  * void solarSystemRebuild()
209  * this a just a wrapper function, provided for use as an interface to the 
210  * event manager
211  *****************************************************************************/
212 void solarSystemRebuild()
213 {
214   SolarSystem::theSolarSystem->rebuild();
215 }