1 /**************************************************************************
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).
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
35 # define exception c_exception
41 #include <Debug/logstream.hxx>
42 #include <Time/sunpos.hxx>
43 #include "solarsystem.hxx"
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
50 *************************************************************************/
51 SolarSystem::SolarSystem(fgTIME *t)
55 FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
58 theSolarSystem = this;
60 earthsMoon = new Moon(t);
61 mercury = new Mercury(t);
64 jupiter = new Jupiter(t);
65 saturn = new Saturn(t);
66 uranus = new Uranus(t);
67 neptune = new Neptune(t);
72 /* --------------------------------------------------------------------------
73 the destructor for class SolarSystem;
74 danger: Huge Explosions ahead! (-:))
75 ------------------------------------------------------------------------*/
76 SolarSystem::~SolarSystem()
89 /****************************************************************************
90 * void SolarSystem::rebuild()
92 * this member function updates the positions for the sun, moon, and planets,
93 * and then rebuilds the display list.
97 ***************************************************************************/
98 void SolarSystem::rebuild()
100 fgLIGHT *l = &cur_light_params;
101 fgTIME *t = &cur_time_params;
106 double x_2, x_4, x_8, x_10;
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};
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);
125 fgUpdateSunPos(); // get the right sun angle (especially important when
126 // running for the first time.
128 xglDeleteLists(displayList, 1);
130 displayList = xglGenLists(1);
131 // Step 2: rebuild the display list
132 xglNewList( displayList, GL_COMPILE);
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 );
142 // Enable blending, in order to effectively eliminate the dark side of the
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);
151 earthsMoon->newImage(ra,dec);
155 xglDisable(GL_LIGHTING);
157 // Step 2b: Add the sun
158 sun_angle = l->sun_angle;
159 x_2 = sun_angle * sun_angle;
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;
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
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;
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;
185 double sun_size = 1400.0;
186 xglTranslatef(x,y,z);
188 glutSolidSphere(sun_size, 10, 10);
193 // sun angle > 100, no need to draw sun
195 // Step 2c: Add the planets
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);
205 xglEnable(GL_LIGHTING);
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
216 * Argument: the astronomical magnitude
218 * return value: the rescaled magnitude
219 ****************************************************************************/
220 double SolarSystem::scaleMagnitude(double magn)
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;
229 /***************************************************************************
230 * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
232 * This private member function first causes the magnitude to be properly
233 * rescaled, and then adds the planet to the display list.
235 * arguments: Right Ascension, declination, and magnitude
238 **************************************************************************/
239 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
242 magnitude = scaleMagnitude ( magn );
244 fgLIGHT *l = &cur_light_params;
246 if ((double) (l->sun_angle - FG_PI_2) >
247 ((magnitude - 1.0) * - 20 * DEG_TO_RAD))
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));
257 SolarSystem* SolarSystem::theSolarSystem = 0;
259 /******************************************************************************
260 * void solarSystemRebuild()
261 * this a just a wrapper function, provided for use as an interface to the
263 *****************************************************************************/
264 void solarSystemRebuild()
266 SolarSystem::theSolarSystem->rebuild();