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