From f3be713d66e09ced3598f2f0395034d0bf1489f9 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 26 Aug 2005 20:01:21 +0000 Subject: [PATCH] Added OveralyNode into VS project file, and fleshed out more code in the OveralyNode implementation. --- VisualStudio/osgSim/osgSim.dsp | 8 +++ include/osgSim/OverlayNode | 9 +++- src/osgSim/Impostor.cpp | 6 +-- src/osgSim/OverlayNode.cpp | 91 +++++++++++++++++++++++++++++++++- 4 files changed, 107 insertions(+), 7 deletions(-) diff --git a/VisualStudio/osgSim/osgSim.dsp b/VisualStudio/osgSim/osgSim.dsp index 18f58550b..df19747a8 100644 --- a/VisualStudio/osgSim/osgSim.dsp +++ b/VisualStudio/osgSim/osgSim.dsp @@ -135,6 +135,10 @@ SOURCE=..\..\src\osgSim\MultiSwitch.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osgSim\OveralyNode.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osgSim\ScalarBar.cpp # End Source File # Begin Source File @@ -211,6 +215,10 @@ SOURCE=..\..\include\osgSim\MultiSwitch # End Source File # Begin Source File +SOURCE=..\..\include\osgSim\OverlayNode +# End Source File +# Begin Source File + SOURCE=..\..\include\osgSim\ScalarBar # End Source File # Begin Source File diff --git a/include/osgSim/OverlayNode b/include/osgSim/OverlayNode index 802122e22..63e1c2dc1 100644 --- a/include/osgSim/OverlayNode +++ b/include/osgSim/OverlayNode @@ -57,9 +57,14 @@ class OSGSIM_EXPORT OverlayNode : public osg::Group protected : virtual ~OverlayNode() {} + + void init(); - typedef osg::buffered_object< osg::ref_ptr > CameraList; - mutable CameraList _cameras; + typedef osg::buffered_value< int > TextureObjectValidList; + + mutable TextureObjectValidList _textureObjectValidList; + + osg::ref_ptr _camera; // overaly subgraph is render to a texture osg::ref_ptr _overlaySubgraph; diff --git a/src/osgSim/Impostor.cpp b/src/osgSim/Impostor.cpp index 357c1fd1b..822db2ac9 100644 --- a/src/osgSim/Impostor.cpp +++ b/src/osgSim/Impostor.cpp @@ -20,11 +20,11 @@ using namespace osgSim; // use this cull callback to allow the camera to traverse the Impostor's children without // actuall having them assigned as children to the camea itself. This make the camera a // decorator without ever directly being assigned to it. -class TraverseNodeCallback : public osg::NodeCallback +class ImpostorTraverseNodeCallback : public osg::NodeCallback { public: - TraverseNodeCallback(osgSim::Impostor* node):_node(node) {} + ImpostorTraverseNodeCallback(osgSim::Impostor* node):_node(node) {} virtual void operator()(osg::Node*, osg::NodeVisitor* nv) { @@ -394,7 +394,7 @@ ImpostorSprite* Impostor::createImpostorSprite(osgUtil::CullVisitor* cv) impostorSprite->setCameraNode(camera); } - camera->setCullCallback(new TraverseNodeCallback(this)); + camera->setCullCallback(new ImpostorTraverseNodeCallback(this)); osgUtil::RenderStage* previous_stage = cv->getRenderStage(); diff --git a/src/osgSim/OverlayNode.cpp b/src/osgSim/OverlayNode.cpp index b3a20f1ba..479776d7d 100644 --- a/src/osgSim/OverlayNode.cpp +++ b/src/osgSim/OverlayNode.cpp @@ -11,26 +11,113 @@ * OpenSceneGraph Public License for more details. */ +#include +#include #include using namespace osgSim; -OverlayNode::OverlayNode() +// use this cull callback to allow the camera to traverse the OverlaySubgraph's children without +// actuall having them assigned as children to the camea itself. This make the camera a +// decorator without ever directly being assigned to it. +class OverlayTraverseNodeCallback : public osg::NodeCallback { +public: + + OverlayTraverseNodeCallback(osg::Node* node):_node(node) {} + + virtual void operator()(osg::Node*, osg::NodeVisitor* nv) + { + _node->accept(*nv); + } + + osg::Node* _node; +}; + +OverlayNode::OverlayNode(): + _textureUnit(0) +{ + init(); } -OverlayNode::OverlayNode(const OverlayNode& es, const osg::CopyOp& copyop) +OverlayNode::OverlayNode(const OverlayNode& copy, const osg::CopyOp& copyop): + Group(copy,copyop), + _overlaySubgraph(copy._overlaySubgraph), + _textureUnit(copy._textureUnit) { + init(); } +void OverlayNode::init() +{ + _camera = new osg::CameraNode; + + _texgenNode = new osg::TexGenNode; + _texgenNode->setTextureUnit(_textureUnit); + + _texture = new osg::Texture2D; + + _mainSubgraphStateSet = new osg::StateSet; + _mainSubgraphStateSet->setTextureAttributeAndModes(_textureUnit, _texture.get(), osg::StateAttribute::ON); +} + + void OverlayNode::traverse(osg::NodeVisitor& nv) { + if (nv.getVisitorType() != osg::NodeVisitor::CULL_VISITOR) + { + Group::traverse(nv); + return; + } + + osgUtil::CullVisitor* cv = dynamic_cast(&nv); + if (!cv) + { + Group::traverse(nv); + return; + } + + unsigned int contextID = cv->getState()!=0 ? cv->getState()->getContextID() : 0; + + // if we need to redraw then do cull traversal on camera. + if (!_textureObjectValidList[contextID]) + { + _camera->accept(*cv); + + _textureObjectValidList[contextID] = 1; + } + + + // now set up the drawing of the main scene. + { + _texgenNode->accept(*cv); + + // push the stateset. + cv->pushStateSet(_mainSubgraphStateSet.get()); + + Group::traverse(nv); + + cv->popStateSet(); + } } void OverlayNode::setOverlaySubgraph(osg::Node* node) { + _overlaySubgraph = node; + dirtyOverlayTexture(); } void OverlayNode::dirtyOverlayTexture() { + _textureObjectValidList.setAllElementsTo(0); +} + +void OverlayNode::setOverlayTextureUnit(unsigned int unit) +{ + if (_textureUnit==unit) return; + + _texgenNode->setTextureUnit(unit); + + _mainSubgraphStateSet->clear(); + _mainSubgraphStateSet->setTextureAttributeAndModes(_textureUnit, _texture.get(), osg::StateAttribute::ON); }