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:
Robert Osfield 2002-04-11 23:20:23 +00:00
parent e85e5a6ce6
commit 6ed233d0d2
18 changed files with 202 additions and 175 deletions

View File

@ -17,16 +17,6 @@ namespace osg {
class Quat;
/** The range of matrix modes that the scene graph might utilize.
* Similar in concept to different modes in OpenGL glMatrixMode().*/
enum MatrixMode
{
PROJECTION,
VIEW,
MODEL,
MODELVIEW
};
class SG_EXPORT Matrix : public Object
{
@ -128,6 +118,7 @@ class SG_EXPORT Matrix : public Object
inline static Matrix rotate( float, float, float, float );
inline static Matrix rotate( float angle, const Vec3& axis);
inline static Matrix rotate( const Quat&);
inline static Matrix inverse( const Matrix&);
/** Create a orthographic projection. See glOrtho for further details.*/
inline static Matrix ortho(const double left, const double right,
@ -276,6 +267,13 @@ inline Matrix Matrix::rotate(const Vec3& from, const Vec3& to )
return m;
}
inline Matrix inverse( const Matrix& matrix)
{
Matrix m;
m.invert(matrix);
return m;
}
inline Matrix Matrix::ortho(const double left, const double right,
const double bottom, const double top,
const double zNear, const double zFar)

View File

@ -158,10 +158,10 @@ class SG_EXPORT NodeVisitor : public Referenced
const NodePath& getNodePath() const { return _nodePath; }
/** Get the Local To World Matrix from the NodePath for specified Transform::Mode, and u.*/
const bool getLocalToWorldMatrix(Matrix& matrix, MatrixMode mode,Node* node);
const bool getLocalToWorldMatrix(Matrix& matrix, Node* node);
/** Get the World To Local Matrix from the NodePath for specified Transform::Mode.*/
const bool getWorldToLocalMatrix(Matrix& matrix, MatrixMode mode,Node* node);
const bool getWorldToLocalMatrix(Matrix& matrix, Node* node);
virtual void apply(Node& node) { traverse(node);}

View File

@ -34,7 +34,7 @@ class SG_EXPORT Object : public Referenced
and therefore cannot be constructed on its own, only derived
classes which override the clone and className methods are
concrete classes and can be constructed.*/
Object() {}
inline Object():Referenced(),_dataVariance(DYNAMIC) {}
/** Copy constructor, optional CopyOp object can be used to control
* shallow vs deep copying of dynamic data.*/
@ -54,6 +54,23 @@ class SG_EXPORT Object : public Referenced
by derived classes.*/
virtual const char* className() const = 0;
enum DataVariance
{
DYNAMIC,
STATIC
};
/** Set the data variance of this object.
* Can be set to either STATIC for values that do not change over the lifetime of the object,
* or DYNAMIC for values that vary over the lifetime of the object. The DataVariance value
* can be used be routines such as optimzation codes that wish to share static data.*/
inline void setDataVariance(const DataVariance dv) { _dataVariance = dv; }
/** Get the data variance of this object.*/
inline const DataVariance getDataVariance() const { return _dataVariance; }
protected:
/** Object destructor. Note, is protected so that Objects cannot
@ -65,6 +82,8 @@ class SG_EXPORT Object : public Referenced
= new Node().*/
virtual ~Object() {}
DataVariance _dataVariance;
private:
/** disallow any copy operator.*/

View File

@ -149,19 +149,11 @@ class SG_EXPORT StateAttribute : public Object
COLORMATRIX
};
/** Range of types that the StateAttribute can be.*/
enum DataType
{
DYNAMIC,
STATIC
};
StateAttribute():_datatype(STATIC) {}
StateAttribute() { setDataVariance(STATIC); }
StateAttribute(const StateAttribute& sa,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(sa,copyop),_datatype(sa._datatype) {}
Object(sa,copyop) {}
/** Clone the type of an attribute, with Object* return type.
@ -182,10 +174,6 @@ class SG_EXPORT StateAttribute : public Object
virtual const Type getType() const = 0;
inline void setDataType(DataType type) { _datatype = type; }
inline const DataType getDataType() const { return _datatype; }
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
virtual int compare(const StateAttribute& sa) const = 0;
@ -213,8 +201,6 @@ class SG_EXPORT StateAttribute : public Object
virtual ~StateAttribute() {}
DataType _datatype;
};
}

View File

@ -50,9 +50,6 @@ class SG_EXPORT StateSet : public Object
nodes which inherit all of their modes for the global state.*/
void setAllToInherit();
inline void setDataType(osg::StateAttribute::DataType type) { _datatype = type; }
inline const osg::StateAttribute::DataType getDataType() const { return _datatype; }
/** merge this stateset with stateset rhs, this overrides
* the rhs if OVERRIDE is specified, otherwise rhs takes precedence.*/
void merge(const StateSet& rhs);
@ -172,8 +169,6 @@ class SG_EXPORT StateSet : public Object
int _binNum;
std::string _binName;
osg::StateAttribute::DataType _datatype;
};
}

