]> git.mxchange.org Git - simgear.git/commitdiff
Support for reading and writing nodes in .osg files, plus some new accessors.
authortimoore <timoore>
Sun, 22 Jul 2007 20:06:20 +0000 (20:06 +0000)
committertimoore <timoore>
Sun, 22 Jul 2007 20:06:20 +0000 (20:06 +0000)
simgear/scene/model/SGOffsetTransform.cxx
simgear/scene/model/SGOffsetTransform.hxx
simgear/scene/model/SGRotateTransform.cxx
simgear/scene/model/SGRotateTransform.hxx
simgear/scene/model/SGScaleTransform.cxx
simgear/scene/model/SGScaleTransform.hxx
simgear/scene/model/SGTranslateTransform.cxx
simgear/scene/model/SGTranslateTransform.hxx
simgear/scene/model/placementtrans.cxx
simgear/scene/model/placementtrans.hxx

index b59aebd8a33475e701366c9a363cb08c0fae0cc3..7a3df3cffe428735464cedbb9e5a300508e96416 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
 #include "SGOffsetTransform.hxx"
 
 SGOffsetTransform::SGOffsetTransform(double scaleFactor) :
@@ -31,6 +35,14 @@ SGOffsetTransform::SGOffsetTransform(double scaleFactor) :
 {
 }
 
+SGOffsetTransform::SGOffsetTransform(const SGOffsetTransform& offset,
+                                     const osg::CopyOp& copyop) :
+    osg::Transform(offset, copyop),
+    _scaleFactor(offset._scaleFactor),
+    _rScaleFactor(offset._rScaleFactor)
+{
+}
+
 bool
 SGOffsetTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
                                              osg::NodeVisitor* nv) const
@@ -66,3 +78,39 @@ SGOffsetTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
   }
   return true;
 }
+
+namespace {
+
+bool OffsetTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGOffsetTransform& offset = static_cast<SGOffsetTransform&>(obj);
+    if (fr[0].matchWord("scaleFactor")) {
+        ++fr;
+        double scaleFactor;
+        if (fr[0].getFloat(scaleFactor))
+            ++fr;
+        else
+            return false;
+        offset.setScaleFactor(scaleFactor);
+    }
+    return true;
+}
+
+bool OffsetTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
+{
+    const SGOffsetTransform& offset
+        = static_cast<const SGOffsetTransform&>(obj);
+    fw.indent() << "scaleFactor " << offset.getScaleFactor() << std::endl;
+    return true;
+}
+
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_SGOffsetTransformProxy
+(
+    new SGOffsetTransform,
+    "SGOffsetTransform",
+    "Object Node Transform SGOffsetTransform Group",
+    &OffsetTransform_readLocalData,
+    &OffsetTransform_writeLocalData
+);
index 5edddadb61973fcbc431951e8df4ca8bb18437d3..641f90bd96c75318fa297b2e3c6d94e5afc78773 100644 (file)
 
 class SGOffsetTransform : public osg::Transform {
 public:
-  SGOffsetTransform(double scaleFactor);
+  SGOffsetTransform(double scaleFactor = 1.0);
+  SGOffsetTransform(const SGOffsetTransform&,
+                    const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
+
+  META_Node(simgear, SGOffsetTransform);
+
+  double getScaleFactor() const { return _scaleFactor; };
+
+  void setScaleFactor(double scaleFactor)
+  {
+    _scaleFactor = scaleFactor;
+    _rScaleFactor = 1.0 / scaleFactor;
+  }
+  
   virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
                                          osg::NodeVisitor* nv) const;
   virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
                                          osg::NodeVisitor* nv) const;
+
 private:
   double _scaleFactor;
   double _rScaleFactor;
index 84fb15d02c2ba9e3ae0af4cbba923561a3b4f981..4f18fe61d9f49182e2abbbf28210ce537ca9d8d1 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
 #include "SGRotateTransform.hxx"
 
 static void
@@ -75,6 +79,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
@@ -120,3 +133,73 @@ SGRotateTransform::computeBound() const
   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;
