]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/matmodel.cxx
Display random objects independently of buildings
[simgear.git] / simgear / scene / material / matmodel.cxx
index c625d9b0af0e71be2bf17e479969a5d336ba2493..84dd923745dd815abad052be3ee5e7138e94f9a4 100644 (file)
@@ -28,7 +28,7 @@
 #include <simgear/compiler.h>
 
 #include <map>
-using std::map;
+
 
 #include <osg/AlphaFunc>
 #include <osg/Group>
@@ -37,7 +37,6 @@ using std::map;
 #include <osg/Transform>
 
 #include <simgear/debug/logstream.hxx>
-#include <simgear/math/SGMath.hxx>
 #include <simgear/math/sg_random.h>
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/sgstream.hxx>
@@ -46,8 +45,8 @@ using std::map;
 #include "matmodel.hxx"
 
 using namespace simgear;
-
-\f
+using std::string;
+using std::map;\f
 ////////////////////////////////////////////////////////////////////////
 // Implementation of SGMatModel.
 ////////////////////////////////////////////////////////////////////////
@@ -55,6 +54,7 @@ using namespace simgear;
 SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
   : _models_loaded(false),
     _coverage_m2(node->getDoubleValue("coverage-m2", 1000000)),
+    _spacing_m(node->getDoubleValue("spacing-m", 20)),
     _range_m(range_m)
 {
                                // Sanity check
@@ -65,7 +65,7 @@ SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
   }
 
                                // Note all the model paths
-  vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
+  std::vector <SGPropertyNode_ptr> path_nodes = node->getChildren("path");
   for (unsigned int i = 0; i < path_nodes.size(); i++)
     _paths.push_back(path_nodes[i]->getStringValue());
 
@@ -77,6 +77,8 @@ SGMatModel::SGMatModel (const SGPropertyNode * node, double range_m)
     _heading_type = HEADING_BILLBOARD;
   } else if (hdg == "random") {
     _heading_type = HEADING_RANDOM;
+  } else if (hdg == "mask") {
+    _heading_type = HEADING_MASK;
   } else {
     _heading_type = HEADING_FIXED;
     SG_LOG(SG_INPUT, SG_ALERT, "Unknown heading type: " << hdg
@@ -128,6 +130,8 @@ SGMatModel::load_models( SGPropertyNode *prop_root )
         
       } else {
         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
+        // Ensure the vector contains something, otherwise get_random_model below fails
+        _models.push_back(new osg::Node());
       }
     }
   }
@@ -135,15 +139,11 @@ SGMatModel::load_models( SGPropertyNode *prop_root )
 }
 
 osg::Node*
-SGMatModel::get_random_model( SGPropertyNode *prop_root )
+SGMatModel::get_random_model( SGPropertyNode *prop_root, mt* seed )
 {
   load_models( prop_root ); // comment this out if preloading models
   int nModels = _models.size();
-  // int index = int(sg_random() * nModels);
-  static int index = -1;
-  if (++index >= nModels)
-    index = 0;
-  return _models[index].get();
+  return _models[mt_rand(seed) * nModels].get();
 }
 
 double
@@ -157,6 +157,11 @@ double SGMatModel::get_range_m() const
   return _range_m;
 }
 
+double SGMatModel::get_spacing_m() const
+{
+  return _spacing_m;
+}
+
 double SGMatModel::get_randomized_range_m(mt* seed) const
 {
   double lrand = mt_rand(seed);
@@ -182,11 +187,11 @@ SGMatModel::get_heading_type () const
 // Implementation of SGMatModelGroup.
 ////////////////////////////////////////////////////////////////////////
 
-SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node)
-  : _range_m(node->getDoubleValue("range-m", 2000))
+SGMatModelGroup::SGMatModelGroup (SGPropertyNode * node, float default_object_range)
+  : _range_m(node->getDoubleValue("range-m", default_object_range))
 {
                                // Load the object subnodes
-  vector<SGPropertyNode_ptr> object_nodes =
+  std::vector<SGPropertyNode_ptr> object_nodes =
     ((SGPropertyNode *)node)->getChildren("object");
   for (unsigned int i = 0; i < object_nodes.size(); i++) {
     const SGPropertyNode * object_node = object_nodes[i];