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 **************************************************************************/
25 #include <Flight/flight.h>
29 #include <Debug/fg_debug.h>
30 #include <Objects/texload.h>
32 static GLuint moon_texid;
33 static GLubyte *moon_texbuf;
35 /*************************************************************************
36 * Moon::Moon(fgTIME *t)
37 * Public constructor for class Moon. Initializes the orbital elements and
38 * sets up the moon texture.
39 * Argument: The current time.
40 * the hard coded orbital elements for Moon are passed to
41 * CelestialBody::CelestialBody();
42 ************************************************************************/
43 Moon::Moon(fgTIME *t) :
44 CelestialBody(125.1228, -0.0529538083,
46 318.0634, 0.1643573223,
49 115.3654, 13.0649929509, t)
51 string tpath, fg_tpath;
54 fgPrintf( FG_GENERAL, FG_INFO, "Initializing Moon Texture\n");
56 xglGenTextures(1, &moon_texid);
57 xglBindTexture(GL_TEXTURE_2D, moon_texid);
58 #elif GL_EXT_texture_object
59 xglGenTexturesEXT(1, &moon_texid);
60 xglBindTextureEXT(GL_TEXTURE_2D, moon_texid);
65 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69 // load in the texture data
70 tpath = current_options.get_fg_root() + "/Textures/" + "moon.rgb";
72 if ( (moon_texbuf = read_rgb_texture(tpath.c_str(), &width, &height))
76 fg_tpath = tpath + ".gz";
77 if ( (moon_texbuf = read_rgb_texture(fg_tpath.c_str(), &width, &height))
80 fgPrintf( FG_GENERAL, FG_EXIT,
81 "Error in loading moon texture %s\n", tpath );
86 glTexImage2D( GL_TEXTURE_2D,
91 GL_RGB, GL_UNSIGNED_BYTE,
94 /*****************************************************************************
95 * void Moon::updatePosition(fgTIME *t, Star *ourSun)
96 * this member function calculates the actual topocentric position (i.e.)
97 * the position of the moon as seen from the current position on the surface
99 ****************************************************************************/
100 void Moon::updatePosition(fgTIME *t, Star *ourSun)
103 eccAnom, ecl, lonecl, latecl, actTime,
104 xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
105 Ls, Lm, D, F, mpar, gclat, rho, HA, g,
111 air = ¤t_aircraft;
114 updateOrbElements(t);
115 actTime = fgCalcActTime(t);
117 // calculate the angle between ecliptic and equatorial coordinate system
119 ecl = ((DEG_TO_RAD * 23.4393) - (DEG_TO_RAD * 3.563E-7) * actTime);
120 eccAnom = fgCalcEccAnom(M, e); // Calculate the eccentric anomaly
121 xv = a * (cos(eccAnom) - e);
122 yv = a * (sqrt(1.0 - e*e) * sin(eccAnom));
123 v = atan2(yv, xv); // the moon's true anomaly
124 r = sqrt (xv*xv + yv*yv); // and its distance
126 // estimate the geocentric rectangular coordinates here
127 xh = r * (cos(N) * cos (v+w) - sin (N) * sin(v+w) * cos(i));
128 yh = r * (sin(N) * cos (v+w) + cos (N) * sin(v+w) * cos(i));
129 zh = r * (sin(v+w) * sin(i));
131 // calculate the ecliptic latitude and longitude here
132 lonecl = atan2 (yh, xh);
133 latecl = atan2(zh, sqrt(xh*xh + yh*yh));
135 /* Calculate a number of perturbatioin, i.e. disturbances caused by the
136 * gravitational infuence of the sun and the other major planets.
137 * The largest of these even have a name */
138 Ls = ourSun->getM() + ourSun->getw();
143 lonecl += DEG_TO_RAD * (-1.274 * sin (M - 2*D)
145 -0.186 * sin(ourSun->getM())
146 -0.059 * sin(2*M - 2*D)
147 -0.057 * sin(M - 2*D + ourSun->getM())
148 +0.053 * sin(M + 2*D)
149 +0.046 * sin(2*D - ourSun->getM())
150 +0.041 * sin(M - ourSun->getM())
152 -0.031 * sin(M + ourSun->getM())
153 -0.015 * sin(2*F - 2*D)
154 +0.011 * sin(M - 4*D)
156 latecl += DEG_TO_RAD * (-0.173 * sin(F-2*D)
157 -0.055 * sin(M - F - 2*D)
158 -0.046 * sin(M + F - 2*D)
159 +0.033 * sin(F + 2*D)
160 +0.017 * sin(2*M + F)
162 r += (-0.58 * cos(M - 2*D)
165 fgPrintf(FG_GENERAL, FG_INFO, "Running moon update\n");
166 xg = r * cos(lonecl) * cos(latecl);
167 yg = r * sin(lonecl) * cos(latecl);
168 zg = r * sin(latecl);
171 ye = yg * cos(ecl) -zg * sin(ecl);
172 ze = yg * sin(ecl) +zg * cos(ecl);
174 geoRa = atan2(ye, xe);
175 geoDec = atan2(ze, sqrt(xe*xe + ye*ye));
177 // Given the moon's geocentric ra and dec, calculate its
178 // topocentric ra and dec. i.e. the position as seen from the
179 // surface of the earth, instead of the center of the earth
181 // First calculates the moon's parrallax, that is, the apparent size of the
182 // (equatorial) radius of the earth, as seen from the moon
183 mpar = asin ( 1 / r);
184 gclat = FG_Latitude - 0.003358 * sin (2 * DEG_TO_RAD * FG_Latitude);
185 rho = 0.99883 + 0.00167 * cos(2 * DEG_TO_RAD * FG_Latitude);
189 HA = t->lst - (3.8197186 * geoRa);
190 g = atan (tan(gclat) / cos ((HA / 3.8197186)));
191 rightAscension = geoRa - mpar * rho * cos(gclat) * sin(HA) / cos (geoDec);
192 declination = geoDec - mpar * rho * sin (gclat) * sin (g - geoDec) / sin(g);
196 /************************************************************************
197 * void Moon::newImage(float ra, float dec)
199 * This function regenerates a new visual image of the moon, which is added to
200 * solarSystem display list.
202 * Arguments: Right Ascension and declination
205 **************************************************************************/
206 void Moon::newImage(float ra, float dec)
208 glEnable(GL_TEXTURE_2D);
209 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
210 glBindTexture(GL_TEXTURE_2D, moon_texid);
212 //xglRotatef(-90, 0.0, 0.0, 1.0);
213 xglRotatef(((RAD_TO_DEG * ra)- 90.0), 0.0, 0.0, 1.0);
214 xglRotatef((RAD_TO_DEG * dec), 1.0, 0.0, 0.0);
216 fgPrintf( FG_GENERAL, FG_INFO,
217 "Ra = (%f), Dec= (%f)", (RAD_TO_DEG *ra), (RAD_TO_DEG *dec) );
218 xglTranslatef(0.0, 58600.0, 0.0);
219 Object = gluNewQuadric();
220 gluQuadricTexture( Object, GL_TRUE );
221 gluSphere( Object, 1367, 12, 12 );
222 glDisable(GL_TEXTURE_2D);