]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGTranslateTransform.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / SGTranslateTransform.cxx
index c7d0f978fe4bce081aad431e3645d72ba6b320c1..9f148b6449e33cfcf8ecadb488dfb8f2340c5d93 100644 (file)
 #  include <simgear_config.h>
 #endif
 
-#include "SGTranslateTransform.hxx"
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
 
-static inline void
-set_translation (osg::Matrix &matrix, double position_m, const SGVec3d &axis)
-{
-  SGVec3d xyz = axis * position_m;
-  matrix.makeIdentity();
-  matrix(3, 0) = xyz[0];
-  matrix(3, 1) = xyz[1];
-  matrix(3, 2) = xyz[2];
-}
+#include <simgear/scene/util/OsgMath.hxx>
+
+#include "SGTranslateTransform.hxx"
 
 SGTranslateTransform::SGTranslateTransform() :
   _axis(0, 0, 0),
@@ -42,18 +38,22 @@ SGTranslateTransform::SGTranslateTransform() :
   setReferenceFrame(RELATIVE_RF);
 }
 
+SGTranslateTransform::SGTranslateTransform(const SGTranslateTransform& trans,
+                                           const osg::CopyOp& copyop) :
+  osg::Transform(trans, copyop),
+  _axis(trans._axis),
+  _value(trans._value)
+{
+}
+
 bool
 SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
                                                 osg::NodeVisitor* nv) const 
 {
   if (_referenceFrame == RELATIVE_RF) {
-    osg::Matrix tmp;
-    set_translation(tmp, _value, _axis);
-    matrix.preMult(tmp);
+    matrix.preMultTranslate(toOsg(_value*_axis));
   } else {
-    osg::Matrix tmp;
-    set_translation(tmp, _value, _axis);
-    matrix = tmp;
+    matrix.setTrans(toOsg(_value*_axis));
   }
   return true;
 }
@@ -63,13 +63,9 @@ SGTranslateTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
                                                 osg::NodeVisitor* nv) const
 {
   if (_referenceFrame == RELATIVE_RF) {
-    osg::Matrix tmp;
-    set_translation(tmp, -_value, _axis);
-    matrix.postMult(tmp);
+    matrix.postMultTranslate(toOsg(-_value*_axis));
   } else {
-    osg::Matrix tmp;
-    set_translation(tmp, -_value, _axis);
-    matrix = tmp;
+    matrix.setTrans(toOsg(-_value*_axis));
   }
   return true;
 }
@@ -78,6 +74,60 @@ osg::BoundingSphere
 SGTranslateTransform::computeBound() const
 {
   osg::BoundingSphere bs = osg::Group::computeBound();
-  bs._center += _axis.osg()*_value;
+  bs._center += toOsg(_axis)*_value;
   return bs;
 }
+
+namespace {
+
+bool TranslateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGTranslateTransform& trans = static_cast<SGTranslateTransform&>(obj);
+
+    if (fr[0].matchWord("axis")) {
+        ++fr;
+        osg::Vec3d axis;
+        if (fr.readSequence(axis))
+            fr += 3;
+        else
+            return false;
+        trans.setAxis(toSG(axis));
+    }
+    if (fr[0].matchWord("value")) {
+        ++fr;
+        double value;
+        if (fr[0].getFloat(value))
+            ++fr;
+        else
+            return false;
+        trans.setValue(value);
+    }
+    return true;
+}
+
+bool TranslateTransform_writeLocalData(const osg::Object& obj,
+                                       osgDB::Output& fw)
+{
+    const SGTranslateTransform& trans
+        = static_cast<const SGTranslateTransform&>(obj);
+    const SGVec3d& axis = trans.getAxis();
+    double value = trans.getValue();
+    
+    fw.indent() << "axis ";
+    for (int i = 0; i < 3; i++)
+        fw << axis(i) << " ";
+    fw << std::endl;
+    fw.indent() << "value " << value << std::endl;
+    return true;
+}
+
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_SGTranslateTransformProxy
+(
+    new SGTranslateTransform,
+    "SGTranslateTransform",
+    "Object Node Transform SGTranslateTransform Group",
+    &TranslateTransform_readLocalData,
+    &TranslateTransform_writeLocalData
+);