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 # define exception c_exception
42 #include <Debug/logstream.hxx>
43 #include <Time/sunpos.hxx>
44 #include "solarsystem.hxx"
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
51 *************************************************************************/
52 SolarSystem::SolarSystem(fgTIME *t)
56 FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
59 theSolarSystem = this;
61 earthsMoon = new Moon(t);
62 mercury = new Mercury(t);
65 jupiter = new Jupiter(t);
66 saturn = new Saturn(t);
67 uranus = new Uranus(t);
68 neptune = new Neptune(t);
73 /* --------------------------------------------------------------------------
74 the destructor for class SolarSystem;
75 danger: Huge Explosions ahead! (-:))
76 ------------------------------------------------------------------------*/
77 SolarSystem::~SolarSystem()
90 /****************************************************************************
91 * void SolarSystem::rebuild()
93 * this member function updates the positions for the sun, moon, and planets,
94 * and then rebuilds the display list.
98 ***************************************************************************/
99 void SolarSystem::rebuild()
101 fgLIGHT *l = &cur_light_params;
102 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 x = 60000.0 * cos(ra) * cos (dec);
148 y = 60000.0 * sin(ra) * cos (dec);
149 z = 60000.0 * sin(dec);
150 xx = cos(ra) * cos(dec);
151 yy = sin(ra) * cos(dec);
153 xglMaterialfv(GL_FRONT, GL_AMBIENT, black);
154 xglMaterialfv(GL_FRONT, GL_DIFFUSE, moonColor);
157 earthsMoon->newImage(ra,dec);
161 xglDisable(GL_LIGHTING);
163 // Step 2b: Add the sun
164 x_2 = l -> sun_angle * l->sun_angle;
168 ambient = (0.4 * pow (1.1, - x_10 / 30.0));
169 if (ambient < 0.3) ambient = 0.3;
170 if (ambient > 1.0) ambient = 1.0;
172 amb[0] = 0.00 + ((ambient * 6.0) - 1.0); // minimum value = 0.8
173 amb[1] = 0.00 + ((ambient * 11.0) - 3.0); // minimum value = 0.3
174 amb[2] = 0.00 + ((ambient * 12.0) - 3.6); // minimum value = 0.0
177 if (amb[0] > 1.0) amb[0] = 1.0;
178 if (amb[1] > 1.0) amb[1] = 1.0;
179 if (amb[2] > 1.0) amb[2] = 1.0;
181 ourSun->getPos(&ra, &dec);
182 x = 60000.0 * cos(ra) * cos(dec);
183 y = 60000.0 * sin(ra) * cos(dec);
184 z = 60000.0 * sin(dec);
188 xglTranslatef(x,y,z);
189 xglColor3f(amb[0], amb[1], amb[2]);
190 glutSolidSphere(1400.0, 10, 10);
193 // Step 2c: Add the planets
195 mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
196 venus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
197 mars ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
198 jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
199 saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
200 uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
201 neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
203 xglEnable(GL_LIGHTING);
208 /*****************************************************************************
209 * double SolarSystem::scaleMagnitude(double magn)
210 * This private member function rescales the original magnitude, as used in the
211 * astronomical sense of the word, into a value used by OpenGL to draw a
212 * convincing Star or planet
214 * Argument: the astronomical magnitude
216 * return value: the rescaled magnitude
217 ****************************************************************************/
218 double SolarSystem::scaleMagnitude(double magn)
220 double magnitude = (0.0 - magn) / 5.0 + 1.0;
221 magnitude = magnitude * 0.7 + (3 * 0.1);
222 if (magnitude > 1.0) magnitude = 1.0;
223 if (magnitude < 0.0) magnitude = 0.0;
227 /***************************************************************************
228 * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
230 * This private member function first causes the magnitude to be properly
231 * rescaled, and then adds the planet to the display list.
233 * arguments: Right Ascension, declination, and magnitude
236 **************************************************************************/
237 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
240 magnitude = scaleMagnitude ( magn );
242 fgLIGHT *l = &cur_light_params;
244 if ((double) (l->sun_angle - FG_PI_2) >
245 ((magnitude - 1.0) * - 20 * DEG_TO_RAD))
247 xglColor3f (magnitude, magnitude, magnitude);
248 xglVertex3f( 50000.0 * cos (ra) * cos (dec),
249 50000.0 * sin (ra) * cos (dec),
250 50000.0 * sin (dec));
255 SolarSystem* SolarSystem::theSolarSystem = 0;
257 /******************************************************************************
258 * void solarSystemRebuild()
259 * this a just a wrapper function, provided for use as an interface to the
261 *****************************************************************************/
262 void solarSystemRebuild()
264 SolarSystem::theSolarSystem->rebuild();