From 055abec7a3d55f6260db68af7e519d184f32aac0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 23 Jan 2007 16:26:15 +0000 Subject: [PATCH] Added osg::StateSet* parameter to constructor to make it more convinient to set up the StateSetManipulator and moved the initialization of locally cached states into the handle method to ensure a representative version of the StateSet is captured --- include/osgGA/StateSetManipulator | 9 ++-- src/osgGA/StateSetManipulator.cpp | 68 +++++++++++++++++-------------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/include/osgGA/StateSetManipulator b/include/osgGA/StateSetManipulator index a68b78cf3..abf30d236 100644 --- a/include/osgGA/StateSetManipulator +++ b/include/osgGA/StateSetManipulator @@ -33,8 +33,7 @@ class OSGGA_EXPORT StateSetManipulator : public GUIEventHandler { public: - StateSetManipulator(); - virtual ~StateSetManipulator(); + StateSetManipulator(osg::StateSet* stateset=0); virtual const char* className() const { return "StateSetManipulator"; } @@ -72,9 +71,11 @@ public: protected: - // Reference pointer to a STATESTATE - osg::ref_ptr _drawState; + virtual ~StateSetManipulator(); + osg::ref_ptr _stateset; + + bool _initialized; bool _backface; bool _lighting; bool _texture; diff --git a/src/osgGA/StateSetManipulator.cpp b/src/osgGA/StateSetManipulator.cpp index 49ca3c420..1dfb37c71 100644 --- a/src/osgGA/StateSetManipulator.cpp +++ b/src/osgGA/StateSetManipulator.cpp @@ -16,46 +16,52 @@ using namespace osg; using namespace osgGA; -StateSetManipulator::StateSetManipulator(): +StateSetManipulator::StateSetManipulator(osg::StateSet* stateset): + _initialized(false), _backface(false), _lighting(false), _texture(false), _maxNumOfTextureUnits(4) { + setStateSet(stateset); } StateSetManipulator::~StateSetManipulator() { } -void StateSetManipulator::setStateSet(StateSet *drawState) +void StateSetManipulator::setStateSet(StateSet *stateset) { - _drawState=drawState; - if(!_drawState.valid()) return; - _backface = (_drawState->getMode(GL_CULL_FACE)&osg::StateAttribute::ON); - _lighting =(_drawState->getMode(GL_LIGHTING)&osg::StateAttribute::ON); - - unsigned int mode = osg::StateAttribute::INHERIT|osg::StateAttribute::ON; - _texture = (_drawState->getTextureMode(0,GL_TEXTURE_1D)&mode) || - (_drawState->getTextureMode(0,GL_TEXTURE_2D)&mode) || - (_drawState->getTextureMode(0,GL_TEXTURE_3D)&mode) || - (_drawState->getTextureMode(0,GL_TEXTURE_RECTANGLE)&mode) || - (_drawState->getTextureMode(0,GL_TEXTURE_CUBE_MAP)&mode); + _stateset = stateset; } StateSet *StateSetManipulator::getStateSet() { - return _drawState.get(); + return _stateset.get(); } const StateSet *StateSetManipulator::getStateSet() const { - return _drawState.get(); + return _stateset.get(); } bool StateSetManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa) { - if(!_drawState.valid()) return false; + if(!_stateset.valid()) return false; + + if (!_initialized) + { + _initialized = true; + _backface = (_stateset->getMode(GL_CULL_FACE)&osg::StateAttribute::ON); + _lighting =(_stateset->getMode(GL_LIGHTING)&osg::StateAttribute::ON); + + unsigned int mode = osg::StateAttribute::INHERIT|osg::StateAttribute::ON; + _texture = (_stateset->getTextureMode(0,GL_TEXTURE_1D)&mode) || + (_stateset->getTextureMode(0,GL_TEXTURE_2D)&mode) || + (_stateset->getTextureMode(0,GL_TEXTURE_3D)&mode) || + (_stateset->getTextureMode(0,GL_TEXTURE_RECTANGLE)&mode) || + (_stateset->getTextureMode(0,GL_TEXTURE_CUBE_MAP)&mode); + } if(ea.getEventType()==GUIEventAdapter::KEYDOWN) { @@ -89,11 +95,11 @@ bool StateSetManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa) #if COMPILE_TEXENVFILTER_USAGE case 'm' : { - osg::TexEnvFilter* texenvfilter = dynamic_cast(_drawState->getTextureAttribute(0,osg::StateAttribute::TEXENVFILTER)); + osg::TexEnvFilter* texenvfilter = dynamic_cast(_stateset->getTextureAttribute(0,osg::StateAttribute::TEXENVFILTER)); if (!texenvfilter) { texenvfilter = new osg::TexEnvFilter; - _drawState->setTextureAttribute(0,texenvfilter); + _stateset->setTextureAttribute(0,texenvfilter); } // cycle through the available modes. @@ -119,33 +125,33 @@ void StateSetManipulator::getUsage(osg::ApplicationUsage& usage) const void StateSetManipulator::setBackfaceEnabled(bool newbackface) { _backface = newbackface; - if( _backface ) _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::ON); - else _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + if( _backface ) _stateset->setMode(GL_CULL_FACE,osg::StateAttribute::ON); + else _stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); } void StateSetManipulator::setLightingEnabled(bool newlighting) { _lighting = newlighting; - if( _lighting ) _drawState->setMode(GL_LIGHTING,osg::StateAttribute::ON); - else _drawState->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + if( _lighting ) _stateset->setMode(GL_LIGHTING,osg::StateAttribute::ON); + else _stateset->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); } void StateSetManipulator::setTextureEnabled(bool newtexture) { _texture = newtexture; // osg::ref_ptr< osg::Texture > tex = dynamic_cast -// ( _drawState->getAttribute( osg::StateAttribute::TEXTURE ) ); +// ( _stateset->getAttribute( osg::StateAttribute::TEXTURE ) ); // cout << tex->numTextureUnits() << endl; unsigned int mode = osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF; if ( _texture ) mode = osg::StateAttribute::INHERIT|osg::StateAttribute::ON; for( unsigned int ii=0; ii<_maxNumOfTextureUnits; ii++ ) { - _drawState->setTextureMode( ii, GL_TEXTURE_1D, mode ); - _drawState->setTextureMode( ii, GL_TEXTURE_2D, mode ); - _drawState->setTextureMode( ii, GL_TEXTURE_3D, mode ); - _drawState->setTextureMode( ii, GL_TEXTURE_RECTANGLE, mode ); - _drawState->setTextureMode( ii, GL_TEXTURE_CUBE_MAP, mode); + _stateset->setTextureMode( ii, GL_TEXTURE_1D, mode ); + _stateset->setTextureMode( ii, GL_TEXTURE_2D, mode ); + _stateset->setTextureMode( ii, GL_TEXTURE_3D, mode ); + _stateset->setTextureMode( ii, GL_TEXTURE_RECTANGLE, mode ); + _stateset->setTextureMode( ii, GL_TEXTURE_CUBE_MAP, mode); } } @@ -172,18 +178,18 @@ void StateSetManipulator::cyclePolygonMode() osg::PolygonMode::Mode StateSetManipulator::getPolygonMode() const { - osg::PolygonMode* polyModeObj = dynamic_cast(_drawState->getAttribute(osg::StateAttribute::POLYGONMODE)); + osg::PolygonMode* polyModeObj = dynamic_cast(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); if (polyModeObj) return polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK); else return osg::PolygonMode::FILL; } osg::PolygonMode* StateSetManipulator::getOrCreatePolygonMode() { - osg::PolygonMode* polyModeObj = dynamic_cast(_drawState->getAttribute(osg::StateAttribute::POLYGONMODE)); + osg::PolygonMode* polyModeObj = dynamic_cast(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE)); if (!polyModeObj) { polyModeObj = new osg::PolygonMode; - _drawState->setAttribute(polyModeObj); + _stateset->setAttribute(polyModeObj); } return polyModeObj; }