Have made osg::Transform more extensible via additions of new getLocalToWorldMatrix()
and getWorldToLocalMatrix(), computeLocalToWorld() and computeWorldToLocal() methods. Have updated the CullVisitor, IntersectVisitor and Optimizer to use the new osg::Transform::getLocalToWorldMatrix() which has the same functionality as the old getMatrix() but is now supports subclasses of osg::Transform transparently. Have added osg::PositionAttitudeTransform as subclass of osg::Transform which manages the transform as position and attitude via a Vec3 and Quat respectively.
This commit is contained in:
@@ -41,6 +41,7 @@ C++FILES = \
|
||||
Point.cpp\
|
||||
PolygonMode.cpp\
|
||||
PolygonOffset.cpp\
|
||||
PositionAttitudeTransform.cpp\
|
||||
Quat.cpp\
|
||||
ShadeModel.cpp\
|
||||
State.cpp\
|
||||
@@ -108,6 +109,7 @@ TARGET_INCLUDE_FILES = \
|
||||
osg/Point\
|
||||
osg/PolygonMode\
|
||||
osg/PolygonOffset\
|
||||
osg/PositionAttitudeTransform\
|
||||
osg/Plane\
|
||||
osg/Quat\
|
||||
osg/Referenced\
|
||||
|
||||
43
src/osg/PositionAttitudeTransform.cpp
Normal file
43
src/osg/PositionAttitudeTransform.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <osg/PositionAttitudeTransform>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
PositionAttitudeTransform::PositionAttitudeTransform()
|
||||
{
|
||||
}
|
||||
|
||||
void PositionAttitudeTransform::computeLocalToWorld() const
|
||||
{
|
||||
if (_localToWorldDirty)
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
_localToWorld->makeRotate(_attitude);
|
||||
_localToWorld->setTrans(_position);
|
||||
}
|
||||
else
|
||||
{
|
||||
_localToWorld->makeTranslate(-_position);
|
||||
_localToWorld->postMult(osg::Matrix::rotate(_attitude.inverse()));
|
||||
}
|
||||
_localToWorldDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
void PositionAttitudeTransform::computeWorldToLocal() const
|
||||
{
|
||||
if (_worldToLocalDirty)
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
_worldToLocal->makeTranslate(-_position);
|
||||
_worldToLocal->postMult(osg::Matrix::rotate(_attitude.inverse()));
|
||||
}
|
||||
else
|
||||
{
|
||||
_worldToLocal->makeRotate(_attitude);
|
||||
_worldToLocal->setTrans(_position);
|
||||
}
|
||||
_worldToLocalDirty = false;
|
||||
}
|
||||
}
|
||||
@@ -5,25 +5,28 @@ using namespace osg;
|
||||
Transform::Transform()
|
||||
{
|
||||
_type = DYNAMIC;
|
||||
_mode = MODEL;
|
||||
|
||||
_matrix = new Matrix;
|
||||
_matrix->makeIdentity();
|
||||
_matrixDirty = false;
|
||||
|
||||
_inverse = new Matrix;
|
||||
_inverse->makeIdentity();
|
||||
_inverseDirty = false;
|
||||
_localToWorld = new Matrix;
|
||||
_localToWorld->makeIdentity();
|
||||
_localToWorldDirty = false;
|
||||
|
||||
_worldToLocal = new Matrix;
|
||||
_worldToLocal->makeIdentity();
|
||||
_worldToLocalDirty = false;
|
||||
}
|
||||
|
||||
|
||||
Transform::Transform(const Matrix& mat )
|
||||
{
|
||||
_type = DYNAMIC;
|
||||
_mode = MODEL;
|
||||
|
||||
_matrix = new Matrix(mat);
|
||||
_matrixDirty = false;
|
||||
_localToWorld = new Matrix(mat);
|
||||
_localToWorldDirty = false;
|
||||
|
||||
_inverseDirty = true; // will neeed to recompute.
|
||||
_worldToLocal = new Matrix;
|
||||
_worldToLocalDirty = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -31,25 +34,81 @@ Transform::~Transform()
|
||||
{
|
||||
}
|
||||
|
||||
void Transform::setMatrix(const Matrix& mat )
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
(*_localToWorld) = mat;
|
||||
_localToWorldDirty = false;
|
||||
_worldToLocalDirty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*_worldToLocal) = mat;
|
||||
_worldToLocalDirty = false;
|
||||
_localToWorldDirty = true;
|
||||
}
|
||||
|
||||
dirtyBound();
|
||||
}
|
||||
|
||||
/** preMult transform.*/
|
||||
void Transform::preMult( const Matrix& mat )
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
_localToWorld->preMult(mat);
|
||||
_localToWorldDirty = false;
|
||||
_worldToLocalDirty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_worldToLocal->preMult(mat);
|
||||
_worldToLocalDirty = false;
|
||||
_localToWorldDirty = true;
|
||||
}
|
||||
|
||||
dirtyBound();
|
||||
}
|
||||
|
||||
/** postMult transform.*/
|
||||
void Transform::postMult( const Matrix& mat )
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
_localToWorld->postMult(mat);
|
||||
_localToWorldDirty = false;
|
||||
_worldToLocalDirty = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_worldToLocal->postMult(mat);
|
||||
_worldToLocalDirty = false;
|
||||
_localToWorldDirty = true;
|
||||
}
|
||||
|
||||
dirtyBound();
|
||||
}
|
||||
|
||||
const bool Transform::computeBound() const
|
||||
{
|
||||
if (!Group::computeBound()) return false;
|
||||
|
||||
if (_matrixDirty) computeMatrix();
|
||||
if (_localToWorldDirty) computeLocalToWorld();
|
||||
|
||||
Vec3 xdash = _bsphere._center;
|
||||
xdash.x() += _bsphere._radius;
|
||||
xdash = xdash*(*_matrix);
|
||||
xdash = xdash*(*_localToWorld);
|
||||
|
||||
Vec3 ydash = _bsphere._center;
|
||||
ydash.y() += _bsphere._radius;
|
||||
ydash = ydash*(*_matrix);
|
||||
ydash = ydash*(*_localToWorld);
|
||||
|
||||
Vec3 zdash = _bsphere._center;
|
||||
zdash.y() += _bsphere._radius;
|
||||
zdash = zdash*(*_matrix);
|
||||
zdash = zdash*(*_localToWorld);
|
||||
|
||||
_bsphere._center = _bsphere._center*(*_matrix);
|
||||
_bsphere._center = _bsphere._center*(*_localToWorld);
|
||||
|
||||
xdash -= _bsphere._center;
|
||||
float len_xdash = xdash.length();
|
||||
@@ -66,3 +125,27 @@ const bool Transform::computeBound() const
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Transform::computeLocalToWorld() const
|
||||
{
|
||||
if (_localToWorldDirty)
|
||||
{
|
||||
if (_mode==VIEW)
|
||||
{
|
||||
_localToWorld->invert(*_worldToLocal);
|
||||
}
|
||||
_localToWorldDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
void Transform::computeWorldToLocal() const
|
||||
{
|
||||
if (_worldToLocalDirty)
|
||||
{
|
||||
if (_mode==MODEL)
|
||||
{
|
||||
_worldToLocal->invert(*_localToWorld);
|
||||
}
|
||||
_worldToLocalDirty = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1054,7 +1054,7 @@ void CullVisitor::apply(Transform& node)
|
||||
StateSet* node_state = node.getStateSet();
|
||||
if (node_state) pushStateSet(node_state);
|
||||
|
||||
pushCullViewState(&node.getMatrix());
|
||||
pushCullViewState(&node.getLocalToWorldMatrix());
|
||||
|
||||
traverse(node);
|
||||
|
||||
|
||||
@@ -555,7 +555,7 @@ void IntersectVisitor::apply(Transform& node)
|
||||
{
|
||||
if (!enterNode(node)) return;
|
||||
|
||||
pushMatrix(node.getMatrix());
|
||||
pushMatrix(node.getLocalToWorldMatrix());
|
||||
|
||||
traverse(node);
|
||||
|
||||
|
||||
@@ -330,11 +330,11 @@ void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Transform& transform)
|
||||
{
|
||||
if (_matrixStack.empty())
|
||||
{
|
||||
_matrixStack.push_back(transform.getMatrix());
|
||||
_matrixStack.push_back(transform.getLocalToWorldMatrix());
|
||||
}
|
||||
else
|
||||
{
|
||||
_matrixStack.push_back(transform.getMatrix()*_matrixStack.back());
|
||||
_matrixStack.push_back(transform.getLocalToWorldMatrix()*_matrixStack.back());
|
||||
}
|
||||
|
||||
_transformStack.push_back(&transform);
|
||||
|
||||
Reference in New Issue
Block a user