]> git.mxchange.org Git - flightgear.git/blobdiff - src/Model/modelmgr.cxx
Merge branch 'next' of git://gitorious.org/fg/flightgear into next
[flightgear.git] / src / Model / modelmgr.cxx
index 0ec617b0b4b9486976681f82292b7346613c50d8..3478a68c5c9bf973a7fe8195c1abe64ad43ae5e2 100644 (file)
@@ -3,13 +3,22 @@
 //
 // This file is in the Public Domain, and comes with no warranty.
 
+#ifdef _MSC_VER
+#  pragma warning( disable: 4355 )
+#endif
+
 #ifdef HAVE_CONFIG_H
 #  include <config.h>
 #endif
 
 #include <simgear/compiler.h>
 
+#include <algorithm>
+#include <functional>
 #include <vector>
+#include <cstring>
+
+#include <osg/Math>
 
 #include <simgear/scene/model/placement.hxx>
 #include <simgear/scene/model/modellib.hxx>
 
 #include "modelmgr.hxx"
 
-SG_USING_STD(vector);
+using std::vector;
+
+using namespace simgear;
 
 // OSGFIXME
 // extern SGShadowVolume *shadows;
 
-
 FGModelMgr::FGModelMgr ()
   : _models(fgGetNode("/models", true)),
     _listener(new Listener(this))
@@ -64,17 +74,12 @@ FGModelMgr::add_model (SGPropertyNode * node)
   SGModelPlacement *model = new SGModelPlacement;
   instance->model = model;
   instance->node = node;
-  SGModelLib *model_lib = globals->get_model_lib();
 
   const char *path = node->getStringValue("path", "Models/Geometry/glider.ac");
   osg::Node *object;
 
   try {
-    object = model_lib->load_model(
-        globals->get_fg_root(),
-        path,
-        globals->get_props(),
-        globals->get_sim_time_sec(), /*cache_object=*/false);
+    object = SGModelLib::loadPagedModel(path, globals->get_props());
   } catch (const sg_throwable& t) {
     SG_LOG(SG_GENERAL, SG_ALERT, "Error loading " << path << ":\n  "
         << t.getFormattedMessage() << t.getOrigin());
@@ -140,38 +145,70 @@ FGModelMgr::unbind ()
 {
 }
 
+namespace
+{
+double testNan(double val) throw (sg_range_exception)
+{
+    if (osg::isNaN(val))
+        throw sg_range_exception("value is nan");
+    return val;
+}
+
+struct UpdateFunctor : public std::unary_function<FGModelMgr::Instance*, void>
+{
+    void operator()(FGModelMgr::Instance* instance) const
+    {
+        SGModelPlacement* model = instance->model;
+        double lon, lat, elev, roll, pitch, heading;
+
+        try {
+            // Optionally set position from properties
+            if (instance->lon_deg_node != 0)
+                lon = testNan(instance->lon_deg_node->getDoubleValue());
+            if (instance->lat_deg_node != 0)
+                lat = testNan(instance->lat_deg_node->getDoubleValue());
+            if (instance->elev_ft_node != 0)
+                elev = testNan(instance->elev_ft_node->getDoubleValue());
+
+            // Optionally set orientation from properties
+            if (instance->roll_deg_node != 0)
+                roll = testNan(instance->roll_deg_node->getDoubleValue());
+            if (instance->pitch_deg_node != 0)
+                pitch = testNan(instance->pitch_deg_node->getDoubleValue());
+            if (instance->heading_deg_node != 0)
+                heading = testNan(instance->heading_deg_node->getDoubleValue());
+        } catch (const sg_range_exception&) {
+            const char *path = instance->node->getStringValue("path",
+                                                              "unknown");
+            SG_LOG(SG_GENERAL, SG_INFO, "Instance of model " << path
+                   << " has invalid values");
+            return;
+        }
+        // Optionally set position from properties
+        if (instance->lon_deg_node != 0)
+            model->setLongitudeDeg(lon);
+        if (instance->lat_deg_node != 0)
+            model->setLatitudeDeg(lat);
+        if (instance->elev_ft_node != 0)
+            model->setElevationFt(elev);
+
+        // Optionally set orientation from properties
+        if (instance->roll_deg_node != 0)
+            model->setRollDeg(roll);
+        if (instance->pitch_deg_node != 0)
+            model->setPitchDeg(pitch);
+        if (instance->heading_deg_node != 0)
+            model->setHeadingDeg(heading);
+
+        instance->model->update();
+    }
+};
+}
+
 void
 FGModelMgr::update (double dt)
 {
-  for (unsigned int i = 0; i < _instances.size(); i++) {
-    Instance * instance = _instances[i];
-    SGModelPlacement * model = instance->model;
-
-                               // Optionally set position from properties
-    if (instance->lon_deg_node != 0)
-      model->setLongitudeDeg(instance->lon_deg_node->getDoubleValue());
-    if (instance->lat_deg_node != 0)
-      model->setLatitudeDeg(instance->lat_deg_node->getDoubleValue());
-    if (instance->elev_ft_node != 0)
-      model->setElevationFt(instance->elev_ft_node->getDoubleValue());
-
-                               // Optionally set orientation from properties
-    if (instance->roll_deg_node != 0)
-      model->setRollDeg(instance->roll_deg_node->getDoubleValue());
-    if (instance->pitch_deg_node != 0)
-      model->setPitchDeg(instance->pitch_deg_node->getDoubleValue());
-    if (instance->heading_deg_node != 0)
-      model->setHeadingDeg(instance->heading_deg_node->getDoubleValue());
-
-    instance->model->update();
-
-    // OSGFIXME
-//     if (shadows && !instance->shadow) {
-//       osg::Node *branch = instance->model->getSceneGraph();
-//       shadows->addOccluder(branch, SGShadowVolume::occluderTypeTileObject);
-//       instance->shadow = true;
-//     }
-  }
+    std::for_each(_instances.begin(), _instances.end(), UpdateFunctor());
 }
 
 void