From ba7d27c9b2fcd430c397400470e63d743b15eca5 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 1 Oct 2006 19:19:31 +0000 Subject: [PATCH] From Chris Hanson, "These changes break out the implementation of the fill/wireframe/point, texture, lighting and backface culling settings from the event handling code in StatesetManipulator into public methods. The event handler now calls the public methods. This allows user code to invoke this same functionality from non-keyboard event inputs without clashing with the keyboard-invoked functionality." From Robert Osfield, tweaks to the above to kepp the coding style the same as the rest of the OSG, also made getPolygonMode() const, and updated the wrappers. --- include/osgGA/StateSetManipulator | 18 +++ src/osgGA/StateSetManipulator.cpp | 117 ++++++++++++------ src/osgWrappers/osgGA/StateSetManipulator.cpp | 14 +++ 3 files changed, 109 insertions(+), 40 deletions(-) diff --git a/include/osgGA/StateSetManipulator b/include/osgGA/StateSetManipulator index afef1db07..b8156b235 100644 --- a/include/osgGA/StateSetManipulator +++ b/include/osgGA/StateSetManipulator @@ -20,6 +20,8 @@ #include #include +#include + namespace osgGA{ @@ -56,6 +58,20 @@ public: void setMaximumNumOfTextureUnits(unsigned int i) { _maxNumOfTextureUnits = i; } unsigned int getMaximumNumOfTextureUnits() const { return _maxNumOfTextureUnits; } + + void setBackfaceEnabled(bool newbackface); + bool getBackfaceEnabled() const {return _backface;}; + + void setLightingEnabled(bool newlighting); + bool getLightingEnabled() const {return _lighting;}; + + void setTextureEnabled(bool newtexture); + bool getTextureEnabled() const {return _texture;}; + + void setPolygonMode(osg::PolygonMode::Mode newpolygonmode); + osg::PolygonMode::Mode getPolygonMode() const; + + void cyclePolygonMode(); protected: @@ -66,6 +82,8 @@ protected: bool _lighting; bool _texture; unsigned int _maxNumOfTextureUnits; + + osg::PolygonMode* getOrCreatePolygonMode(); }; } diff --git a/src/osgGA/StateSetManipulator.cpp b/src/osgGA/StateSetManipulator.cpp index dfbd242aa..93a433aaa 100644 --- a/src/osgGA/StateSetManipulator.cpp +++ b/src/osgGA/StateSetManipulator.cpp @@ -64,62 +64,27 @@ bool StateSetManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa) { case 'b' : - _backface = !_backface; - if( _backface ) _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::ON); - else _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + setBackfaceEnabled(!getBackfaceEnabled()); aa.requestRedraw(); return true; break; case 'l' : - _lighting = !_lighting ; - if( _lighting ) _drawState->setMode(GL_LIGHTING,osg::StateAttribute::ON); - else _drawState->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF); + setLightingEnabled(!getLightingEnabled()); aa.requestRedraw(); return true; break; case 't' : - { - _texture = !_texture; - -// osg::ref_ptr< osg::Texture > tex = dynamic_cast -// ( _drawState->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); - } + setTextureEnabled(!getTextureEnabled()); aa.requestRedraw(); return true; - } break; case 'w' : - { - osg::PolygonMode* polyModeObj = dynamic_cast(_drawState->getAttribute(osg::StateAttribute::POLYGONMODE)); - if (!polyModeObj) - { - polyModeObj = new osg::PolygonMode; - _drawState->setAttribute(polyModeObj); - } - - // cycle through the available modes. - switch(polyModeObj->getMode(osg::PolygonMode::FRONT_AND_BACK)) - { - case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); break; - case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::POINT); break; - case osg::PolygonMode::POINT : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL); break; - } - } + cyclePolygonMode(); break; + #if COMPILE_TEXENVFILTER_USAGE case 'm' : { @@ -152,3 +117,75 @@ void StateSetManipulator::accept(GUIEventHandlerVisitor& gehv) { gehv.visit(*this); } + +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); +} + +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); +} + +void StateSetManipulator::setTextureEnabled(bool newtexture) +{ + _texture = newtexture; +// osg::ref_ptr< osg::Texture > tex = dynamic_cast +// ( _drawState->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); + } +} + +void StateSetManipulator::setPolygonMode(osg::PolygonMode::Mode newpolygonmode) +{ + osg::PolygonMode* polyModeObj = getOrCreatePolygonMode(); + + polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,newpolygonmode); +} + +void StateSetManipulator::cyclePolygonMode() +{ + osg::PolygonMode* polyModeObj = getOrCreatePolygonMode(); + + osg::PolygonMode::Mode currentMode = getPolygonMode(); + // cycle through the available modes. + switch(currentMode) + { + case osg::PolygonMode::FILL : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::LINE); break; + case osg::PolygonMode::LINE : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::POINT); break; + case osg::PolygonMode::POINT : polyModeObj->setMode(osg::PolygonMode::FRONT_AND_BACK,osg::PolygonMode::FILL); break; + } +} + +osg::PolygonMode::Mode StateSetManipulator::getPolygonMode() const +{ + osg::PolygonMode* polyModeObj = dynamic_cast(_drawState->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)); + if (!polyModeObj) + { + polyModeObj = new osg::PolygonMode; + _drawState->setAttribute(polyModeObj); + } + return polyModeObj; +} diff --git a/src/osgWrappers/osgGA/StateSetManipulator.cpp b/src/osgWrappers/osgGA/StateSetManipulator.cpp index f487a1e9a..bdfc0f991 100644 --- a/src/osgWrappers/osgGA/StateSetManipulator.cpp +++ b/src/osgWrappers/osgGA/StateSetManipulator.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -37,7 +38,20 @@ BEGIN_OBJECT_REFLECTOR(osgGA::StateSetManipulator) I_Method1(void, getUsage, IN, osg::ApplicationUsage &, usage); I_Method1(void, setMaximumNumOfTextureUnits, IN, unsigned int, i); I_Method0(unsigned int, getMaximumNumOfTextureUnits); + I_Method1(void, setBackfaceEnabled, IN, bool, newbackface); + I_Method0(bool, getBackfaceEnabled); + I_Method1(void, setLightingEnabled, IN, bool, newlighting); + I_Method0(bool, getLightingEnabled); + I_Method1(void, setTextureEnabled, IN, bool, newtexture); + I_Method0(bool, getTextureEnabled); + I_Method1(void, setPolygonMode, IN, osg::PolygonMode::Mode, newpolygonmode); + I_Method0(osg::PolygonMode::Mode, getPolygonMode); + I_Method0(void, cyclePolygonMode); + I_Property(bool, BackfaceEnabled); + I_Property(bool, LightingEnabled); I_Property(unsigned int, MaximumNumOfTextureUnits); + I_Property(osg::PolygonMode::Mode, PolygonMode); I_Property(osg::StateSet *, StateSet); + I_Property(bool, TextureEnabled); END_REFLECTOR