]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/sky/newcloud.cxx
hla: Provide a directly property based api for property data element.
[simgear.git] / simgear / scene / sky / newcloud.cxx
index 162f137a0c58c38941c449d99cbdd2d27fe88f1d..3b5321f8cc759029639df66930cddf5637ff7be9 100644 (file)
 
 #include <simgear/compiler.h>
 
-#include <plib/sg.h>
 #include <simgear/math/sg_random.h>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/PathOptions.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/scene/model/model.hxx>
+#include <simgear/scene/model/SGReaderWriterXMLOptions.hxx>
 #include <simgear/scene/util/StateAttributeFactory.hxx>
 #include <simgear/scene/util/SGUpdateVisitor.hxx>
 
@@ -69,36 +69,24 @@ EffectMap effectMap;
 
 double SGNewCloud::sprite_density = 1.0;
 
-SGNewCloud::SGNewCloud(string type,
-                       const SGPath &tex_path, 
-                       string tex,
-                       double min_w,
-                       double max_w,
-                       double min_h,
-                       double max_h,
-                       double min_sprite_w,
-                       double max_sprite_w,
-                       double min_sprite_h,
-                       double max_sprite_h,
-                       double b,
-                       int n,
-                       int nt_x,
-                       int nt_y) :
-        min_width(min_w),
-        max_width(max_w),
-        min_height(min_h),
-        max_height(max_h),
-        min_sprite_width(min_sprite_w),
-        max_sprite_width(max_sprite_w),
-        min_sprite_height(min_sprite_h),
-        max_sprite_height(max_sprite_h),
-        bottom_shade(b),
-        num_sprites(n),
-        num_textures_x(nt_x),
-        num_textures_y(nt_y),
-        texture(tex),
-        name(type)
+SGNewCloud::SGNewCloud(const SGPath &texture_root, const SGPropertyNode *cld_def)
 {
+    min_width = cld_def->getDoubleValue("min-cloud-width-m", 500.0);
+    max_width = cld_def->getDoubleValue("max-cloud-width-m", min_width*2);
+    min_height = cld_def->getDoubleValue("min-cloud-height-m", 400.0);
+    max_height = cld_def->getDoubleValue("max-cloud-height-m", min_height*2);
+    min_sprite_width = cld_def->getDoubleValue("min-sprite-width-m", 200.0);
+    max_sprite_width = cld_def->getDoubleValue("max-sprite-width-m", min_sprite_width*1.5);
+    min_sprite_height = cld_def->getDoubleValue("min-sprite-height-m", 150);
+    max_sprite_height = cld_def->getDoubleValue("max-sprite-height-m", min_sprite_height*1.5);
+    num_sprites = cld_def->getIntValue("num-sprites", 20);
+    num_textures_x = cld_def->getIntValue("num-textures-x", 4);
+    num_textures_y = cld_def->getIntValue("num-textures-y", 4);
+    height_map_texture = cld_def->getBoolValue("height-map-texture", false);
+    bottom_shade = cld_def->getDoubleValue("bottom-shade", 1.0);
+    zscale = cld_def->getDoubleValue("z-scale", 1.0);
+    texture = cld_def->getStringValue("texture", "cl_cumulus.png");
+
     // Create a new Effect for the texture, if required.
     EffectMap::iterator iter = effectMap.find(texture);
     if (iter == effectMap.end()) {
@@ -108,9 +96,11 @@ SGNewCloud::SGNewCloud(string type,
                                      "texture"),
                            "image"),
                  texture);
-        osg::ref_ptr<osgDB::ReaderWriter::Options> options
-            = makeOptionsFromPath(tex_path);
-        if ((effect = makeEffect(pcloudEffect, true, options)))
+        ref_ptr<osgDB::ReaderWriter::Options> options
+            = makeOptionsFromPath(texture_root);
+        ref_ptr<SGReaderWriterXMLOptions> sgOptions
+            = new SGReaderWriterXMLOptions(*options.get());
+        if ((effect = makeEffect(pcloudEffect, true, sgOptions.get())))
             effectMap.insert(EffectMap::value_type(texture, effect));
     } else {
         effect = iter->second.get();
@@ -177,7 +167,11 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
     
     osg::ref_ptr<EffectGeode> geode = new EffectGeode;
         
-    CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, num_textures_y, max_width, max_height);
+    CloudShaderGeometry* sg = new CloudShaderGeometry(num_textures_x, 
+                                                      num_textures_y, 
+                                                      max_width + max_sprite_width, 
+                                                      max_height + max_sprite_height, 
+                                                      zscale);
     
     // 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
@@ -197,26 +191,28 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
         // Determine the position of the sprite. Rather than being completely random,
         // we place them on the surface of a distorted sphere. However, we place
         // the first sprite in the center of the sphere (and at maximum size) to
-       // ensure good coverage and reduce the chance of there being "holes" in our
+             // ensure good coverage and reduce the chance of there being "holes" in the
+             // middle of our cloud. Also note that (0,0,0) defines the _bottom_ of the
+             // cloud, not the middle. 
 
         float x, y, z;
 
         if (i == 0) {
             x = 0;
             y = 0;
-            z = 0;
+            z = height * 0.5;
         } else {
             double theta = sg_random() * SGD_2PI;
             double elev  = sg_random() * SGD_PI;
             x = width * cos(theta) * 0.5f * sin(elev);
             y = width * sin(theta) * 0.5f * sin(elev);
-            z = height * cos(elev) * 0.5f;
+            z = height * cos(elev) * 0.5f + height * 0.5f;
         }
         
         // Determine the height and width as scaling factors on the minimum size (used to create the quad).
         float sprite_width = 1.0f + sg_random() * (max_sprite_width - min_sprite_width) / min_sprite_width;
         float sprite_height = 1.0f + sg_random() * (max_sprite_height - min_sprite_height) / min_sprite_height;
-
+        
         // Sprites are never taller than square.
         if (sprite_height * min_sprite_height > sprite_width * min_sprite_width)
         {
@@ -228,17 +224,31 @@ osg::ref_ptr<EffectGeode> SGNewCloud::genCloud() {
             sprite_width = 1.0f + (max_sprite_width - min_sprite_width) / min_sprite_width;
             sprite_height = 1.0f + (max_sprite_height - min_sprite_height) / min_sprite_height;
         }
+        
+        // If the center of the sprite is less than half the sprite heightthe sprite will extend 
+        // below the bottom of the cloud and must be shifted upwards. This is particularly important 
+        // for cumulus clouds which have a very well defined base.
+        if (z < 0.5f * sprite_height * min_sprite_height)
+        {
+            z = 0.5f * sprite_height * min_sprite_height;          
+        }        
 
         // Determine the sprite texture indexes.
         int index_x = (int) floor(sg_random() * num_textures_x);
         if (index_x == num_textures_x) { index_x--; }
-
-        // The y index depends on the positing of the sprite within the cloud.
-        // This allows cloud designers to have particular sprites for the base
-        // and tops of the cloud.
-        int index_y = (int) floor((z / height + 0.5f) * num_textures_y);
+                
+        int index_y = (int) floor(sg_random() * num_textures_y);
+        
+        if (height_map_texture) {
+          // The y index depends on the position of the sprite within the cloud.
+          // This allows cloud designers to have particular sprites for the base
+          // and tops of the cloud.
+          index_y = (int) floor((z / height + 0.5f) * num_textures_y);
+        }
+        
         if (index_y == num_textures_y) { index_y--; }
         
+        
         sg->addSprite(SGVec3f(x, y, z), 
                     index_x, 
                     index_y,