]> git.mxchange.org Git - flightgear.git/blob - Scenery/moon.c
e72404f9ec7d69b59a23f36791cfeb96418d7cf2
[flightgear.git] / Scenery / moon.c
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 #include <math.h>
25 #include <GL/glut.h>
26
27 #include "orbits.h"
28 #include "moon.h"
29
30 #include "../general.h"
31 #include "../Main/views.h"
32 #include "../Time/fg_time.h"
33
34 struct CelestialCoord moonPos;
35
36 float xMoon, yMoon, zMoon;
37 GLint moon;
38
39 /*
40 static GLfloat vdata[12][3] =
41 {
42    {-X, 0.0, Z }, { X, 0.0, Z }, {-X, 0.0, -Z}, {X, 0.0, -Z },
43    { 0.0, Z, X }, { 0.0, Z, -X}, {0.0, -Z, -X}, {0.0, -Z, -X},
44    { Z, X, 0.0 }, { -Z, X, 0.0}, {Z, -X, 0.0 }, {-Z, -X, 0.0}
45 };
46
47 static GLuint tindices[20][3] =
48 {
49    {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
50    {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
51    {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
52    {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
53 };*/
54
55 /* -------------------------------------------------------------
56       This section contains the code that generates a yellow
57       Icosahedron. It's under development... (of Course)
58 ______________________________________________________________*/
59 /*
60 void NormalizeVector(float v[3])
61 {
62    GLfloat d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
63    if (d == 0.0)
64    {
65       printf("zero length vector\n");
66       return;
67    }
68    v[0] /= d;
69    v[1] /= d;
70    v[2] /= d;
71 }
72
73 void drawTriangle(float *v1, float *v2, float *v3)
74 {
75    glBegin(GL_POLYGON);
76    //glBegin(GL_POINTS);
77       glNormal3fv(v1);
78       glVertex3fv(v1);
79       glNormal3fv(v2);
80       glVertex3fv(v2);
81       glNormal3fv(v3);
82       glVertex3fv(v3);
83    glEnd();
84 }
85
86 void subdivide(float *v1, float *v2, float *v3, long depth)
87 {
88    GLfloat v12[3], v23[3], v31[3];
89    GLint i;
90
91    if (!depth)
92    {
93      drawTriangle(v1, v2, v3);
94      return;
95    }
96    for (i = 0; i < 3; i++)
97    {
98        v12[i] = (v1[i] + v2[i]);
99        v23[i] = (v2[i] + v3[i]);
100        v31[i] = (v3[i] + v1[i]);
101    }
102    NormalizeVector(v12);
103    NormalizeVector(v23);
104    NormalizeVector(v31);
105    subdivide(v1, v12, v31, depth - 1);
106    subdivide(v2, v23, v12, depth - 1);
107    subdivide(v3, v31, v23, depth - 1);
108    subdivide(v12, v23, v31,depth - 1);
109
110 } */
111 /*
112 void display(void)
113 {
114    int i;
115    glClear(GL_COLOR_BUFFER_BIT);
116    glPushMatrix();
117    glRotatef(spin, 0.0, 0.0, 0.0);
118    glColor3f(1.0, 1.0, 0.0);
119 //   glBegin(GL_LINE_LOOP);
120    for (i = 0; i < 20; i++)
121    {
122
123        //glVertex3fv(&vdata[tindices[i][0]][0]);
124        //glVertex3fv(&vdata[tindices[i][1]][0]);
125        //glVertex3fv(&vdata[tindices[i][2]][0]);
126
127        subdivide(&vdata[tindices[i][0]][0],
128                  &vdata[tindices[i][1]][0],
129                  &vdata[tindices[i][2]][0], 3);
130
131
132    }
133 //   glEnd();
134   // glFlush();
135   glPopMatrix();
136   glutSwapBuffers();
137 } */
138
139 /* --------------------------------------------------------------
140
141       This section contains the code that calculates the actual
142       position of the moon in the night sky.
143
144 ----------------------------------------------------------------*/
145
146 struct CelestialCoord fgCalculateMoon(struct OrbElements params,
147                                       struct OrbElements sunParams,
148                                       struct fgTIME t)
149 {
150     struct CelestialCoord
151         result;
152     
153     double
154         eccAnom, ecl, lonecl, latecl, actTime,
155         xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
156         Ls, Lm, D, F;
157
158     /* calculate the angle between ecliptic and equatorial coordinate system */
159     actTime = fgCalcActTime(t);
160     ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime);  // in radians of course
161
162     /* calculate the eccentric anomaly */
163     eccAnom = fgCalcEccAnom(params.M, params.e);
164
165     /* calculate the moon's distance (d) and  true anomaly (v) */
166          xv = params.a * ( cos(eccAnom) - params.e);
167     yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom));
168     v =atan2(yv, xv);
169     r = sqrt(xv*xv + yv*yv);
170
171     /* estimate the geocentric rectangular coordinates here */
172     xh = r * (cos(params.N) * cos(v + params.w) - sin(params.N) * sin(v + params.w) * cos(params.i));
173     yh = r * (sin(params.N) * cos(v + params.w) + cos(params.N) * sin(v + params.w) * cos(params.i));
174     zh = r * (sin(v + params.w) * sin(params.i));
175
176     /* calculate the ecliptic latitude and longitude here */
177     lonecl = atan2( yh, xh);
178     latecl = atan2( zh, sqrt( xh*xh + yh*yh));
179
180     /* calculate a number of perturbations */
181     Ls = sunParams.M + sunParams.w;
182     Lm =    params.M +    params.w + params.N;
183     D = Lm - Ls;
184     F = Lm - params.N;
185
186     lonecl += fgDegToRad(
187                             - 1.274 * sin (params.M - 2*D)                      // the Evection
188                 + 0.658 * sin (2 * D)                                                   // the Variation
189                 - 0.186 * sin (sunParams.M)                                     // the yearly variation
190                 - 0.059 * sin (2*params.M - 2*D)
191                 - 0.057 * sin (params.M - 2*D + sunParams.M)
192                 + 0.053 * sin (params.M + 2*D)
193                 + 0.046 * sin (2*D - sunParams.M)
194                 + 0.041 * sin (params.M - sunParams.M)
195                 - 0.035 * sin (D)                             // the Parallactic Equation
196                 - 0.031 * sin (params.M + sunParams.M)
197                 - 0.015 * sin (2*F - 2*D)
198                 + 0.011 * sin (params.M - 4*D)
199               ); /* Pheeuuwwww */
200     latecl += fgDegToRad(
201                 - 0.173 * sin (F - 2*D)
202                 - 0.055 * sin (params.M - F - 2*D)
203                 - 0.046 * sin (params.M + F - 2*D)
204                 + 0.033 * sin (F + 2*D)
205                 + 0.017 * sin (2 * params.M + F)
206               );  /* Yep */
207
208     r += (
209                      - 0.58 * cos(params.M - 2*D)
210               - 0.46 * cos(2*D)
211           ); /* Ok! */
212
213     xg = r * cos(lonecl) * cos(latecl);
214     yg = r * sin(lonecl) * cos(latecl);
215     zg = r *               sin(latecl);
216
217     xe  = xg;
218     ye = yg * cos(ecl) - zg * sin(ecl);
219     ze = yg * sin(ecl) + zg * cos(ecl);
220
221          result.RightAscension = atan2(ye, xe);
222     result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
223     return result;
224 }
225
226
227 void fgMoonInit() {
228     struct fgLIGHT *l;
229 //   int i;
230
231     l = &cur_light_params;
232
233     moon = glGenLists(1);
234     glNewList(moon, GL_COMPILE );
235
236     /* glMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear);
237     glMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color); */
238
239     fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
240     moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], 
241                               cur_time_params);
242 #ifdef DEBUG
243     printf("Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension, 
244            moonPos.Declination);
245 #endif
246
247     /* xMoon = 90000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
248        yMoon = 90000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
249        zMoon = 90000.0 * sin(moonPos.Declination); */
250
251     xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
252     yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
253     zMoon = 60000.0 * sin(moonPos.Declination);
254
255     glutSolidSphere(1.0, 15, 15);
256
257     glEndList();
258 }
259
260
261 /* Draw the moon */
262 void fgMoonRender() {
263     struct fgLIGHT *l;
264     GLfloat black[4] = { 0.0, 0.0, 0.0, 1.0 };
265     GLfloat moon_color[4] = { 1.0, 1.0, 1.0, 1.0 };
266
267     l = &cur_light_params;
268
269     /* set lighting parameters */
270     glLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_clear );
271     glLightfv(GL_LIGHT0, GL_DIFFUSE, moon_color );
272
273     glMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear );
274     glMaterialfv(GL_FRONT, GL_AMBIENT, moon_color );
275     glMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color);
276
277     glPushMatrix();
278     glTranslatef(xMoon, yMoon, zMoon);
279     glScalef(1400, 1400, 1400);
280
281     glColor3fv(moon_color);
282     /* glutSolidSphere(1.0, 25, 25); */
283     glCallList(moon);
284
285     glPopMatrix();
286 }
287