]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/cloudfield.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / sky / cloudfield.cxx
index 1a801909542aff905156f9860f974496ae4228bf..45ae15f83c4b4cd9feb71f7d376b28e5578b0d0d 100644 (file)
@@ -28,6 +28,7 @@
 #include <osg/Texture2D>
 #include <osg/PositionAttitudeTransform>
 #include <osg/Vec4f>
+#include <osgSim/Impostor>
 
 #include <simgear/compiler.h>
 
@@ -68,8 +69,15 @@ float SGCloudField::fieldSize = 50000.0f;
 double SGCloudField::timer_dt = 0.0;
 float SGCloudField::view_distance = 20000.0f;
 bool SGCloudField::wrap = true;
-float SGCloudField::RADIUS_LEVEL_1 = 5000.0f;
-float SGCloudField::RADIUS_LEVEL_2 = 1000.0f;
+float SGCloudField::MAX_CLOUD_DEPTH = 2000.0f;
+bool SGCloudField::use_impostors = false;
+float SGCloudField::lod1_range = 8000.0f;
+float SGCloudField::lod2_range = 4000.0f;
+float SGCloudField::impostor_distance = 15000.0f;
+
+int impostorcount = 0;
+int lodcount = 0;
+int cloudcount = 0;
 
 SGVec3f SGCloudField::view_vec, SGCloudField::view_X, SGCloudField::view_Y;
 
