Added DataVariance enum and set/get fields to osg::Object to help identify
which objects have values that vary over the lifetime of the object (DYNAMIC) and ones that do not vary (STATIC). Removed the equivalent code in osg::Transform, StateSet and StateAttribute, as these are now encompassed by the new DataVariance field. Removed MatrixMode enum from Matrix, and associated fields/parameters from osg::Transfrom and osg::NodeVisitor, since MatrixMode was not providing any useful functionality, but made the interface more complex (MatrixMode was an experimental API) Added ReferenceFrame field to osg::Transform which allows users to specify transforms that are relative to their parents (the default, and previous behavior) or absolute reference frame, which can be used for HUD's, camera relative light sources etc etc. Note, the view frustum culling for absolute Transform are disabled, and all their parents up to the root are also automatically have view frustum culling disabled. However, once passed an absolute Transform node culling will return to its default state of on, so you can still cull underneath an absolute transform, its only the culling above which is disabled.
This commit is contained in:
@@ -50,7 +50,7 @@ Node* OrientationConverter::convert( Node *node )
|
||||
osg::Group* root = new osg::Group;
|
||||
osg::Transform* transform = new osg::Transform;
|
||||
|
||||
transform->setType(osg::Transform::STATIC);
|
||||
transform->setDataVariance(osg::Object::STATIC);
|
||||
transform->setMatrix( C * R * S * T );
|
||||
|
||||
root->addChild(transform);
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
#include <math.h>
|
||||
#include <osg/Group>
|
||||
#include <osg/BoundingBox>
|
||||
#include <osg/Transform>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#define square(x) ((x)*(x))
|
||||
|
||||
using namespace osg;
|
||||
|
||||
Group::Group()
|
||||
@@ -202,6 +201,10 @@ const bool Group::computeBound() const
|
||||
_bsphere.init();
|
||||
if (_children.empty()) return false;
|
||||
|
||||
// note, special handling of the case when a child is an Transform,
|
||||
// such that only Transforms which are relative to their parents coordinates frame (i.e this group)
|
||||
// are handled, Transform relative to and absolute reference frame are ignored.
|
||||
|
||||
BoundingBox bb;
|
||||
bb.init();
|
||||
ChildList::const_iterator itr;
|
||||
@@ -209,7 +212,11 @@ const bool Group::computeBound() const
|
||||
itr!=_children.end();
|
||||
++itr)
|
||||
{
|
||||
bb.expandBy((*itr)->getBound());
|
||||
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(itr->get());
|
||||
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_TO_PARENTS)
|
||||
{
|
||||
bb.expandBy((*itr)->getBound());
|
||||
}
|
||||
}
|
||||
|
||||
if (!bb.isValid()) return false;
|
||||
@@ -220,7 +227,11 @@ const bool Group::computeBound() const
|
||||
itr!=_children.end();
|
||||
++itr)
|
||||
{
|
||||
_bsphere.expandRadiusBy((*itr)->getBound());
|
||||
const osg::Transform* transform = dynamic_cast<const osg::Transform*>(itr->get());
|
||||
if (!transform || transform->getReferenceFrame()==osg::Transform::RELATIVE_TO_PARENTS)
|
||||
{
|
||||
_bsphere.expandRadiusBy((*itr)->getBound());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@@ -57,14 +57,12 @@ class TransformVisitor : public NodeVisitor
|
||||
};
|
||||
|
||||
|
||||
MatrixMode _matrixMode;
|
||||
CoordMode _coordMode;
|
||||
Matrix& _matrix;
|
||||
NodeVisitor* _nodeVisitor;
|
||||
|
||||
TransformVisitor(Matrix& matrix,MatrixMode matrixMode,CoordMode coordMode,NodeVisitor* nv):
|
||||
TransformVisitor(Matrix& matrix,CoordMode coordMode,NodeVisitor* nv):
|
||||
NodeVisitor(),
|
||||
_matrixMode(matrixMode),
|
||||
_coordMode(coordMode),
|
||||
_matrix(matrix),
|
||||
_nodeVisitor(nv)
|
||||
@@ -72,51 +70,44 @@ class TransformVisitor : public NodeVisitor
|
||||
|
||||
virtual void apply(Transform& transform)
|
||||
{
|
||||
bool applyTransform =
|
||||
(_matrixMode==transform.getMatrixMode()) ||
|
||||
(_matrixMode==MODELVIEW && (transform.getMatrixMode()==MODEL || transform.getMatrixMode()==VIEW));
|
||||
|
||||
if (applyTransform)
|
||||
if (_coordMode==LOCAL_TO_WORLD)
|
||||
{
|
||||
if (_coordMode==LOCAL_TO_WORLD)
|
||||
{
|
||||
osg::Matrix localToWorldMat;
|
||||
transform.getLocalToWorldMatrix(localToWorldMat,_nodeVisitor);
|
||||
_matrix.preMult(localToWorldMat);
|
||||
}
|
||||
else // worldToLocal
|
||||
{
|
||||
osg::Matrix worldToLocalMat;
|
||||
transform.getWorldToLocalMatrix(worldToLocalMat,_nodeVisitor);
|
||||
_matrix.postMult(worldToLocalMat);
|
||||
}
|
||||
osg::Matrix localToWorldMat;
|
||||
transform.getLocalToWorldMatrix(localToWorldMat,_nodeVisitor);
|
||||
_matrix.preMult(localToWorldMat);
|
||||
}
|
||||
else // worldToLocal
|
||||
{
|
||||
osg::Matrix worldToLocalMat;
|
||||
transform.getWorldToLocalMatrix(worldToLocalMat,_nodeVisitor);
|
||||
_matrix.postMult(worldToLocalMat);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
const bool NodeVisitor::getLocalToWorldMatrix(Matrix& matrix, MatrixMode mode, Node* node)
|
||||
const bool NodeVisitor::getLocalToWorldMatrix(Matrix& matrix, Node* node)
|
||||
{
|
||||
TransformVisitor tv(matrix,mode,TransformVisitor::LOCAL_TO_WORLD,this);
|
||||
TransformVisitor tv(matrix,TransformVisitor::LOCAL_TO_WORLD,this);
|
||||
for(NodePath::iterator itr=_nodePath.begin();
|
||||
itr!=_nodePath.end();
|
||||
++itr)
|
||||
{
|
||||
if (*itr==node) return true; // don't account for matrix attached to specofied node
|
||||
if (*itr==node) return true; // don't account for matrix attached to specified node
|
||||
(*itr)->accept(tv);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const bool NodeVisitor::getWorldToLocalMatrix(Matrix& matrix, MatrixMode mode, Node* node)
|
||||
const bool NodeVisitor::getWorldToLocalMatrix(Matrix& matrix, Node* node)
|
||||
{
|
||||
TransformVisitor tv(matrix,mode,TransformVisitor::WORLD_TO_LOCAL,this);
|
||||
TransformVisitor tv(matrix,TransformVisitor::WORLD_TO_LOCAL,this);
|
||||
for(NodePath::iterator itr=_nodePath.begin();
|
||||
itr!=_nodePath.end();
|
||||
++itr)
|
||||
{
|
||||
if (*itr==node) return true; // don't account for matrix attached to specofied node
|
||||
if (*itr==node) return true; // don't account for matrix attached to specified node
|
||||
(*itr)->accept(tv);
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -14,5 +14,6 @@ Referenced::~Referenced()
|
||||
}
|
||||
|
||||
|
||||
Object::Object(const Object&,const CopyOp&):
|
||||
Referenced() {}
|
||||
Object::Object(const Object& obj,const CopyOp&):
|
||||
Referenced(),
|
||||
_dataVariance(obj._dataVariance) {}
|
||||
|
||||
@@ -8,33 +8,36 @@ PositionAttitudeTransform::PositionAttitudeTransform()
|
||||
|
||||
const bool PositionAttitudeTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
|
||||
{
|
||||
if (_mode==MODEL || _mode==MODELVIEW)
|
||||
if (_referenceFrame==RELATIVE_TO_PARENTS)
|
||||
{
|
||||
osg::Matrix tmp;
|
||||
tmp.makeRotate(_attitude);
|
||||
tmp.setTrans(_position);
|
||||
|
||||
matrix.preMult(tmp);
|
||||
}
|
||||
else // absolute
|
||||
{
|
||||
matrix.makeRotate(_attitude);
|
||||
matrix.setTrans(_position);
|
||||
return true;
|
||||
}
|
||||
else // _mode==VIEW
|
||||
{
|
||||
matrix.makeTranslate(-_position);
|
||||
matrix.postMult(osg::Matrix::rotate(_attitude.inverse()));
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
const bool PositionAttitudeTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
|
||||
{
|
||||
if (_mode==MODEL || _mode==MODELVIEW)
|
||||
if (_referenceFrame==RELATIVE_TO_PARENTS)
|
||||
{
|
||||
osg::Matrix tmp;
|
||||
tmp.makeTranslate(-_position);
|
||||
tmp.postMult(osg::Matrix::rotate(_attitude.inverse()));
|
||||
matrix.postMult(tmp);
|
||||
}
|
||||
else // absolute
|
||||
{
|
||||
matrix.makeTranslate(-_position);
|
||||
matrix.postMult(osg::Matrix::rotate(_attitude.inverse()));
|
||||
return true;
|
||||
}
|
||||
else // _mode==VIEW
|
||||
{
|
||||
matrix.makeRotate(_attitude);
|
||||
matrix.setTrans(_position);
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,9 @@ using namespace osg;
|
||||
|
||||
StateSet::StateSet()
|
||||
{
|
||||
setDataVariance(osg::StateAttribute::STATIC);
|
||||
|
||||
_renderingHint = DEFAULT_BIN;
|
||||
_datatype = osg::StateAttribute::STATIC;
|
||||
|
||||
setRendingBinToInherit();
|
||||
}
|
||||
@@ -41,7 +42,6 @@ StateSet::StateSet(const StateSet& rhs,const CopyOp& copyop):Object(rhs,copyop)
|
||||
_binMode = rhs._binMode;
|
||||
_binNum = rhs._binNum;
|
||||
_binName = rhs._binName;
|
||||
_datatype = rhs._datatype;
|
||||
}
|
||||
|
||||
StateSet::~StateSet()
|
||||
|
||||
@@ -4,8 +4,7 @@ using namespace osg;
|
||||
|
||||
Transform::Transform()
|
||||
{
|
||||
_type = DYNAMIC;
|
||||
_mode = MODEL;
|
||||
_referenceFrame = RELATIVE_TO_PARENTS;
|
||||
|
||||
_matrix = osgNew Matrix;
|
||||
_inverse = osgNew Matrix;
|
||||
@@ -14,9 +13,8 @@ Transform::Transform()
|
||||
|
||||
Transform::Transform(const Transform& transform,const CopyOp& copyop):
|
||||
Group(transform,copyop),
|
||||
_type(transform._type),
|
||||
_mode(transform._mode),
|
||||
_computeTransformCallback(_computeTransformCallback),
|
||||
_referenceFrame(transform._referenceFrame),
|
||||
_matrix(osgNew Matrix(*transform._matrix)),
|
||||
_inverse(osgNew Matrix(*transform._inverse)),
|
||||
_inverseDirty(transform._inverseDirty)
|
||||
@@ -25,8 +23,7 @@ Transform::Transform(const Transform& transform,const CopyOp& copyop):
|
||||
|
||||
Transform::Transform(const Matrix& mat )
|
||||
{
|
||||
_type = DYNAMIC;
|
||||
_mode = MODEL;
|
||||
_referenceFrame = RELATIVE_TO_PARENTS;
|
||||
|
||||
_matrix = osgNew Matrix(mat);
|
||||
_inverse = osgNew Matrix();
|
||||
@@ -38,6 +35,17 @@ Transform::~Transform()
|
||||
{
|
||||
}
|
||||
|
||||
void Transform::setReferenceFrame(ReferenceFrame rf)
|
||||
{
|
||||
if (_referenceFrame == rf) return;
|
||||
|
||||
_referenceFrame = rf;
|
||||
|
||||
// switch off culling if transform is absolute.
|
||||
if (_referenceFrame==ABSOLUTE) setCullingActive(false);
|
||||
else setCullingActive(true);
|
||||
}
|
||||
|
||||
const bool Transform::computeBound() const
|
||||
{
|
||||
if (!Group::computeBound()) return false;
|
||||
@@ -45,41 +53,36 @@ const bool Transform::computeBound() const
|
||||
// note, NULL pointer for NodeVisitor, so compute's need
|
||||
// to handle this case gracefully, normally this should not be a problem.
|
||||
Matrix l2w;
|
||||
if (_mode!=PROJECTION && getLocalToWorldMatrix(l2w,NULL))
|
||||
{
|
||||
|
||||
Vec3 xdash = _bsphere._center;
|
||||
xdash.x() += _bsphere._radius;
|
||||
xdash = xdash*l2w;
|
||||
getLocalToWorldMatrix(l2w,NULL);
|
||||
|
||||
Vec3 ydash = _bsphere._center;
|
||||
ydash.y() += _bsphere._radius;
|
||||
ydash = ydash*l2w;
|
||||
Vec3 xdash = _bsphere._center;
|
||||
xdash.x() += _bsphere._radius;
|
||||
xdash = xdash*l2w;
|
||||
|
||||
Vec3 zdash = _bsphere._center;
|
||||
zdash.y() += _bsphere._radius;
|
||||
zdash = zdash*l2w;
|
||||
Vec3 ydash = _bsphere._center;
|
||||
ydash.y() += _bsphere._radius;
|
||||
ydash = ydash*l2w;
|
||||
|
||||
_bsphere._center = _bsphere._center*l2w;
|
||||
Vec3 zdash = _bsphere._center;
|
||||
zdash.y() += _bsphere._radius;
|
||||
zdash = zdash*l2w;
|
||||
|
||||
xdash -= _bsphere._center;
|
||||
float len_xdash = xdash.length();
|
||||
_bsphere._center = _bsphere._center*l2w;
|
||||
|
||||
ydash -= _bsphere._center;
|
||||
float len_ydash = ydash.length();
|
||||
xdash -= _bsphere._center;
|
||||
float len_xdash = xdash.length();
|
||||
|
||||
zdash -= _bsphere._center;
|
||||
float len_zdash = zdash.length();
|
||||
ydash -= _bsphere._center;
|
||||
float len_ydash = ydash.length();
|
||||
|
||||
_bsphere._radius = len_xdash;
|
||||
if (_bsphere._radius<len_ydash) _bsphere._radius = len_ydash;
|
||||
if (_bsphere._radius<len_zdash) _bsphere._radius = len_zdash;
|
||||
zdash -= _bsphere._center;
|
||||
float len_zdash = zdash.length();
|
||||
|
||||
_bsphere._radius = len_xdash;
|
||||
if (_bsphere._radius<len_ydash) _bsphere._radius = len_ydash;
|
||||
if (_bsphere._radius<len_zdash) _bsphere._radius = len_zdash;
|
||||
|
||||
return true;
|
||||
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_bsphere.init();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -603,7 +603,7 @@ osg::Node* ConvertFromFLT::visitDOF(osg::Group* osgParent, DofRecord* rec)
|
||||
|
||||
visitPrimaryNode(transform, (PrimNodeRecord*)rec);
|
||||
transform->setName(rec->getData()->szIdent);
|
||||
transform->setType(osg::Transform::DYNAMIC);
|
||||
transform->setDataVariance(osg::Object::DYNAMIC);
|
||||
|
||||
// note for Judd (and others) shouldn't there be code in here to set up the transform matrix?
|
||||
// as a transform with an identity matrix is effectively only a
|
||||
@@ -1179,7 +1179,7 @@ osg::Node* ConvertFromFLT::visitMatrix(osg::Group* osgParent, MatrixRecord* rec)
|
||||
pos *= (float)_unitScale;
|
||||
m *= osg::Matrix::translate(pos);
|
||||
|
||||
transform->setType(osg::Transform::STATIC);
|
||||
transform->setDataVariance(osg::Object::STATIC);
|
||||
transform->setMatrix(m);
|
||||
|
||||
osgParent->addChild(transform);
|
||||
|
||||
@@ -8,8 +8,8 @@ using namespace osg;
|
||||
using namespace osgDB;
|
||||
|
||||
// forward declare functions to use later.
|
||||
// bool Object_readLocalData(Object& obj, Input& fr);
|
||||
// bool Object_writeLocalData(const Object& obj, Output& fw);
|
||||
bool Object_readLocalData(Object& obj, Input& fr);
|
||||
bool Object_writeLocalData(const Object& obj, Output& fw);
|
||||
|
||||
// register the read and write functions with the osgDB::Registry.
|
||||
// note, Object doesn't currently require any read and write.
|
||||
@@ -18,6 +18,41 @@ RegisterDotOsgWrapperProxy g_ObjectProxy
|
||||
/*new osg::Object*/NULL,
|
||||
"Object",
|
||||
"Object",
|
||||
NULL,
|
||||
NULL
|
||||
&Object_readLocalData,
|
||||
&Object_writeLocalData
|
||||
);
|
||||
|
||||
bool Object_readLocalData(Object& obj, Input& fr)
|
||||
{
|
||||
bool iteratorAdvanced = false;
|
||||
|
||||
if (fr[0].matchWord("DataVariance"))
|
||||
{
|
||||
if (fr[1].matchWord("DYNAMIC"))
|
||||
{
|
||||
obj.setDataVariance(osg::Object::DYNAMIC);
|
||||
fr +=2 ;
|
||||
iteratorAdvanced = true;
|
||||
}
|
||||
else if (fr[1].matchWord("STATIC"))
|
||||
{
|
||||
obj.setDataVariance(osg::Object::STATIC);
|
||||
fr +=2 ;
|
||||
iteratorAdvanced = true;
|
||||
}
|
||||
}
|
||||
|
||||
return iteratorAdvanced;
|
||||
}
|
||||
|
||||
|
||||
bool Object_writeLocalData(const Object& obj, Output& fw)
|
||||
{
|
||||
switch(obj.getDataVariance())
|
||||
{
|
||||
case(osg::Object::STATIC): fw.indent() << "DataVariance STATIC" << std::endl;break;
|
||||
default: fw.indent() << "DataVariance DYNAMIC" << std::endl;break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -44,13 +44,15 @@ bool Transform_readLocalData(Object& obj, Input& fr)
|
||||
{
|
||||
if (fr[1].matchWord("DYNAMIC"))
|
||||
{
|
||||
transform.setType(osg::Transform::DYNAMIC);
|
||||
transform.setDataVariance(osg::Object::DYNAMIC);
|
||||
fr +=2 ;
|
||||
iteratorAdvanced = true;
|
||||
}
|
||||
else if (fr[1].matchWord("STATIC"))
|
||||
{
|
||||
transform.setType(osg::Transform::STATIC);
|
||||
transform.setDataVariance(osg::Object::STATIC);
|
||||
fr +=2 ;
|
||||
iteratorAdvanced = true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -75,12 +77,6 @@ bool Transform_writeLocalData(const Object& obj, Output& fw)
|
||||
{
|
||||
const Transform& transform = static_cast<const Transform&>(obj);
|
||||
|
||||
switch(transform.getType())
|
||||
{
|
||||
case(osg::Transform::STATIC): fw.indent() << "Type STATIC" << std::endl;break;
|
||||
default: fw.indent() << "Type DYNAMIC" << std::endl;break;
|
||||
}
|
||||
|
||||
fw.writeObject(transform.getMatrix());
|
||||
|
||||
return true;
|
||||
|
||||
@@ -577,8 +577,8 @@ void CullVisitor::apply(Transform& node)
|
||||
if (node_state) pushStateSet(node_state);
|
||||
|
||||
ref_ptr<osg::Matrix> matrix = createOrReuseMatrix();
|
||||
*matrix = *getCurrentMatrix();
|
||||
node.getLocalToWorldMatrix(*matrix,this);
|
||||
matrix->postMult(*getCurrentMatrix());
|
||||
pushModelViewMatrix(matrix.get());
|
||||
|
||||
traverse(node);
|
||||
|
||||
@@ -129,7 +129,7 @@ void Optimizer::StateVisitor::addStateSet(osg::StateSet* stateset,osg::Object* o
|
||||
void Optimizer::StateVisitor::apply(osg::Node& node)
|
||||
{
|
||||
osg::StateSet* ss = node.getStateSet();
|
||||
if (ss && ss->getDataType()==osg::StateAttribute::STATIC) addStateSet(ss,&node);
|
||||
if (ss && ss->getDataVariance()==osg::Object::STATIC) addStateSet(ss,&node);
|
||||
|
||||
traverse(node);
|
||||
}
|
||||
@@ -137,14 +137,14 @@ void Optimizer::StateVisitor::apply(osg::Node& node)
|
||||
void Optimizer::StateVisitor::apply(osg::Geode& geode)
|
||||
{
|
||||
osg::StateSet* ss = geode.getStateSet();
|
||||
if (ss && ss->getDataType()==osg::StateAttribute::STATIC) addStateSet(ss,&geode);
|
||||
if (ss && ss->getDataVariance()==osg::Object::STATIC) addStateSet(ss,&geode);
|
||||
for(int i=0;i<geode.getNumDrawables();++i)
|
||||
{
|
||||
osg::Drawable* drawable = geode.getDrawable(i);
|
||||
if (drawable)
|
||||
{
|
||||
ss = drawable->getStateSet();
|
||||
if (ss && ss->getDataType()==osg::StateAttribute::STATIC) addStateSet(ss,drawable);
|
||||
if (ss && ss->getDataVariance()==osg::Object::STATIC) addStateSet(ss,drawable);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,7 +172,7 @@ void Optimizer::StateVisitor::optimize()
|
||||
aitr!=attributes.end();
|
||||
++aitr)
|
||||
{
|
||||
if (aitr->second.first->getDataType()==osg::StateAttribute::STATIC)
|
||||
if (aitr->second.first->getDataVariance()==osg::Object::STATIC)
|
||||
{
|
||||
_attributeToStateSetMap[aitr->second.first.get()].insert(sitr->first);
|
||||
}
|
||||
@@ -328,7 +328,7 @@ void Optimizer::FlattenStaticTransformsVisitor::apply(osg::LOD& lod)
|
||||
|
||||
void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Transform& transform)
|
||||
{
|
||||
if (_ignoreDynamicTransforms && transform.getType()==osg::Transform::DYNAMIC)
|
||||
if (_ignoreDynamicTransforms && transform.getDataVariance()==osg::Object::DYNAMIC)
|
||||
{
|
||||
// simple traverse the children as if this Transform didn't exist.
|
||||
traverse(transform);
|
||||
|
||||
Reference in New Issue
Block a user