]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/model.cxx
- don't need a guarded pointer here
[simgear.git] / simgear / scene / model / model.cxx
index 676f834cf3305f2b2478682e8f004df0a4b65784..622e8fcd8c5025fff286c0aead8838dfaaa62891 100644 (file)
@@ -7,8 +7,6 @@
 #include <simgear_config.h>
 #endif
 
-#include <simgear/compiler.h>
-
 #include <string.h>             // for strcmp()
 
 #include <vector>
@@ -21,6 +19,7 @@
 #include <simgear/structure/exception.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
+#include <simgear/props/condition.hxx>
 
 #include "animation.hxx"
 #include "model.hxx"
@@ -66,6 +65,23 @@ restore_callback (ssgEntity * entity, int mask)
     return 1;
 }
 
+/**
+ * Callback and userData class for conditional rendering.
+ */
+class SGConditionalRender : public ssgBase {
+public:
+  SGConditionalRender(SGCondition *c) : _condition(c) {}
+  bool test() { return _condition->test(); }
+private:
+  SGCondition *_condition;
+};
+
+static int
+model_condition_callback (ssgEntity * entity, int mask)
+{
+  return ((SGConditionalRender *)entity->getUserData())->test();
+}
+
 
 /**
  * Locate a named SSG node in a branch.
@@ -165,6 +181,10 @@ sgMakeAnimation( ssgBranch * model,
     animation = new SGFlashAnimation(node);
   } else if (!strcmp("dist-scale", type)) {
     animation = new SGDistScaleAnimation(node);
+  } else if (!strcmp("noshadow", type)) {
+    animation = new SGShadowAnimation(prop_root, node);
+  } else if (!strcmp("shader", type)) {
+    animation = new SGShaderAnimation(prop_root, node);
   } else {
     animation = new SGNullAnimation(node);
     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
@@ -224,6 +244,7 @@ static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
   for (int i = 0; i<nb; i++) {
     ssgEntity *e = b->getKid(i);
     if (e->isAKindOf(ssgTypeLeaf())) {
+     if( ((ssgLeaf*)e)->getNumVertices() > 0)
       ((ssgLeaf*)e)->makeDList();
     } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
       makeDList( (ssgBranch*)e, ignore );
@@ -231,6 +252,13 @@ static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
   }
 }
 
+class SGLoaderOptions : public ssgLoaderOptions {
+public:
+  SGLoaderOptions() { ssgSetCurrentOptions( this ); } // Install our own loader options at startup
+  void endLoad() {} // Avoid clearing the texture cache after every model load
+};
+
+static SGLoaderOptions loaderOptions;
 
 \f
 ////////////////////////////////////////////////////////////////////////
@@ -240,7 +268,8 @@ static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
 ssgBranch *
 sgLoad3DModel( const string &fg_root, const string &path,
                SGPropertyNode *prop_root,
-               double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *) )
+               double sim_time_sec, ssgEntity *(*load_panel)(SGPropertyNode *),
+               SGModelData *data )
 {
   ssgBranch * model = 0;
   SGPropertyNode props;
@@ -278,7 +307,8 @@ sgLoad3DModel( const string &fg_root, const string &path,
     ssgTexturePath((char *)texturepath.c_str());
     model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
     if (model == 0)
-      throw sg_exception("Failed to load 3D model");
+      throw sg_io_exception("Failed to load 3D model", 
+                         sg_location(modelpath.str()));
   }
                                 // Set up the alignment node
   ssgTransform * alignmainmodel = new ssgTransform;
@@ -312,13 +342,43 @@ sgLoad3DModel( const string &fg_root, const string &path,
                         node->getFloatValue("offsets/z-m", 0.0));
     align->setTransform(res_matrix);
 
-    ssgBranch * kid = sgLoad3DModel( fg_root, node->getStringValue("path"),
-                                     prop_root, sim_time_sec, load_panel );
+    ssgBranch * kid;
+    const char * submodel = node->getStringValue("path");
+    try {
+      kid = sgLoad3DModel( fg_root, submodel, prop_root, sim_time_sec, load_panel );
+
+    } catch (const sg_throwable &t) {
+      SG_LOG(SG_INPUT, SG_ALERT, "Failed to load submodel: " << t.getFormattedMessage());
+      throw;
+    }
+
+    SGPropertyNode *cond = node->getNode("condition", false);
+    if (cond) {
+      align->setUserData(new SGConditionalRender(sgReadCondition(prop_root, cond)));
+      align->setTravCallback(SSG_CALLBACK_PRETRAV, model_condition_callback);
+    }
+
     align->addKid(kid);
     align->setName(node->getStringValue("name", ""));
     model->addKid(align);
   }
 
+  if ( load_panel ) {
+                                // Load panels
+    vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
+    for (i = 0; i < panel_nodes.size(); i++) {
+        SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
+        ssgEntity * panel = load_panel(panel_nodes[i]);
+        if (panel_nodes[i]->hasValue("name"))
+            panel->setName((char *)panel_nodes[i]->getStringValue("name"));
+        model->addKid(panel);
+    }
+  }
+
+  if (data) {
+    alignmainmodel->setUserData(data);
+    data->modelLoaded(path, &props, alignmainmodel);
+  }
                                 // Load animations
   set<ssgBranch *> ignore_branches;
   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
@@ -336,17 +396,9 @@ sgLoad3DModel( const string &fg_root, const string &path,
   }
 #endif
 
-  if ( load_panel ) {
-                                // Load panels
-    vector<SGPropertyNode_ptr> panel_nodes = props.getChildren("panel");
-    for (i = 0; i < panel_nodes.size(); i++) {
-        SG_LOG(SG_INPUT, SG_DEBUG, "Loading a panel");
-        ssgEntity * panel = load_panel(panel_nodes[i]);
-        if (panel_nodes[i]->hasValue("name"))
-            panel->setName((char *)panel_nodes[i]->getStringValue("name"));
-        model->addKid(panel);
-    }
-  }
+  int m = props.getIntValue("dump", 0);
+  if (m > 0)
+    model->print(stderr, "", m - 1);
 
   return alignmainmodel;
 }
@@ -359,5 +411,10 @@ sgSetModelFilter( bool filter )
   return old;
 }
 
+bool 
+sgCheckAnimationBranch (ssgEntity * entity)
+{
+    return entity->getTravCallback(SSG_CALLBACK_PRETRAV) == animation_callback;
+}
 
 // end of model.cxx