]> git.mxchange.org Git - flightgear.git/blobdiff - src/Model/model.cxx
Make sure that all elapsed time gets passed to update when a subsystem
[flightgear.git] / src / Model / model.cxx
index d8cea383f0d7719eb6074a3e22ab4d209efaa99a..00773806e5d0ff90359812115550497734126954 100644 (file)
@@ -11,6 +11,7 @@
 
 #include <plib/sg.h>
 #include <plib/ssg.h>
+#include <plib/ul.h>
 
 #include <simgear/compiler.h>
 #include <simgear/debug/logstream.hxx>
@@ -26,6 +27,7 @@
 #include <Scenery/scenery.hxx>
 
 #include "model.hxx"
+#include "panelnode.hxx"
 
 
 \f
 // Static utility functions.
 ////////////////////////////////////////////////////////////////////////
 
+/**
+ * Callback to update an animation.
+ */
+static int
+animation_callback (ssgEntity * entity, int mask)
+{
+    ((Animation *)entity->getUserData())->update();
+    return true;
+}
+
+
 /**
  * Locate a named SSG node in a branch.
  */
@@ -125,19 +138,34 @@ set_translation (sgMat4 &matrix, double position_m, sgVec3 &axis)
 }
 
 
+/**
+ * Make an offset matrix from rotations and position offset.
+ */
+static void
+make_offsets_matrix (sgMat4 * result, double h_rot, double p_rot, double r_rot,
+                     double x_off, double y_off, double z_off)
+{
+  sgMat4 rot_matrix;
+  sgMat4 pos_matrix;
+  sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
+  sgMakeTransMat4(pos_matrix, x_off, y_off, z_off);
+  sgMultMat4(*result, pos_matrix, rot_matrix);
+}
+
+
 /**
  * Read an interpolation table from properties.
  */
 static SGInterpTable *
-read_interpolation_table (const SGPropertyNode * props)
+read_interpolation_table (SGPropertyNode_ptr props)
 {
-  const SGPropertyNode * table_node = props->getNode("interpolation");
+  SGPropertyNode_ptr table_node = props->getNode("interpolation");
   if (table_node != 0) {
     SGInterpTable * table = new SGInterpTable();
-    vector<const SGPropertyNode *> entries = table_node->getChildren("entry");
-    for (int i = 0; i < entries.size(); i++)
+    vector<SGPropertyNode_ptr> entries = table_node->getChildren("entry");
+    for (unsigned int i = 0; i < entries.size(); i++)
       table->addEntry(entries[i]->getDoubleValue("ind", 0.0),
-                     entries[i]->getDoubleValue("dep", 0.0));
+                      entries[i]->getDoubleValue("dep", 0.0));
     return table;
   } else {
     return 0;
@@ -145,40 +173,94 @@ read_interpolation_table (const SGPropertyNode * props)
 }
 
 
