]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/model.cxx
model.[ch]xx:
[simgear.git] / simgear / scene / model / model.cxx
index 645b6fc43fbc33019bd0591f18b3b1c0b990e14b..fe3c5c5b71f49da2e88d2915a26b2a5ef6d6ba67 100644 (file)
 #include <string.h>             // for strcmp()
 
 #include <vector>
+#include <set>
 
 #include <plib/sg.h>
 #include <plib/ssg.h>
 #include <plib/ul.h>
 
 #include <simgear/structure/exception.hxx>
-#include <simgear/misc/sg_path.hxx>
 #include <simgear/props/props.hxx>
 #include <simgear/props/props_io.hxx>
 
@@ -26,8 +26,9 @@
 #include "model.hxx"
 
 SG_USING_STD(vector);
+SG_USING_STD(set);
 
-
+bool sgUseDisplayList = true;
 \f
 ////////////////////////////////////////////////////////////////////////
 // Global state
@@ -122,8 +123,11 @@ sgMakeAnimation( ssgBranch * model,
                  vector<SGPropertyNode_ptr> &name_nodes,
                  SGPropertyNode *prop_root,
                  SGPropertyNode_ptr node,
-                 double sim_time_sec )
+                 double sim_time_sec,
+                 SGPath &texture_path,
+                 set<ssgBranch *> &ignore_branches )
 {
+  bool ignore = false;
   SGAnimation * animation = 0;
   const char * type = node->getStringValue("type", "none");
   if (!strcmp("none", type)) {
@@ -152,12 +156,19 @@ sgMakeAnimation( ssgBranch * model,
     animation = new SGTexMultipleAnimation(prop_root, node);
   } else if (!strcmp("blend", type)) {
     animation = new SGBlendAnimation(prop_root, node);
+    ignore = true;
   } else if (!strcmp("alpha-test", type)) {
     animation = new SGAlphaTestAnimation(node);
+  } else if (!strcmp("material", type)) {
+    animation = new SGMaterialAnimation(prop_root, node, texture_path);
   } else if (!strcmp("flash", type)) {
     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);
@@ -204,19 +215,23 @@ sgMakeAnimation( ssgBranch * model,
     branch->setUserData(animation);
     branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
     branch->setTravCallback(SSG_CALLBACK_POSTTRAV, restore_callback);
+    if ( ignore ) {
+      ignore_branches.insert( branch );
+    }
   }
 }
 
 
-static void makeDList( ssgBranch *b )
+static void makeDList( ssgBranch *b, const set<ssgBranch *> &ignore )
 {
   int nb = b->getNumKids();
   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())) {
-      makeDList( (ssgBranch*)e );
+    } else if (e->isAKindOf(ssgTypeBranch()) && ignore.find((ssgBranch *)e) == ignore.end()) {
+      makeDList( (ssgBranch*)e, ignore );
     }
   }
 }
@@ -230,7 +245,8 @@ static void makeDList( ssgBranch *b )
 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;
@@ -268,13 +284,9 @@ 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()));
   }
-
-  if ( model != 0 ) {
-    makeDList( model );
-  }
-
                                 // Set up the alignment node
   ssgTransform * alignmainmodel = new ssgTransform;
   if ( load_panel == 0 )
@@ -292,18 +304,6 @@ sgLoad3DModel( const string &fg_root, const string &path,
 
   unsigned int i;
 
-  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);
-    }
-  }
-
                                 // Load sub-models
   vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
   for (i = 0; i < model_nodes.size(); i++) {
@@ -326,16 +326,43 @@ sgLoad3DModel( const string &fg_root, const string &path,
     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) {
+    data->modelLoaded(path, &props, model);
+    model->setUserData(data);
+  }
                                 // Load animations
+  set<ssgBranch *> ignore_branches;
   vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
   for (i = 0; i < animation_nodes.size(); i++) {
     const char * name = animation_nodes[i]->getStringValue("name", 0);
     vector<SGPropertyNode_ptr> name_nodes =
       animation_nodes[i]->getChildren("object-name");
     sgMakeAnimation( model, name, name_nodes, prop_root, animation_nodes[i],
-                     sim_time_sec);
+                     sim_time_sec, texturepath, ignore_branches);
   }
 
+#if PLIB_VERSION > 183
+  if ( model != 0 && sgUseDisplayList ) {
+     makeDList( model, ignore_branches );
+  }
+#endif
+
+  int m = props.getIntValue("dump", 0);
+  if (m > 0)
+    model->print(stderr, "", m - 1);
+
   return alignmainmodel;
 }
 
@@ -347,5 +374,10 @@ sgSetModelFilter( bool filter )
   return old;
 }
 
+bool 
+sgCheckAnimationBranch (ssgEntity * entity)
+{
+    return entity->getTravCallback(SSG_CALLBACK_PRETRAV) == animation_callback;
+}
 
 // end of model.cxx