Support for reading and writing nodes in .osg files, plus some new accessors.

This commit is contained in:
timoore 2007-07-22 20:06:20 +00:00
parent 46a32dd3ee
commit 418856769b
10 changed files with 412 additions and 4 deletions

View File

@ -23,6 +23,10 @@
# include <simgear_config.h> # include <simgear_config.h>
#endif #endif
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include "SGOffsetTransform.hxx" #include "SGOffsetTransform.hxx"
SGOffsetTransform::SGOffsetTransform(double scaleFactor) : 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 bool
SGOffsetTransform::computeLocalToWorldMatrix(osg::Matrix& matrix, SGOffsetTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const osg::NodeVisitor* nv) const
@ -66,3 +78,39 @@ SGOffsetTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
} }
return true; 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
);

View File

@ -26,11 +26,25 @@
class SGOffsetTransform : public osg::Transform { class SGOffsetTransform : public osg::Transform {
public: 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, virtual bool computeLocalToWorldMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const; osg::NodeVisitor* nv) const;
virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix, virtual bool computeWorldToLocalMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const; osg::NodeVisitor* nv) const;
private: private:
double _scaleFactor; double _scaleFactor;
double _rScaleFactor; double _rScaleFactor;

View File

@ -23,6 +23,10 @@
# include <simgear_config.h> # include <simgear_config.h>
#endif #endif
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include "SGRotateTransform.hxx" #include "SGRotateTransform.hxx"
static void static void
@ -75,6 +79,15 @@ SGRotateTransform::SGRotateTransform() :
setReferenceFrame(RELATIVE_RF); 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 bool
SGRotateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix, SGRotateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const osg::NodeVisitor* nv) const
@ -120,3 +133,73 @@ SGRotateTransform::computeBound() const
return centerbs; 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
);

View File

@ -28,6 +28,10 @@
class SGRotateTransform : public osg::Transform { class SGRotateTransform : public osg::Transform {
public: public:
SGRotateTransform(); SGRotateTransform();
SGRotateTransform(const SGRotateTransform&,
const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Node(simgear, SGRotateTransform);
void setCenter(const SGVec3f& center) void setCenter(const SGVec3f& center)
{ setCenter(toVec3d(center)); } { setCenter(toVec3d(center)); }

View File

@ -44,6 +44,10 @@
# include <simgear_config.h> # include <simgear_config.h>
#endif #endif
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include "SGScaleTransform.hxx" #include "SGScaleTransform.hxx"
SGScaleTransform::SGScaleTransform() : SGScaleTransform::SGScaleTransform() :
@ -54,6 +58,15 @@ SGScaleTransform::SGScaleTransform() :
setReferenceFrame(RELATIVE_RF); 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 bool
SGScaleTransform::computeLocalToWorldMatrix(osg::Matrix& matrix, SGScaleTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const osg::NodeVisitor* nv) const
@ -107,3 +120,59 @@ SGScaleTransform::computeBound() const
bs.radius() *= _boundScale; bs.radius() *= _boundScale;
return bs; 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
);

View File

@ -28,6 +28,10 @@
class SGScaleTransform : public osg::Transform { class SGScaleTransform : public osg::Transform {
public: public:
SGScaleTransform(); SGScaleTransform();
SGScaleTransform(const SGScaleTransform&,
const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Node(simgear, SGScaleTransform);
void setCenter(const SGVec3f& center) void setCenter(const SGVec3f& center)
{ setCenter(toVec3d(center)); } { setCenter(toVec3d(center)); }

View File

@ -23,6 +23,10 @@
# include <simgear_config.h> # include <simgear_config.h>
#endif #endif
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include "SGTranslateTransform.hxx" #include "SGTranslateTransform.hxx"
static inline void static inline void
@ -42,6 +46,14 @@ SGTranslateTransform::SGTranslateTransform() :
setReferenceFrame(RELATIVE_RF); setReferenceFrame(RELATIVE_RF);
} }
SGTranslateTransform::SGTranslateTransform(const SGTranslateTransform& trans,
const osg::CopyOp& copyop) :
osg::Transform(trans, copyop),
_axis(trans._axis),
_value(trans._value)
{
}
bool bool
SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix, SGTranslateTransform::computeLocalToWorldMatrix(osg::Matrix& matrix,
osg::NodeVisitor* nv) const osg::NodeVisitor* nv) const
@ -81,3 +93,57 @@ SGTranslateTransform::computeBound() const
bs._center += _axis.osg()*_value; bs._center += _axis.osg()*_value;
return bs; 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
);

View File

@ -28,6 +28,10 @@
class SGTranslateTransform : public osg::Transform { class SGTranslateTransform : public osg::Transform {
public: public:
SGTranslateTransform(); SGTranslateTransform();
SGTranslateTransform(const SGTranslateTransform&,
const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
META_Node(simgear, SGTranslateTransform);
void setAxis(const SGVec3d& axis) void setAxis(const SGVec3d& axis)
{ _axis = axis; dirtyBound(); } { _axis = axis; dirtyBound(); }

View File

@ -27,6 +27,10 @@
# error This library requires C++ # error This library requires C++
#endif #endif
#include <osgDB/Registry>
#include <osgDB/Input>
#include <osgDB/Output>
#include <simgear/compiler.h> #include <simgear/compiler.h>
#include <simgear/constants.h> #include <simgear/constants.h>
@ -69,6 +73,16 @@ SGPlacementTransform::SGPlacementTransform(void) :
setUpdateCallback(new UpdateCallback); 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) SGPlacementTransform::~SGPlacementTransform(void)
{ {
} }
@ -111,3 +125,97 @@ SGPlacementTransform::computeWorldToLocalMatrix(osg::Matrix& matrix,
matrix = t; matrix = t;
return true; 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
);

View File

@ -38,7 +38,10 @@ class SGPlacementTransform : public osg::Transform
public: public:
SGPlacementTransform(void); 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) void setTransform(const SGVec3d& off)
{ _placement_offset = off; dirtyBound(); } { _placement_offset = off; dirtyBound(); }
@ -49,10 +52,15 @@ public:
const SGVec3d& getGlobalPos() const const SGVec3d& getGlobalPos() const
{ return _placement_offset; } { 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 computeLocalToWorldMatrix(osg::Matrix&,osg::NodeVisitor*) const;
virtual bool computeWorldToLocalMatrix(osg::Matrix&,osg::NodeVisitor*) const; virtual bool computeWorldToLocalMatrix(osg::Matrix&,osg::NodeVisitor*) const;
protected:
virtual ~SGPlacementTransform(void);
private: private:
class UpdateCallback; class UpdateCallback;