]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/sky.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / sky / sky.cxx
index 2eec5cc3bbd65b184e277d0e29560d1f867cccd0..77a491b7c46b33f9e259a9f15009670a28ba10e4 100644 (file)
@@ -29,8 +29,9 @@
 #include "cloudfield.hxx"
 #include "newcloud.hxx"
 
-#include <simgear/math/sg_random.h>
 #include <simgear/scene/util/RenderConstants.hxx>
+#include <simgear/scene/util/OsgMath.hxx>
+#include <simgear/sg_inlines.h>
 
 #include <osg/StateSet>
 #include <osg/Depth>
@@ -38,6 +39,7 @@
 // Constructor
 SGSky::SGSky( void ) {
     effective_visibility = visibility = 10000.0;
+    minimum_sky_visibility = 1000;
 
     // near cloud visibility state variables
     in_puff = false;
@@ -59,12 +61,16 @@ SGSky::SGSky( void ) {
     pre_root->setStateSet(preStateSet);
     cloud_root = new osg::Group;
     cloud_root->setNodeMask(simgear::MODEL_BIT);
-
+    cloud_root->setName("SGSky-cloud-root");
     pre_selector = new osg::Switch;
 
     pre_transform = new osg::Group;
 
     _ephTransform = new osg::MatrixTransform;
+    
+    // Set up a RNG that is repeatable within 10 minutes to ensure that clouds
+    // are synced up in multi-process deployments.
+    mt_init_time_10(&seed);
 }
 
 
@@ -78,10 +84,11 @@ SGSky::~SGSky( void )
 // the provided branch
 void SGSky::build( double h_radius_m, double v_radius_m,
                    double sun_size, double moon_size,
-                   const SGEphemeris& eph, SGPropertyNode *property_tree_node )
+                   const SGEphemeris& eph, SGPropertyNode *property_tree_node,
+                   simgear::SGReaderWriterOptions* options )
 {
     dome = new SGSkyDome;
-    pre_transform->addChild( dome->build( h_radius_m, v_radius_m ) );
+    pre_transform->addChild( dome->build( h_radius_m, v_radius_m, options ) );
 
     pre_transform->addChild(_ephTransform.get());
     planets = new SGStars;
@@ -111,7 +118,7 @@ void SGSky::build( double h_radius_m, double v_radius_m,
 // 180 degrees = darkest midnight
 bool SGSky::repaint( const SGSkyColor &sc, const SGEphemeris& eph )
 {
-    if ( effective_visibility > 1000.0 ) {
+    if ( effective_visibility > minimum_sky_visibility ) {
        enable();
        dome->repaint( sc.adj_sky_color, sc.sky_color, sc.fog_color,
                        sc.sun_angle, effective_visibility );
@@ -147,22 +154,25 @@ bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt
     double angle = st.gst * 15;        // degrees
     double angleRad = SGMiscd::deg2rad(angle);
 
-    SGVec3f view_pos, zero_elev, view_up;
+    SGVec3f zero_elev, view_up;
     double lon, lat, alt;
 
-    view_pos = toVec3f( SGVec3d::fromGeod(st.pos) );
-    SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos, 0);
+    SGGeod geodZeroViewPos = SGGeod::fromGeodM(st.pos_geod, 0);
     zero_elev = toVec3f( SGVec3d::fromGeod(geodZeroViewPos) );
 
-    view_up = toVec3f( st.ori.backTransform(SGVec3d::e2()) );
-    lon = st.pos.getLongitudeRad();
-    lat = st.pos.getLatitudeRad();
-    alt = st.pos.getElevationM();
+    // calculate the scenery up vector
+    SGQuatd hlOr = SGQuatd::fromLonLat(st.pos_geod);
+    view_up = toVec3f(hlOr.backTransform(-SGVec3d::e3()));
+
+    // viewer location
+    lon = st.pos_geod.getLongitudeRad();
+    lat = st.pos_geod.getLatitudeRad();
+    alt = st.pos_geod.getElevationM();
 
     dome->reposition( zero_elev, alt, lon, lat, st.spin );
 
     osg::Matrix m = osg::Matrix::rotate(angleRad, osg::Vec3(0, 0, -1));
-    m.postMultTranslate(toOsg(view_pos));
+    m.postMultTranslate(toOsg(st.pos));
     _ephTransform->setMatrix(m);
 
     double sun_ra = eph.getSunRightAscension();
@@ -174,11 +184,13 @@ bool SGSky::reposition( const SGSkyState &st, const SGEphemeris& eph, double dt
     moon->reposition( moon_ra, moon_dec, st.moon_dist );
 
     for ( unsigned i = 0; i < cloud_layers.size(); ++i ) {
-        if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ) {
+        if ( cloud_layers[i]->getCoverage() != SGCloudLayer::SG_CLOUD_CLEAR ||
+               cloud_layers[i]->get_layer3D()->isDefined3D() ) {
             cloud_layers[i]->reposition( zero_elev, view_up, lon, lat, alt, dt);
-        } else
+        } else {
           cloud_layers[i]->getNode()->setAllChildrenOff();
     }
+    }
 
     return true;
 }
@@ -227,10 +239,74 @@ void SGSky::set_3dCloudVisRange(float vis)
 {
     SGCloudField::setVisRange(vis);
     for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
-        cloud_layers[i]->get_layer3D()->applyVisRange();
+        cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
+    }
+}
+
+float SGSky::get_3dCloudImpostorDistance() const {
+    return SGCloudField::getImpostorDistance();
+}
+
+void SGSky::set_3dCloudImpostorDistance(float vis)
+{
+    SGCloudField::setImpostorDistance(vis);
+    for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
+        cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
     }
 }
 
+float SGSky::get_3dCloudLoD1Range() const {
+    return SGCloudField::getLoD1Range();
+}
+
+void SGSky::set_3dCloudLoD1Range(float vis)
+{
+    SGCloudField::setLoD1Range(vis);
+    for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
+        cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
+    }
+}
+
+float SGSky::get_3dCloudLoD2Range() const {
+    return SGCloudField::getLoD2Range();
+}
+
+void SGSky::set_3dCloudLoD2Range(float vis)
+{
+    SGCloudField::setLoD2Range(vis);
+    for ( int i = 0; i < (int)cloud_layers.size(); ++i ) {
+        cloud_layers[i]->get_layer3D()->applyVisAndLoDRange();
+    }
+}
+
+bool SGSky::get_3dCloudWrap() const {
+    return SGCloudField::getWrap();
+}
+
+void SGSky::set_3dCloudWrap(bool wrap)
+{
+    SGCloudField::setWrap(wrap);
+}
+
+bool SGSky::get_3dCloudUseImpostors() const {
+    return SGCloudField::getUseImpostors();
+}
+
+void SGSky::set_3dCloudUseImpostors(bool imp)
+{
+    SGCloudField::setUseImpostors(imp);
+}
+
+float SGSky::get_minimum_sky_visibility() const 
+{
+    return minimum_sky_visibility;
+}
+
+void SGSky::set_minimum_sky_visibility( float value )
+{
+    minimum_sky_visibility = value;
+}
+
 void SGSky::texture_path( const string& path ) {
        tex_path = SGPath( path );
 }
