]> git.mxchange.org Git - flightgear.git/blobdiff - src/Model/model.cxx
Default to glider again when model is not present.
[flightgear.git] / src / Model / model.cxx
index d8cea383f0d7719eb6074a3e22ab4d209efaa99a..f13c8e13a575e39586cdf8adb9146f3830c08e16 100644 (file)
@@ -134,7 +134,7 @@ read_interpolation_table (const SGPropertyNode * props)
   const SGPropertyNode * table_node = props->getNode("interpolation");
   if (table_node != 0) {
     SGInterpTable * table = new SGInterpTable();
-    vector<const SGPropertyNode *> entries = table_node->getChildren("entry");
+    vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
     for (int i = 0; i < entries.size(); i++)
       table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
                      entries[i]->getDoubleValue("dep", 0.0));
@@ -162,12 +162,9 @@ FG3DModel::~FG3DModel ()
   // since the nodes are attached to the scene graph, they'll be
   // deleted automatically
 
-  for (int i = 0; i < _animations.size(); i++) {
-    Animation * tmp = _animations[i];
-    _animations[i] = 0;
-    delete tmp;
-  }
-
+  int i;
+  for (i = 0; i < _animations.size(); i++)
+    delete _animations[i];
 }
 
 void 
@@ -194,39 +191,13 @@ FG3DModel::init (const string &path)
                                 // Assume that textures are in
                                 // the same location as the XML file.
   ssgTexturePath((char *)xmlpath.dir().c_str());
-  _model = ssgLoad((char *)modelpath.c_str());
+  _model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
   if (_model == 0)
     throw sg_exception("Failed to load 3D model");
 
-                                // Load animations
-  vector<SGPropertyNode *> animation_nodes = props.getChildren("animation");
-  for (unsigned int i = 0; i < animation_nodes.size(); i++) {
-    vector<SGPropertyNode *> name_nodes =
-      animation_nodes[i]->getChildren("object-name");
-    if (name_nodes.size() < 1) {
-      SG_LOG(SG_INPUT, SG_ALERT, "No object-name given for transformation");
-    } else {
-      for (unsigned int j = 0; j < name_nodes.size(); j++) {
-        Animation * animation =
-          make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
-        if (animation != 0)
-          _animations.push_back(animation);
-      }
-    }
-  }
-
-                                // Set up the range selector node
-  float ranges[2];
-  ssgRangeSelector * lod = new ssgRangeSelector;
-  lod->addKid(_model);
-  ranges[0] = props.getFloatValue("range/min-m", 0);
-  ranges[1] = props.getFloatValue("range/max-m", 5000);
-  lod->setRanges(ranges, 2);
-
-
                                 // Set up the alignment node
   ssgTransform * align = new ssgTransform;
-  align->addKid(lod);
+  align->addKid(_model);
   sgMat4 rot_matrix;
   sgMat4 off_matrix;
   sgMat4 res_matrix;
@@ -251,10 +222,27 @@ FG3DModel::init (const string &path)
                                 // Set up a location class
   _location = (FGLocation *) new FGLocation;
 
+                                // Load animations
+  vector<SGPropertyNode_ptr> animation_nodes = props.getChildren("animation");
+  unsigned int i;
+  for (i = 0; i < animation_nodes.size(); i++) {
+    vector<SGPropertyNode_ptr> name_nodes =
+      animation_nodes[i]->getChildren("object-name");
+    if (name_nodes.size() < 1) {
+      Animation * animation = make_animation(0, animation_nodes[i]);
+    } else {
+      for (unsigned int j = 0; j < name_nodes.size(); j++) {
+        Animation * animation =
+          make_animation(name_nodes[j]->getStringValue(), animation_nodes[i]);
+        if (animation != 0)
+          _animations.push_back(animation);
+      }
+    }
+  }
 }
 
 void
-FG3DModel::update (int dt)
+FG3DModel::update (double dt)
 {
   unsigned int i;
 
@@ -282,7 +270,7 @@ FG3DModel::update (int dt)
 bool
 FG3DModel::getVisible () const
 {
-  return _selector->getSelect();
+  return (_selector->getSelect() != 0);
 }
 
 void
@@ -354,6 +342,8 @@ FG3DModel::make_animation (const char * object_name,
     animation = new NullAnimation();
   } else if (!strcmp("range", type)) {
     animation = new RangeAnimation();
+  } else if (!strcmp("billboard", type)) {
+    animation = new BillboardAnimation();
   } else if (!strcmp("select", type)) {
     animation = new SelectAnimation();
   } else if (!strcmp("spin", type)) {
@@ -367,15 +357,19 @@ FG3DModel::make_animation (const char * object_name,
     SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
   }
 
-  ssgEntity * object = find_named_node(_model, object_name);
-  if (object == 0) {
-    SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
-    delete animation;
-    animation = 0;
+  ssgEntity * object;
+  if (object_name != 0) {
+    object = find_named_node(_model, object_name);
+    if (object == 0) {
+      SG_LOG(SG_INPUT, SG_WARN, "Object " << object_name << " not found");
+      delete animation;
+      animation = 0;
+    }
   } else {
-    animation->init(object, node);
+    object = _model;
   }
 
+  animation->init(object, node);
   return animation;
 }
 
@@ -418,7 +412,7 @@ FG3DModel::NullAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::NullAnimation::update (int dt)
+FG3DModel::NullAnimation::update (double dt)
 {
 }
 
@@ -451,7 +445,39 @@ FG3DModel::RangeAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::RangeAnimation::update (int dt)
+FG3DModel::RangeAnimation::update (double dt)
+{
+}
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FG3DModel::BillboardAnimation
+////////////////////////////////////////////////////////////////////////
+
+FG3DModel::BillboardAnimation::BillboardAnimation ()
+  : _branch(0)
+{
+  // Note: we cannot allocate the branch until we know whether
+  // it can rotate around the x axis as well as the z axis.
+}
+
+FG3DModel::BillboardAnimation::~BillboardAnimation ()
+{
+  _branch = 0;
+}
+
+void
+FG3DModel::BillboardAnimation::init (ssgEntity * object,
+                                    SGPropertyNode * props)
+{
+  _branch = new ssgCutout(props->getBoolValue("spherical", true));
+  splice_branch(_branch, object);
+  _branch->setName(props->getStringValue("name", 0));
+}
+
+void
+FG3DModel::BillboardAnimation::update (double dt)
 {
 }
 
@@ -486,7 +512,7 @@ FG3DModel::SelectAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::SelectAnimation::update (int dt)
+FG3DModel::SelectAnimation::update (double dt)
 {
   if (_condition != 0 && _condition->test()) 
     _selector->select(0xffff);
@@ -533,9 +559,9 @@ FG3DModel::SpinAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::SpinAnimation::update (int dt)
+FG3DModel::SpinAnimation::update (double dt)
 {
-  float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
+  float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
   _position_deg += (dt * velocity_rpms * 360);
   while (_position_deg < 0)
     _position_deg += 360.0;
@@ -601,7 +627,7 @@ FG3DModel::RotateAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::RotateAnimation::update (int dt)
+FG3DModel::RotateAnimation::update (double dt)
 {
   if (_table == 0) {
     _position_deg = (_prop->getDoubleValue() + _offset_deg) * _factor;
@@ -669,7 +695,7 @@ FG3DModel::TranslateAnimation::init (ssgEntity * object,
 }
 
 void
-FG3DModel::TranslateAnimation::update (int dt)
+FG3DModel::TranslateAnimation::update (double dt)
 {
   if (_table == 0) {
     _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;