]> git.mxchange.org Git - simgear.git/blobdiff - simgear/scene/model/SGRotateTransform.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / SGRotateTransform.cxx
index 84fb15d02c2ba9e3ae0af4cbba923561a3b4f981..d1792f19175d61fcb6c7e033e5681228ae8da7c8 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
+#include <simgear/scene/util/OsgMath.hxx>
+
 #include "SGRotateTransform.hxx"
 
-static void
-set_rotation (osg::Matrix &matrix, double position_rad,
-              const SGVec3d &center, const SGVec3d &axis)
+void SGRotateTransform::set_rotation (osg::Matrix &matrix, double position_rad,
+                                      const SGVec3d &center,
+                                      const SGVec3d &axis)
 {
   double temp_angle = -position_rad;
   
@@ -75,6 +81,15 @@ SGRotateTransform::SGRotateTransform() :
   setReferenceFrame(RELATIVE_RF);
 }
 
+SGRotateTransform::SGRotateTransform(const SGRotateTransform& rot,
+                                     const osg::CopyOp& copyop) :
+    osg::Transform(rot, copyop),
+    _center(rot._center),
+    _axis(rot._axis),
+    _angleRad(rot._angleRad)
+{
+}
+
 bool
 SGRotateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
                                              osg::NodeVisitor* nv) const
@@ -115,8 +130,78 @@ osg::BoundingSphere
 SGRotateTransform::computeBound() const
 {
   osg::BoundingSphere bs = osg::Group::computeBound();
-  osg::BoundingSphere centerbs(_center.osg(), bs.radius());
+  osg::BoundingSphere centerbs(toOsg(_center), bs.radius());
   centerbs.expandBy(bs);
   return centerbs;
 }
 
+// Functions to read/write SGRotateTransform from/to a .osg file.
+
+namespace {
+
+bool RotateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGRotateTransform& rot = static_cast<SGRotateTransform&>(obj);
+    if (fr[0].matchWord("center")) {
+        ++fr;
+        osg::Vec3d center;
+        if (fr.readSequence(center))
+            fr += 3;
+        else
+            return false;
+        rot.setCenter(toSG(center));
+    }
+    if (fr[0].matchWord("axis")) {
+        ++fr;
+        osg::Vec3d axis;
+        if (fr.readSequence(axis))
+            fr += 3;
+        else
+            return false;
+        rot.setCenter(toSG(axis));
+    }
+    if (fr[0].matchWord("angle")) {
+        ++fr;
+        double angle;
+        if (fr[0].getFloat(angle))
+            ++fr;
+        else
+            return false;
+        rot.setAngleRad(angle);
+    }
+    return true;
+}
+
+bool RotateTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
+{
+    const SGRotateTransform& rot = static_cast<const SGRotateTransform&>(obj);
+    const SGVec3d& center = rot.getCenter();
+    const SGVec3d& axis = rot.getAxis();
+    const double angle = rot.getAngleRad();
+    int prec = fw.precision();
+    fw.precision(15);
+    fw.indent() << "center ";
+    for (int i = 0; i < 3; i++) {
+        fw << center(i) << " ";
+    }
+    fw << std::endl;
+    fw.precision(prec);
+    fw.indent() << "axis ";
+    for (int i = 0; i < 3; i++) {
+        fw << axis(i) << " ";
+    }
+    fw << std::endl;
+    fw.indent() << "angle ";
+    fw << angle << std::endl;
+    return true;
+}
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_SGRotateTransformProxy
+(
+    new SGRotateTransform,
+    "SGRotateTransform",
+    "Object Node Transform SGRotateTransform Group",
+    &RotateTransform_readLocalData,
+    &RotateTransform_writeLocalData
+);