]> git.mxchange.org Git - flightgear.git/blobdiff - src/Time/sunpos.cxx
Patch from Cameron Moore:
[flightgear.git] / src / Time / sunpos.cxx
index eb06ffd3023774ff629216176ee344ac25be4b0b..ebc45f9d5f3dc50219e928712b0c101145d68660 100644 (file)
 #  include <config.h>
 #endif
 
-#include <Include/compiler.h>
+#include <simgear/compiler.h>
 
-#ifdef FG_HAVE_STD_INCLUDES
+#ifdef SG_HAVE_STD_INCLUDES
 #  include <cmath>
 #  include <cstdio>
 #  include <ctime>
+#  ifdef macintosh
+     SG_USING_STD(time_t);
+#  endif
 #else
 #  include <math.h>
 #  include <stdio.h>
 #  include <time.h>
 #endif
 
-#include <Debug/logstream.hxx>
-#include <Astro/solarsystem.hxx>
-#include <Include/fg_constants.h>
-#include <Main/views.hxx>
-#include <Math/fg_geodesy.hxx>
-#include <Math/mat3.h>
-#include <Math/point3d.hxx>
-#include <Math/polar3d.hxx>
-#include <Math/vector.hxx>
+#include <simgear/constants.h>
+#include <simgear/debug/logstream.hxx>
+#include <simgear/ephemeris/ephemeris.hxx>
+#include <simgear/math/point3d.hxx>
+#include <simgear/math/polar3d.hxx>
+#include <simgear/math/sg_geodesy.hxx>
+#include <simgear/math/vector.hxx>
+#include <simgear/timing/sg_time.hxx>
+
+#include <Main/globals.hxx>
+#include <Main/viewer.hxx>
 #include <Scenery/scenery.hxx>
+#include <Time/light.hxx>
 
-#include "fg_time.hxx"
 #include "sunpos.hxx"
 
-extern SolarSystem *solarSystem;
-
-#undef E
-#define MeanObliquity (23.440592*(FG_2PI/360))
+// #undef E // should no longer be needed
+#define MeanObliquity (23.440592*(SGD_2PI/360))
 
 static void   ecliptic_to_equatorial(double, double, double *, double *);
 static double julian_date(int, int, int);
