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 **************************************************************************/
36 #include <Debug/logstream.hxx>
37 #include <Time/sunpos.hxx>
38 #include "solarsystem.hxx"
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
45 *************************************************************************/
46 SolarSystem::SolarSystem(fgTIME *t)
50 FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
53 theSolarSystem = this;
55 earthsMoon = new Moon(t);
56 mercury = new Mercury(t);
59 jupiter = new Jupiter(t);
60 saturn = new Saturn(t);
61 uranus = new Uranus(t);
62 neptune = new Neptune(t);
67 /* --------------------------------------------------------------------------
68 the destructor for class SolarSystem;
69 danger: Huge Explosions ahead! (-:))
70 ------------------------------------------------------------------------*/
71 SolarSystem::~SolarSystem()
84 /****************************************************************************
85 * void SolarSystem::rebuild()
87 * this member function updates the positions for the sun, moon, and planets,
88 * and then rebuilds the display list.
92 ***************************************************************************/
93 void SolarSystem::rebuild()
95 fgLIGHT *l = &cur_light_params;
96 fgTIME *t = &cur_time_params;
100 double x_2, x_4, x_8, x_10;
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};
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);
119 fgUpdateSunPos(); // get the right sun angle (especially important when
120 // running for the first time.
122 xglDeleteLists(displayList, 1);
124 displayList = xglGenLists(1);
125 // Step 2: rebuild the display list
126 xglNewList( displayList, GL_COMPILE);
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 );
136 // Enable blending, in order to effectively eliminate the dark side of the
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);
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 x_2 = l -> sun_angle * l->sun_angle;
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;
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
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;
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);
182 xglTranslatef(x,y,z);
183 xglColor3f(amb[0], amb[1], amb[2]);
184 glutSolidSphere(1400.0, 10, 10);
187 // Step 2c: Add the planets
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);
197 xglEnable(GL_LIGHTING);
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
208 * Argument: the astronomical magnitude
210 * return value: the rescaled magnitude
211 ****************************************************************************/
212 double SolarSystem::scaleMagnitude(double magn)
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;
221 /***************************************************************************
222 * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
224 * This private member function first causes the magnitude to be properly
225 * rescaled, and then adds the planet to the display list.
227 * arguments: Right Ascension, declination, and magnitude
230 **************************************************************************/
231 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
234 magnitude = scaleMagnitude ( magn );
236 fgLIGHT *l = &cur_light_params;
238 if ((double) (l->sun_angle - FG_PI_2) >
239 ((magnitude - 1.0) * - 20 * DEG_TO_RAD))
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));
249 SolarSystem* SolarSystem::theSolarSystem = 0;
251 /******************************************************************************
252 * void solarSystemRebuild()
253 * this a just a wrapper function, provided for use as an interface to the
255 *****************************************************************************/
256 void solarSystemRebuild()
258 SolarSystem::theSolarSystem->rebuild();