/**************************************************************************
* moon.c
+ * Written by Durk Talsma. Started October 1997, for the flight gear project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
#include "orbits.h"
#include "moon.h"
+#include "../general.h"
#include "../Main/views.h"
#include "../Time/fg_time.h"
-/* #include "../Aircraft/aircraft.h"*/
-#include "../general.h"
+struct CelestialCoord
+ moonPos;
+
+float xMoon, yMoon, zMoon;
+
+/*
+static GLfloat vdata[12][3] =
+{
+ {-X, 0.0, Z }, { X, 0.0, Z }, {-X, 0.0, -Z}, {X, 0.0, -Z },
+ { 0.0, Z, X }, { 0.0, Z, -X}, {0.0, -Z, -X}, {0.0, -Z, -X},
+ { Z, X, 0.0 }, { -Z, X, 0.0}, {Z, -X, 0.0 }, {-Z, -X, 0.0}
+};
+
+static GLuint tindices[20][3] =
+{
+ {0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
+ {8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
+ {7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
+ {6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
+};*/
+
+GLint moon;
+
+/* -------------------------------------------------------------
+ This section contains the code that generates a yellow
+ Icosahedron. It's under development... (of Course)
+______________________________________________________________*/
+/*
+void NormalizeVector(float v[3])
+{
+ GLfloat d = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
+ if (d == 0.0)
+ {
+ printf("zero length vector\n");
+ return;
+ }
+ v[0] /= d;
+ v[1] /= d;
+ v[2] /= d;
+}
+
+void drawTriangle(float *v1, float *v2, float *v3)
+{
+ glBegin(GL_POLYGON);
+ //glBegin(GL_POINTS);
+ glNormal3fv(v1);
+ glVertex3fv(v1);
+ glNormal3fv(v2);
+ glVertex3fv(v2);
+ glNormal3fv(v3);
+ glVertex3fv(v3);
+ glEnd();
+}
-static GLint moon;
+void subdivide(float *v1, float *v2, float *v3, long depth)
+{
+ GLfloat v12[3], v23[3], v31[3];
+ GLint i;
+
+ if (!depth)
+ {
+ drawTriangle(v1, v2, v3);
+ return;
+ }
+ for (i = 0; i < 3; i++)
+ {
+ v12[i] = (v1[i] + v2[i]);
+ v23[i] = (v2[i] + v3[i]);
+ v31[i] = (v3[i] + v1[i]);
+ }
+ NormalizeVector(v12);
+ NormalizeVector(v23);
+ NormalizeVector(v31);
+ subdivide(v1, v12, v31, depth - 1);
+ subdivide(v2, v23, v12, depth - 1);
+ subdivide(v3, v31, v23, depth - 1);
+ subdivide(v12, v23, v31,depth - 1);
+
+} */
+/*
+void display(void)
+{
+ int i;
+ glClear(GL_COLOR_BUFFER_BIT);
+ glPushMatrix();
+ glRotatef(spin, 0.0, 0.0, 0.0);
+ glColor3f(1.0, 1.0, 0.0);
+// glBegin(GL_LINE_LOOP);
+ for (i = 0; i < 20; i++)
+ {
+
+ //glVertex3fv(&vdata[tindices[i][0]][0]);
+ //glVertex3fv(&vdata[tindices[i][1]][0]);
+ //glVertex3fv(&vdata[tindices[i][2]][0]);
+
+ subdivide(&vdata[tindices[i][0]][0],
+ &vdata[tindices[i][1]][0],
+ &vdata[tindices[i][2]][0], 3);
+
+
+ }
+// glEnd();
+ // glFlush();
+ glPopMatrix();
+ glutSwapBuffers();
+} */
+
+/* --------------------------------------------------------------
+
+ This section contains the code that calculates the actual
+ position of the moon in the night sky.
+
+----------------------------------------------------------------*/
struct CelestialCoord fgCalculateMoon(struct OrbElements params,
struct OrbElements sunParams,
result;
double
- eccAnom, ecl, lonecl, latecl, actTime,
+ eccAnom, ecl, lonecl, latecl, actTime,
xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
Ls, Lm, D, F;
eccAnom = fgCalcEccAnom(params.M, params.e);
/* calculate the moon's distance (d) and true anomaly (v) */
- xv = params.a * ( cos(eccAnom) - params.e);
+ xv = params.a * ( cos(eccAnom) - params.e);
yv = params.a * ( sqrt(1.0 - params.e*params.e) * sin(eccAnom));
v =atan2(yv, xv);
r = sqrt(xv*xv + yv*yv);
F = Lm - params.N;
lonecl += fgDegToRad(
- - 1.274 * sin (params.M - 2*D) // the Evection
+ - 1.274 * sin (params.M - 2*D) // the Evection
+ 0.658 * sin (2 * D) // the Variation
- 0.186 * sin (sunParams.M) // the yearly variation
- 0.059 * sin (2*params.M - 2*D)
); /* Yep */
r += (
- - 0.58 * cos(params.M - 2*D)
+ - 0.58 * cos(params.M - 2*D)
- 0.46 * cos(2*D)
- );
+ ); /* Ok! */
+
xg = r * cos(lonecl) * cos(latecl);
yg = r * sin(lonecl) * cos(latecl);
zg = r * sin(latecl);
ye = yg * cos(ecl) - zg * sin(ecl);
ze = yg * sin(ecl) + zg * cos(ecl);
- result.RightAscension = atan2(ye, xe);
+ result.RightAscension = atan2(ye, xe);
result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
-
return result;
}
void fgMoonInit()
{
- struct CelestialCoord
- moonPos;
+// int i;
+// moon = glGenLists(1);
+// glNewList(moon, GL_COMPILE );
- moon = glGenLists(1);
- glNewList(moon, GL_COMPILE );
- glBegin( GL_POINTS );
+ fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], cur_time_params);
+ #ifdef DEBUG
printf("Moon found at %f (ra), %f (dec)\n", moonPos.RightAscension, moonPos.Declination);
- /* give the moon a temporary color, for testing purposes */
- glColor3f( 0.0, 1.0, 0.0);
- glVertex3f( 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination),
- 190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination),
- 190000.0 * sin(moonPos.Declination) );
- glEnd();
- glEndList();
-}
+ #endif
+ glColor3f(1.0, 1.0, 0.0);
-void fgMoonRender()
-{
- double angle;
- static double warp = 0;
- struct VIEW *v;
- struct fgTIME *t;
-
- t = &cur_time_params;
- v = ¤t_view;
-
-
- glDisable( GL_FOG );
- glDisable( GL_LIGHTING );
- glPushMatrix();
- glTranslatef( v->view_pos.x, v->view_pos.y, v->view_pos.z );
- angle = t->gst * 15.0; /* 15 degrees per hour rotation */
- /* warp += 1.0; */
- /* warp = 15.0; */
- warp = 0.0;
- glRotatef( (angle+warp), 0.0, 0.0, -1.0 );
- printf("Rotating moon by %.2f degrees + %.2f degrees\n",angle,warp);
-
- glCallList(moon);
-
- glPopMatrix();
- glEnable( GL_LIGHTING );
- glEnable( GL_FOG );
+ /* xMoon = 90000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
+ yMoon = 90000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
+ zMoon = 90000.0 * sin(moonPos.Declination); */
+
+ xMoon = 60000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
+ yMoon = 60000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
+ zMoon = 60000.0 * sin(moonPos.Declination);
+
+// glPushMatrix();
+// glBegin(GL_TRIANGLES);
+ /*
+ for (i = 0; i < 20; i++)
+ subdivide(&vdata[tindices[i][0]][0],
+ &vdata[tindices[i][1]][0],
+ &vdata[tindices[i][2]][0], 3);*/
+// glutSolidSphere(1.0, 25, 25);
+
+// glEnd();
+// glPopMatrix();
+// glEndList();
}
-/* $Log$
-/* Revision 1.2 1997/10/28 21:00:21 curt
-/* Changing to new terrain format.
-/*
- * Revision 1.1 1997/10/25 03:16:08 curt
- * Initial revision of code contributed by Durk Talsma.
- *
- */
+/* Draw the moon */
+void fgMoonRender() {
+ GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
+
+ /* set lighting parameters */
+ glLightfv(GL_LIGHT0, GL_AMBIENT, color );
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, color );
+ glMaterialfv(GL_FRONT, GL_AMBIENT, fgClearColor);
+ glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
+
+ glPushMatrix();
+ glTranslatef(xMoon, yMoon, zMoon);
+ glScalef(1400, 1400, 1400);
+ /* glutSolidSphere(1.0, 25, 25); */
+ glutSolidSphere(1.0, 15, 15);
+ glPopMatrix();
+}
+
* (Log is kept at end of this file)
**************************************************************************/
-
+#include <GL/glut.h>
#include "../Time/fg_time.h"
+#include "../Main/views.h"
#include "orbits.h"
+#include "sun.h"
+
+GLint sun;
+
+static struct CelestialCoord
+ sunPos;
+
+float xSun, ySun, zSun;
struct SunPos fgCalcSunPos(struct OrbElements params)
{
EccAnom = fgCalcEccAnom(params.M, params.e);
/* calculate the Suns distance (r) and its true anomaly (v) */
- xv = cos(EccAnom) - params.e;
+ xv = cos(EccAnom) - params.e;
yv = sqrt(1.0 - params.e*params.e) * sin(EccAnom);
v = atan2(yv, xv);
r = sqrt(xv*xv + yv*yv);
- /* calculate the the Suns true longitude (lonsun) */
+ /* calculate the the Sun's true longitude (lonsun) */
lonSun = v + params.w;
/* convert true longitude and distance to ecliptic rectangular geocentric
- coordinates (xs, ys) */
+ coordinates (xs, ys) */
solarPosition.xs = r * cos(lonSun);
solarPosition.ys = r * sin(lonSun);
return solarPosition;
}
+struct CelestialCoord fgCalculateSun(struct OrbElements params, struct fgTIME t)
+{
+ struct CelestialCoord
+ result;
+ struct SunPos
+ SolarPosition;
+ double
+ xe, ye, ze, ecl, actTime;
+
+ /* calculate the angle between ecliptic and equatorial coordinate system */
+ actTime = fgCalcActTime(t);
+ ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime); // Angle now in Rads
+
+ /* calculate the sun's ecliptic position */
+ SolarPosition = fgCalcSunPos(params);
+
+ /* convert ecliptic coordinates to equatorial rectangular geocentric coordinates */
+ xe = SolarPosition.xs;
+ ye = SolarPosition.ys * cos(ecl);
+ ze = SolarPosition.ys * sin(ecl);
+
+ /* and finally... Calulate Right Ascention and Declination */
+ result.RightAscension = atan2( ye, xe);
+ result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
+ return result;
+}
+
+
+/* Initialize the Sun */
+void fgSunInit()
+{
+// int i;
+// sun = glGenLists(1);
+// glNewList(sun, GL_COMPILE );
+// glBegin( GL_POINTS );
+ fgSolarSystemUpdate(&(pltOrbElements[0]), cur_time_params);
+ sunPos = fgCalculateSun(pltOrbElements[0], cur_time_params);
+ #ifdef DEBUG
+ printf("Sun found at %f (ra), %f (dec)\n", sunPos.RightAscension, sunPos.Declination);
+ #endif
+ /* give the moon a temporary color, for testing purposes */
+// glColor3f( 0.0, 1.0, 0.0);
+// glVertex3f( 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination),
+ // 190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination),
+// 190000.0 * sin(moonPos.Declination) );
+ //glVertex3f(0.0, 0.0, 0.0);
+// glEnd();
+// glColor3f(1.0, 1.0, 1.0);
+ //xMoon = 190000.0 * cos(moonPos.RightAscension) * cos(moonPos.Declination);
+ //yMoon = 190000.0 * sin(moonPos.RightAscension) * cos(moonPos.Declination);
+ //zMoon = 190000.0 * sin(moonPos.Declination);
+ xSun = 60000.0 * cos(sunPos.RightAscension) * cos(sunPos.Declination);
+ ySun = 60000.0 * sin(sunPos.RightAscension) * cos(sunPos.Declination);
+ zSun = 60000.0 * sin(sunPos.Declination);
+
+// glPushMatrix();
+// glTranslatef(x, y, z);
+// glScalef(16622.8, 16622.8, 16622.8);
+// glBegin(GL_TRIANGLES);
+// for (i = 0; i < 20; i++)
+// subdivide(&vdata[tindices[i][0]][0],
+// &vdata[tindices[i][1]][0],
+// &vdata[tindices[i][2]][0], 3);
+// glutSolidSphere(1.0, 25, 25);
+
+// glEnd();
+ //glPopMatrix();
+// glEndList();
+}
+
+
+/* Draw the Sun */
+void fgSunRender() {
+ struct VIEW *v;
+ struct fgTIME *t;
+ GLfloat color[4] = { 0.85, 0.65, 0.05, 1.0 };
+ /* double x_2, x_4, x_8, x_10; */
+ /* GLfloat ambient; */
+ /* GLfloat amb[3], diff[3]; */
+
+
+ t = &cur_time_params;
+ v = ¤t_view;
+
+ /* x_2 = t->sun_angle * t->sun_angle;
+ x_4 = x_2 * x_2;
+ x_8 = x_4 * x_4;
+ x_10 = x_8 * x_2; */
+
+ /* ambient = (0.4 * pow(1.1, -x_10 / 30.0));
+ if ( ambient < 0.3 ) ambient = 0.3;
+ if ( ambient > 1.0 ) ambient = 1.0;
+ amb[0] = 0.50 + ((ambient * 6.66) - 1.6);
+ amb[1] = 0.00 + ((ambient * 6.66) - 1.6);
+ amb[2] = 0.00 + ((ambient * 6.66) - 1.6);
+ amb[3] = 0.00;
+#ifdef DEBUG
+ printf("Color of the sun: %f, %f, %f\n"
+ "Ambient value : %f\n"
+ "Sun Angle : %f\n" , amb[0], amb[1], amb[2], ambient, t->sun_angle);
+#endif
+ diff[0] = 0.0;
+ diff[1] = 0.0;
+ diff[2] = 0.0;
+ diff[3] = 0.0; */
+
+ /* set lighting parameters */
+ /* glLightfv(GL_LIGHT0, GL_AMBIENT, color );
+ glLightfv(GL_LIGHT0, GL_DIFFUSE, color );
+ glMaterialfv(GL_FRONT, GL_AMBIENT, amb);
+ glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); */
+
+ glDisable( GL_LIGHTING );
+ glPushMatrix();
+ glTranslatef(xSun, ySun, zSun);
+ glScalef(1400, 1400, 1400);
+ glColor3fv( color );
+ /* glutSolidSphere(1.0, 25, 25); */
+ glutSolidSphere(1.0, 10, 10);
+ //glCallList(sun);
+ glPopMatrix();
+ glEnable( GL_LIGHTING );
+}
+
+
/* $Log$
-/* Revision 1.1 1997/10/25 03:16:11 curt
-/* Initial revision of code contributed by Durk Talsma.
+/* Revision 1.2 1997/11/25 19:25:39 curt
+/* Changes to integrate Durk's moon/sun code updates + clean up.
/*
+ * Revision 1.1 1997/10/25 03:16:11 curt
+ * Initial revision of code contributed by Durk Talsma.
+ *
*/
+
+
+
+
+