From 7010c1c4f8dd318773bfb76d56578548ef5f92b3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 11 Aug 2002 21:26:58 +0000 Subject: [PATCH] Removed dependency of the new osg::DOFTransform and osg::Sequence Node's on osgUtil by implementing a NodeVisitor::VisitorType enum, and associated g/setVisitorType. This allows callbacks to querry the visitor/traversal type without doing down cast's to specific visitor subclasses such as osgUtil::AppVisitor/CullVisitor. --- include/osg/NodeVisitor | 23 +++++- src/osg/CollectOccludersVisitor.cpp | 5 +- src/osg/DOFTransform.cpp | 110 ++++++++++++++-------------- src/osg/NodeVisitor.cpp | 12 +++ src/osg/Sequence.cpp | 6 +- src/osgUtil/AppVisitor.cpp | 2 +- src/osgUtil/CullVisitor.cpp | 2 +- 7 files changed, 93 insertions(+), 67 deletions(-) diff --git a/include/osg/NodeVisitor b/include/osg/NodeVisitor index 3bcc98ff1..368c54ab3 100644 --- a/include/osg/NodeVisitor +++ b/include/osg/NodeVisitor @@ -43,15 +43,27 @@ class SG_EXPORT NodeVisitor : public Referenced { public: - enum TraversalMode { + enum TraversalMode + { TRAVERSE_NONE, TRAVERSE_PARENTS, TRAVERSE_ALL_CHILDREN, TRAVERSE_ACTIVE_CHILDREN, TRAVERSE_VISITOR }; + + enum VisitorType + { + NODE_VISITOR = 0, + APP_VISITOR, + COLLECT_OCCLUDER_VISITOR, + CULL_VISITOR + }; NodeVisitor(TraversalMode tm=TRAVERSE_NONE); + + NodeVisitor(VisitorType type,TraversalMode tm=TRAVERSE_NONE); + virtual ~NodeVisitor(); /** Method to call to reset visitor. Useful for your visitor accumulates @@ -61,6 +73,14 @@ class SG_EXPORT NodeVisitor : public Referenced virtual void reset() {} + /** Set the VisitorType, used to distingush different visitors during + * traversal of the scene, typically used in the Node::traverse() method + * to select which behaviour to use for different types of traversal/visitors.*/ + inline void setVisitorType(VisitorType type) { _visitorType = type; } + + /** Get the VisitorType.*/ + inline VisitorType getVisitorType() const { return _visitorType; } + /** Set the traversal number. Typically used to denote the frame count.*/ inline void setTraversalNumber(const int fn) { _traversalNumber = fn; } @@ -187,6 +207,7 @@ class SG_EXPORT NodeVisitor : public Referenced protected: + VisitorType _visitorType; int _traversalNumber; ref_ptr _frameStamp; diff --git a/src/osg/CollectOccludersVisitor.cpp b/src/osg/CollectOccludersVisitor.cpp index e1d86e089..be503f494 100644 --- a/src/osg/CollectOccludersVisitor.cpp +++ b/src/osg/CollectOccludersVisitor.cpp @@ -9,10 +9,9 @@ using namespace osg; -CollectOccludersVisitor::CollectOccludersVisitor() +CollectOccludersVisitor::CollectOccludersVisitor(): + NodeVisitor(COLLECT_OCCLUDER_VISITOR,TRAVERSE_ACTIVE_CHILDREN) { - // overide the default node visitor mode. - setTraversalMode(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN); setCullingMode(VIEW_FRUSTUM_CULLING| NEAR_PLANE_CULLING| diff --git a/src/osg/DOFTransform.cpp b/src/osg/DOFTransform.cpp index 5f225efd2..c90eae101 100644 --- a/src/osg/DOFTransform.cpp +++ b/src/osg/DOFTransform.cpp @@ -1,8 +1,5 @@ #include -#include -#include - using namespace osg; struct DOFCullCallback : public Transform::ComputeTransformCallback @@ -10,36 +7,32 @@ struct DOFCullCallback : public Transform::ComputeTransformCallback /**/ virtual const bool computeLocalToWorldMatrix(Matrix& matrix, const Transform* trans, NodeVisitor* nv) const { - osgUtil::CullVisitor* cv = dynamic_cast(nv); - if(cv) + const DOFTransform* dof = dynamic_cast(trans); + if(dof) { - const DOFTransform* dof = dynamic_cast(trans); - if(dof) - { - //here we are: - //put the PUT matrix first: - Matrix l2w(dof->getPutMatrix()); + //here we are: + //put the PUT matrix first: + Matrix l2w(dof->getPutMatrix()); - //now the current matrix: - Matrix current; - current.makeTranslate(dof->getCurrentTranslate()); + //now the current matrix: + Matrix current; + current.makeTranslate(dof->getCurrentTranslate()); - //now create the local rotation: - current.preMult(Matrix::rotate(dof->getCurrentHPR()[1], 1.0, 0.0, 0.0));//pitch - current.preMult(Matrix::rotate(dof->getCurrentHPR()[2], 0.0, 1.0, 0.0));//roll - current.preMult(Matrix::rotate(dof->getCurrentHPR()[0], 0.0, 0.0, 1.0));//heading + //now create the local rotation: + current.preMult(Matrix::rotate(dof->getCurrentHPR()[1], 1.0, 0.0, 0.0));//pitch + current.preMult(Matrix::rotate(dof->getCurrentHPR()[2], 0.0, 1.0, 0.0));//roll + current.preMult(Matrix::rotate(dof->getCurrentHPR()[0], 0.0, 0.0, 1.0));//heading - //and scale: - current.preMult(Matrix::scale(dof->getCurrentScale())); + //and scale: + current.preMult(Matrix::scale(dof->getCurrentScale())); - l2w.postMult(current); + l2w.postMult(current); - //and impose inverse put: - l2w.postMult(dof->getInversePutMatrix()); + //and impose inverse put: + l2w.postMult(dof->getInversePutMatrix()); - //finally: - matrix.preMult(l2w); - } + //finally: + matrix.preMult(l2w); } return true; @@ -48,38 +41,34 @@ struct DOFCullCallback : public Transform::ComputeTransformCallback /**/ virtual const bool computeWorldToLocalMatrix(Matrix& matrix, const Transform* trans, NodeVisitor* nv) const { - osgUtil::CullVisitor* cv = dynamic_cast(nv); - if(cv) + const DOFTransform* dof = dynamic_cast(trans); + if(dof) { - const DOFTransform* dof = dynamic_cast(trans); - if(dof) - { - //here we are: - //put the PUT matrix first: - Matrix w2l(dof->getInversePutMatrix()); + //here we are: + //put the PUT matrix first: + Matrix w2l(dof->getInversePutMatrix()); - //now the current matrix: - Matrix current; - current.makeTranslate(-dof->getCurrentTranslate()); + //now the current matrix: + Matrix current; + current.makeTranslate(-dof->getCurrentTranslate()); - //now create the local rotation: - - - current.postMult(Matrix::rotate(-dof->getCurrentHPR()[0], 0.0, 0.0, 1.0));//heading - current.postMult(Matrix::rotate(-dof->getCurrentHPR()[2], 0.0, 1.0, 0.0));//roll - current.postMult(Matrix::rotate(-dof->getCurrentHPR()[1], 1.0, 0.0, 0.0));//pitch + //now create the local rotation: - //and scale: - current.postMult(Matrix::scale(1./dof->getCurrentScale()[0], 1./dof->getCurrentScale()[1], 1./dof->getCurrentScale()[2])); - w2l.postMult(current); + current.postMult(Matrix::rotate(-dof->getCurrentHPR()[0], 0.0, 0.0, 1.0));//heading + current.postMult(Matrix::rotate(-dof->getCurrentHPR()[2], 0.0, 1.0, 0.0));//roll + current.postMult(Matrix::rotate(-dof->getCurrentHPR()[1], 1.0, 0.0, 0.0));//pitch - //and impose inverse put: - w2l.postMult(dof->getPutMatrix()); + //and scale: + current.postMult(Matrix::scale(1./dof->getCurrentScale()[0], 1./dof->getCurrentScale()[1], 1./dof->getCurrentScale()[2])); - //finally: - matrix.postMult(w2l); - } + w2l.postMult(current); + + //and impose inverse put: + w2l.postMult(dof->getPutMatrix()); + + //finally: + matrix.postMult(w2l); } return true; @@ -91,14 +80,19 @@ class DOFAnimationAppCallback : public osg::NodeCallback /** Callback method call by the NodeVisitor when visiting a node.*/ virtual void operator()(Node* node, NodeVisitor* nv) { - //this is aspp visitor, see if we have DOFTransform: - DOFTransform* dof = dynamic_cast(node); - if(dof) + if (nv && nv->getVisitorType()==NodeVisitor::APP_VISITOR) { - //see if it is ina animation mode: - dof->animate(); - } + //this is aspp visitor, see if we have DOFTransform: + DOFTransform* dof = dynamic_cast(node); + if(dof) + { + //see if it is ina animation mode: + dof->animate(); + } + + } + // note, callback is repsonsible for scenegraph traversal so // should always include call the traverse(node,nv) to ensure // that the rest of cullbacks and the scene graph are traversed. @@ -379,4 +373,6 @@ void DOFTransform::animate() new_value[2] -= _incrementScale[2]; setCurrentScale(new_value); + + dirtyBound(); } diff --git a/src/osg/NodeVisitor.cpp b/src/osg/NodeVisitor.cpp index 26e617f15..fde0e3963 100644 --- a/src/osg/NodeVisitor.cpp +++ b/src/osg/NodeVisitor.cpp @@ -6,6 +6,18 @@ using namespace osg; NodeVisitor::NodeVisitor(TraversalMode tm) { + _visitorType = NODE_VISITOR; + _traversalNumber = -1; + + _traversalVisitor = NULL; + _traversalMode = tm; + _traversalMask = 0xffffffff; + _nodeMaskOverride = 0x0; +} + +NodeVisitor::NodeVisitor(VisitorType type,TraversalMode tm) +{ + _visitorType = type; _traversalNumber = -1; _traversalVisitor = NULL; diff --git a/src/osg/Sequence.cpp b/src/osg/Sequence.cpp index b4456542e..9521a38a1 100644 --- a/src/osg/Sequence.cpp +++ b/src/osg/Sequence.cpp @@ -2,8 +2,6 @@ #include -#include - using namespace osg; /** @@ -100,8 +98,8 @@ void Sequence::setMode(SequenceMode mode) void Sequence::traverse(NodeVisitor& nv) { - osgUtil::AppVisitor* app = dynamic_cast(&nv); - if (app && _mode == START && _nrepsremain) { + if (nv.getVisitorType()==NodeVisitor::APP_VISITOR && _mode == START && _nrepsremain) + { double t = nv.getFrameStamp()->getReferenceTime(); if (_last == -1.0) _last = t; diff --git a/src/osgUtil/AppVisitor.cpp b/src/osgUtil/AppVisitor.cpp index a11665a0c..c10a18ed2 100644 --- a/src/osgUtil/AppVisitor.cpp +++ b/src/osgUtil/AppVisitor.cpp @@ -3,7 +3,7 @@ using namespace osg; using namespace osgUtil; -AppVisitor::AppVisitor():NodeVisitor(TRAVERSE_ACTIVE_CHILDREN) +AppVisitor::AppVisitor():NodeVisitor(APP_VISITOR,TRAVERSE_ACTIVE_CHILDREN) { } diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index a4809c402..f712f97f2 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -75,7 +75,7 @@ class PrintVisitor : public NodeVisitor }; CullVisitor::CullVisitor(): - NodeVisitor(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN), + NodeVisitor(CULL_VISITOR,TRAVERSE_ACTIVE_CHILDREN), _currentRenderGraph(NULL), _currentRenderBin(NULL), _computeNearFar(COMPUTE_NEAR_FAR_USING_BOUNDING_VOLUMES),