+        SGVec3d center;
+        if (fr.readSequence(center.osg()))
+            fr += 3;
+        else
+            return false;
+        rot.setCenter(center);
+    }
+    if (fr[0].matchWord("axis")) {
+        ++fr;
+        SGVec3d axis;
+        if (fr.readSequence(axis.osg()))
+            fr += 3;
+        else
+            return false;
+        rot.setCenter(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
+);
index 22611ed43f1dbda4f11207ecf95b3fcc4a27f263..2cf1d375f553a6167dc58934b004ceaf8109c222 100644 (file)
 class SGRotateTransform : public osg::Transform {
 public:
   SGRotateTransform();
+  SGRotateTransform(const SGRotateTransform&,
+                    const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
 
+  META_Node(simgear, SGRotateTransform);
+  
   void setCenter(const SGVec3f& center)
   { setCenter(toVec3d(center)); }
   void setCenter(const SGVec3d& center)
index f8d4ca3179b63e489951533a3c1742ec48850478..6a4a581ef8f8ac2fcf51b3b0929536c36ef0eacf 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
 #include "SGScaleTransform.hxx"
 
 SGScaleTransform::SGScaleTransform() :
@@ -54,6 +58,15 @@ SGScaleTransform::SGScaleTransform() :
   setReferenceFrame(RELATIVE_RF);
 }
 
+SGScaleTransform::SGScaleTransform(const SGScaleTransform& scale,
+                                   const osg::CopyOp& copyop) :
+    osg::Transform(scale, copyop),
+    _center(scale._center),
+    _scaleFactor(scale._scaleFactor),
+    _boundScale(scale._boundScale)
+{
+}
+
 bool
 SGScaleTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
                                             osg::NodeVisitor* nv) const
@@ -107,3 +120,59 @@ SGScaleTransform::computeBound() const
   bs.radius() *= _boundScale;
   return bs;
 }