View File

@ -40,33 +40,26 @@ class SG_EXPORT Transform : public Group
META_Node(Transform);
/** Range of types that the Transform can be.*/
enum Type
enum ReferenceFrame
{
DYNAMIC,
STATIC
RELATIVE_TO_PARENTS,
ABSOLUTE
};
/** Set the Transform Type, which can be DYNAMIC - the Matrix
* value is updated during the main loop, or STATIC - the Matrix
* is constant throughout the life of the main loop. STATIC
* Transforms can be optimized away is some instances, which
* can improve performance so unless you plan to modify the
* Matrix explicitly set the Matrix to STATIC. The default
* value is DYNAMIC.*/
inline void setType(Type type) { _type = type; }
/** Set the transform's ReferenceFrame, either to be realtive to its parent reference frame,
* or relative to an absolute coordinate frame. RELATIVE_TO_PARENTS is the default.
* Note, setting the RefrenceFrame to be ABSOLUTE will also set the CullingActive flag on the
* transform, and hence all its parents, to false, therby disabling culling of it and all its
* parents. This is neccessary to prevent inappropriate culling, but may impact of cull times
* if the absolute transform is deep in the scene graph, it is therefore recommend to only use
* abolsoute Transforms at the top of the scene, for such things as headlight LightSource's or
* Head up displays.*/
void setReferenceFrame(ReferenceFrame rf);
/** Get the Transform Type.*/
inline const Type getType() const { return _type; }
const ReferenceFrame getReferenceFrame() const { return _referenceFrame; }
/** Set the matrix mode which tells traversers them how to treat this Transform - as Projection, View or Model transformation.*/
inline void setMatrixMode(MatrixMode mode) { _mode = mode; }
/** Get the transform mode.*/
inline const MatrixMode getMatrixMode() const { return _mode; }
/** Callback attached to an Transform to specifiy how to compute the modelview or projection transformation
/** Callback attached to an Transform to specifiy how to compute the modelview transformation
* for the transform below the Transform node.*/
struct ComputeTransformCallback : public osg::Referenced
{
@ -137,32 +130,28 @@ class SG_EXPORT Transform : public Group
virtual const bool computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_mode==VIEW)
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
computeInverse();
matrix = *_inverse;
return true;
matrix.preMult(*_matrix);
}
else
else // absolute
{
matrix = *_matrix;
return true;
}
return true;
}
virtual const bool computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_mode==VIEW)
if (_referenceFrame==RELATIVE_TO_PARENTS)
{
matrix = *_matrix;
return true;
matrix.postMult(*_inverse);
}
else
else // absolute
{
computeInverse();
matrix = *_inverse;
return true;
}
return true;
}
inline void computeInverse() const
@ -175,10 +164,10 @@ class SG_EXPORT Transform : public Group
}
Type _type;
MatrixMode _mode;
ref_ptr<ComputeTransformCallback> _computeTransformCallback;
ReferenceFrame _referenceFrame;
ref_ptr<Matrix> _matrix;
mutable ref_ptr<Matrix> _inverse;
mutable bool _inverseDirty;

View File

@ -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);

View File

@ -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,15 +201,23 @@ 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;
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
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;
@ -219,9 +226,13 @@ const bool Group::computeBound() const
for(itr=_children.begin();
itr!=_children.end();
++itr)
{
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;
}

View File

@ -57,26 +57,18 @@ 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)
{}
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)
{
@ -91,32 +83,31 @@ class TransformVisitor : public 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;

View File

@ -14,5 +14,6 @@ Referenced::~Referenced()
}
Object::Object(const Object&,const CopyOp&):
Referenced() {}
Object::Object(const Object& obj,const CopyOp&):
Referenced(),
_dataVariance(obj._dataVariance) {}

View File

@ -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;
}
}
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;
}
}

View File

@ -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()

View File

@ -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,8 +53,8 @@ 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))
{
getLocalToWorldMatrix(l2w,NULL);
Vec3 xdash = _bsphere._center;
xdash.x() += _bsphere._radius;
@ -76,10 +84,5 @@ const bool Transform::computeBound() const
if (_bsphere._radius<len_zdash) _bsphere._radius = len_zdash;
return true;
}
else
{
_bsphere.init();
return false;
}
}

View File

@ -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);

View File

@ -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;
}

View File

@ -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;

View File

@ -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);

View File

@ -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);