]> git.mxchange.org Git - simgear.git/blobdiff - Scenery/moon.c
Tweaks to help building with MSVC++
[simgear.git] / Scenery / moon.c
index f9ce58bb1df49af0edae78be7fe3ed43f02b07ae..dd39f072229f77cd1bbfaaa6fb953ece6dd37a6d 100644 (file)
 
 #include <math.h>
 #include <GL/glut.h>
+#include "../XGL/xgl.h"
 
 #include "orbits.h"
 #include "moon.h"
 
-#include "../general.h"
+#include "../Aircraft/aircraft.h"
+#include "../Include/constants.h"
+#include "../Include/general.h"
 #include "../Main/views.h"
 #include "../Time/fg_time.h"
 
 struct CelestialCoord moonPos;
 
-float xMoon, yMoon, zMoon;
-GLint moon;
+static float xMoon, yMoon, zMoon;
+static GLint moon;
 
 /*
 static GLfloat vdata[12][3] =
@@ -72,15 +75,15 @@ void NormalizeVector(float v[3])
 
 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();
+   xglBegin(GL_POLYGON);
+   //xglBegin(GL_POINTS);
+      xglNormal3fv(v1);
+      xglVertex3fv(v1);
+      xglNormal3fv(v2);
+      xglVertex3fv(v2);
+      xglNormal3fv(v3);
+      xglVertex3fv(v3);
+   xglEnd();
 }
 
 void subdivide(float *v1, float *v2, float *v3, long depth)
@@ -112,17 +115,17 @@ void subdivide(float *v1, float *v2, float *v3, long depth)
 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);
+   xglClear(GL_COLOR_BUFFER_BIT);
+   xglPushMatrix();
+   xglRotatef(spin, 0.0, 0.0, 0.0);
+   xglColor3f(1.0, 1.0, 0.0);
+//   xglBegin(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]);
+       //xglVertex3fv(&vdata[tindices[i][0]][0]);
+       //xglVertex3fv(&vdata[tindices[i][1]][0]);
+       //xglVertex3fv(&vdata[tindices[i][2]][0]);
 
        subdivide(&vdata[tindices[i][0]][0],
                  &vdata[tindices[i][1]][0],
@@ -130,9 +133,9 @@ void display(void)
 
 
    }
-//   glEnd();
-  // glFlush();
-  glPopMatrix();
+//   xglEnd();
+  // xglFlush();
+  xglPopMatrix();
   glutSwapBuffers();
 } */
 
