From af6da1d3c1aff4dd28626c6dbfd83b3bfccec705 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 9 Nov 2009 10:28:59 +0000 Subject: [PATCH] sun postition fixes Was: allow sound effects in the configuration file to be added to the 'avionics' sample group by setting 'avionics'. but this change obviously snuck in with that. --- src/Time/tmp.cxx | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Time/tmp.cxx b/src/Time/tmp.cxx index ab4ca22fc..0e04d5b24 100644 --- a/src/Time/tmp.cxx +++ b/src/Time/tmp.cxx @@ -76,25 +76,31 @@ void fgUpdateSunPos( void ) { SG_LOG( SG_EVENT, SG_DEBUG, " Updating Sun position" ); SG_LOG( SG_EVENT, SG_DEBUG, " Gst = " << time_now->getGst() ); - double sun_lon; - double sun_gd_lat; - fgSunPositionGST(time_now->getGst(), &sun_lon, &sun_gd_lat); + double sun_lon, sun_lat; + fgSunPositionGST(time_now->getGst(), &sun_lon, &sun_lat); light->set_sun_lon(sun_lon); - light->set_sun_lat(sun_gd_lat); + light->set_sun_lat(sun_lat); // update the sun light vector - // calculations are in the horizontal normal plane: - // x-north, y-east, z-down + // calculations are in the horizontal normal plane: x-north, y-east, z-down + static SGQuatd q = SGQuatd::fromLonLat(SGGeod::fromRad(0,0)); - SGGeod geodViewPos = SGGeod::fromCart(viewer->getViewPosition()); - SGGeod geodSunPos = SGGeod::fromRad(sun_lon, sun_gd_lat); + // sun orientation + SGGeod geodSunPos = SGGeod::fromRad(sun_lon, sun_lat); + SGQuatd sunOr = SGQuatd::fromLonLat(geodSunPos); - //static SGQuatd q = SGQuatd::fromLonLat(SGGeod::fromRad(0,0)); + // scenery orientation + SGGeod geodViewPos = SGGeod::fromCart(viewer->getViewPosition()); SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos); - SGQuatd sunOr = SGQuatd::fromLonLat(geodSunPos); + SGVec3d localAt = hlOr.backTransform(SGVec3d::e3()); + + // transpose the sun direction from (lat,lon) to (0,0) + SGVec3d transSunDir = (q*sunOr).transform(-localAt); + SGQuatd sunDirOr = SGQuatd::fromRealImag(0, transSunDir); - SGVec3d sunDirection = (hlOr*sunOr).transform(SGVec3d::e3()); - light->set_sun_rotation( acos(sunDirection[1]) - SGD_PI_2 ); + // transpose the calculated sun vector back to (lat,lon) + SGVec3d sunDirection = sunDirOr.transform(localAt); + light->set_sun_rotation( acos(sunDirection[1])-SGD_PI_2 ); light->set_sun_angle( acos(-sunDirection[2]) ); SGVec3d sunPos = SGVec3d::fromGeod(geodSunPos); @@ -128,15 +134,14 @@ void fgUpdateSunPos( void ) { // 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(sunpos))); + SGVec3f world_up = toVec3f(hlOr.backTransform(-SGVec3d::e3())); + SGVec3f nsun = toVec3f(normalize(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 ) ) ); + l->set_sun_angle( acos( dot ( world_up, nsun ) ) ); SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = " << l->get_sun_angle() ); @@ -152,10 +157,9 @@ void fgUpdateSunPos( void ) { // 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; + SGVec3f view_pos = toVec3f(v->get_view_pos()); 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); -- 2.39.5