From 2d2710d5daea9972abee2935835a41a12cd8aee9 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 5 Nov 2009 12:49:29 +0000 Subject: [PATCH 1/4] pass the geodetic position and view orientation quat to the sky repositioning function --- src/Main/renderer.cxx | 31 ++----------------------------- 1 file changed, 2 insertions(+), 29 deletions(-) diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx index ef3fe6e10..74cf55fb0 100644 --- a/src/Main/renderer.cxx +++ b/src/Main/renderer.cxx @@ -611,42 +611,15 @@ FGRenderer::update( bool refresh_camera_settings ) { } SGSkyState sstate; - - SGVec3d viewPos = current__view->getViewPosition(); - sstate.view_pos = toVec3f(viewPos); - SGGeod geodViewPos = SGGeod::fromCart(viewPos); - SGGeod geodZeroViewPos = SGGeod::fromGeodM(geodViewPos, 0); - sstate.zero_elev = toVec3f(SGVec3d::fromGeod(geodZeroViewPos)); - SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos); - sstate.view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3())); - sstate.lon = geodViewPos.getLongitudeRad(); - sstate.lat = geodViewPos.getLatitudeRad(); - sstate.alt = geodViewPos.getElevationM(); + sstate.pos = current__view->getPosition(); + sstate.ori = current__view->getViewOrientation(); sstate.spin = l->get_sun_rotation(); sstate.gst = globals->get_time_params()->getGst(); sstate.sun_dist = 50000.0 * sun_horiz_eff; sstate.moon_dist = 40000.0 * moon_horiz_eff; sstate.sun_angle = l->get_sun_angle(); - - /* - SG_LOG( SG_GENERAL, SG_BULK, "thesky->repaint() sky_color = " - << l->sky_color()[0] << " " - << l->sky_color()[1] << " " - << l->sky_color()[2] << " " - << l->sky_color()[3] ); - SG_LOG( SG_GENERAL, SG_BULK, " fog = " - << l->fog_color()[0] << " " - << l->fog_color()[1] << " " - << l->fog_color()[2] << " " - << l->fog_color()[3] ); - SG_LOG( SG_GENERAL, SG_BULK, - " sun_angle = " << l->sun_angle - << " moon_angle = " << l->moon_angle ); - */ - SGSkyColor scolor; - scolor.sky_color = SGVec3f(l->sky_color().data()); scolor.adj_sky_color = SGVec3f(l->adj_sky_color().data()); scolor.fog_color = SGVec3f(l->adj_fog_color().data()); From fe834dedfbc85d34aa4258ee04aff08f7cb3f9b4 Mon Sep 17 00:00:00 2001 From: ehofman Date: Thu, 5 Nov 2009 13:46:49 +0000 Subject: [PATCH 2/4] Save a costly SGVec3d::fromGeod() calculation --- src/Main/renderer.cxx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main/renderer.cxx b/src/Main/renderer.cxx index 74cf55fb0..bafc5d488 100644 --- a/src/Main/renderer.cxx +++ b/src/Main/renderer.cxx @@ -611,7 +611,8 @@ FGRenderer::update( bool refresh_camera_settings ) { } SGSkyState sstate; - sstate.pos = current__view->getPosition(); + sstate.pos = current__view->getViewPosition(); + sstate.pos_geod = current__view->getPosition(); sstate.ori = current__view->getViewOrientation(); sstate.spin = l->get_sun_rotation(); sstate.gst = globals->get_time_params()->getGst(); From 06187368d25785ca9e4d59772295871eb430b1fa Mon Sep 17 00:00:00 2001 From: ehofman Date: Fri, 6 Nov 2009 01:05:43 +0000 Subject: [PATCH 3/4] first stab at reorganizing fgUpdateSunPos() to make it use quats --- src/Time/light.cxx | 2 -- src/Time/light.hxx | 9 ------ src/Time/tmp.cxx | 69 ++++++++++++++++++++++++++++++++++++---------- 3 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/Time/light.cxx b/src/Time/light.cxx index c1d204ac3..2d02aab7d 100644 --- a/src/Time/light.cxx +++ b/src/Time/light.cxx @@ -57,8 +57,6 @@ FGLight::FGLight () _sun_lat(0), _moon_lon(0), _moon_gc_lat(0), - _sunpos(0, 0, 0), - _moonpos(0, 0, 0), _sun_vec(0, 0, 0, 0), _moon_vec(0, 0, 0, 0), _sun_vec_inv(0, 0, 0, 0), diff --git a/src/Time/light.hxx b/src/Time/light.hxx index ffffa5ab2..6a7500c9a 100644 --- a/src/Time/light.hxx +++ b/src/Time/light.hxx @@ -60,9 +60,6 @@ private: double _sun_lon, _sun_lat; double _moon_lon, _moon_gc_lat; - // in cartesian coordiantes - SGVec3d _sunpos, _moonpos; - // (in view coordinates) SGVec4f _sun_vec, _moon_vec; @@ -141,9 +138,6 @@ public: inline double get_sun_lat () const { return _sun_lat; } inline void set_sun_lat (double l) { _sun_lat = l; } - inline const SGVec3d& get_sunpos () const { return _sunpos; } - inline void set_sunpos (const SGVec3d& p) { _sunpos = p; } - inline SGVec4f& sun_vec () { return _sun_vec; } inline SGVec4f& sun_vec_inv () { return _sun_vec_inv; } @@ -162,9 +156,6 @@ public: inline double get_moon_gc_lat () const { return _moon_gc_lat; } inline void set_moon_gc_lat (double l) { _moon_gc_lat = l; } - inline const SGVec3d& get_moonpos () const { return _moonpos; } - inline void set_moonpos (const SGVec3d& p) { _moonpos = p; } - inline const SGVec4f& moon_vec () const { return _moon_vec; } inline const SGVec4f& moon_vec_inv () const { return _moon_vec_inv; } }; diff --git a/src/Time/tmp.cxx b/src/Time/tmp.cxx index 70cac6f82..ab4ca22fc 100644 --- a/src/Time/tmp.cxx +++ b/src/Time/tmp.cxx @@ -65,6 +65,43 @@ void fgUpdateLocalTime() { // update the cur_time_params structure with the current sun position void fgUpdateSunPos( void ) { +#if 0 + // This only works at lat,lon = 0,0 + // need to find a way to get it working at other locations + + FGLight *light = (FGLight *)(globals->get_subsystem("lighting")); + FGViewer *viewer = globals->get_current_view(); + SGTime *time_now = globals->get_time_params(); + + 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); + light->set_sun_lon(sun_lon); + light->set_sun_lat(sun_gd_lat); + + // update the sun light vector + // calculations are in the horizontal normal plane: + // x-north, y-east, z-down + + SGGeod geodViewPos = SGGeod::fromCart(viewer->getViewPosition()); + SGGeod geodSunPos = SGGeod::fromRad(sun_lon, sun_gd_lat); + + //static SGQuatd q = SGQuatd::fromLonLat(SGGeod::fromRad(0,0)); + SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos); + SGQuatd sunOr = SGQuatd::fromLonLat(geodSunPos); + + SGVec3d sunDirection = (hlOr*sunOr).transform(SGVec3d::e3()); + light->set_sun_rotation( acos(sunDirection[1]) - SGD_PI_2 ); + light->set_sun_angle( acos(-sunDirection[2]) ); + + SGVec3d sunPos = SGVec3d::fromGeod(geodSunPos); + light->sun_vec() = SGVec4f(toVec3f(normalize(sunPos)), 0); + light->sun_vec_inv() = -light->sun_vec(); + +#else FGLight *l = (FGLight *)(globals->get_subsystem("lighting")); SGTime *t = globals->get_time_params(); FGViewer *v = globals->get_current_view(); @@ -77,15 +114,15 @@ void fgUpdateSunPos( void ) { 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))); + SGVec3d 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 ); + " 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() = SGVec4f(toVec3f(normalize(sunpos)), 0); l->sun_vec_inv() = - l->sun_vec(); // calculate the sun's relative angle to local up @@ -93,18 +130,18 @@ void fgUpdateSunPos( void ) { 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] << "," + SGVec3f nsun(toVec3f(normalize(sunpos))); + // cout << "nup = " << nup[0] << "," << nup[1] << "," // << nup[2] << endl; - // cout << "nsun = " << nsun[0] << "," << nsun[1] << "," + // 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() ); - + << 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(); + SGVec3d rel_sunpos = 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); @@ -120,13 +157,13 @@ void fgUpdateSunPos( void ) { // 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() ); + 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[0] << "," // << surface_to_sun[1] << "," // << surface_to_sun[2] << endl; - // cout << "Should be close to zero = " + // cout << "Should be close to zero = " // << sgScalarProductVec3(nup, surface_to_sun) << endl; // calculate the angle between surface_to_sun and @@ -156,11 +193,13 @@ void fgUpdateSunPos( void ) { } if ( east_dot >= 0 ) { - l->set_sun_rotation( acos(dot_) ); + l->set_sun_rotation( acos(dot_) ); } else { - l->set_sun_rotation( -acos(dot_) ); + l->set_sun_rotation( -acos(dot_) ); } // cout << " Sky needs to rotate = " << angle << " rads = " // << angle * SGD_RADIANS_TO_DEGREES << " degrees." << endl; + +#endif } From af6da1d3c1aff4dd28626c6dbfd83b3bfccec705 Mon Sep 17 00:00:00 2001 From: ehofman Date: Mon, 9 Nov 2009 10:28:59 +0000 Subject: [PATCH 4/4] 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 | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 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); - - //static SGQuatd q = SGQuatd::fromLonLat(SGGeod::fromRad(0,0)); - SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos); + // sun orientation + SGGeod geodSunPos = SGGeod::fromRad(sun_lon, sun_lat); SGQuatd sunOr = SGQuatd::fromLonLat(geodSunPos); - SGVec3d sunDirection = (hlOr*sunOr).transform(SGVec3d::e3()); - light->set_sun_rotation( acos(sunDirection[1]) - SGD_PI_2 ); + // scenery orientation + SGGeod geodViewPos = SGGeod::fromCart(viewer->getViewPosition()); + SGQuatd hlOr = SGQuatd::fromLonLat(geodViewPos); + 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); + + // 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);