-\f
-////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel
-////////////////////////////////////////////////////////////////////////
-
-FG3DModel::FG3DModel ()
-  : _model(0),
-    _selector(new ssgSelector),
-    _position(new ssgTransform)
-{
-}
-
-FG3DModel::~FG3DModel ()
+static void
+make_animation (ssgBranch * model,
+                const char * name,
+                vector<SGPropertyNode_ptr> &name_nodes,
+                SGPropertyNode_ptr node)
 {
-  // since the nodes are attached to the scene graph, they'll be
-  // deleted automatically
+  Animation * animation = 0;
+  const char * type = node->getStringValue("type", "none");
+  if (!strcmp("none", type)) {
+    animation = new NullAnimation(node);
+  } else if (!strcmp("range", type)) {
+    animation = new RangeAnimation(node);
+  } else if (!strcmp("billboard", type)) {
+    animation = new BillboardAnimation(node);
+  } else if (!strcmp("select", type)) {
+    animation = new SelectAnimation(node);
+  } else if (!strcmp("spin", type)) {
+    animation = new SpinAnimation(node);
+  } else if (!strcmp("timed", type)) {
+    animation = new TimedAnimation(node);
+  } else if (!strcmp("rotate", type)) {
+    animation = new RotateAnimation(node);
+  } else if (!strcmp("translate", type)) {
+    animation = new TranslateAnimation(node);
+  } else {
+    animation = new NullAnimation(node);
+    SG_LOG(SG_INPUT, SG_WARN, "Unknown animation type " << type);
+  }
 
-  for (int i = 0; i < _animations.size(); i++) {
-    Animation * tmp = _animations[i];
-    _animations[i] = 0;
-    delete tmp;
+  if (name != 0)
+      animation->setName((char *)name);
+
+  ssgEntity * object;
+  if (name_nodes.size() > 0) {
+    object = find_named_node(model, name_nodes[0]->getStringValue());
+    if (object == 0) {
+      SG_LOG(SG_INPUT, SG_WARN, "Object " << name_nodes[0]->getStringValue()
+             << " not found");
+      delete animation;
+      animation = 0;
+    }
+  } else {
+    object = model;
+  }
+  
+  ssgBranch * branch = animation->getBranch();
+  splice_branch(branch, object);
+
+  for (int i = 1; i < name_nodes.size(); i++) {
+      const char * name = name_nodes[i]->getStringValue();
+      object = find_named_node(model, name);
+      if (object == 0) {
+          SG_LOG(SG_INPUT, SG_WARN, "Object " << name << " not found");
+          delete animation;
+          animation = 0;
+      }
+      ssgBranch * oldParent = object->getParent(0);
+      branch->addKid(object);
+      oldParent->removeKid(object);
   }
 
+  animation->init();
+  branch->setUserData(animation);
+  branch->setTravCallback(SSG_CALLBACK_PRETRAV, animation_callback);
 }
 
-void 
-FG3DModel::init (const string &path)
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Global functions.
+////////////////////////////////////////////////////////////////////////
+
+ssgBranch *
+fgLoad3DModel (const string &path)
 {
+  ssgBranch * model = 0;
   SGPropertyNode props;
 
                                 // Load the 3D aircraft object itself
-  SGPath xmlpath = globals->get_fg_root();
+  SGPath xmlpath;
   SGPath modelpath = path;
-  xmlpath.append(modelpath.str());
+  if ( ulIsAbsolutePathName( path.c_str() ) ) {
+    xmlpath = modelpath;
+  }
+  else {
+    xmlpath = globals->get_fg_root();
+    xmlpath.append(modelpath.str());
+  }
 
                                 // Check for an XML wrapper
   if (xmlpath.str().substr(xmlpath.str().size() - 4, 4) == ".xml") {
@@ -187,410 +269,272 @@ FG3DModel::init (const string &path)
       modelpath = modelpath.dir();
       modelpath.append(props.getStringValue("/path"));
     } else {
-      throw sg_exception("No path for model");
+      if (model == 0)
+        model = new ssgBranch;
     }
   }
 
                                 // Assume that textures are in
                                 // the same location as the XML file.
-  ssgTexturePath((char *)xmlpath.dir().c_str());
-  _model = 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);
-      }
-    }
+  if (model == 0) {
+    ssgTexturePath((char *)xmlpath.dir().c_str());
+    model = (ssgBranch *)ssgLoad((char *)modelpath.c_str());
+    if (model == 0)
+      throw sg_exception("Failed to load 3D model");
   }
 
-                                // 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);
-  sgMat4 rot_matrix;
-  sgMat4 off_matrix;
+  ssgTransform * alignmainmodel = new ssgTransform;
+  alignmainmodel->addKid(model);
   sgMat4 res_matrix;