@@ -122,6 +130,11 @@ bool SGCloudField::reposition( const SGVec3f& p, const SGVec3f& up, double lon,
             ++itr) {
               
              osg::ref_ptr<osg::PositionAttitudeTransform> pat = itr->second;
+            
+             if (pat == 0) {
+                continue;
+             }
+             
              osg::Vec3f currpos = field_transform->getPosition() + fta * pat->getPosition();
                                       
              // Determine the vector from the new position to the cloud in cloud-space.
@@ -164,10 +177,13 @@ SGCloudField::SGCloudField() :
     field_transform->addChild(altitude_transform.get());
     placed_root = new osg::Group();
     altitude_transform->addChild(placed_root);
+    impostorcount = 0;
+    lodcount = 0;
+    cloudcount = 0;
 }
     
 SGCloudField::~SGCloudField() {
-        }
+}
 
 
 void SGCloudField::clear(void) {
@@ -181,14 +197,15 @@ void SGCloudField::clear(void) {
     cloud_hash.clear();
 }
 
-void SGCloudField::applyVisRange(void)
+void SGCloudField::applyVisAndLoDRange(void)
 {
     for (unsigned int i = 0; i < placed_root->getNumChildren(); i++) {
         osg::ref_ptr<osg::LOD> lodnode1 = (osg::LOD*) placed_root->getChild(i);
         for (unsigned int j = 0; j < lodnode1->getNumChildren(); j++) {
+            lodnode1->setRange(j, 0.0f, lod1_range + lod2_range + view_distance + MAX_CLOUD_DEPTH);
             osg::ref_ptr<osg::LOD> lodnode2 = (osg::LOD*) lodnode1->getChild(j);
             for (unsigned int k = 0; k < lodnode2->getNumChildren(); k++) {
-                lodnode2->setRange(k, 0.0f, view_distance);
+                lodnode2->setRange(k, 0.0f, view_distance + MAX_CLOUD_DEPTH);
             }
         }
     }
@@ -196,7 +213,7 @@ void SGCloudField::applyVisRange(void)
             
 bool SGCloudField::addCloud(float lon, float lat, float alt, int index, osg::ref_ptr<EffectGeode> geode) {
   return addCloud(lon, lat, alt, 0.0f, 0.0f, index, geode);
-        }
+}
 
 bool SGCloudField::addCloud(float lon, float lat, float alt, float x, float y, int index, osg::ref_ptr<EffectGeode> geode) {
     // If this cloud index already exists, don't replace it.
@@ -208,22 +225,31 @@ bool SGCloudField::addCloud(float lon, float lat, float alt, float x, float y, i
     addCloudToTree(transform, lon, lat, alt, x, y);
     cloud_hash[index] = transform;
     return true;
-    }
-    
+}
+
 // Remove a give cloud from inside the tree, without removing it from the cloud hash
 void SGCloudField::removeCloudFromTree(osg::ref_ptr<osg::PositionAttitudeTransform> transform)
 {
+    if (transform == 0)
+    {
+        // Ooops!
+        return;
+    }
     osg::ref_ptr<osg::Group> lodnode = transform->getParent(0);
     lodnode->removeChild(transform);
+    cloudcount--;
 
-    // Clean up the LOD nodes if required
     if (lodnode->getNumChildren() == 0) {
         osg::ref_ptr<osg::Group> lodnode1 = lodnode->getParent(0);
-            
+        osg::ref_ptr<osgSim::Impostor> impostornode = (osgSim::Impostor*) lodnode1->getParent(0);
+
         lodnode1->removeChild(lodnode);
-            
+        lodcount--;
+
         if (lodnode1->getNumChildren() == 0) {
-            placed_root->removeChild(lodnode1);
+          impostornode->removeChild(lodnode1);
+          placed_root->removeChild(impostornode);
+          impostorcount--;
         }
     }
 }
@@ -289,39 +315,55 @@ void SGCloudField::addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> t
     bool found = false;
     osg::ref_ptr<osg::LOD> lodnode1;
     osg::ref_ptr<osg::LOD> lodnode;
+    osg::ref_ptr<osgSim::Impostor> impostornode;
 
     for (unsigned int i = 0; (!found) && (i < placed_root->getNumChildren()); i++) {
         lodnode1 = (osg::LOD*) placed_root->getChild(i);
-        if ((lodnode1->getCenter() - pos).length2() < RADIUS_LEVEL_1*RADIUS_LEVEL_1) {
-            // New cloud is within RADIUS_LEVEL_1 of the center of the LOD node.
-            found = true;
-        }
+        if ((lodnode1->getCenter() - pos).length2() < lod1_range*lod1_range) {
+          // New cloud is within RADIUS_LEVEL_1 of the center of the LOD node.
+          found = true;
+        }         
     }
 
     if (!found) {
-        lodnode1 = new osg::LOD();
-        placed_root->addChild(lodnode1.get());
+        if (use_impostors) {
+          impostornode = new osgSim::Impostor();
+          impostornode->setImpostorThreshold(impostor_distance);
+          //impostornode->setImpostorThresholdToBound();
+          //impostornode->setCenter(pos);                
+          placed_root->addChild(impostornode.get());
+          lodnode1 = (osg::ref_ptr<osg::LOD>) impostornode;
+        } else {
+          lodnode1 = new osg::LOD();
+          placed_root->addChild(lodnode1.get());
+        }
+        impostorcount++;
     }
 
     // Now check if there is a second level LOD node at an appropriate distance
     found = false;
-
-    for (unsigned int j = 0; (!found) && (j < lodnode1->getNumChildren()); j++) {
+    
+    for (unsigned int j = 0; (!found) && (j < lodnode1->getNumChildren()); j++) {      
         lodnode = (osg::LOD*) lodnode1->getChild(j);
-        if ((lodnode->getCenter() - pos).length2() < RADIUS_LEVEL_2*RADIUS_LEVEL_2) {
+        if ((lodnode->getCenter() - pos).length2() < lod2_range*lod2_range) {
             // We've found the right leaf LOD node
             found = true;
         }
     }
 
     if (!found) {
-        // No suitable leave node was found, so we need to add one.
+        // No suitable leaf node was found, so we need to add one.
         lodnode = new osg::LOD();
-        lodnode1->addChild(lodnode, 0.0f, 4*RADIUS_LEVEL_1);
-    }
-
+        lodnode1->addChild(lodnode, 0.0f, lod1_range + lod2_range + view_distance + MAX_CLOUD_DEPTH);
+        lodcount++;
+    } 
+    
     transform->setPosition(pos);
-    lodnode->addChild(transform.get(), 0.0f, view_distance);
+    lodnode->addChild(transform.get(), 0.0f, view_distance + MAX_CLOUD_DEPTH);
+    cloudcount++;
+    SG_LOG(SG_ENVIRONMENT, SG_DEBUG, "Impostors: " << impostorcount <<
+                                     " LoD: " << lodcount << 
+                                     " Clouds: " << cloudcount);
 
     lodnode->dirtyBound();
     lodnode1->dirtyBound();
@@ -330,7 +372,7 @@ void SGCloudField::addCloudToTree(osg::ref_ptr<osg::PositionAttitudeTransform> t
         
 bool SGCloudField::deleteCloud(int identifier) {
     osg::ref_ptr<osg::PositionAttitudeTransform> transform = cloud_hash[identifier];
-    if (transform == NULL) return false;
+    if (transform == 0) return false;
         
     removeCloudFromTree(transform);
     cloud_hash.erase(identifier);
@@ -353,7 +395,7 @@ bool SGCloudField::repositionCloud(int identifier, float lon, float lat, float a
     }
 
 bool SGCloudField::isDefined3D(void) {
-    return (cloud_hash.size() > 0);
+    return (! cloud_hash.empty());
 }
 
 SGCloudField::CloudFog::CloudFog() {