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
This commit is contained in:
Robert Osfield
2007-01-23 16:26:15 +00:00
parent 45dea96d75
commit 055abec7a3
2 changed files with 42 additions and 35 deletions

View File

@@ -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<osg::TexEnvFilter*>(_drawState->getTextureAttribute(0,osg::StateAttribute::TEXENVFILTER));
osg::TexEnvFilter* texenvfilter = dynamic_cast<osg::TexEnvFilter*>(_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<osg::Texture*>
// ( _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<osg::PolygonMode*>(_drawState->getAttribute(osg::StateAttribute::POLYGONMODE));
osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_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<osg::PolygonMode*>(_drawState->getAttribute(osg::StateAttribute::POLYGONMODE));
osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_stateset->getAttribute(osg::StateAttribute::POLYGONMODE));
if (!polyModeObj)
{
polyModeObj = new osg::PolygonMode;
_drawState->setAttribute(polyModeObj);
_stateset->setAttribute(polyModeObj);
}
return polyModeObj;
}