]> git.mxchange.org Git - flightgear.git/blob - Astro/solarsystem.cxx
Converted to new logstream debugging facility. This allows release
[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 #include <GL/glut.h>
35 #include <XGL/xgl.h>
36 #include <Debug/logstream.hxx>
37 #include <Time/sunpos.hxx>
38 #include "solarsystem.hxx"
39
40 /***************************************************************************
41  * default constructor for class  SolarSystem:   
42  * or course there can only be one way to create an entire solar system -:) )
43  * the fgTIME argument is needed to properly initialize the the current orbital
44  * elements
45  *************************************************************************/
46 SolarSystem::SolarSystem(fgTIME *t)
47 {
48   if (theSolarSystem)
49     {
50       FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
51       exit(-1);
52     }
53   theSolarSystem = this;
54   ourSun     = new Star(t);   
55   earthsMoon = new Moon(t);
56   mercury    = new Mercury(t);
57   venus      = new Venus(t);
58   mars       = new Mars(t);
59   jupiter    = new Jupiter(t);
60   saturn     = new Saturn(t);
61   uranus     = new Uranus(t);
62   neptune    = new Neptune(t);
63
64   displayList = 0;
65 };
66
67 /* --------------------------------------------------------------------------
68    the destructor for class SolarSystem;
69    danger: Huge Explosions ahead! (-:))
70    ------------------------------------------------------------------------*/
71 SolarSystem::~SolarSystem()
72 {
73   delete ourSun;
74   delete earthsMoon;
75   delete mercury;
76   delete venus;
77   delete mars;
78   delete jupiter;
79   delete saturn;
80   delete uranus;
81   delete neptune;
82   //delete pluto;
83 }
84 /****************************************************************************
85  * void SolarSystem::rebuild()
86  *
87  * this member function updates the positions for the sun, moon, and planets,
88  * and then rebuilds the display list. 
89  *
90  * arguments: none
91  * return value: none
92  ***************************************************************************/
93 void SolarSystem::rebuild()
94 {
95   fgLIGHT *l = &cur_light_params;
96   fgTIME  *t = &cur_time_params;  
97   float x, y, z,
98     xx, yy,zz;
99   double ra, dec;
100   double x_2, x_4, x_8, x_10;
101   double magnitude;
102   GLfloat ambient;
103   GLfloat amb[4];
104   GLfloat moonColor[4] = {0.85, 0.75, 0.35, 1.0};
105   GLfloat black[4] = {0.0, 0.0,0.0,1.0};
106   GLfloat white[4] = {1.0, 1.0,1.0,1.0};
107
108   // Step 1: update all the positions
109   ourSun->updatePosition(t);
110   earthsMoon->updatePosition(t, ourSun);
111   mercury->updatePosition(t, ourSun);
112   venus->updatePosition(t, ourSun);
113   mars->updatePosition(t, ourSun);
114   jupiter->updatePosition(t, ourSun);
115   saturn->updatePosition(t, ourSun);
116   uranus->updatePosition(t, ourSun);
117   neptune->updatePosition(t, ourSun);
118   
119   fgUpdateSunPos();   // get the right sun angle (especially important when 
120                       // running for the first time.
121   if (displayList)
122     xglDeleteLists(displayList, 1);
123
124   displayList = xglGenLists(1);
125   // Step 2: rebuild the display list
126   xglNewList( displayList, GL_COMPILE);
127   {
128     // Step 2a: Add the moon...
129     xglEnable( GL_LIGHTING );
130     xglEnable( GL_LIGHT0 );
131     // set lighting parameters
132     xglLightfv(GL_LIGHT0, GL_AMBIENT, white );
133     xglLightfv(GL_LIGHT0, GL_DIFFUSE, white );
134     xglEnable( GL_CULL_FACE );
135     
136     // Enable blending, in order to effectively eliminate the dark side of the
137     // moon
138     glEnable(GL_BLEND);
139     glBlendFunc(GL_ONE, GL_ONE);
140     earthsMoon->getPos(&ra, &dec);
141     x = 60000.0 * cos(ra) * cos (dec);
142     y = 60000.0 * sin(ra) * cos (dec);
143     z = 60000.0 * sin(dec);
144     xx = cos(ra) * cos(dec);
145     yy = sin(ra) * cos(dec);
146     zz = sin(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     x_2 = l -> sun_angle * l->sun_angle;
159     x_4 = x_2 * x_2;
160     x_8 = x_4 * x_4;
161     x_10 = x_8 * x_2;
162     ambient = (0.4 * pow (1.1, - x_10 / 30.0));
163     if (ambient < 0.3) ambient = 0.3;
164     if (ambient > 1.0) ambient = 1.0;
165
166     amb[0] = 0.00 + ((ambient * 6.0)  - 1.0); // minimum value = 0.8
167     amb[1] = 0.00 + ((ambient * 11.0) - 3.0); // minimum value = 0.3
168     amb[2] = 0.00 + ((ambient * 12.0) - 3.6); // minimum value = 0.0
169     amb[3] = 1.00;
170
171     if (amb[0] > 1.0) amb[0] = 1.0;
172     if (amb[1] > 1.0) amb[1] = 1.0;
173     if (amb[2] > 1.0) amb[2] = 1.0;
174
175     ourSun->getPos(&ra, &dec);
176     x = 60000.0 * cos(ra) * cos(dec);
177     y = 60000.0 * sin(ra) * cos(dec);
178     z = 60000.0 * sin(dec);
179     xglPushMatrix();
180     {
181       // xglPushMatrix();
182       xglTranslatef(x,y,z);
183       xglColor3f(amb[0], amb[1], amb[2]);
184       glutSolidSphere(1400.0, 10, 10);
185     }
186     glPopMatrix();
187     // Step 2c: Add the planets
188     xglBegin(GL_POINTS);
189     mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
190     venus  ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
191     mars   ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
192     jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
193     saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
194     uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
195     neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
196     xglEnd();
197     xglEnable(GL_LIGHTING);
198   }
199   xglEndList();
200 }
201
202 /*****************************************************************************
203  * double SolarSystem::scaleMagnitude(double magn)
204  * This private member function rescales the original magnitude, as used in the
205  * astronomical sense of the word, into a value used by OpenGL to draw a 
206  * convincing Star or planet
207  * 
208  * Argument: the astronomical magnitude
209  *
210  * return value: the rescaled magnitude
211  ****************************************************************************/
212 double SolarSystem::scaleMagnitude(double magn)
213 {
214   double magnitude = (0.0 - magn) / 5.0 + 1.0;
215   magnitude = magnitude * 0.7 + (3 * 0.1);
216   if (magnitude > 1.0) magnitude = 1.0;
217   if (magnitude < 0.0) magnitude = 0.0;
218   return magnitude;
219 }
220
221 /***************************************************************************
222  * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
223  *
224  * This private member function first causes the magnitude to be properly
225  * rescaled, and then adds the planet to the display list.
226  * 
227  * arguments: Right Ascension, declination, and magnitude
228  *
229  * return value: none
230  **************************************************************************/
231 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
232 {
233   double
234     magnitude = scaleMagnitude ( magn );
235
236   fgLIGHT *l = &cur_light_params;
237
238   if ((double) (l->sun_angle - FG_PI_2) > 
239       ((magnitude - 1.0) * - 20 * DEG_TO_RAD)) 
240     {
241       xglColor3f (magnitude, magnitude, magnitude);
242       xglVertex3f( 50000.0 * cos (ra) * cos (dec),
243                    50000.0 * sin (ra) * cos (dec),
244                    50000.0 * sin (dec));
245     }
246 }
247
248
249 SolarSystem* SolarSystem::theSolarSystem = 0;
250
251 /******************************************************************************
252  * void solarSystemRebuild()
253  * this a just a wrapper function, provided for use as an interface to the 
254  * event manager
255  *****************************************************************************/
256 void solarSystemRebuild()
257 {
258   SolarSystem::theSolarSystem->rebuild();
259 }
260
261
262
263
264
265
266