]> git.mxchange.org Git - flightgear.git/commitdiff
remove some ugly while loops
authorErik Hofman <erik@ehofman.com>
Sun, 8 Jan 2012 12:51:38 +0000 (13:51 +0100)
committerErik Hofman <erik@ehofman.com>
Sun, 8 Jan 2012 12:51:38 +0000 (13:51 +0100)
src/Time/light.cxx
src/Time/sunsolver.cxx

index 2aedce6ede3b4432e7b3ca353e038a57b352f67e..14ae6c9df320c68ec29ca912519638a5d4a72843 100644 (file)
@@ -401,8 +401,6 @@ void FGLight::update_adj_fog_color () {
 void FGLight::updateSunPos()
 {
     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() );
 
@@ -421,10 +419,11 @@ void FGLight::updateSunPos()
             << " Geodcentric lat = " << _sun_lat );
 
     // update the sun light vector
-    sun_vec() = SGVec4f(toVec3f(normalize(sunpos)), 0);
-    sun_vec_inv() = - sun_vec();
+    _sun_vec = SGVec4f(toVec3f(normalize(sunpos)), 0);
+    _sun_vec_inv = - _sun_vec;
 
     // calculate the sun's relative angle to local up
+    FGViewer *v = globals->get_current_view();
     SGVec3d viewPos = v->get_view_pos();
     SGQuatd hlOr = SGQuatd::fromLonLat(SGGeod::fromCart(viewPos));
     SGVec3d world_up = hlOr.backTransform(-SGVec3d::e3());
index b45e0bf2ca52785010ae468605dfe72de73a1e39..4bad897c92f7c9342e4b78523dccde4cefb9a04d 100644 (file)
@@ -65,13 +65,9 @@ void fgSunPositionGST(double gst, double *lon, double *lat) {
     double dec = atan2(ze, sqrt(xs * xs + ye * ye));
 
     tmp = ra - (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);
-    }
+    
+    double signnedPI = (tmp < 0.0) ? -SGD_PI : SGD_PI;
+    tmp = fmod(tmp+signnedPI, SGD_2PI) - signnedPI;
 
     *lon = tmp;
     *lat = dec;
@@ -100,10 +96,13 @@ static double sun_angle( const SGTime &t, const SGVec3d& world_up,
     //      << nsun[2] << endl;
 
     double sun_angle = acos( dot( nup, nsun ) );
+
+    double signnedPI = (sun_angle < 0.0) ? -SGD_PI : SGD_PI;
+    sun_angle = fmod(sun_angle+signnedPI, SGD_2PI) - signnedPI;
+
     double sun_angle_deg = sun_angle * SG_RADIANS_TO_DEGREES;
-    while ( sun_angle_deg < -180 ) { sun_angle += 360; }
     SG_LOG( SG_EVENT, SG_DEBUG, "sun angle relative to current location = "
-           << sun_angle_deg );
+           << sun_anglei_deg );
 
     return sun_angle_deg;
 }