Have taken a few more steps towards support for view dependant transformations

by adding a ComputeTransformCallback to osg::Transform, and have now removed
the recently added AutoTransform since it is nolonger required.  Have also
updated CullVisitor to account for the new ways for tracking transformation
matrices in the scene.
This commit is contained in:
Robert Osfield
2002-02-05 21:54:46 +00:00
parent 7293af59ed
commit a703130aa0
18 changed files with 255 additions and 343 deletions

View File

@@ -1,25 +0,0 @@
#include <osg/AutoTransform>
using namespace osg;
AutoTransform::AutoTransform()
{
}
AutoTransform::AutoTransform(const AutoTransform& transform,const CopyOp& copyop):
Group(transform,copyop),
_calcTransformCallback(dynamic_cast<CalcTransformCallback*>(copyop(transform._calcTransformCallback.get())))
{
}
AutoTransform::~AutoTransform()
{
}
const bool AutoTransform::computeBound() const
{
// can't calc the bound of a automatically transform object,
// it position isn't know until cull time.
return false;
}

View File

@@ -3,7 +3,6 @@ include $(OSGHOME)/Make/makedefs
C++FILES = \
AlphaFunc.cpp\
AutoTransform.cpp\
Billboard.cpp\
BoundingBox.cpp\
BoundingSphere.cpp\
@@ -68,7 +67,6 @@ TARGET_LIB_FILES = lib$(TARGET_BASENAME).$(SO_EXT)
TARGET_INCLUDE_FILES = \
osg/Notify\
osg/AlphaFunc\
osg/AutoTransform\
osg/Billboard\
osg/BoundingBox\
osg/BoundingSphere\

View File

@@ -44,7 +44,12 @@ Node::~Node()
void Node::accept(NodeVisitor& nv)
{
if (nv.validNodeMask(*this)) nv.apply(*this);
if (nv.validNodeMask(*this))
{
nv.pushOntoNodePath(this);
nv.apply(*this);
nv.popFromNodePath();
}
}

View File

@@ -43,3 +43,13 @@ void NodeVisitor::setTraversalVisitor(NodeVisitor* nv)
if (_traversalVisitor.valid()) _traversalMode = TRAVERSE_VISITOR;
else _traversalMode = TRAVERSE_NONE;
}
const bool getLocalToWorldMatrix(Matrix& matrix, MatrixMode mode)
{
return false;
}
const bool getWorldToLocalMatrix(Matrix& matrix, MatrixMode mode)
{
return false;
}

View File