@@ -268,7 +344,7 @@ void SGSky::modify_vis( float alt, float time_factor ) {
        }
 
         if ( cloud_layers[i]->getCoverage() == SGCloudLayer::SG_CLOUD_CLEAR ||
-             cloud_layers[i]->get_layer3D()->defined3D) {
+             cloud_layers[i]->get_layer3D()->isDefined3D()) {
             // do nothing, clear layers aren't drawn, don't affect
             // visibility andn dont' need to be faded in or out.
         } else if ( (cloud_layers[i]->getCoverage() == 
@@ -300,11 +376,11 @@ void SGSky::modify_vis( float alt, float time_factor ) {
        if ( ratio < 1.0 ) {
            if ( ! in_puff ) {
                // calc chance of entering cloud puff
-               double rnd = sg_random();
+               double rnd = mt_rand(&seed);
                double chance = rnd * rnd * rnd;
                if ( chance > 0.95 /* * (diff - 25) / 50.0 */ ) {
                    in_puff = true;
-                   puff_length = sg_random() * 2.0; // up to 2 seconds
+                   puff_length = mt_rand(&seed) * 2.0; // up to 2 seconds
                    puff_progression = 0.0;
                }
            }
@@ -356,10 +432,8 @@ void SGSky::modify_vis( float alt, float time_factor ) {
        }
 #endif
 
-        // never let visibility drop below 25 meters
-        if ( effvis <= 25.0 ) {
-            effvis = 25.0;
-        }
+        // never let visibility drop below the layer's configured visibility
+       effvis = SG_MAX2<float>(cloud_layers[i]->getVisibility_m(), effvis );
 
     } // for