]> git.mxchange.org Git - simgear.git/blob - Astro/moon.cxx
Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
[simgear.git] / Astro / moon.cxx
1 /**************************************************************************
2  * moon.c
3  * Written by Durk Talsma. Started October 1997, for the flight gear project.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  * (Log is kept at end of this file)
21  **************************************************************************/
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <math.h>
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include <Aircraft/aircraft.h>
37 #include <Debug/fg_debug.h>
38 #include <Include/fg_constants.h>
39 #include <Include/general.h>
40 #include <Main/views.hxx>
41 #include <Time/fg_time.hxx>
42
43 #include "orbits.hxx"
44 #include "moon.hxx"
45
46
47 struct CelestialCoord moonPos;
48
49 static float xMoon, yMoon, zMoon;
50 static GLint moon = 0;
51
52
53
54 /* --------------------------------------------------------------
55       This section contains the code that calculates the actual
56       position of the moon in the night sky.
57 ----------------------------------------------------------------*/
58 struct CelestialCoord fgCalculateMoon(struct OrbElements params,
59                                       struct OrbElements sunParams,
60                                       struct fgTIME t)
61 {
62   struct CelestialCoord
63     geocCoord, topocCoord; 
64   
65   double
66     eccAnom, ecl, lonecl, latecl, actTime,
67     xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
68     Ls, Lm, D, F, mpar, gclat, rho, HA, g;
69   
70   fgAIRCRAFT *a;
71   fgFLIGHT *f;
72
73   a = &current_aircraft;
74   f = a->flight;
75   
76   /* calculate the angle between ecliptic and equatorial coordinate
77    * system, in Radians */
78   actTime = fgCalcActTime(t);
79   ecl = ((DEG_TO_RAD * 23.4393) - (DEG_TO_RAD * 3.563E-7) * actTime);
80   /*ecl = 0.409093 - 6.2186E-9 * actTime; */
81                                                         
82   /* calculate the eccentric anomaly */
83   eccAnom = fgCalcEccAnom(params.M, params.e);
84
85   /* calculate the moon's distance (r) and  true anomaly (v) */
86   xv = params.a * ( cos(eccAnom) - params.e);
87   yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom));
88   v =atan2(yv, xv);
89   r = sqrt(xv*xv + yv*yv);
90   
91   /* estimate the geocentric rectangular coordinates here */
92   xh = r * (cos (params.N) * cos (v + params.w) -
93             sin (params.N) * sin (v + params.w) * cos (params.i));
94   yh = r * (sin (params.N) * cos (v + params.w) +
95             cos (params.N) * sin (v + params.w) * cos (params.i));
96   zh = r * (sin(v + params.w) * sin(params.i));
97   
98   /* calculate the ecliptic latitude and longitude here */
99   lonecl = atan2( yh, xh);
100   latecl = atan2( zh, sqrt( xh*xh + yh*yh));
101
102   /* calculate a number of perturbations, i.e. disturbances caused by
103    * the gravitational influence of the sun and the other mayor
104    * planets. The largest of these even have their own names */
105   Ls = sunParams.M + sunParams.w;
106   Lm =    params.M +    params.w + params.N;
107   D = Lm - Ls;
108   F = Lm - params.N;
109   
110   lonecl += DEG_TO_RAD * (
111                           - 1.274 * sin (params.M - 2*D)                        /* the Evection         */
112                           + 0.658 * sin (2 * D)                                 /* the Variation        */
113                           - 0.186 * sin (sunParams.M)                           /* the yearly variation */
114                           - 0.059 * sin (2*params.M - 2*D)
115                           - 0.057 * sin (params.M - 2*D + sunParams.M)
116                           + 0.053 * sin (params.M + 2*D)
117                           + 0.046 * sin (2*D - sunParams.M)
118                           + 0.041 * sin (params.M - sunParams.M)
119                           - 0.035 * sin (D)                                      /* the Parallactic Equation */
120                           - 0.031 * sin (params.M + sunParams.M)
121                           - 0.015 * sin (2*F - 2*D)
122                           + 0.011 * sin (params.M - 4*D)
123                           );
124   latecl += DEG_TO_RAD * (
125                           - 0.173 * sin (F - 2*D)
126                           - 0.055 * sin (params.M - F - 2*D)
127                           - 0.046 * sin (params.M + F - 2*D)
128                           + 0.033 * sin (F + 2*D)
129                           + 0.017 * sin (2 * params.M + F)
130                           );
131   
132   r += (
133         - 0.58 * cos(params.M - 2*D)
134         - 0.46 * cos(2*D)
135         );
136   
137   xg = r * cos(lonecl) * cos(latecl);
138   yg = r * sin(lonecl) * cos(latecl);
139   zg = r *               sin(latecl);
140
141   xe  = xg;
142   ye = yg * cos(ecl) - zg * sin(ecl);
143   ze = yg * sin(ecl) + zg * cos(ecl);
144   
145
146   
147
148   geocCoord.RightAscension = atan2(ye, xe);
149   geocCoord.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
150   
151   /* New since 25 december 1997 */
152   /* Calculate the moon's topocentric position instead of it's geocentric! */
153
154   /* calculate the moon's parrallax, i.e. the apparent size of the
155    * (equatorial) radius of the Earth, as seen from the moon */
156   mpar = asin( 1 / r); 
157   gclat = FG_Latitude - 0.083358 * sin (2 * DEG_TO_RAD *  FG_Latitude);
158   rho = 0.99883 + 0.00167 * cos(2 * DEG_TO_RAD * FG_Latitude);
159
160   if (geocCoord.RightAscension < 0)
161     geocCoord.RightAscension += (2*FG_PI);
162
163   HA = t.lst - (3.8197186 * geocCoord.RightAscension);
164
165   g = atan (tan(gclat) / cos( (HA / 3.8197186))); 
166
167      
168
169   topocCoord.RightAscension = geocCoord.RightAscension -
170       mpar * rho * cos (gclat) * sin (HA) / cos (geocCoord.Declination);
171   topocCoord.Declination = geocCoord.Declination -
172       mpar * rho * sin (gclat) * sin (g - geocCoord.Declination) / sin (g);
173   return topocCoord;
174 }
175
176
177 void fgMoonInit( void ) {
178     GLfloat moonColor[4] = {0.85, 0.75, 0.35, 1.0};
179     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
180
181     fgPrintf( FG_ASTRO, FG_INFO, "Initializing the Moon\n");
182     fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
183     moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], 
184                               cur_time_params);
185     fgPrintf( FG_ASTRO, FG_DEBUG, 
186               "Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension, 
187               moonPos.Declination);
188
189     xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
190     yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
191     zMoon = 60000.0 * sin(moonPos.Declination);
192
193     if (moon) {
194         xglDeleteLists (moon, 1);
195     }
196
197     moon = xglGenLists (1);
198     xglNewList (moon, GL_COMPILE);
199   
200     xglMaterialfv (GL_FRONT, GL_AMBIENT, black);
201     xglMaterialfv (GL_FRONT, GL_DIFFUSE, moonColor);
202     xglPushMatrix ();
203     xglTranslatef (xMoon, yMoon, zMoon);
204     xglScalef (1400, 1400, 1400);
205   
206     glutSolidSphere (1.0, 10, 10);
207     xglPopMatrix ();
208     xglEndList ();
209 }
210
211
212 /* Draw the moon */
213 void fgMoonRender( void ) {
214     xglCallList(moon);
215 }
216
217
218 /* $Log$
219 /* Revision 1.2  1998/04/24 00:45:00  curt
220 /* Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
221 /* Fixed a bug when generating sky colors.
222 /*
223  * Revision 1.1  1998/04/22 13:21:28  curt
224  * C++ - ifing the code a bit.
225  *
226  * Revision 1.9  1998/04/18 04:13:56  curt
227  * Moved fg_debug.c to it's own library.
228  *
229  * Revision 1.8  1998/04/03 21:52:49  curt
230  * Converting to Gnu autoconf system.
231  *
232  * Revision 1.7  1998/02/23 19:07:54  curt
233  * Incorporated Durk's Astro/ tweaks.  Includes unifying the sun position
234  * calculation code between sun display, and other FG sections that use this
235  * for things like lighting.
236  *
237  * Revision 1.6  1998/02/07 15:29:32  curt
238  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
239  * <chotchkiss@namg.us.anritsu.com>
240  *
241  * Revision 1.5  1998/02/02 20:53:21  curt
242  * To version 0.29
243  *
244  * Revision 1.4  1998/01/27 00:47:46  curt
245  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
246  * system and commandline/config file processing code.
247  *
248  * Revision 1.3  1998/01/19 19:26:57  curt
249  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
250  * This should simplify things tremendously.
251  *
252  * Revision 1.2  1998/01/19 18:40:16  curt
253  * Tons of little changes to clean up the code and to remove fatal errors
254  * when building with the c++ compiler.
255  *
256  * Revision 1.1  1998/01/07 03:16:16  curt
257  * Moved from .../Src/Scenery/ to .../Src/Astro/
258  *
259  * Revision 1.16  1998/01/06 01:20:24  curt
260  * Tweaks to help building with MSVC++
261  *
262  * Revision 1.15  1998/01/05 18:44:35  curt
263  * Add an option to advance/decrease time from keyboard.
264  *
265  * Revision 1.14  1997/12/30 20:47:50  curt
266  * Integrated new event manager with subsystem initializations.
267  *
268  * Revision 1.13  1997/12/30 16:41:00  curt
269  * Added log at end of file.
270  *
271  */