-  float h_rot = props.getFloatValue("/offsets/heading-deg", 0.0);
-  float p_rot = props.getFloatValue("/offsets/roll-deg", 0.0);
-  float r_rot = props.getFloatValue("/offsets/pitch-deg", 0.0);
-  float x_off = props.getFloatValue("/offsets/x-m", 0.0);
-  float y_off = props.getFloatValue("/offsets/y-m", 0.0);
-  float z_off = props.getFloatValue("/offsets/z-m", 0.0);
-  sgMakeRotMat4(rot_matrix, h_rot, p_rot, r_rot);
-  sgMakeTransMat4(off_matrix, x_off, y_off, z_off);
-  sgMultMat4(res_matrix, off_matrix, rot_matrix);
-  align->setTransform(res_matrix);
-
-                                // Set up the position node
-  _position->addKid(align);
-
-                                // Set up the selector node
-  _selector->addKid(_position);
-  _selector->clrTraversalMaskBits(SSGTRAV_HOT);
-
-                                // Set up a location class
-  _location = (FGLocation *) new FGLocation;
-
-}
-
-void
-FG3DModel::update (int dt)
-{
+  make_offsets_matrix(&res_matrix,
+                      props.getFloatValue("/offsets/heading-deg", 0.0),
+                      props.getFloatValue("/offsets/roll-deg", 0.0),
+                      props.getFloatValue("/offsets/pitch-deg", 0.0),
+                      props.getFloatValue("/offsets/x-m", 0.0),
+                      props.getFloatValue("/offsets/y-m", 0.0),
+                      props.getFloatValue("/offsets/z-m", 0.0));
+  alignmainmodel->setTransform(res_matrix);
+
+                                // Load panels
   unsigned int i;
-
-  for (i = 0; i < _animations.size(); i++)
-    _animations[i]->update(dt);
-
-  _location->setPosition( _lon_deg, _lat_deg, _elev_ft );
-  _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
-
-  sgMat4 POS;
-  sgCopyMat4(POS, _location->getTransformMatrix());
-  
-  sgVec3 trans;
-  sgCopyVec3(trans, _location->get_view_pos());
-
-  for(i = 0; i < 4; i++) {
-    float tmp = POS[i][3];
-    for( int j=0; j<3; j++ ) {
-      POS[i][j] += (tmp * trans[j]);
-    }
+  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");
+    FGPanelNode * panel = new FGPanelNode(panel_nodes[i]);
+    if (panel_nodes[i]->hasValue("name"))
+        panel->setName((char *)panel_nodes[i]->getStringValue("name"));
+    model->addKid(panel);
   }
-  _position->setTransform(POS);
-}
 
-bool
-FG3DModel::getVisible () const
-{
-  return _selector->getSelect();
-}
-
-void
-FG3DModel::setVisible (bool visible)
-{
-  _selector->select(visible);
-}
+                                // Load animations
+  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");
+    make_animation(model, name, name_nodes, animation_nodes[i]);
+  }
 
-void
-FG3DModel::setLongitudeDeg (double lon_deg)
-{
-  _lon_deg = lon_deg;
-}
+                                // Load sub-models
+  vector<SGPropertyNode_ptr> model_nodes = props.getChildren("model");
+  for (i = 0; i < model_nodes.size(); i++) {
+    SGPropertyNode_ptr node = model_nodes[i];
+    ssgTransform * align = new ssgTransform;
+    sgMat4 res_matrix;
+    make_offsets_matrix(&res_matrix,
+                        node->getFloatValue("offsets/heading-deg", 0.0),
+                        node->getFloatValue("offsets/roll-deg", 0.0),
+                        node->getFloatValue("offsets/pitch-deg", 0.0),
+                        node->getFloatValue("offsets/x-m", 0.0),
+                        node->getFloatValue("offsets/y-m", 0.0),
+                        node->getFloatValue("offsets/z-m", 0.0));
+    align->setTransform(res_matrix);
+
+    ssgBranch * kid = fgLoad3DModel(node->getStringValue("path"));
+    align->addKid(kid);
+    model->addKid(align);
+  }
 
-void
-FG3DModel::setLatitudeDeg (double lat_deg)
-{
-  _lat_deg = lat_deg;
+  return alignmainmodel;
 }
 
-void
-FG3DModel::setElevationFt (double elev_ft)
-{
-  _elev_ft = elev_ft;
-}
 
-void
-FG3DModel::setPosition (double lon_deg, double lat_deg, double elev_ft)
-{
-  _lon_deg = lon_deg;
-  _lat_deg = lat_deg;
-  _elev_ft = elev_ft;
-}
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of Animation
+////////////////////////////////////////////////////////////////////////
 
-void
-FG3DModel::setRollDeg (double roll_deg)
+Animation::Animation (SGPropertyNode_ptr props, ssgBranch * branch)
+    : _branch(branch)
 {
-  _roll_deg = roll_deg;
+    _branch->setName(props->getStringValue("name", 0));
 }
 
-void
-FG3DModel::setPitchDeg (double pitch_deg)
+Animation::~Animation ()
 {
-  _pitch_deg = pitch_deg;
 }
 
 void
-FG3DModel::setHeadingDeg (double heading_deg)
+Animation::init ()
 {
-  _heading_deg = heading_deg;
 }
 
 void
