]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/material/matmodel.cxx
Merge branch 'jmt/waypt' into next
[simgear.git] / simgear / scene / material / matmodel.cxx
index 3d58d18ebba003be7a27e5158d110d0a24f34a6b..c625d9b0af0e71be2bf17e479969a5d336ba2493 100644 (file)
@@ -28,7 +28,7 @@
 #include <simgear/compiler.h>
 
 #include <map>
-SG_USING_STD(map);
+using std::map;
 
 #include <osg/AlphaFunc>
 #include <osg/Group>
@@ -36,11 +36,8 @@ SG_USING_STD(map);
 #include <osg/StateSet>
 #include <osg/Transform>
 
-#ifdef SG_MATH_EXCEPTION_CLASH
-#  include <math.h>
-#endif
-
 #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>
@@ -48,27 +45,7 @@ SG_USING_STD(map);
 
 #include "matmodel.hxx"
 
-\f
-////////////////////////////////////////////////////////////////////////
-// Local static functions.
-////////////////////////////////////////////////////////////////////////
-
-/**
- * Internal method to test whether a file exists.
- *
- * TODO: this should be moved to a SimGear library of local file
- * functions.
- */
-static inline bool
-local_file_exists( const string& path ) {
-    sg_gzifstream in( path );
-    if ( ! in.is_open() ) {
-       return false;
-    } else {
-       return true;
-    }
-}
-
+using namespace simgear;
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -115,60 +92,25 @@ SGMatModel::~SGMatModel ()
 }
 
 int
-SGMatModel::get_model_count( SGModelLib *modellib,
-                             const string &fg_root,
-                             SGPropertyNode *prop_root,
-                             double sim_time_sec )
+SGMatModel::get_model_count( SGPropertyNode *prop_root )
 {
-  load_models( modellib, fg_root, prop_root, sim_time_sec );
+  load_models( prop_root );
   return _models.size();
 }
 
 inline void
-SGMatModel::load_models ( SGModelLib *modellib,
-                          const string &fg_root,
-                          SGPropertyNode *prop_root,
-                          double sim_time_sec )
+SGMatModel::load_models( SGPropertyNode *prop_root )
 {
                                // Load model only on demand
   if (!_models_loaded) {
     for (unsigned int i = 0; i < _paths.size(); i++) {
-      osg::Node *entity = modellib->load_model( fg_root, _paths[i],
-                                                prop_root, sim_time_sec,
-                                                /*cache_object*/ true );
+      osg::Node *entity = SGModelLib::loadModel(_paths[i], prop_root);
       if (entity != 0) {
         // FIXME: this stuff can be handled
         // in the XML wrapper as well (at least,
         // the billboarding should be handled
         // there).
         
-        // Create multiple LoD nodes so instead of all objects
-        // of the same type appearing at once, some appear further
-        // away.
-        //
-        // Very basic hardcoded distribution:
-        // 4 at normal range
-        // 2 at 1.5 times normal range
-        // 1 at 2 time normal range.
-        //
-        // We achieve this by creating the three different LoD
-        // nodes and adding them to the _models list multiple times.
-        
-        osg::LOD * lod1 = new osg::LOD;
-        lod1->setName("Model LOD");
-        lod1->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
-        lod1->setRange(0, 0, _range_m);
-        
-        osg::LOD * lod15 = new osg::LOD;
-        lod15->setName("Model LOD - 1.5");
-        lod15->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
-        lod15->setRange(0, 0, 1.5 * _range_m);
-
-        osg::LOD * lod2 = new osg::LOD;
-        lod2->setName("Model LOD - 2.0");
-        lod2->setRangeMode(osg::LOD::DISTANCE_FROM_EYE_POINT);
-        lod2->setRange(0, 0, 2.0 * _range_m);
-        
         if (_heading_type == HEADING_BILLBOARD) {
           // if the model is a billboard, it is likely :
           // 1. a branch with only leaves,
@@ -180,27 +122,10 @@ SGMatModel::load_models ( SGModelLib *modellib,
           stateSet->setAttributeAndModes(alphaFunc,
                                          osg::StateAttribute::OVERRIDE);
           stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
-          
-             lod1->addChild(entity);
-             lod15->addChild(entity);
-             lod2->addChild(entity);
-        } else {
-          lod1->addChild(entity);
-          lod15->addChild(entity);
-          lod2->addChild(entity);
-        }
-      
-        // Vary the distribution of LoDs by adding multiple times.
-        _models.push_back(lod1);
-        _models.push_back(lod1);
-        _models.push_back(lod1);
-        _models.push_back(lod1);
-
-        _models.push_back(lod15);
-        _models.push_back(lod15);
-
-        _models.push_back(lod2);
-
+        } 
+        
+        _models.push_back(entity);
+        
       } else {
         SG_LOG(SG_INPUT, SG_ALERT, "Failed to load object " << _paths[i]);
       }
@@ -210,26 +135,13 @@ SGMatModel::load_models ( SGModelLib *modellib,
 }
 
 osg::Node*
-SGMatModel::get_model( int index,
-                       SGModelLib *modellib,
-                       const string &fg_root,
-                       SGPropertyNode *prop_root,
-                       double sim_time_sec )
-{
-  load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
-  return _models[index].get();
-}
-
-osg::Node*
-SGMatModel::get_random_model( SGModelLib *modellib,
-                              const string &fg_root,
-                              SGPropertyNode *prop_root,
-                              double sim_time_sec )
+SGMatModel::get_random_model( SGPropertyNode *prop_root )
 {
-  load_models( modellib, fg_root, prop_root, sim_time_sec ); // comment this out if preloading models
+  load_models( prop_root ); // comment this out if preloading models
   int nModels = _models.size();
-  int index = int(sg_random() * nModels);
-  if (index >= nModels)
+  // int index = int(sg_random() * nModels);
+  static int index = -1;
+  if (++index >= nModels)
     index = 0;
   return _models[index].get();
 }
@@ -240,6 +152,24 @@ SGMatModel::get_coverage_m2 () const
   return _coverage_m2;
 }
 
+double SGMatModel::get_range_m() const
+{
+  return _range_m;
+}
+
+double SGMatModel::get_randomized_range_m(mt* seed) const
+{
+  double lrand = mt_rand(seed);
+  
+  // Note that the LoD is not completely randomized.
+  // 10% at 2   * range_m
+  // 30% at 1.5 * range_m
+  // 60% at 1   * range_m
+  if (lrand < 0.1) return 2   * _range_m;
+  if (lrand < 0.4) return 1.5 * _range_m;
+  else return _range_m;
+}
+
 SGMatModel::HeadingType
 SGMatModel::get_heading_type () const
 {
@@ -289,7 +219,4 @@ SGMatModelGroup::get_object (int index) const
   return _objects[index];
 }
 
-
-
-
 // end of matmodel.cxx