]> git.mxchange.org Git - flightgear.git/blob - Astro/moon.cxx
694e9a7aec02bf6f5701499b055da487e05bd7ae
[flightgear.git] / Astro / moon.cxx
1 /**************************************************************************
2  * moon.cxx
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). 
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25 #include <Flight/flight.hxx>
26
27 #include <string.h>
28 #include "moon.hxx"
29 #include <Debug/logstream.hxx>
30 #include <Objects/texload.h>
31
32
33 #ifdef __BORLANDC__
34 #  define exception c_exception
35 #endif
36 #include <math.h>
37
38 static GLuint moon_texid;
39 static GLubyte *moon_texbuf;
40
41 /*************************************************************************
42  * Moon::Moon(fgTIME *t)
43  * Public constructor for class Moon. Initializes the orbital elements and 
44  * sets up the moon texture.
45  * Argument: The current time.
46  * the hard coded orbital elements for Moon are passed to 
47  * CelestialBody::CelestialBody();
48  ************************************************************************/
49 Moon::Moon(fgTIME *t) :
50   CelestialBody(125.1228, -0.0529538083,
51                 5.1454,    0.00000,
52                 318.0634,  0.1643573223,
53                 60.266600, 0.000000,
54                 0.054900,  0.000000,
55                 115.3654,  13.0649929509, t)
56 {
57   string tpath, fg_tpath;
58   int width, height;
59   
60   FG_LOG( FG_GENERAL, FG_INFO, "Initializing Moon Texture");
61 #ifdef GL_VERSION_1_1
62   xglGenTextures(1, &moon_texid);
63   xglBindTexture(GL_TEXTURE_2D, moon_texid);
64 #elif GL_EXT_texture_object
65   xglGenTexturesEXT(1, &moon_texid);
66   xglBindTextureEXT(GL_TEXTURE_2D, moon_texid);
67 #else
68 #  error port me
69 #endif
70
71   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
72   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
73   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
74
75   // load in the texture data
76   tpath = current_options.get_fg_root() + "/Textures/" + "moon.rgb";
77   
78   if ( (moon_texbuf = read_rgb_texture(tpath.c_str(), &width, &height)) 
79        == NULL )
80   {
81     // Try compressed
82     fg_tpath = tpath + ".gz";
83     if ( (moon_texbuf = read_rgb_texture(fg_tpath.c_str(), &width, &height)) 
84          == NULL )
85     {
86         FG_LOG( FG_GENERAL, FG_ALERT, 
87                 "Error in loading moon texture " << tpath );
88         exit(-1);
89     } 
90   } 
91
92   glTexImage2D( GL_TEXTURE_2D,
93                 0,
94                 GL_RGB,
95                 256, 256,
96                 0,
97                 GL_RGB, GL_UNSIGNED_BYTE,
98                 moon_texbuf);
99 }
100 /*****************************************************************************
101  * void Moon::updatePosition(fgTIME *t, Star *ourSun)
102  * this member function calculates the actual topocentric position (i.e.) 
103  * the position of the moon as seen from the current position on the surface
104  * of the moon. 
105  ****************************************************************************/
106 void Moon::updatePosition(fgTIME *t, Star *ourSun)
107 {
108   double 
109     eccAnom, ecl, lonecl, latecl, actTime,
110     xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
111     Ls, Lm, D, F, mpar, gclat, rho, HA, g,
112     geoRa, geoDec;
113   
114   fgAIRCRAFT *air;
115   fgFLIGHT *f;
116
117   air = &current_aircraft;
118   f = air->flight;
119  
120   updateOrbElements(t);
121   actTime = fgCalcActTime(t);
122
123   // calculate the angle between ecliptic and equatorial coordinate system
124   // in Radians
125   ecl = ((DEG_TO_RAD * 23.4393) - (DEG_TO_RAD * 3.563E-7) * actTime);  
126   eccAnom = fgCalcEccAnom(M, e);  // Calculate the eccentric anomaly
127   xv = a * (cos(eccAnom) - e);
128   yv = a * (sqrt(1.0 - e*e) * sin(eccAnom));
129   v = atan2(yv, xv);               // the moon's true anomaly
130   r = sqrt (xv*xv + yv*yv);       // and its distance
131   
132   // estimate the geocentric rectangular coordinates here
133   xh = r * (cos(N) * cos (v+w) - sin (N) * sin(v+w) * cos(i));
134   yh = r * (sin(N) * cos (v+w) + cos (N) * sin(v+w) * cos(i));
135   zh = r * (sin(v+w) * sin(i));
136
137   // calculate the ecliptic latitude and longitude here
138   lonecl = atan2 (yh, xh);
139   latecl = atan2(zh, sqrt(xh*xh + yh*yh));
140
141   /* Calculate a number of perturbatioin, i.e. disturbances caused by the 
142    * gravitational infuence of the sun and the other major planets.
143    * The largest of these even have a name */
144   Ls = ourSun->getM() + ourSun->getw();
145   Lm = M + w + N;
146   D = Lm - Ls;
147   F = Lm - N;
148   
149   lonecl += DEG_TO_RAD * (-1.274 * sin (M - 2*D)
150                           +0.658 * sin (2*D)
151                           -0.186 * sin(ourSun->getM())
152                           -0.059 * sin(2*M - 2*D)
153                           -0.057 * sin(M - 2*D + ourSun->getM())
154                           +0.053 * sin(M + 2*D)
155                           +0.046 * sin(2*D - ourSun->getM())
156                           +0.041 * sin(M - ourSun->getM())
157                           -0.035 * sin(D)
158                           -0.031 * sin(M + ourSun->getM())
159                           -0.015 * sin(2*F - 2*D)
160                           +0.011 * sin(M - 4*D)
161                           );
162   latecl += DEG_TO_RAD * (-0.173 * sin(F-2*D)
163                           -0.055 * sin(M - F - 2*D)
164                           -0.046 * sin(M + F - 2*D)
165                           +0.033 * sin(F + 2*D)
166                           +0.017 * sin(2*M + F)
167                           );
168   r += (-0.58 * cos(M - 2*D)
169         -0.46 * cos(2*D)
170         );
171   FG_LOG(FG_GENERAL, FG_INFO, "Running moon update");
172   xg = r * cos(lonecl) * cos(latecl);
173   yg = r * sin(lonecl) * cos(latecl);
174   zg = r *               sin(latecl);
175   
176   xe = xg;
177   ye = yg * cos(ecl) -zg * sin(ecl);
178   ze = yg * sin(ecl) +zg * cos(ecl);
179
180   geoRa  = atan2(ye, xe);
181   geoDec = atan2(ze, sqrt(xe*xe + ye*ye));
182
183   // Given the moon's geocentric ra and dec, calculate its 
184   // topocentric ra and dec. i.e. the position as seen from the
185   // surface of the earth, instead of the center of the earth
186
187   // First calculates the moon's parrallax, that is, the apparent size of the 
188   // (equatorial) radius of the earth, as seen from the moon 
189   mpar = asin ( 1 / r);
190   gclat = f->get_Latitude() - 0.003358 * 
191       sin (2 * DEG_TO_RAD * f->get_Latitude() );
192   rho = 0.99883 + 0.00167 * cos(2 * DEG_TO_RAD * f->get_Latitude());
193   if (geoRa < 0)
194     geoRa += (2*FG_PI);
195   
196   HA = t->lst - (3.8197186 * geoRa);
197   g = atan (tan(gclat) / cos ((HA / 3.8197186)));
198   rightAscension = geoRa - mpar * rho * cos(gclat) * sin(HA) / cos (geoDec);
199   declination = geoDec - mpar * rho * sin (gclat) * sin (g - geoDec) / sin(g);
200 }
201
202
203 /************************************************************************
204  * void Moon::newImage(float ra, float dec)
205  *
206  * This function regenerates a new visual image of the moon, which is added to
207  * solarSystem display list.
208  *
209  * Arguments: Right Ascension and declination
210  *
211  * return value: none
212  **************************************************************************/
213 void Moon::newImage(float ra, float dec)
214 {
215   glEnable(GL_TEXTURE_2D);
216   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
217   glBindTexture(GL_TEXTURE_2D, moon_texid);
218
219   //xglRotatef(-90, 0.0, 0.0, 1.0);
220   xglRotatef(((RAD_TO_DEG * ra)- 90.0), 0.0, 0.0, 1.0);
221   xglRotatef((RAD_TO_DEG * dec), 1.0, 0.0, 0.0);
222
223   FG_LOG( FG_GENERAL, FG_INFO, 
224           "Ra = (" << (RAD_TO_DEG *ra) 
225           << "), Dec= (" << (RAD_TO_DEG *dec) << ")" );
226   xglTranslatef(0.0, 58600.0, 0.0);
227   Object = gluNewQuadric();
228   gluQuadricTexture( Object, GL_TRUE );   
229   gluSphere( Object,  1367, 12, 12 );
230   glDisable(GL_TEXTURE_2D);
231 }
232
233
234
235
236
237
238