]> git.mxchange.org Git - flightgear.git/blob - Astro/solarsystem.cxx
MSVC++ portability tweaks contributed by Bernie Bright.
[flightgear.git] / 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 "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    danger: Huge Explosions ahead! (-:))
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   //delete pluto;
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 = &cur_time_params;  
102   float x, y, z;
103   // float xx, yy,zz;
104   double sun_angle;
105   double ra, dec;
106   double x_2, x_4, x_8, x_10;
107   double magnitude;
108   GLfloat ambient;
109   GLfloat amb[4];
110   GLfloat moonColor[4] = {0.85, 0.75, 0.35, 1.0};
111   GLfloat black[4] = {0.0, 0.0,0.0,1.0};
112   GLfloat white[4] = {1.0, 1.0,1.0,1.0};
113
114   // Step 1: update all the positions
115   ourSun->updatePosition(t);
116   earthsMoon->updatePosition(t, ourSun);
117   mercury->updatePosition(t, ourSun);
118   venus->updatePosition(t, ourSun);
119   mars->updatePosition(t, ourSun);
120   jupiter->updatePosition(t, ourSun);
121   saturn->updatePosition(t, ourSun);
122   uranus->updatePosition(t, ourSun);
123   neptune->updatePosition(t, ourSun);
124   
125   fgUpdateSunPos();   // get the right sun angle (especially important when 
126                       // running for the first time.
127   if (displayList)
128     xglDeleteLists(displayList, 1);
129
130   displayList = xglGenLists(1);
131   // Step 2: rebuild the display list
132   xglNewList( displayList, GL_COMPILE);
133   {
134     // Step 2a: Add the moon...
135     xglEnable( GL_LIGHTING );
136     xglEnable( GL_LIGHT0 );
137     // set lighting parameters
138     xglLightfv(GL_LIGHT0, GL_AMBIENT, white );
139     xglLightfv(GL_LIGHT0, GL_DIFFUSE, white );
140     xglEnable( GL_CULL_FACE );
141     
142     // Enable blending, in order to effectively eliminate the dark side of the
143     // moon
144     glEnable(GL_BLEND);
145     glBlendFunc(GL_ONE, GL_ONE);
146     earthsMoon->getPos(&ra, &dec);
147     xglMaterialfv(GL_FRONT, GL_AMBIENT, black);
148     xglMaterialfv(GL_FRONT, GL_DIFFUSE, moonColor); 
149     xglPushMatrix();
150     {
151         earthsMoon->newImage(ra,dec);
152     }
153     xglPopMatrix();
154     glDisable(GL_BLEND);
155     xglDisable(GL_LIGHTING);
156  
157     // Step 2b:  Add the sun
158     sun_angle = l->sun_angle;
159     x_2 = sun_angle * sun_angle;
160     x_4 = x_2 * x_2;
161     x_8 = x_4 * x_4;
162     x_10 = x_8 * x_2;
163     ambient = (0.4 * pow (1.1, - x_10 / 30.0));
164     if (ambient < 0.3) ambient = 0.3;
165     if (ambient > 1.0) ambient = 1.0;
166
167     amb[0] = ((ambient * 6.0)  - 1.0); // minimum value = 0.8
168     amb[1] = ((ambient * 11.0) - 3.0); // minimum value = 0.3
169     amb[2] = ((ambient * 12.0) - 3.6); // minimum value = 0.0
170     amb[3] = 1.00;
171
172     if (amb[0] > 1.0) amb[0] = 1.0;
173     if (amb[1] > 1.0) amb[1] = 1.0;
174     if (amb[2] > 1.0) amb[2] = 1.0;
175
176     sun_angle = l->sun_angle * RAD_TO_DEG;
177     if ( sun_angle < 100 ) {
178         ourSun->getPos(&ra, &dec);
179         double cos_dec = cos(dec) * 60000.0;
180         x = cos(ra) * cos_dec;
181         y = sin(ra) * cos_dec;
182         z = sin(dec) * 60000.0;
183         xglPushMatrix();
184         {
185             double sun_size = 1400.0;
186             xglTranslatef(x,y,z);
187             xglColor3fv(amb);
188             glutSolidSphere(sun_size, 10, 10);
189         }
190         glPopMatrix();
191
192     } else {
193         // sun angle > 100, no need to draw sun
194     }
195     // Step 2c: Add the planets
196     xglBegin(GL_POINTS);
197     mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
198     venus  ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
199     mars   ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
200     jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
201     saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
202     uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
203     neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
204     xglEnd();
205     xglEnable(GL_LIGHTING);
206   }
207   xglEndList();
208 }
209
210 /*****************************************************************************
211  * double SolarSystem::scaleMagnitude(double magn)
212  * This private member function rescales the original magnitude, as used in the
213  * astronomical sense of the word, into a value used by OpenGL to draw a 
214  * convincing Star or planet
215  * 
216  * Argument: the astronomical magnitude
217  *
218  * return value: the rescaled magnitude
219  ****************************************************************************/
220 double SolarSystem::scaleMagnitude(double magn)
221 {
222   double magnitude = (0.0 - magn) / 5.0 + 1.0;
223   magnitude = magnitude * 0.7 + (3 * 0.1);
224   if (magnitude > 1.0) magnitude = 1.0;
225   if (magnitude < 0.0) magnitude = 0.0;
226   return magnitude;
227 }
228
229 /***************************************************************************
230  * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
231  *
232  * This private member function first causes the magnitude to be properly
233  * rescaled, and then adds the planet to the display list.
234  * 
235  * arguments: Right Ascension, declination, and magnitude
236  *
237  * return value: none
238  **************************************************************************/
239 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
240 {
241   double
242     magnitude = scaleMagnitude ( magn );
243
244   fgLIGHT *l = &cur_light_params;
245
246   if ((double) (l->sun_angle - FG_PI_2) > 
247       ((magnitude - 1.0) * - 20 * DEG_TO_RAD)) 
248     {
249       xglColor3f (magnitude, magnitude, magnitude);
250       xglVertex3f( 50000.0 * cos (ra) * cos (dec),
251                    50000.0 * sin (ra) * cos (dec),
252                    50000.0 * sin (dec));
253     }
254 }
255
256
257 SolarSystem* SolarSystem::theSolarSystem = 0;
258
259 /******************************************************************************
260  * void solarSystemRebuild()
261  * this a just a wrapper function, provided for use as an interface to the 
262  * event manager
263  *****************************************************************************/
264 void solarSystemRebuild()
265 {
266   SolarSystem::theSolarSystem->rebuild();
267 }