-FG3DModel::setOrientation (double roll_deg, double pitch_deg,
-                           double heading_deg)
+Animation::update ()
 {
-  _roll_deg = roll_deg;
-  _pitch_deg = pitch_deg;
-  _heading_deg = heading_deg;
-}
-
-FG3DModel::Animation *
-FG3DModel::make_animation (const char * object_name,
-                          SGPropertyNode * node)
-{
-  Animation * animation = 0;
-  const char * type = node->getStringValue("type");
-  if (!strcmp("none", type)) {
-    animation = new NullAnimation();
-  } else if (!strcmp("range", type)) {
-    animation = new RangeAnimation();
-  } else if (!strcmp("select", type)) {
-    animation = new SelectAnimation();
-  } else if (!strcmp("spin", type)) {
-    animation = new SpinAnimation();
-  } else if (!strcmp("rotate", type)) {
-    animation = new RotateAnimation();
-  } else if (!strcmp("translate", type)) {
-    animation = new TranslateAnimation();
-  } else {
-    animation = new NullAnimation();
-    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;
-  } else {
-    animation->init(object, node);
-  }
-
-  return animation;
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::Animation
+// Implementation of NullAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::Animation::Animation ()
+NullAnimation::NullAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgBranch)
 {
 }
 
-FG3DModel::Animation::~Animation ()
+NullAnimation::~NullAnimation ()
 {
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::NullAnimation
+// Implementation of RangeAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::NullAnimation::NullAnimation ()
-  : _branch(new ssgBranch)
-{
-}
-
-FG3DModel::NullAnimation::~NullAnimation ()
-{
-  _branch = 0;
-}
-
-void
-FG3DModel::NullAnimation::init (ssgEntity * object,
-                                      SGPropertyNode * props)
+RangeAnimation::RangeAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgRangeSelector)
 {
-  splice_branch(_branch, object);
-  _branch->setName(props->getStringValue("name", 0));
+    float ranges[] = { props->getFloatValue("min-m", 0),
+                       props->getFloatValue("max-m", 5000) };
+    ((ssgRangeSelector *)_branch)->setRanges(ranges, 2);
+                       
 }
 
