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 **************************************************************************/
34 # define exception c_exception
39 #include <simgear/xgl/xgl.h>
41 #include <simgear/debug/logstream.hxx>
43 #include <Time/sunpos.hxx>
44 #include <Time/moonpos.hxx>
45 #include "solarsystem.hxx"
47 /***************************************************************************
48 * default constructor for class SolarSystem:
49 * or course there can only be one way to create an entire solar system -:) )
50 * the FGTime argument is needed to properly initialize the the current orbital
52 *************************************************************************/
53 SolarSystem::SolarSystem(FGTime *t)
57 FG_LOG( FG_GENERAL, FG_ALERT, "Error: only one solarsystem allowed" );
60 theSolarSystem = this;
62 earthsMoon = new Moon(t);
63 mercury = new Mercury(t);
66 jupiter = new Jupiter(t);
67 saturn = new Saturn(t);
68 uranus = new Uranus(t);
69 neptune = new Neptune(t);
74 /**************************************************************************
75 * the destructor for class SolarSystem;
76 **************************************************************************/
77 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 = FGTime::cur_time_params;
105 //double x_2, x_4, x_8, x_10;*/
111 glDisable(GL_LIGHTING);
113 // Step 1: update all the positions
114 ourSun->updatePosition(t);
115 earthsMoon->updatePosition(t, ourSun);
116 mercury->updatePosition(t, ourSun);
117 venus->updatePosition(t, ourSun);
118 mars->updatePosition(t, ourSun);
119 jupiter->updatePosition(t, ourSun);
120 saturn->updatePosition(t, ourSun);
121 uranus->updatePosition(t, ourSun);
122 neptune->updatePosition(t, ourSun);
124 fgUpdateSunPos(); // get the right sun angle (especially important when
125 // running for the first time).
129 xglDeleteLists(displayList, 1);
131 displayList = xglGenLists(1);
133 FG_LOG( FG_ASTRO, FG_INFO, "Rebuilding astro display list" );
135 // Step 2: rebuild the display list
136 xglNewList( displayList, GL_COMPILE);
138 // Step 2a: Add the moon...
139 // Not that it is preferred to draw the moon first, and the sun next, in order to mime a
140 // solar eclipse. This is yet untested though...
141 // Euhh, actually the ecplise doesn't work...
143 // earthsMoon->newImage();
144 // Step 2b: Add the sun
145 // ourSun->newImage();
146 // Step 2c: Add the planets
148 mercury->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
149 venus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
150 mars ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
151 jupiter->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
152 saturn ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
153 uranus ->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
154 neptune->getPos(&ra, &dec, &magnitude);addPlanetToList(ra, dec, magnitude);
156 xglEnable(GL_LIGHTING);
162 /*****************************************************************************
163 * double SolarSystem::scaleMagnitude(double magn)
164 * This private member function rescales the original magnitude, as used in the
165 * astronomical sense of the word, into a value used by OpenGL to draw a
166 * convincing Star or planet
168 * Argument: the astronomical magnitude
170 * return value: the rescaled magnitude
171 ****************************************************************************/
172 double SolarSystem::scaleMagnitude(double magn)
174 double magnitude = (0.0 - magn) / 5.0 + 1.0;
175 magnitude = magnitude * 0.7 + (3 * 0.1);
176 if (magnitude > 1.0) magnitude = 1.0;
177 if (magnitude < 0.0) magnitude = 0.0;
181 /***************************************************************************
182 * void SolarSytem::addPlanetToList(double ra, double dec, double magn);
184 * This private member function first causes the magnitude to be properly
185 * rescaled, and then adds the planet to the display list.
187 * arguments: Right Ascension, declination, and magnitude
190 **************************************************************************/
191 void SolarSystem::addPlanetToList(double ra, double dec, double magn)
194 magnitude = scaleMagnitude ( magn );
196 fgLIGHT *l = &cur_light_params;
199 if ((double) (l->sun_angle - FG_PI_2) >
200 ((magnitude - 1.0) * - 20 * DEG_TO_RAD))
202 xglColor3f (magnitude, magnitude, magnitude);
203 xglVertex3f( 50000.0 * cos (ra) * cos (dec),
204 50000.0 * sin (ra) * cos (dec),
205 50000.0 * sin (dec));
211 SolarSystem* SolarSystem::theSolarSystem = 0;
213 /******************************************************************************
214 * void solarSystemRebuild()
215 * this a just a wrapper function, provided for use as an interface to the
217 *****************************************************************************/
218 void solarSystemRebuild()
220 SolarSystem::theSolarSystem->rebuild();