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 **************************************************************************/
27 #include <Debug/fg_debug.h>
28 #include <Time/sunpos.hxx>
29 #include "solarsystem.hxx"
31 /***************************************************************************
32 * default constructor for class SolarSystem:
33 * or course there can only be one way to create an entire solar system -:) )
34 * the fgTIME argument is needed to properly initialize the the current orbital
36 *************************************************************************/
37 SolarSystem::SolarSystem(fgTIME *t)
41 fgPrintf(FG_GENERAL, FG_EXIT, "Error: only one solarsystem allowed\n");
43 theSolarSystem = this;
45 earthsMoon = new Moon(t);
46 mercury = new Mercury(t);
49 jupiter = new Jupiter(t);
50 saturn = new Saturn(t);
51 uranus = new Uranus(t);
52 neptune = new Neptune(t);
57 /* --------------------------------------------------------------------------
58 the destructor for class SolarSystem;
59 danger: Huge Explosions ahead! (-:))
60 ------------------------------------------------------------------------*/
61 SolarSystem::~SolarSystem()
74 /****************************************************************************
75 * void SolarSystem::rebuild()
77 * this member function updates the positions for the sun, moon, and planets,
78 * and then rebuilds the display list.
82 ***************************************************************************/
83 void SolarSystem::rebuild()
85 fgLIGHT *l = &cur_light_params;
86 fgTIME *t = &cur_time_params;
90 double x_2, x_4, x_8, x_10;
94 GLfloat moonColor[4] = {0.85, 0.75, 0.35, 1.0};
95 GLfloat black[4] = {0.0, 0.0,0.0,1.0};
96 GLfloat white[4] = {1.0, 1.0,1.0,1.0};
98 // Step 1: update all the positions
99 ourSun->updatePosition(t);
100 earthsMoon->updatePosition(t, ourSun);
101 mercury->updatePosition(t, ourSun);
102 venus->updatePosition(t, ourSun);
103 mars->updatePosition(t, ourSun);
104 jupiter->updatePosition(t, ourSun);
105 saturn->updatePosition(t, ourSun);
106 uranus->updatePosition(t, ourSun);
107 neptune->updatePosition(t, ourSun);
109 fgUpdateSunPos(); // get the right sun angle (especially important when
110 // running for the first time.
112 xglDeleteLists(displayList, 1);
114 displayList = xglGenLists(1);
115 // Step 2: rebuild the display list
116 xglNewList( displayList, GL_COMPILE);
118 // Step 2a: Add the moon...
119 xglEnable( GL_LIGHTING );
120 xglEnable( GL_LIGHT0 );
121 // set lighting parameters
122 xglLightfv(GL_LIGHT0, GL_AMBIENT, white );
123 xglLightfv(GL_LIGHT0, GL_DIFFUSE, white );
124 xglEnable( GL_CULL_FACE );
126 // Enable blending, in order to effectively eliminate the dark side of the
129 glBlendFunc(GL_ONE, GL_ONE);
130 earthsMoon->getPos(&ra, &dec);
131 x = 60000.0 * cos(ra) * cos (dec);
132 y = 60000.0 * sin(ra) * cos (dec);
133 z = 60000.0 * sin(dec);
134 xx = cos(ra) * cos(dec);
135 yy = sin(ra) * cos(dec);
137 xglMaterialfv(GL_FRONT, GL_AMBIENT, black);
138 xglMaterialfv(GL_FRONT, GL_DIFFUSE, moonColor);
141 earthsMoon->newImage(ra,dec);
145 xglDisable(GL_LIGHTING);
147 // Step 2b: Add the sun
148 x_2 = l -> sun_angle * l->sun_angle;
152 ambient = (0.4 * pow (1.1, - x_10 / 30.0));
153 if (ambient < 0.3) ambient = 0.3;
154 if (ambient > 1.0) ambient = 1.0;
156 amb[0] = 0.00 + ((ambient * 6.0) - 1.0); // minimum value = 0.8
157 amb[1] = 0.00 + ((ambient * 11.0) - 3.0); // minimum value = 0.3
158 amb[2] = 0.00 + ((ambient * 12.0) - 3.6); // minimum value = 0.0
161 if (amb[0] > 1.0) amb[0] = 1.0;
162 if (amb[1] > 1.0) amb[1] = 1.0;
163 if (amb[2] > 1.0) amb[2] = 1.0;
165 ourSun->getPos(&ra, &dec);
166 x = 60000.0 * cos(ra) * cos(dec);
167 y = 60000.0 * sin(ra) * cos(dec);
168 z = 60000.0 * sin(dec);
172 xglTranslatef(x,y,z);
173 xglColor3f(amb[0], amb[1], amb[2]);
174 glutSolidSphere(1400.0, 10, 10);
177 // Step 2c: Add the planets
179 mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
180 venus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
181 mars ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
182 jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
183 saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
184 uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
185 neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
187 xglEnable(GL_LIGHTING);
192 /*****************************************************************************
193 * double SolarSystem::scaleMagnitude(double magn)
194 * This private member function rescales the original magnitude, as used in the
195 * astronomical sense of the word, into a value used by OpenGL to draw a
196 * convincing Star or planet
198 * Argument: the astronomical magnitude
200 * return value: the rescaled magnitude
201 ****************************************************************************/
202 double SolarSystem::scaleMagnitude(double magn)
204 double magnitude = (0.0 - magn) / 5.0 + 1.0;
205 magnitude = magnitude * 0.7 + (3 * 0.1);
206 if (magnitude > 1.0) magnitude = 1.0;
207 if (magnitude < 0.0) magnitude = 0.0;
211 /***************************************************************************
212 * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
214 * This private member function first causes the magnitude to be properly
215 * rescaled, and then adds the planet to the display list.
217 * arguments: Right Ascension, declination, and magnitude
220 **************************************************************************/
221 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
224 magnitude = scaleMagnitude ( magn );
226 fgLIGHT *l = &cur_light_params;
228 if ((double) (l->sun_angle - FG_PI_2) >
229 ((magnitude - 1.0) * - 20 * DEG_TO_RAD))
231 xglColor3f (magnitude, magnitude, magnitude);
232 xglVertex3f( 50000.0 * cos (ra) * cos (dec),
233 50000.0 * sin (ra) * cos (dec),
234 50000.0 * sin (dec));
239 SolarSystem* SolarSystem::theSolarSystem = 0;
241 /******************************************************************************
242 * void solarSystemRebuild()
243 * this a just a wrapper function, provided for use as an interface to the
245 *****************************************************************************/
246 void solarSystemRebuild()
248 SolarSystem::theSolarSystem->rebuild();