-void
-FG3DModel::NullAnimation::update (int dt)
+RangeAnimation::~RangeAnimation ()
 {
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::RangeAnimation
+// Implementation of BillboardAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::RangeAnimation::RangeAnimation ()
-  : _branch(new ssgRangeSelector)
-{
-}
-
-FG3DModel::RangeAnimation::~RangeAnimation ()
+BillboardAnimation::BillboardAnimation (SGPropertyNode_ptr props)
+    : Animation(props, new ssgCutout(props->getBoolValue("spherical", true)))
 {
-  _branch = 0;
 }
 
-void
-FG3DModel::RangeAnimation::init (ssgEntity * object,
-                                      SGPropertyNode * props)
-{
-  float ranges[2];
-  splice_branch(_branch, object);
-  _branch->setName(props->getStringValue("name", 0));
-  ranges[0] = props->getFloatValue("min-m", 0);
-  ranges[1] = props->getFloatValue("max-m", 5000);
-  _branch->setRanges(ranges, 2);
-}
-
-void
-FG3DModel::RangeAnimation::update (int dt)
+BillboardAnimation::~BillboardAnimation ()
 {
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::SelectAnimation
+// Implementation of SelectAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::SelectAnimation::SelectAnimation ()
-  : _condition(0),
-    _selector(new ssgSelector)
+SelectAnimation::SelectAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgSelector),
+    _condition(0)
 {
+  SGPropertyNode_ptr node = props->getChild("condition");
+  if (node != 0)
+    _condition = fgReadCondition(node);
 }
 
-FG3DModel::SelectAnimation::~SelectAnimation ()
+SelectAnimation::~SelectAnimation ()
 {
   delete _condition;
-  _selector = 0;
 }
 
 void
-FG3DModel::SelectAnimation::init (ssgEntity * object,
-                                      SGPropertyNode * props)
-{
-  splice_branch(_selector, object);
-  _selector->setName(props->getStringValue("name", 0));
-  SGPropertyNode * node = props->getChild("condition");
-  if (node != 0) {
-    _condition = fgReadCondition(node);
-  }
-}
-
-void
-FG3DModel::SelectAnimation::update (int dt)
+SelectAnimation::update ()
 {
   if (_condition != 0 && _condition->test()) 
-    _selector->select(0xffff);
+      ((ssgSelector *)_branch)->select(0xffff);
   else
-    _selector->select(0x0000);
+      ((ssgSelector *)_branch)->select(0x0000);
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::SpinAnimation
+// Implementation of SpinAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::SpinAnimation::SpinAnimation ()
-  : _prop(0),
-    _factor(0),
-    _position_deg(0),
-    _transform(new ssgTransform)
+SpinAnimation::SpinAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgTransform),
+    _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
+    _factor(props->getDoubleValue("factor", 1.0)),
+    _position_deg(props->getDoubleValue("starting-position-deg", 0)),
+    _last_time_sec(globals->get_sim_time_sec())
 {
+    _center[0] = props->getFloatValue("center/x-m", 0);
+    _center[1] = props->getFloatValue("center/y-m", 0);
+    _center[2] = props->getFloatValue("center/z-m", 0);
+    _axis[0] = props->getFloatValue("axis/x", 0);
+    _axis[1] = props->getFloatValue("axis/y", 0);
+    _axis[2] = props->getFloatValue("axis/z", 0);
+    sgNormalizeVec3(_axis);
 }
 
-FG3DModel::SpinAnimation::~SpinAnimation ()
+SpinAnimation::~SpinAnimation ()
 {
-  _transform = 0;
 }
 
 void
-FG3DModel::SpinAnimation::init (ssgEntity * object,
-                                      SGPropertyNode * props)
-{
-                                // Splice in the new transform node
-  splice_branch(_transform, object);
-  _transform->setName(props->getStringValue("name", 0));
-  _prop = fgGetNode(props->getStringValue("property", "/null"), true);
-  _factor = props->getDoubleValue("factor", 1.0);
-  _position_deg = props->getDoubleValue("starting-position-deg", 0);
-  _center[0] = props->getFloatValue("center/x-m", 0);
-  _center[1] = props->getFloatValue("center/y-m", 0);
-  _center[2] = props->getFloatValue("center/z-m", 0);
-  _axis[0] = props->getFloatValue("axis/x", 0);
-  _axis[1] = props->getFloatValue("axis/y", 0);
-  _axis[2] = props->getFloatValue("axis/z", 0);
-  sgNormalizeVec3(_axis);
-}
-
-void
-FG3DModel::SpinAnimation::update (int dt)
+SpinAnimation::update ()
 {
-  float velocity_rpms = (_prop->getDoubleValue() * _factor / 60000.0);
+  double sim_time = globals->get_sim_time_sec();
+  double dt = sim_time - _last_time_sec;
+  _last_time_sec = sim_time;
+
+  float velocity_rpms = (_prop->getDoubleValue() * _factor / 60.0);
   _position_deg += (dt * velocity_rpms * 360);
   while (_position_deg < 0)
     _position_deg += 360.0;
   while (_position_deg >= 360.0)
     _position_deg -= 360.0;
   set_rotation(_matrix, _position_deg, _center, _axis);
-  _transform->setTransform(_matrix);
+  ((ssgTransform *)_branch)->setTransform(_matrix);
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::RotateAnimation
+// Implementation of TimedAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::RotateAnimation::RotateAnimation ()
-  : _prop(0),
-    _offset_deg(0.0),
-    _factor(1.0),
-    _table(0),
-    _has_min(false),
-    _min_deg(0.0),
-    _has_max(false),
-    _max_deg(1.0),
-    _position_deg(0.0),
-    _transform(new ssgTransform)
+TimedAnimation::TimedAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgSelector),
+    _duration_sec(props->getDoubleValue("duration-sec", 1.0)),
+    _last_time_sec(0),
+    _step(-1)
 {
 }
 
-FG3DModel::RotateAnimation::~RotateAnimation ()
+TimedAnimation::~TimedAnimation ()
 {
-  delete _table;
-  _transform = 0;
 }
 
 void
-FG3DModel::RotateAnimation::init (ssgEntity * object,
-                                 SGPropertyNode * props)
-{
-                                // Splice in the new transform node
-  splice_branch(_transform, object);
-  _transform->setName(props->getStringValue("name", 0));
-  _prop = fgGetNode(props->getStringValue("property", "/null"), true);
-  _offset_deg = props->getDoubleValue("offset-deg", 0.0);
-  _factor = props->getDoubleValue("factor", 1.0);
-  _table = read_interpolation_table(props);
-  if (props->hasValue("min-deg")) {
-    _has_min = true;
-    _min_deg = props->getDoubleValue("min-deg");
-  }
-  if (props->hasValue("max-deg")) {
-    _has_max = true;
-    _max_deg = props->getDoubleValue("max-deg");
-  }
-  _position_deg = props->getDoubleValue("starting-position-deg", 0);
+TimedAnimation::update ()
+{
+    float sim_time_sec = globals->get_sim_time_sec();
+    if ((sim_time_sec - _last_time_sec) >= _duration_sec) {
+        _last_time_sec = sim_time_sec;
+        _step++;
+        if (_step >= getBranch()->getNumKids())
+            _step = 0;
+        ((ssgSelector *)getBranch())->selectStep(_step);
+    }
+}
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of RotateAnimation
+////////////////////////////////////////////////////////////////////////
+
+RotateAnimation::RotateAnimation (SGPropertyNode_ptr props)
+    : Animation(props, new ssgTransform),
+      _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
+      _offset_deg(props->getDoubleValue("offset-deg", 0.0)),
+      _factor(props->getDoubleValue("factor", 1.0)),
+      _table(read_interpolation_table(props)),
+      _has_min(props->hasValue("min-deg")),
+      _min_deg(props->getDoubleValue("min-deg")),
+      _has_max(props->hasValue("max-deg")),
+      _max_deg(props->getDoubleValue("max-deg")),
+      _position_deg(props->getDoubleValue("starting-position-deg", 0))
+{
   _center[0] = props->getFloatValue("center/x-m", 0);
   _center[1] = props->getFloatValue("center/y-m", 0);
   _center[2] = props->getFloatValue("center/z-m", 0);
@@ -600,11 +544,16 @@ FG3DModel::RotateAnimation::init (ssgEntity * object,
   sgNormalizeVec3(_axis);
 }
 
+RotateAnimation::~RotateAnimation ()
+{
+  delete _table;
+}
+
 void
-FG3DModel::RotateAnimation::update (int dt)
+RotateAnimation::update ()
 {
   if (_table == 0) {
-    _position_deg = (_prop->getDoubleValue() + _offset_deg) * _factor;
+   _position_deg = _prop->getDoubleValue() * _factor + _offset_deg;
    if (_has_min && _position_deg < _min_deg)
      _position_deg = _min_deg;
    if (_has_max && _position_deg > _max_deg)
@@ -613,63 +562,40 @@ FG3DModel::RotateAnimation::update (int dt)
     _position_deg = _table->interpolate(_prop->getDoubleValue());
   }
   set_rotation(_matrix, _position_deg, _center, _axis);
-  _transform->setTransform(_matrix);
+  ((ssgTransform *)_branch)->setTransform(_matrix);
 }
 
 
 \f
 ////////////////////////////////////////////////////////////////////////
-// Implementation of FG3DModel::TranslateAnimation
+// Implementation of TranslateAnimation
 ////////////////////////////////////////////////////////////////////////
 
-FG3DModel::TranslateAnimation::TranslateAnimation ()
-  : _prop(0),
-    _offset_m(0.0),
-    _factor(1.0),
-    _table(0),
-    _has_min(false),
-    _min_m(0.0),
-    _has_max(false),
-    _max_m(1.0),
-    _position_m(0.0),
-    _transform(new ssgTransform)
-{
-}
-
-FG3DModel::TranslateAnimation::~TranslateAnimation ()
+TranslateAnimation::TranslateAnimation (SGPropertyNode_ptr props)
+  : Animation(props, new ssgTransform),
+    _prop(fgGetNode(props->getStringValue("property", "/null"), true)),
+    _offset_m(props->getDoubleValue("offset-m", 0.0)),
+    _factor(props->getDoubleValue("factor", 1.0)),
+    _table(read_interpolation_table(props)),
+    _has_min(props->hasValue("min-m")),
+    _min_m(props->getDoubleValue("min-m")),
+    _has_max(props->hasValue("max-m")),
+    _max_m(props->getDoubleValue("max-m")),
+    _position_m(props->getDoubleValue("starting-position-m", 0))
 {
-  delete _table;
-  _transform = 0;
-}
-
-void
-FG3DModel::TranslateAnimation::init (ssgEntity * object,
-                                    SGPropertyNode * props)
-{
-                                // Splice in the new transform node
-  splice_branch(_transform, object);
-  _transform->setName(props->getStringValue("name", 0));
-  _prop = fgGetNode(props->getStringValue("property", "/null"), true);
-  _offset_m = props->getDoubleValue("offset-m", 0.0);
-  _factor = props->getDoubleValue("factor", 1.0);
-  _table = read_interpolation_table(props);
-  if (props->hasValue("min-m")) {
-    _has_min = true;
-    _min_m = props->getDoubleValue("min-m");
-  }
-  if (props->hasValue("max-m")) {
-    _has_max = true;
-    _max_m = props->getDoubleValue("max-m");
-  }
-  _position_m = props->getDoubleValue("starting-position-m", 0);
   _axis[0] = props->getFloatValue("axis/x", 0);
   _axis[1] = props->getFloatValue("axis/y", 0);
   _axis[2] = props->getFloatValue("axis/z", 0);
   sgNormalizeVec3(_axis);
 }
 
+TranslateAnimation::~TranslateAnimation ()
+{
+  delete _table;
+}
+
 void
-FG3DModel::TranslateAnimation::update (int dt)
+TranslateAnimation::update ()
 {
   if (_table == 0) {
     _position_m = (_prop->getDoubleValue() + _offset_m) * _factor;
@@ -681,8 +607,126 @@ FG3DModel::TranslateAnimation::update (int dt)
     _position_m = _table->interpolate(_prop->getDoubleValue());
   }
   set_translation(_matrix, _position_m, _axis);
-  _transform->setTransform(_matrix);
+  ((ssgTransform *)_branch)->setTransform(_matrix);
+}
+
+
+\f
+////////////////////////////////////////////////////////////////////////
+// Implementation of FGModelPlacement.
+////////////////////////////////////////////////////////////////////////
+
+FGModelPlacement::FGModelPlacement ()
+  : _lon_deg(0),
+    _lat_deg(0),
+    _elev_ft(0),
+    _roll_deg(0),
+    _pitch_deg(0),
+    _heading_deg(0),
+    _selector(new ssgSelector),
+    _position(new ssgTransform),
+    _location(new FGLocation)
+{
+}
+
+FGModelPlacement::~FGModelPlacement ()
+{
+}
+
+void
+FGModelPlacement::init (const string &path)
+{
+  ssgBranch * model = fgLoad3DModel(path);
+  if (model != 0)
+      _position->addKid(model);
+  _selector->addKid(_position);
+  _selector->clrTraversalMaskBits(SSGTRAV_HOT);
 }
 
+void
+FGModelPlacement::update ()
+{
+  _location->setPosition( _lon_deg, _lat_deg, _elev_ft );
+  _location->setOrientation( _roll_deg, _pitch_deg, _heading_deg );
+
+  sgMat4 POS;
+  sgCopyMat4(POS, _location->getTransformMatrix());
+  
+  sgVec3 trans;
+  sgCopyVec3(trans, _location->get_view_pos());
+
+  for(int i = 0; i < 4; i++) {
+    float tmp = POS[i][3];
+    for( int j=0; j<3; j++ ) {
+      POS[i][j] += (tmp * trans[j]);
+    }
+  }
+  _position->setTransform(POS);
+}
+
+bool
+FGModelPlacement::getVisible () const
+{
+  return (_selector->getSelect() != 0);
+}
+
+void
+FGModelPlacement::setVisible (bool visible)
+{
+  _selector->select(visible);
+}
+
+void
+FGModelPlacement::setLongitudeDeg (double lon_deg)
+{
+  _lon_deg = lon_deg;
+}
+
+void
+FGModelPlacement::setLatitudeDeg (double lat_deg)
+{
+  _lat_deg = lat_deg;
+}
+
+void
+FGModelPlacement::setElevationFt (double elev_ft)
+{
+  _elev_ft = elev_ft;
+}
+
+void
+FGModelPlacement::setPosition (double lon_deg, double lat_deg, double elev_ft)
+{
+  _lon_deg = lon_deg;
+  _lat_deg = lat_deg;
+  _elev_ft = elev_ft;
+}
+
+void
+FGModelPlacement::setRollDeg (double roll_deg)
+{
+  _roll_deg = roll_deg;
+}
+
+void
+FGModelPlacement::setPitchDeg (double pitch_deg)
+{
+  _pitch_deg = pitch_deg;
+}
+
+void
+FGModelPlacement::setHeadingDeg (double heading_deg)
+{
+  _heading_deg = heading_deg;
+}
+
+void
+FGModelPlacement::setOrientation (double roll_deg, double pitch_deg,
+                                  double heading_deg)
+{
+  _roll_deg = roll_deg;
+  _pitch_deg = pitch_deg;
+  _heading_deg = heading_deg;
+}
 
 // end of model.cxx