@@ -107,7 +110,7 @@ static double julian_date(int y, int m, int d) {
 
     /* lazy test to ensure gregorian calendar */
     if (y < 1583) {
-       FG_LOG( FG_EVENT, FG_ALERT, 
+       SG_LOG( SG_EVENT, SG_ALERT, 
                "WHOOPS! Julian dates only valid for 1582 oct 15 or later" );
     }
 
@@ -188,15 +191,15 @@ void fgSunPosition(time_t ssue, double *lon, double *lat) {
      * every ten minutes. (Comment added by Durk Talsma).
      ************************************************************************/
 
-    ecliptic_to_equatorial( SolarSystem::theSolarSystem->getSun()->getLon(),
+    ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
                            0.0, &alpha, &delta );
-    tmp = alpha - (FG_2PI/24)*GST(ssue);
-    if (tmp < -FG_PI) {
-       do tmp += FG_2PI;
-       while (tmp < -FG_PI);
-    } else if (tmp > FG_PI) {
-       do tmp -= FG_2PI;
-       while (tmp < -FG_PI);
+    tmp = alpha - (SGD_2PI/24)*GST(ssue);
+    if (tmp < -SGD_PI) {
+       do tmp += SGD_2PI;
+       while (tmp < -SGD_PI);
+    } else if (tmp > SGD_PI) {
+       do tmp -= SGD_2PI;
+       while (tmp < -SGD_PI);
     }
 
     *lon = tmp;
@@ -220,18 +223,18 @@ static void fgSunPositionGST(double gst, double *lon, double *lat) {
     /* lambda = sun_ecliptic_longitude(ssue); */
     /* ecliptic_to_equatorial(lambda, 0.0, &alpha, &delta); */
     //ecliptic_to_equatorial (solarPosition.lonSun, 0.0, &alpha, &delta);
-    ecliptic_to_equatorial( SolarSystem::theSolarSystem->getSun()->getLon(),
-                           SolarSystem::theSolarSystem->getSun()->getLat(),
-                           &alpha,  &delta );
-
-//    tmp = alpha - (FG_2PI/24)*GST(ssue);
-    tmp = alpha - (FG_2PI/24)*gst;     
-    if (tmp < -FG_PI) {
-       do tmp += FG_2PI;
-       while (tmp < -FG_PI);
-    } else if (tmp > FG_PI) {
-       do tmp -= FG_2PI;
-       while (tmp < -FG_PI);
+    ecliptic_to_equatorial( globals->get_ephem()->get_sun()->getLon(),
+                           globals->get_ephem()->get_sun()->getLat(),
+                           &alpha, &delta );
+
+//    tmp = alpha - (SGD_2PI/24)*GST(ssue);
+    tmp = alpha - (SGD_2PI/24)*gst;    
+    if (tmp < -SGD_PI) {
+       do tmp += SGD_2PI;
+       while (tmp < -SGD_PI);
+    } else if (tmp > SGD_PI) {
+       do tmp -= SGD_2PI;
+       while (tmp < -SGD_PI);
     }
 
     *lon = tmp;
@@ -242,101 +245,107 @@ static void fgSunPositionGST(double gst, double *lon, double *lat) {
 // update the cur_time_params structure with the current sun position
 void fgUpdateSunPos( void ) {
     fgLIGHT *l;
-    FGTime *t;
-    FGView *v;
-    MAT3vec nup, nsun, v0, surface_to_sun;
+    FGViewer *v;
+    sgVec3 nup, nsun;
     Point3D p, rel_sunpos;
     double dot, east_dot;
     double sun_gd_lat, sl_radius;
-    double ntmp;
+
+    // vector in cartesian coordinates from current position to the
+    // postion on the earth's surface the sun is directly over
+    sgVec3 to_sun;
+
+    // surface direction to go to head towards sun
+    sgVec3 surface_to_sun;
 
     l = &cur_light_params;
-    t = FGTime::cur_time_params;
-    v = &current_view;
+    SGTime *t = globals->get_time_params();
+    v = globals->get_current_view();
 
-    FG_LOG( FG_EVENT, FG_INFO, "  Updating Sun position" );
+    SG_LOG( SG_EVENT, SG_INFO, "  Updating Sun position" );
+    SG_LOG( SG_EVENT, SG_INFO, "  Gst = " << t->getGst() );
 
-    // (not sure why there was two)
-    // fgSunPosition(t->cur_time, &l->sun_lon, &sun_gd_lat);
     fgSunPositionGST(t->getGst(), &l->sun_lon, &sun_gd_lat);
 
-    fgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
+    sgGeodToGeoc(sun_gd_lat, 0.0, &sl_radius, &l->sun_gc_lat);
 
     p = Point3D( l->sun_lon, l->sun_gc_lat, sl_radius );
-    l->fg_sunpos = fgPolarToCart3d(p);
+    l->fg_sunpos = sgPolarToCart3d(p);
 
-    FG_LOG( FG_EVENT, FG_INFO, "    t->cur_time = " << t->get_cur_time() );
-    FG_LOG( FG_EVENT, FG_INFO, 
+    SG_LOG( SG_EVENT, SG_INFO, "    t->cur_time = " << t->get_cur_time() );
+    SG_LOG( SG_EVENT, SG_INFO, 
            "    Sun Geodetic lat = " << sun_gd_lat
            << " Geocentric lat = " << l->sun_gc_lat );
 
-    // I think this will work better for generating the sun light vector
-    l->sun_vec[0] = l->fg_sunpos.x();
-    l->sun_vec[1] = l->fg_sunpos.y();
-    l->sun_vec[2] = l->fg_sunpos.z();
-    MAT3_NORMALIZE_VEC(l->sun_vec, ntmp);
-    MAT3_SCALE_VEC(l->sun_vec_inv, l->sun_vec, -1.0);
+    // update the sun light vector
+    sgSetVec4( l->sun_vec, 
+              l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z(), 0.0 );
+    sgNormalizeVec4( l->sun_vec );
+    sgCopyVec4( l->sun_vec_inv, l->sun_vec );
+    sgNegateVec4( l->sun_vec_inv );
 
     // make sure these are directional light sources only
-    l->sun_vec[3] = 0.0;
-    l->sun_vec_inv[3] = 0.0;
-
-    // printf("  l->sun_vec = %.2f %.2f %.2f\n", l->sun_vec[0], l->sun_vec[1],
-    //        l->sun_vec[2]);
+    l->sun_vec[3] = l->sun_vec_inv[3] = 0.0;
+    // cout << "  l->sun_vec = " << l->sun_vec[0] << "," << l->sun_vec[1]
+    //      << ","<< l->sun_vec[2] << endl;
 
     // calculate the sun's relative angle to local up
-    MAT3_COPY_VEC(nup, v->get_local_up());
-    nsun[0] = l->fg_sunpos.x(); 
-    nsun[1] = l->fg_sunpos.y();
-    nsun[2] = l->fg_sunpos.z();
-    MAT3_NORMALIZE_VEC(nup, ntmp);
-    MAT3_NORMALIZE_VEC(nsun, ntmp);
-
-    l->sun_angle = acos(MAT3_DOT_PRODUCT(nup, nsun));
-    // printf("  SUN ANGLE relative to current location = %.3f rads.\n", 
-    //        l->sun_angle);
+    sgCopyVec3( nup, v->get_world_up() );
+    sgSetVec3( nsun, l->fg_sunpos.x(), l->fg_sunpos.y(), l->fg_sunpos.z() );
+    sgNormalizeVec3(nup);
+    sgNormalizeVec3(nsun);
+    // cout << "nup = " << nup[0] << "," << nup[1] << "," 
+    //      << nup[2] << endl;
+    // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," 
+    //      << nsun[2] << endl;
+
+    l->sun_angle = acos( sgScalarProductVec3 ( nup, nsun ) );
+    SG_LOG( SG_EVENT, SG_INFO, "sun angle relative to current location = "
+           << l->sun_angle );
     
     // calculate vector to sun's position on the earth's surface
-    rel_sunpos = l->fg_sunpos - (v->get_view_pos() + scenery.center);
-    v->set_to_sun( rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
+    Point3D vp( v->get_view_pos()[0],
+               v->get_view_pos()[1],
+               v->get_view_pos()[2] );
+    rel_sunpos = l->fg_sunpos - ( vp + globals->get_scenery()->get_center() );
+    sgSetVec3( to_sun, rel_sunpos.x(), rel_sunpos.y(), rel_sunpos.z() );
     // printf( "Vector to sun = %.2f %.2f %.2f\n",
     //         v->to_sun[0], v->to_sun[1], v->to_sun[2]);
 
-    // make a vector to the current view position
-    Point3D view_pos = v->get_view_pos();
-    MAT3_SET_VEC(v0, view_pos.x(), view_pos.y(), view_pos.z());
-
     // Given a vector from the view position to the point on the
     // earth's surface the sun is directly over, map into onto the
     // local plane representing "horizontal".
-    map_vec_onto_cur_surface_plane( v->get_local_up(), v0, v->get_to_sun(), 
-                                   surface_to_sun );
-    MAT3_NORMALIZE_VEC(surface_to_sun, ntmp);
-    v->set_surface_to_sun( surface_to_sun[0], surface_to_sun[1], 
-                          surface_to_sun[2] );
-    // printf("Surface direction to sun is %.2f %.2f %.2f\n",
-    //        v->surface_to_sun[0], v->surface_to_sun[1], v->surface_to_sun[2]);
-    // printf("Should be close to zero = %.2f\n", 
-    //        MAT3_DOT_PRODUCT(v->local_up, v->surface_to_sun));
 
-    // calculate the angle between v->surface_to_sun and
-    // v->surface_east.  We do this so we can sort out the acos()
-    // ambiguity.  I wish I could think of a more efficient way ... :-(
-    east_dot = MAT3_DOT_PRODUCT( surface_to_sun, v->get_surface_east() );
-    // printf("  East dot product = %.2f\n", east_dot);
+    sgmap_vec_onto_cur_surface_plane( v->get_world_up(), v->get_view_pos(),
+                                     to_sun, surface_to_sun );
+    sgNormalizeVec3(surface_to_sun);
+    // cout << "(sg) Surface direction to sun is "
+    //   << surface_to_sun[0] << "," 
+    //   << surface_to_sun[1] << ","
+    //   << surface_to_sun[2] << endl;
+    // cout << "Should be close to zero = " 
+    //   << sgScalarProductVec3(nup, surface_to_sun) << endl;
+
+    // calculate the angle between surface_to_sun and
+    // v->get_surface_east().  We do this so we can sort out the
+    // acos() ambiguity.  I wish I could think of a more efficient
+    // way. :-(
+    east_dot = sgScalarProductVec3( surface_to_sun, v->get_surface_east() );
+    // cout << "  East dot product = " << east_dot << endl;
 
     // calculate the angle between v->surface_to_sun and
     // v->surface_south.  this is how much we have to rotate the sky
     // for it to align with the sun
-    dot = MAT3_DOT_PRODUCT( surface_to_sun, v->get_surface_south() );
-    // printf("  Dot product = %.2f\n", dot);
+    dot = sgScalarProductVec3( surface_to_sun, v->get_surface_south() );
+    // cout << "  Dot product = " << dot << endl;
+
     if ( east_dot >= 0 ) {
        l->sun_rotation = acos(dot);
     } else {
        l->sun_rotation = -acos(dot);
     }
-    // printf("  Sky needs to rotate = %.3f rads = %.1f degrees.\n", 
-    //        angle, angle * RAD_TO_DEG); */
+    // cout << "  Sky needs to rotate = " << angle << " rads = "
+    //      << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl;
 }