@@ -147,96 +150,122 @@ struct CelestialCoord fgCalculateMoon(struct OrbElements params,
                                       struct OrbElements sunParams,
                                       struct fgTIME t)
 {
-       struct CelestialCoord
-          result;
-
-    double
-         eccAnom, ecl, lonecl, latecl, actTime,
-        xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
-        Ls, Lm, D, F;
-
-    /* calculate the angle between ecliptic and equatorial coordinate system */
-    actTime = fgCalcActTime(t);
-    ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime);  // in radians of course
-
-    /* calculate the eccentric anomaly */
-    eccAnom = fgCalcEccAnom(params.M, params.e);
-
-    /* calculate the moon's distance (d) and  true anomaly (v) */
-        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);
-
-    /* estimate the geocentric rectangular coordinates here */
-    xh = r * (cos(params.N) * cos(v + params.w) - sin(params.N) * sin(v + params.w) * cos(params.i));
-    yh = r * (sin(params.N) * cos(v + params.w) + cos(params.N) * sin(v + params.w) * cos(params.i));
-    zh = r * (sin(v + params.w) * sin(params.i));
-
-    /* calculate the ecliptic latitude and longitude here */
-    lonecl = atan2( yh, xh);
-    latecl = atan2( zh, sqrt( xh*xh + yh*yh));
-
-    /* calculate a number of perturbations */
-    Ls = sunParams.M + sunParams.w;
-    Lm =    params.M +    params.w + params.N;
-    D = Lm - Ls;
-    F = Lm - params.N;
-
-    lonecl += fgDegToRad(
-                           - 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)
-                - 0.057 * sin (params.M - 2*D + sunParams.M)
-                + 0.053 * sin (params.M + 2*D)
-                + 0.046 * sin (2*D - sunParams.M)
-                + 0.041 * sin (params.M - sunParams.M)
-                - 0.035 * sin (D)                             // the Parallactic Equation
-                - 0.031 * sin (params.M + sunParams.M)
-                - 0.015 * sin (2*F - 2*D)
-                + 0.011 * sin (params.M - 4*D)
-              ); /* Pheeuuwwww */
-    latecl += fgDegToRad(
-                - 0.173 * sin (F - 2*D)
-                - 0.055 * sin (params.M - F - 2*D)
-                - 0.046 * sin (params.M + F - 2*D)
-                + 0.033 * sin (F + 2*D)
-                + 0.017 * sin (2 * params.M + F)
-              );  /* Yep */
-
-    r += (
-                    - 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);
-
-    xe  = xg;
-    ye = yg * cos(ecl) - zg * sin(ecl);
-    ze = yg * sin(ecl) + zg * cos(ecl);
-
-        result.RightAscension = atan2(ye, xe);
-    result.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
-    return result;
+  struct CelestialCoord
+    geocCoord, topocCoord; 
+  
+  
+  double
+    eccAnom, ecl, lonecl, latecl, actTime,
+    xv, yv, v, r, xh, yh, zh, xg, yg, zg, xe, ye, ze,
+    Ls, Lm, D, F, mpar, gclat, rho, HA, g;
+  
+  struct fgAIRCRAFT *a;
+  struct fgFLIGHT *f;
+
+  a = &current_aircraft;
+  f = &a->flight;
+  
+/* calculate the angle between ecliptic and equatorial coordinate system */
+  actTime = fgCalcActTime(t);
+  ecl = fgDegToRad(23.4393 - 3.563E-7 * actTime);  // in radians of course
+                                                       
+  /* calculate the eccentric anomaly */
+  eccAnom = fgCalcEccAnom(params.M, params.e);
+
+  /* calculate the moon's distance (d) and  true anomaly (v) */
+  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);
+  
+  /* estimate the geocentric rectangular coordinates here */
+  xh = r * (cos(params.N) * cos(v + params.w) - sin(params.N) * sin(v + params.w) * cos(params.i));
+  yh = r * (sin(params.N) * cos(v + params.w) + cos(params.N) * sin(v + params.w) * cos(params.i));
+  zh = r * (sin(v + params.w) * sin(params.i));
+  
+  /* calculate the ecliptic latitude and longitude here */
+  lonecl = atan2( yh, xh);
+  latecl = atan2( zh, sqrt( xh*xh + yh*yh));
+
+  /* calculate a number of perturbations */
+  Ls = sunParams.M + sunParams.w;
+  Lm =    params.M +    params.w + params.N;
+  D = Lm - Ls;
+  F = Lm - params.N;
+  
+  lonecl += fgDegToRad(
+                      - 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)
+                      - 0.057 * sin (params.M - 2*D + sunParams.M)
+                      + 0.053 * sin (params.M + 2*D)
+                      + 0.046 * sin (2*D - sunParams.M)
+                      + 0.041 * sin (params.M - sunParams.M)
+                      - 0.035 * sin (D)                             // the Parallactic Equation
+                      - 0.031 * sin (params.M + sunParams.M)
+                      - 0.015 * sin (2*F - 2*D)
+                      + 0.011 * sin (params.M - 4*D)
+                      ); /* Pheeuuwwww */
+  latecl += fgDegToRad(
+                      - 0.173 * sin (F - 2*D)
+                      - 0.055 * sin (params.M - F - 2*D)
+                      - 0.046 * sin (params.M + F - 2*D)
+                      + 0.033 * sin (F + 2*D)
+                      + 0.017 * sin (2 * params.M + F)
+                      );  /* Yep */
+
+  r += (
+       - 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);
+
+  xe  = xg;
+  ye = yg * cos(ecl) - zg * sin(ecl);
+  ze = yg * sin(ecl) + zg * cos(ecl);
+  
+
+  
+
+  geocCoord.RightAscension = atan2(ye, xe);
+  geocCoord.Declination = atan2(ze, sqrt(xe*xe + ye*ye));
+  
+  /* New since 25 december 1997 */
+  /* Calculate the moon's topocentric position instead of it's geocentric! */
+
+  mpar = asin( 1 / r); /* calculate the moon's parrallax, i.e. the apparent size of the
+                         (equatorial) radius of the Earth, as seen from the moon */
+  gclat = FG_Latitude - 0.083358 * sin (2 * fgDegToRad( FG_Latitude));
+  rho = 0.99883 + 0.00167 * cos(2 * fgDegToRad(FG_Latitude));
+
+  if (geocCoord.RightAscension < 0)
+    geocCoord.RightAscension += (2*FG_PI);
+
+  HA = t.lst - (3.8197186 * geocCoord.RightAscension);
+
+  g = atan (tan(gclat) / cos( (HA / 3.8197186))); 
+
+     
+
+  topocCoord.RightAscension = geocCoord.RightAscension - mpar * rho * cos(gclat) * sin(HA) / cos(geocCoord.Declination);
+  topocCoord.Declination = geocCoord.Declination - mpar * rho * sin(gclat) * sin(g - geocCoord.Declination) / sin(g);
+  return topocCoord;
 }
 
 
 void fgMoonInit() {
     struct fgLIGHT *l;
-    static GLfloat moon_color[4] = { 1.0, 1.0, 1.0, 1.0 };
-//   int i;
+    static int dl_exists = 0;
 
-    l = &cur_light_params;
-
-    moon = glGenLists(1);
-    glNewList(moon, GL_COMPILE );
+    printf("Initializing the Moon\n");
 
-    /* glMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear);
-    glMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color); */
+    l = &cur_light_params;
 
+    /* position the moon */
     fgSolarSystemUpdate(&(pltOrbElements[1]), cur_time_params);
     moonPos = fgCalculateMoon(pltOrbElements[1], pltOrbElements[0], 
                              cur_time_params);
@@ -245,39 +274,62 @@ void fgMoonInit() {
           moonPos.Declination);
 #endif
 
-    /* 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);
 
-    glutSolidSphere(1.0, 10, 10);
+    if ( !dl_exists ) {
+       dl_exists = 1;
+
+       /* printf("First time through, creating moon display list\n"); */
+
+       moon = xglGenLists(1);
+       xglNewList(moon, GL_COMPILE );
+
+       /* xglMaterialfv(GL_FRONT, GL_AMBIENT, l->scene_clear);
+          xglMaterialfv(GL_FRONT, GL_DIFFUSE, moon_color); */
+
 
-    glEndList();
+       glutSolidSphere(1.0, 10, 10);
+
+       xglEndList();
+    }
 }
 
 
 /* Draw the moon */
 void fgMoonRender() {
     struct fgLIGHT *l;
-    GLfloat moon_color[4] = { 1.0, 1.0, 1.0, 1.0 };
+    GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
+
+    /* printf("Rendering moon\n"); */
 
     l = &cur_light_params;
 
-    /* set lighting parameters */
-    glLightfv(GL_LIGHT0, GL_AMBIENT, l->scene_clear );
-    glLightfv(GL_LIGHT0, GL_DIFFUSE, moon_color );
+    xglMaterialfv(GL_FRONT, GL_AMBIENT, l->sky_color );
+    xglMaterialfv(GL_FRONT, GL_DIFFUSE, white);
 
-    glPushMatrix();
-    glTranslatef(xMoon, yMoon, zMoon);
-    glScalef(1400, 1400, 1400);
+    xglPushMatrix();
+    xglTranslatef(xMoon, yMoon, zMoon);
+    xglScalef(1400, 1400, 1400);
 
-    /* glColor3fv(color); */
-    /* glutSolidSphere(1.0, 25, 25); */
-    glCallList(moon);
+    xglCallList(moon);
 
-    glPopMatrix();
+    xglPopMatrix();
 }
 
+
+/* $Log$
+/* Revision 1.16  1998/01/06 01:20:24  curt
+/* Tweaks to help building with MSVC++
+/*
+ * Revision 1.15  1998/01/05 18:44:35  curt
+ * Add an option to advance/decrease time from keyboard.
+ *
+ * Revision 1.14  1997/12/30 20:47:50  curt
+ * Integrated new event manager with subsystem initializations.
+ *
+ * Revision 1.13  1997/12/30 16:41:00  curt
+ * Added log at end of file.
+ *
+ */