@@ -6,38 +6,35 @@ PositionAttitudeTransform::PositionAttitudeTransform()
{
}
void PositionAttitudeTransform::computeLocalToWorld() const
const bool PositionAttitudeTransform::computeLocalToWorldMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_localToWorldDirty)
if (_mode==MODEL || _mode==MODELVIEW)
{
if (_mode==MODEL)
{
_localToWorld->makeRotate(_attitude);
_localToWorld->setTrans(_position);
}
else
{
_localToWorld->makeTranslate(-_position);
_localToWorld->postMult(osg::Matrix::rotate(_attitude.inverse()));
}
_localToWorldDirty = false;
matrix.makeRotate(_attitude);
matrix.setTrans(_position);
return true;
}
else // _mode==VIEW
{
matrix.makeTranslate(-_position);
matrix.postMult(osg::Matrix::rotate(_attitude.inverse()));
return true;
}
}
void PositionAttitudeTransform::computeWorldToLocal() const
const bool PositionAttitudeTransform::computeWorldToLocalMatrix(Matrix& matrix,NodeVisitor*) const
{
if (_worldToLocalDirty)
if (_mode==MODEL || _mode==MODELVIEW)
{
if (_mode==MODEL)
{
_worldToLocal->makeTranslate(-_position);
_worldToLocal->postMult(osg::Matrix::rotate(_attitude.inverse()));
}
else
{
_worldToLocal->makeRotate(_attitude);
_worldToLocal->setTrans(_position);
}
_worldToLocalDirty = false;
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

@@ -6,24 +6,16 @@ Transform::Transform()
{
_type = DYNAMIC;
_mode = MODEL;
_localToWorld = new Matrix;
_localToWorld->makeIdentity();
_localToWorldDirty = false;
_worldToLocal = new Matrix;
_worldToLocal->makeIdentity();
_worldToLocalDirty = false;
_matrix = new Matrix;
}
Transform::Transform(const Transform& transform,const CopyOp& copyop):
Group(transform,copyop),
_type(transform._type),
_mode(transform._mode),
_localToWorldDirty(transform._localToWorldDirty),
_localToWorld(transform._localToWorld),
_worldToLocalDirty(transform._worldToLocalDirty),
_worldToLocal(transform._localToWorld)
_computeTransformCallback(_computeTransformCallback),
_matrix(new Matrix(*transform._matrix))
{
}
@@ -32,11 +24,7 @@ Transform::Transform(const Matrix& mat )
_type = DYNAMIC;
_mode = MODEL;
_localToWorld = new Matrix(mat);
_localToWorldDirty = false;
_worldToLocal = new Matrix;
_worldToLocalDirty = true;
_matrix = new Matrix(mat);
}
@@ -44,118 +32,48 @@ 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 (_localToWorldDirty) computeLocalToWorld();
Vec3 xdash = _bsphere._center;
xdash.x() += _bsphere._radius;
xdash = xdash*(*_localToWorld);
Vec3 ydash = _bsphere._center;
ydash.y() += _bsphere._radius;
ydash = ydash*(*_localToWorld);
Vec3 zdash = _bsphere._center;
zdash.y() += _bsphere._radius;
zdash = zdash*(*_localToWorld);
_bsphere._center = _bsphere._center*(*_localToWorld);
xdash -= _bsphere._center;
float len_xdash = xdash.length();
ydash -= _bsphere._center;
float len_ydash = ydash.length();
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;
}
void Transform::computeLocalToWorld() const
{
if (_localToWorldDirty)
// note, NULL pointer for NodeVisitor, so compute's need
// to handle this case gracefully, normally this should not be a problem.
Matrix l2w;
if (getLocalToWorldMatrix(l2w,NULL))
{
if (_mode==VIEW)
{
_localToWorld->invert(*_worldToLocal);
}
_localToWorldDirty = false;
}
}
void Transform::computeWorldToLocal() const
{
if (_worldToLocalDirty)
{
if (_mode==MODEL)
{
_worldToLocal->invert(*_localToWorld);
}
_worldToLocalDirty = false;
Vec3 xdash = _bsphere._center;
xdash.x() += _bsphere._radius;
xdash = xdash*l2w;
Vec3 ydash = _bsphere._center;
ydash.y() += _bsphere._radius;
ydash = ydash*l2w;
Vec3 zdash = _bsphere._center;
zdash.y() += _bsphere._radius;
zdash = zdash*l2w;
_bsphere._center = _bsphere._center*l2w;
xdash -= _bsphere._center;
float len_xdash = xdash.length();
ydash -= _bsphere._center;
float len_ydash = ydash.length();
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;
}
else
{
_bsphere.init();
return false;
}
}

View File

