X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FTime%2Ftmp.cxx;h=70cac6f823c14c167ded46db0ae339cf7534f15a;hb=fc71333bdd0217a9ff4880305daf6d4d10fb0b40;hp=d2fec7dcc00eff5c547565aeed1ac56abf392af0;hpb=0ffa19cd32c02405d0e740393b37f4009ed5a312;p=flightgear.git diff --git a/src/Time/tmp.cxx b/src/Time/tmp.cxx index d2fec7dcc..70cac6f82 100644 --- a/src/Time/tmp.cxx +++ b/src/Time/tmp.cxx @@ -2,7 +2,7 @@ // // Written by Curtis Olson, started July 2000. // -// Copyright (C) 2000 Curtis L. Olson - curt@flightgear.org +// Copyright (C) 2000 Curtis L. Olson - http://www.flightgear.org/~curt // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as @@ -16,7 +16,7 @@ // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software -// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // // $Id$ @@ -25,48 +25,142 @@ # include #endif -#include +#include +#include +#include #include -#include -#include
+#include
+#include
+#include
+#include #include "light.hxx" -#include "moonpos.hxx" -#include "sunpos.hxx" +#include "sunsolver.hxx" #include "tmp.hxx" -FGMagVar::FGMagVar() { -} - -FGMagVar::~FGMagVar() { -} - - -void FGMagVar::update( double lon, double lat, double alt_m, double jd ) { - // Calculate local magnetic variation - double field[6]; - // cout << "alt_m = " << alt_m << endl; - magvar = SGMagVar( lat, lon, alt_m / 1000.0, (long)jd, field ); - magdip = atan(field[5]/sqrt(field[3]*field[3]+field[4]*field[4])); -} - -FGMagVar cur_magvar; - - // periodic time updater wrapper void fgUpdateLocalTime() { - - SGTime::cur_time_params->updateLocal( cur_fdm_state->get_Longitude(), - cur_fdm_state->get_Latitude(), - current_options.get_fg_root() ); + static const SGPropertyNode *longitude + = fgGetNode("/position/longitude-deg"); + static const SGPropertyNode *latitude + = fgGetNode("/position/latitude-deg"); + + SGPath zone( globals->get_fg_root() ); + zone.append( "Timezone" ); + + SG_LOG(SG_GENERAL, SG_INFO, "updateLocal(" + << longitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS + << ", " + << latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS + << ", " << zone.str() << ")"); + globals->get_time_params()->updateLocal( longitude->getDoubleValue() + * SGD_DEGREES_TO_RADIANS, + latitude->getDoubleValue() + * SGD_DEGREES_TO_RADIANS, + zone.str() ); } -// update sky and lighting parameters -void fgUpdateSkyAndLightingParams() { - fgUpdateSunPos(); - fgUpdateMoonPos(); - cur_light_params.Update(); +// update the cur_time_params structure with the current sun position +void fgUpdateSunPos( void ) { + FGLight *l = (FGLight *)(globals->get_subsystem("lighting")); + SGTime *t = globals->get_time_params(); + FGViewer *v = globals->get_current_view(); + + SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" ); + SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << t->getGst() ); + + double sun_l; + double sun_gd_lat; + fgSunPositionGST(t->getGst(), &sun_l, &sun_gd_lat); + l->set_sun_lon(sun_l); + l->set_sun_lat(sun_gd_lat); + l->set_sunpos(SGVec3d::fromGeod(SGGeod::fromRad(sun_l, sun_gd_lat))); + + SG_LOG( SG_EVENT, SG_DEBUG, " t->cur_time = " << t->get_cur_time() ); + SG_LOG( SG_EVENT, SG_DEBUG, + " Sun Geodetic lat = " << sun_gd_lat + << " Geodetic lat = " << sun_gd_lat ); + + // update the sun light vector + l->sun_vec() = SGVec4f(toVec3f(normalize(l->get_sunpos())), 0); + l->sun_vec_inv() = - l->sun_vec(); + + // calculate the sun's relative angle to local up + SGVec3d viewPos = v->get_view_pos(); + SGQuatd hlOr = SGQuatd::fromLonLat(SGGeod::fromCart(viewPos)); + SGVec3f nup(toVec3f(hlOr.backTransform(-SGVec3d::e3()))); + + SGVec3f nsun(toVec3f(normalize(l->get_sunpos()))); + // cout << "nup = " << nup[0] << "," << nup[1] << "," + // << nup[2] << endl; + // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," + // << nsun[2] << endl; + + l->set_sun_angle( acos( dot ( nup, nsun ) ) ); + SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = " + << l->get_sun_angle() ); + + // calculate vector to sun's position on the earth's surface + SGVec3d rel_sunpos = l->get_sunpos() - v->get_view_pos(); + // vector in cartesian coordinates from current position to the + // postion on the earth's surface the sun is directly over + SGVec3f to_sun = toVec3f(rel_sunpos); + // printf( "Vector to sun = %.2f %.2f %.2f\n", + // v->to_sun[0], v->to_sun[1], v->to_sun[2]); + + // 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". + + SGVec3f world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3())); + SGVec3f view_pos = toVec3f(v->get_view_pos()); + // surface direction to go to head towards sun + SGVec3f surface_to_sun; + sgmap_vec_onto_cur_surface_plane( world_up.data(), view_pos.data(), + to_sun.data(), surface_to_sun.data() ); + surface_to_sun = normalize(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. :-( + SGVec3f surface_east(toVec3f(hlOr.backTransform(SGVec3d::e2()))); + float east_dot = dot( surface_to_sun, 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 + SGVec3f surface_south(toVec3f(hlOr.backTransform(-SGVec3d::e1()))); + float dot_ = dot( surface_to_sun, surface_south ); + // cout << " Dot product = " << dot << endl; + + if (dot_ > 1.0) { + SG_LOG( SG_ASTRO, SG_INFO, + "Dot product = " << dot_ << " is greater than 1.0" ); + dot_ = 1.0; + } + else if (dot_ < -1.0) { + SG_LOG( SG_ASTRO, SG_INFO, + "Dot product = " << dot_ << " is less than -1.0" ); + dot_ = -1.0; + } + + if ( east_dot >= 0 ) { + l->set_sun_rotation( acos(dot_) ); + } else { + l->set_sun_rotation( -acos(dot_) ); + } + // cout << " Sky needs to rotate = " << angle << " rads = " + // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl; } +