+
+namespace {
+
+bool ScaleTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGScaleTransform& scale = static_cast<SGScaleTransform&>(obj);
+    if (fr[0].matchWord("center")) {
+        ++fr;
+        SGVec3d center;
+        if (fr.readSequence(center.osg()))
+            fr += 3;
+        else
+            return false;
+        scale.setCenter(center);
+    }
+    if (fr[0].matchWord("scaleFactor")) {
+        ++fr;
+        SGVec3d scaleFactor;
+        if (fr.readSequence(scaleFactor.osg()))
+            fr += 3;
+        else
+            return false;
+        scale.setScaleFactor(scaleFactor);
+    }
+    return true;
+}
+
+bool ScaleTransform_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
+{
+    const SGScaleTransform& scale = static_cast<const SGScaleTransform&>(obj);
+    const SGVec3d& center = scale.getCenter();
+    const SGVec3d& scaleFactor = scale.getScaleFactor();
+    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() << "scaleFactor ";
+    for (int i = 0; i < 3; i++)
+        fw << scaleFactor(i) << " ";
+    fw << std::endl;
+    return true;
+}
+
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_ScaleTransformProxy
+(
+    new SGScaleTransform,
+    "SGScaleTransform",
+    "Object Node Transform SGScaleTransform Group",
+    &ScaleTransform_readLocalData,
+    &ScaleTransform_writeLocalData
+);
index 03b1c8a008055c919aa46b1de13c93cb10b63091..3e69aed15c39ad97478dd31a35a66e1ffb7e9fcc 100644 (file)
 class SGScaleTransform : public osg::Transform {
 public:
   SGScaleTransform();
+  SGScaleTransform(const SGScaleTransform&,
+                   const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
+
+  META_Node(simgear, SGScaleTransform);
 
   void setCenter(const SGVec3f& center)
   { setCenter(toVec3d(center)); }
index c7d0f978fe4bce081aad431e3645d72ba6b320c1..9f6e5d8d6dfba1dac0f4090f4fa8130318629e7e 100644 (file)
 #  include <simgear_config.h>
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
 #include "SGTranslateTransform.hxx"
 
 static inline void
@@ -42,6 +46,14 @@ 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 
@@ -81,3 +93,57 @@ SGTranslateTransform::computeBound() const
   bs._center += _axis.osg()*_value;
   return bs;
 }
+
+namespace {
+
+bool TranslateTransform_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGTranslateTransform& trans = static_cast<SGTranslateTransform&>(trans);
+
+    if (fr[0].matchWord("axis")) {
+        ++fr;
+        SGVec3d axis;
+        if (fr.readSequence(axis.osg()))
+            fr += 3;
+        else
+            return false;
+        trans.setAxis(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
+);
index e4bd8f1db8cc5db513aa069cdbb30ab13f89b35e..be6ac0a729803ec50d7c77cc6262040946e88e52 100644 (file)
 class SGTranslateTransform : public osg::Transform {
 public:
   SGTranslateTransform();
+  SGTranslateTransform(const SGTranslateTransform&,
+                       const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
 
+  META_Node(simgear, SGTranslateTransform);
+  
   void setAxis(const SGVec3d& axis)
   { _axis = axis; dirtyBound(); }
   const SGVec3d& getAxis() const
index 73011b2f8ac521a2887268610ed0128468a52054..dfb2f7e0f97661ab7a1620a42b422cd38b08940c 100644 (file)
 # error This library requires C++
 #endif
 
+#include <osgDB/Registry>
+#include <osgDB/Input>
+#include <osgDB/Output>
+
 #include <simgear/compiler.h>
 #include <simgear/constants.h>
 
@@ -69,6 +73,16 @@ SGPlacementTransform::SGPlacementTransform(void) :
   setUpdateCallback(new UpdateCallback);
 }
 
+SGPlacementTransform::SGPlacementTransform(const SGPlacementTransform& trans,
+                                           const osg::CopyOp& copyop):
+  osg::Transform(trans, copyop),
+  _placement_offset(trans._placement_offset),
+  _scenery_center(trans._scenery_center),
+  _rotation(trans._rotation)
+{
+  
+}
+
 SGPlacementTransform::~SGPlacementTransform(void)
 {
 }
@@ -111,3 +125,97 @@ SGPlacementTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
     matrix = t;
   return true;
 }
+
+// Functions to read / write SGPlacementTrans from / to a .osg file,
+// mostly for debugging purposes.
+
+namespace {
+
+bool PlacementTrans_readLocalData(osg::Object& obj, osgDB::Input& fr)
+{
+    SGPlacementTransform& trans = static_cast<SGPlacementTransform&>(obj);
+    SGMatrixd rotation(1, 0, 0, 0,
+                       0, 1, 0, 0,
+                       0, 0, 1, 0,
+                       0, 0, 0, 1);
+    SGVec3d placementOffset(0, 0, 0);
+    SGVec3d sceneryCenter(0, 0, 0);
+    
+    if (fr[0].matchWord("rotation") && fr[1].isOpenBracket()) {
+        fr += 2;
+        for (int i = 0; i < 3; i++) {
+            SGVec3d scratch;
+            if (!fr.readSequence(scratch.osg()))
+                return false;
+            fr += 3;
+            for (int j = 0; j < 3; j++)
+                rotation(j, i) = scratch[j];
+        }
+        if (fr[0].isCloseBracket())
+            ++fr;
+        else
+            return false;
+    }
+    if (fr[0].matchWord("placement")) {
+        ++fr;
+        if (fr.readSequence(placementOffset.osg()))
+            fr += 3;
+        else
+            return false;
+    }
+    if (fr[0].matchWord("sceneryCenter")) {
+        ++fr;
+        if (fr.readSequence(sceneryCenter.osg()))
+            fr += 3;
+        else
+            return false;
+    }
+    trans.setTransform(placementOffset, rotation);
+    trans.setSceneryCenter(sceneryCenter);
+    return true;
+}
+
+bool PlacementTrans_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
+{
+    const SGPlacementTransform& trans
+        = static_cast<const SGPlacementTransform&>(obj);
+    const SGMatrixd& rotation = trans.getRotation();
+    const SGVec3d& placement = trans.getGlobalPos();
+    const SGVec3d& sceneryCenter = trans.getSceneryCenter();
+    
+    fw.indent() << "rotation {" << std::endl;
+    fw.moveIn();
+    for (int i = 0; i < 3; i++) {
+        fw.indent();
+        for (int j = 0; j < 3; j++) {
+            fw << rotation(j, i) << " ";
+        }
+        fw << std::endl;
+    }
+    fw.moveOut();
+    fw.indent() << "}" << std::endl;
+    int prec = fw.precision();
+    fw.precision(15);
+    fw.indent() << "placement ";
+    for (int i = 0; i < 3; i++) {
+        fw << placement(i) << " ";
+    }
+    fw << std::endl;
+    fw.indent() << "sceneryCenter ";
+    for (int i = 0; i < 3; i++) {
+        fw << sceneryCenter(i) << " ";
+    }
+    fw << std::endl;
+    fw.precision(prec);
+    return true;
+}
+}
+
+osgDB::RegisterDotOsgWrapperProxy g_SGPlacementTransProxy
+(
+    new SGPlacementTransform,
+    "SGPlacementTransform",
+    "Object Node Transform SGPlacementTransform Group",
+    &PlacementTrans_readLocalData,
+    &PlacementTrans_writeLocalData
+);
index 93ca1080bb6e557c08b1c2350fd055eb84d028a0..21aded0a59892de990065f44c689ce9c3b201f79 100644 (file)
@@ -38,21 +38,29 @@ class SGPlacementTransform : public osg::Transform
 public:
   
   SGPlacementTransform(void);
-  virtual ~SGPlacementTransform(void);
+  SGPlacementTransform(const SGPlacementTransform&,
+                       const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
 
+  META_Node(simgear, SGPlacementTransform);
+  
   void setTransform(const SGVec3d& off)
   { _placement_offset = off; dirtyBound(); }
   void setTransform(const SGVec3d& off, const SGMatrixd& rot)
   { _placement_offset = off; _rotation = rot; dirtyBound(); }
   void setSceneryCenter(const SGVec3d& center)
   { _scenery_center = center; dirtyBound(); }
-
+  
   const SGVec3d& getGlobalPos() const
   { return _placement_offset; }
-
+  const SGMatrixd& getRotation() const { return _rotation; }
+  const SGVec3d& getSceneryCenter() const { return _scenery_center; }
+  
   virtual bool computeLocalToWorldMatrix(osg::Matrix&,osg::NodeVisitor*) const;
   virtual bool computeWorldToLocalMatrix(osg::Matrix&,osg::NodeVisitor*) const;
 
+protected:
+  virtual ~SGPlacementTransform(void);
+
 private:
 
   class UpdateCallback;