@@ -453,19 +453,19 @@ void Viewer::showStats(const unsigned int /*viewport*/)
char clin[72]; // buffer to print
glColor4fv((GLfloat * )&app_color);
sprintf(clin,"App %.1f ms.", timeApp);
sprintf(clin,"App %.2f ms.", timeApp);
displaytext((int)(.15f*tmax),(int)(0.98f*vh),clin);
glColor4fv((GLfloat * )&cull_color);
sprintf(clin,"Cull %.1f ms.", timeCull);
sprintf(clin,"Cull %.2f ms.", timeCull);
displaytext((int)(.35*tmax),(int)(0.98f*vh),clin);
glColor4fv((GLfloat * )&draw_color);
sprintf(clin,"Draw %.1f ms.", timeDraw);
sprintf(clin,"Draw %.2f ms.", timeDraw);
displaytext((int)(.55*tmax),(int)(0.98f*vh),clin);
glColor4fv((GLfloat * )&frame_color);
sprintf(clin,"Frame %.1f ms.", timeFrame);
sprintf(clin,"Frame %.2f ms.", timeFrame);
displaytext((int)(.75*tmax),(int)(0.98f*vh),clin);
/* osg::notify(osg::NOTICE) << "Time of App "<<timeApp<<"ms "<< std::endl;
@@ -1483,7 +1483,7 @@ int writePrims( const int ypos, osg::Statistics& stats)
npix+=12;
strcpy(clin,"Vertices: ");
for (i=0; i<=osg::Statistics::POLYGON; i++) {
if (stats.primtypes[i]) {
if (stats.primverts[i]) {
sprintf(ctmp,"%5d", stats.primverts[i]);
strcat(clin, ctmp);
}

View File

@@ -54,15 +54,15 @@ class PrintVisitor : public NodeVisitor
moveOut();
}
virtual void apply(Geode& node) { apply((Node&)node); }
virtual void apply(Billboard& node) { apply((Geode&)node); }
virtual void apply(LightSource& node){ apply((Node&)node); }
virtual void apply(Geode& node) { apply((Node&)node); }
virtual void apply(Billboard& node) { apply((Geode&)node); }
virtual void apply(LightSource& node) { apply((Node&)node); }
virtual void apply(Group& node) { apply((Node&)node); }
virtual void apply(Transform& node) { apply((Group&)node); }
virtual void apply(Switch& node) { apply((Group&)node); }
virtual void apply(LOD& node) { apply((Group&)node); }
virtual void apply(Impostor& node) { apply((LOD&)node); }
virtual void apply(Group& node) { apply((Node&)node); }
virtual void apply(Transform& node) { apply((Group&)node); }
virtual void apply(Switch& node) { apply((Group&)node); }
virtual void apply(LOD& node) { apply((Group&)node); }
virtual void apply(Impostor& node) { apply((LOD&)node); }
protected:
@@ -479,7 +479,7 @@ void CullVisitor::setCamera(const Camera& camera)
}
void CullVisitor::pushCullViewState(const Matrix* matrix)
void CullVisitor::pushCullViewState(Matrix* matrix)
{
osg::ref_ptr<CullViewState> nvs = new CullViewState;
@@ -487,20 +487,18 @@ void CullVisitor::pushCullViewState(const Matrix* matrix)
Matrix* inverse_world = NULL;
if (matrix)
{
{
if (_cvs.valid() && _cvs->_matrix.valid())
{
nvs->_matrix = new Matrix;
nvs->_matrix->mult(*matrix,*(_cvs->_matrix));
}
else
{
nvs->_matrix = new Matrix(*matrix);
matrix->postMult(*(_cvs->_matrix));
}
inverse_world = new Matrix;
inverse_world->invert(*(nvs->_matrix));
nvs->_inverse = inverse_world;
nvs->_matrix = matrix;
inverse_world = createOrReuseMatrix();
inverse_world->invert(*(matrix));
nvs->_inverse = inverse_world;
}
else
{
@@ -1034,7 +1032,6 @@ void CullVisitor::apply(Group& node)
_cullingModeStack.pop_back();
}
void CullVisitor::apply(Transform& node)
{
// return if object's bounding sphere is culled.
@@ -1051,7 +1048,12 @@ void CullVisitor::apply(Transform& node)
StateSet* node_state = node.getStateSet();
if (node_state) pushStateSet(node_state);
pushCullViewState(&node.getLocalToWorldMatrix());
ref_ptr<osg::Matrix> matrix = createOrReuseMatrix();
matrix->makeIdentity();
node.getLocalToWorldMatrix(*matrix,this);
pushCullViewState(matrix.get());
// pushCullViewState(&node.getMatrix());
traverse(node);
@@ -1064,7 +1066,6 @@ void CullVisitor::apply(Transform& node)
_cullingModeStack.pop_back();
}
void CullVisitor::apply(Switch& node)
{
apply((Group&)node);

View File

@@ -505,7 +505,10 @@ void IntersectVisitor::apply(Transform& node)
{
if (!enterNode(node)) return;
pushMatrix(node.getLocalToWorldMatrix());
osg::ref_ptr<Matrix> matrix = new Matrix;
node.getLocalToWorldMatrix(*matrix,this);
pushMatrix(*matrix);
traverse(node);

View File

@@ -185,7 +185,12 @@ bool RenderBin::getStats(osg::Statistics* primStats)
primStats->addOpaque(); // number of geosets
if (rl->_matrix.get()) primStats->addMatrix(); // number of matrices
if (dw) { // then tot up the types 1-14
// commenting out as having intrusive stats in base classes is
// undersirable.
dw->getStats(*primStats); // use sub-class to find the stats for each drawable
// use an AttributeOption to get the stats we require.
dw->applyAttributeOperation(*primStats);
}
}
somestats=true;