]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/newcloud.cxx
Added some OSG headers for the correct evaluation of the OSG_VERSION_LESS_THAN macro.
[simgear.git] / simgear / scene / sky / newcloud.cxx
index 3dee4ab479e90bba6a2f90e20fe8457440383c1f..18b7710c20115ca4f07f42584fdce8f678066997 100644 (file)
 
 #include <simgear/compiler.h>
 
-#include <simgear/math/sg_random.h>
 #include <simgear/misc/sg_path.hxx>
-#include <simgear/scene/util/PathOptions.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/scene/model/model.hxx>
 #include <simgear/scene/util/SGReaderWriterOptions.hxx>
 #include <simgear/scene/util/StateAttributeFactory.hxx>
 #include <simgear/scene/util/SGUpdateVisitor.hxx>
+#include <simgear/scene/util/RenderConstants.hxx>
 
 #include <algorithm>
 #include <osg/BlendFunc>
@@ -65,14 +64,18 @@ using namespace std;
 
 namespace
 {
-typedef std::map<std::string, osg::ref_ptr<Effect> > EffectMap;
+typedef std::map<std::string, osg::observer_ptr<Effect> > EffectMap;
 EffectMap effectMap;
 }
 
 float SGNewCloud::sprite_density = 1.0;
 
-SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
+SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def, mt* s)
 {
+    // Set up the RNG with the passed in seed. This allows us to make the RNG repeatable
+    // if required.
+    seed = s;
+  
     min_width = cld_def->getFloatValue("min-cloud-width-m", 500.0);
     max_width = cld_def->getFloatValue("max-cloud-width-m", min_width*2);
     min_height = cld_def->getFloatValue("min-cloud-height-m", 400.0);
@@ -103,21 +106,26 @@ SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def
 
     // Create a new Effect for the texture, if required.
     EffectMap::iterator iter = effectMap.find(texture);
-    if (iter == effectMap.end()) {
+
+    if ((iter == effectMap.end())||
+        (!iter->second.lock(effect)))
+    {
         SGPropertyNode_ptr pcloudEffect = new SGPropertyNode;
         makeChild(pcloudEffect, "inherits-from")->setValue("Effects/cloud");
         setValue(makeChild(makeChild(makeChild(pcloudEffect, "parameters"),
                                      "texture"),
                            "image"),
                  texture);
-        ref_ptr<osgDB::Options> options
-            = makeOptionsFromPath(texture_root);
-        ref_ptr<SGReaderWriterOptions> sgOptions
-            = new SGReaderWriterOptions(*options.get());
-        if ((effect = makeEffect(pcloudEffect, true, sgOptions.get())))
-            effectMap.insert(EffectMap::value_type(texture, effect));
-    } else {
-        effect = iter->second.get();
+        ref_ptr<SGReaderWriterOptions> options;
+        options = SGReaderWriterOptions::fromPath(texture_root.str());
+        effect = makeEffect(pcloudEffect, true, options.get());
+        if (effect.valid())
+        {
+            if (iter == effectMap.end())
+                effectMap.insert(EffectMap::value_type(texture, effect));
+            else
+                iter->second = effect; // update existing, but empty observer
+        }
     }
 }
 
@@ -127,7 +135,7 @@ SGNewCloud::~SGNewCloud() {
 #if 0
 // return a random number between -n/2 and n/2, tending to 0
 static float Rnd(float n) {
-    return n * (-0.5f + (sg_random() + sg_random()) / 2.0f);
+    return n * (-0.5f + (mt_rand(seed) + mt_rand(seed)) / 2.0f);
 }
 #endif
 
@@ -138,17 +146,17 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
     // Determine how big this specific cloud instance is. Note that we subtract
     // the sprite size because the width/height is used to define the limits of
     // the center of the sprites, not their edges.
-    float width = min_width + sg_random() * (max_width - min_width) - min_sprite_width;
-    float height = min_height + sg_random() * (max_height - min_height) - min_sprite_height;
+    float width = min_width + mt_rand(seed) * (max_width - min_width) - min_sprite_width;
+    float height = min_height + mt_rand(seed) * (max_height - min_height) - min_sprite_height;
     
     if (width  < 0.0) { width  = 0.0; }
     if (height < 0.0) { height = 0.0; }
     
     // Determine appropriate shading factors
-    float top_factor = min_top_lighting_factor + sg_random() * (max_top_lighting_factor - min_top_lighting_factor);
-    float middle_factor = min_middle_lighting_factor + sg_random() * (max_middle_lighting_factor - min_middle_lighting_factor);
-    float bottom_factor = min_bottom_lighting_factor + sg_random() * (max_bottom_lighting_factor - min_bottom_lighting_factor);
-    float shade_factor = min_shade_lighting_factor + sg_random() * (max_shade_lighting_factor - min_shade_lighting_factor);
+    float top_factor = min_top_lighting_factor + mt_rand(seed) * (max_top_lighting_factor - min_top_lighting_factor);
+    float middle_factor = min_middle_lighting_factor + mt_rand(seed) * (max_middle_lighting_factor - min_middle_lighting_factor);
+    float bottom_factor = min_bottom_lighting_factor + mt_rand(seed) * (max_bottom_lighting_factor - min_bottom_lighting_factor);
+    float shade_factor = min_shade_lighting_factor + mt_rand(seed) * (max_shade_lighting_factor - min_shade_lighting_factor);
     
     //printf("Cloud: %2f, %2f, %2f, %2f\n", top_factor, middle_factor, bottom_factor, shade_factor); 
     
@@ -168,7 +176,7 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
     float cull_distance_squared = min_sprite_height * min_sprite_height * 0.1f;
     
     // The number of sprites we actually use is a function of the (user-controlled) density
-    int n_sprites = num_sprites * sprite_density * (0.5f + sg_random());
+    int n_sprites = num_sprites * sprite_density * (0.5f + mt_rand(seed));
     
     for (int i = 0; i < n_sprites; i++)
     {
@@ -186,16 +194,16 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
             y = 0;
             z = height * 0.5;
         } else {
-            float theta = sg_random() * SGD_2PI;
-            float elev  = sg_random() * SGD_PI;
+            float theta = mt_rand(seed) * SGD_2PI;
+            float elev  = mt_rand(seed) * SGD_PI;
             x = width * cos(theta) * 0.5f * sin(elev);
             y = width * sin(theta) * 0.5f * sin(elev);
             z = height * cos(elev) * 0.5f + height * 0.5f;
         }
         
         // Determine the height and width
-        float sprite_width = min_sprite_width + sg_random() * (max_sprite_width - min_sprite_width);
-        float sprite_height = min_sprite_height + sg_random() * (max_sprite_height - min_sprite_height);
+        float sprite_width = min_sprite_width + mt_rand(seed) * (max_sprite_width - min_sprite_width);
+        float sprite_height = min_sprite_height + mt_rand(seed) * (max_sprite_height - min_sprite_height);
         
         // Sprites are never taller than square.
         if (sprite_height > sprite_width )
@@ -218,10 +226,10 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
         }        
 
         // Determine the sprite texture indexes.
-        int index_x = (int) floor(sg_random() * num_textures_x);
+        int index_x = (int) floor(mt_rand(seed) * num_textures_x);
         if (index_x >= num_textures_x) { index_x = num_textures_x - 1; }
                 
-        int index_y = (int) floor(sg_random() * num_textures_y);
+        int index_y = (int) floor(mt_rand(seed) * num_textures_y);
         
         if (height_map_texture) {
           // The y index depends on the position of the sprite within the cloud.
@@ -244,6 +252,7 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
     geode->addDrawable(sg);
     geode->setName("3D cloud");
     geode->setEffect(effect.get());
+    geode->setNodeMask( ~simgear::MODELLIGHT_BIT );
     
     return geode;
 }