]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/animation.cxx
Provide something more sensible for the properties root
[simgear.git] / simgear / scene / model / animation.cxx
index 81040876016cc8d519c967b8d73b02ee3a7d8d7f..cb5b0eee586eaac9ef78f0d4e9e2aa1026b789c6 100644 (file)
 #include "SGRotateTransform.hxx"
 #include "SGScaleTransform.hxx"
 
+using OpenThreads::Mutex;
+using OpenThreads::ReentrantMutex;
+using OpenThreads::ScopedLock;
+
 \f
 ////////////////////////////////////////////////////////////////////////
 // Static utility functions.
@@ -174,6 +178,19 @@ read_factor_offset(const SGPropertyNode* configNode, SGExpressiond* expr,
 }
 
 static SGExpressiond*
+read_offset_factor(const SGPropertyNode* configNode, SGExpressiond* expr,
+                   const std::string& factor, const std::string& offset)
+{
+  double offsetValue = configNode->getDoubleValue(offset, 0);
+  if (offsetValue != 0)
+    expr = new SGBiasExpression<double>(expr, offsetValue);
+  double factorValue = configNode->getDoubleValue(factor, 1);
+  if (factorValue != 1)
+    expr = new SGScaleExpression<double>(expr, factorValue);
+  return expr;
+}
+
+SGExpressiond*
 read_value(const SGPropertyNode* configNode, SGPropertyNode* modelRoot,
            const char* unit, double defMin, double defMax)
 {
@@ -638,9 +655,20 @@ SGTranslateAnimation::SGTranslateAnimation(const SGPropertyNode* configNode,
   else
     _initialValue = 0;
 
-  _axis[0] = configNode->getDoubleValue("axis/x", 0);
-  _axis[1] = configNode->getDoubleValue("axis/y", 0);
-  _axis[2] = configNode->getDoubleValue("axis/z", 0);
+  if (configNode->hasValue("axis/x1-m")) {
+    SGVec3d v1, v2;
+    v1[0] = configNode->getDoubleValue("axis/x1-m", 0);
+    v1[1] = configNode->getDoubleValue("axis/y1-m", 0);
+    v1[2] = configNode->getDoubleValue("axis/z1-m", 0);
+    v2[0] = configNode->getDoubleValue("axis/x2-m", 0);
+    v2[1] = configNode->getDoubleValue("axis/y2-m", 0);
+    v2[2] = configNode->getDoubleValue("axis/z2-m", 0);
+    _axis = v2 - v1;
+  } else {
+    _axis[0] = configNode->getDoubleValue("axis/x", 0);
+    _axis[1] = configNode->getDoubleValue("axis/y", 0);
+    _axis[2] = configNode->getDoubleValue("axis/z", 0);
+  }
   if (8*SGLimitsd::min() < norm(_axis))
     _axis = normalize(_axis);
 }
@@ -918,12 +946,12 @@ SGScaleAnimation::createAnimationGroup(osg::Group& parent)
 
 namespace
 {
-OpenThreads::Mutex normalizeMutex;
+Mutex normalizeMutex;
 
 osg::StateSet* getNormalizeStateSet()
 {
     static osg::ref_ptr<osg::StateSet> normalizeStateSet;
-    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(normalizeMutex);
+    ScopedLock<Mutex> lock(normalizeMutex);
     if (!normalizeStateSet.valid()) {
         normalizeStateSet = new osg::StateSet;
         normalizeStateSet->setMode(GL_NORMALIZE, osg::StateAttribute::ON);
@@ -1374,13 +1402,12 @@ SGAlphaTestAnimation::SGAlphaTestAnimation(const SGPropertyNode* configNode,
 namespace
 {
 // Keep one copy of the most common alpha test its state set.
-OpenThreads::ReentrantMutex alphaTestMutex;
+ReentrantMutex alphaTestMutex;
 osg::ref_ptr<osg::AlphaFunc> standardAlphaFunc;
 osg::ref_ptr<osg::StateSet> alphaFuncStateSet;
 
 osg::AlphaFunc* makeAlphaFunc(float clamp)
 {
-    using namespace OpenThreads;
     ScopedLock<ReentrantMutex> lock(alphaTestMutex);
     if (osg::equivalent(clamp, 0.01f)) {
         if (standardAlphaFunc.valid())
@@ -1726,8 +1753,6 @@ public:
   Translation(const SGVec3d& axis) :
     _axis(axis)
   { }
-  void setValue(double value)
-  { _value = value; }
   virtual void transform(osg::Matrix& matrix)
   {
     osg::Matrix tmp;
@@ -1806,6 +1831,7 @@ SGTexTransformAnimation::createAnimationGroup(osg::Group& parent)
   osg::Group* group = new osg::Group;
   group->setName("texture transform group");
   osg::StateSet* stateSet = group->getOrCreateStateSet();
+  stateSet->setDataVariance(osg::Object::DYNAMIC);  
   osg::TexMat* texMat = new osg::TexMat;
   UpdateCallback* updateCallback = new UpdateCallback(getCondition());
   // interpret the configs ...
@@ -1842,14 +1868,15 @@ void
 SGTexTransformAnimation::appendTexTranslate(const SGPropertyNode* config,
                                             UpdateCallback* updateCallback)
 {
-  std::string propertyName = config->getStringValue("property", "/null");
+  std::string propertyName = config->getStringValue("property", "");
   SGSharedPtr<SGExpressiond> value;
-  if (getModelRoot()->hasChild(propertyName))
-    value = new SGPropertyExpression<double>(getModelRoot()->getNode(propertyName));
-  else
+  if (propertyName.empty())
     value = new SGConstExpression<double>(0);
+  else {
+    SGPropertyNode* inputProperty = getModelRoot()->getNode(propertyName, true);
+    value = new SGPropertyExpression<double>(inputProperty);
+  }
 
-  SGSharedPtr<SGExpressiond> animationValue;
   SGInterpTable* table = read_interpolation_table(config);
   if (table) {
     value = new SGInterpTableExpression<double>(value, table);
@@ -1859,7 +1886,7 @@ SGTexTransformAnimation::appendTexTranslate(const SGPropertyNode* config,
     value = new SGStepExpression<double>(value,
                                          config->getDoubleValue("step", 0),
                                          config->getDoubleValue("scroll", 0));
-    animationValue = value->simplify();
+    value = value->simplify();
   } else {
     double biasValue = config->getDoubleValue("bias", 0);
     if (biasValue != 0)
@@ -1867,14 +1894,14 @@ SGTexTransformAnimation::appendTexTranslate(const SGPropertyNode* config,
     value = new SGStepExpression<double>(value,
                                          config->getDoubleValue("step", 0),
                                          config->getDoubleValue("scroll", 0));
-    value = read_factor_offset(config, value, "factor", "offset");
+    value = read_offset_factor(config, value, "factor", "offset");
 
     if (config->hasChild("min") || config->hasChild("max")) {
       double minClip = config->getDoubleValue("min", -SGLimitsd::max());
       double maxClip = config->getDoubleValue("max", SGLimitsd::max());
       value = new SGClipExpression<double>(value, minClip, maxClip);
     }
-    animationValue = value->simplify();
+    value = value->simplify();
   }
   SGVec3d axis(config->getDoubleValue("axis/x", 0),
                config->getDoubleValue("axis/y", 0),
@@ -1882,21 +1909,22 @@ SGTexTransformAnimation::appendTexTranslate(const SGPropertyNode* config,
   Translation* translation;
   translation = new Translation(normalize(axis));
   translation->setValue(config->getDoubleValue("starting-position", 0));
-  updateCallback->appendTransform(translation, animationValue);
+  updateCallback->appendTransform(translation, value);
 }
 
 void
 SGTexTransformAnimation::appendTexRotate(const SGPropertyNode* config,
                                          UpdateCallback* updateCallback)
 {
-  std::string propertyName = config->getStringValue("property", "/null");
+  std::string propertyName = config->getStringValue("property", "");
   SGSharedPtr<SGExpressiond> value;
-  if (getModelRoot()->hasChild(propertyName))
-    value = new SGPropertyExpression<double>(getModelRoot()->getNode(propertyName));
-  else
+  if (propertyName.empty())
     value = new SGConstExpression<double>(0);
+  else {
+    SGPropertyNode* inputProperty = getModelRoot()->getNode(propertyName, true);
+    value = new SGPropertyExpression<double>(inputProperty);
+  }
 
-  SGSharedPtr<SGExpressiond> animationValue;
   SGInterpTable* table = read_interpolation_table(config);
   if (table) {
     value = new SGInterpTableExpression<double>(value, table);
@@ -1906,7 +1934,7 @@ SGTexTransformAnimation::appendTexRotate(const SGPropertyNode* config,
     value = new SGStepExpression<double>(value,
                                          config->getDoubleValue("step", 0),
                                          config->getDoubleValue("scroll", 0));
-    animationValue = value->simplify();
+    value = value->simplify();
   } else {
     double biasValue = config->getDoubleValue("bias", 0);
     if (biasValue != 0)
@@ -1914,14 +1942,14 @@ SGTexTransformAnimation::appendTexRotate(const SGPropertyNode* config,
     value = new SGStepExpression<double>(value,
                                          config->getDoubleValue("step", 0),
                                          config->getDoubleValue("scroll", 0));
-    value = read_factor_offset(config, value, "factor", "offset-deg");
+    value = read_offset_factor(config, value, "factor", "offset-deg");
 
     if (config->hasChild("min-deg") || config->hasChild("max-deg")) {
       double minClip = config->getDoubleValue("min-deg", -SGLimitsd::max());
       double maxClip = config->getDoubleValue("max-deg", SGLimitsd::max());
       value = new SGClipExpression<double>(value, minClip, maxClip);
     }
-    animationValue = value->simplify();
+    value = value->simplify();
   }
   SGVec3d axis(config->getDoubleValue("axis/x", 0),
                config->getDoubleValue("axis/y", 0),
@@ -1932,7 +1960,7 @@ SGTexTransformAnimation::appendTexRotate(const SGPropertyNode* config,
   Rotation* rotation;
   rotation = new Rotation(normalize(axis), center);
   rotation->setValue(config->getDoubleValue("starting-position-deg", 0));
-  updateCallback->appendTransform(rotation, animationValue);
+  updateCallback->appendTransform(rotation, value);
 }
 
 
@@ -2014,14 +2042,16 @@ SGPickAnimation::createAnimationGroup(osg::Group& parent)
 
   // Contains the normal geometry that is interactive
   osg::ref_ptr<osg::Group> normalGroup = new osg::Group;
+  normalGroup->setName("pick normal group");
   normalGroup->addChild(commonGroup);
 
   // Used to render the geometry with just yellow edges
   osg::Group* highlightGroup = new osg::Group;
+  highlightGroup->setName("pick highlight group");
   highlightGroup->setNodeMask(SG_NODEMASK_PICK_BIT);
   highlightGroup->addChild(commonGroup);
   SGSceneUserData* ud;
-  ud = SGSceneUserData::getOrCreateSceneUserData(highlightGroup);
+  ud = SGSceneUserData::getOrCreateSceneUserData(commonGroup);
   std::vector<SGPropertyNode_ptr> actions;
   actions = getConfig()->getChildren("action");
   for (unsigned int i = 0; i < actions.size(); ++i)