2002-05-09 18:31:03 +08:00
|
|
|
#include <osgGA/StateSetManipulator>
|
|
|
|
|
2003-03-12 21:54:59 +08:00
|
|
|
#include <osg/PolygonMode>
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
using namespace osg;
|
|
|
|
using namespace osgGA;
|
|
|
|
|
|
|
|
StateSetManipulator::StateSetManipulator(): _drawState(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
StateSetManipulator::~StateSetManipulator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void StateSetManipulator::setStateSet(StateSet *drawState)
|
|
|
|
{
|
|
|
|
_drawState=drawState;
|
|
|
|
if(!_drawState.valid()) return;
|
2002-08-07 01:07:05 +08:00
|
|
|
_backface = (_drawState->getMode(GL_CULL_FACE)&osg::StateAttribute::ON);
|
|
|
|
_lighting =(_drawState->getMode(GL_LIGHTING)&osg::StateAttribute::ON);
|
|
|
|
_texture =(_drawState->getTextureMode(0,GL_TEXTURE_2D)&osg::StateAttribute::ON);
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
StateSet *StateSetManipulator::getStateSet()
|
|
|
|
{
|
|
|
|
return _drawState.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const StateSet *StateSetManipulator::getStateSet() const
|
|
|
|
{
|
|
|
|
return _drawState.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StateSetManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter& aa)
|
|
|
|
{
|
|
|
|
if(!_drawState.valid()) return false;
|
|
|
|
|
2003-01-14 22:25:56 +08:00
|
|
|
if(ea.getEventType()==GUIEventAdapter::KEYDOWN){
|
2002-05-09 18:31:03 +08:00
|
|
|
|
|
|
|
switch( ea.getKey() ){
|
|
|
|
|
|
|
|
case 'b' :
|
|
|
|
_backface = !_backface;
|
|
|
|
if( _backface ) _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::ON);
|
2002-08-07 01:07:05 +08:00
|
|
|
else _drawState->setMode(GL_CULL_FACE,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
|
2002-05-09 18:31:03 +08:00
|
|
|
aa.requestRedraw();
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l' :
|
|
|
|
_lighting = !_lighting ;
|
|
|
|
if( _lighting ) _drawState->setMode(GL_LIGHTING,osg::StateAttribute::ON);
|
2002-08-07 01:07:05 +08:00
|
|
|
else _drawState->setMode(GL_LIGHTING,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
|
2002-05-09 18:31:03 +08:00
|
|
|
aa.requestRedraw();
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't' :
|
|
|
|
_texture = !_texture;
|
2002-08-07 01:07:05 +08:00
|
|
|
if (_texture) _drawState->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::INHERIT);
|
|
|
|
else _drawState->setTextureMode(0,GL_TEXTURE_2D,osg::StateAttribute::OVERRIDE|osg::StateAttribute::OFF);
|
2002-05-09 18:31:03 +08:00
|
|
|
aa.requestRedraw();
|
|
|
|
return true;
|
|
|
|
break;
|
|
|
|
|
2003-03-12 21:54:59 +08:00
|
|
|
case 'w' :
|
|
|
|
{
|
|
|
|
osg::PolygonMode* polyModeObj = dynamic_cast<osg::PolygonMode*>(_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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-05-09 18:31:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2003-02-19 18:43:02 +08:00
|
|
|
void StateSetManipulator::getUsage(osg::ApplicationUsage& usage) const
|
|
|
|
{
|
|
|
|
usage.addKeyboardMouseBinding("b","Toggle backface culling");
|
|
|
|
usage.addKeyboardMouseBinding("l","Toggle lighting");
|
|
|
|
usage.addKeyboardMouseBinding("t","Toggle texturing");
|
2003-03-12 21:54:59 +08:00
|
|
|
usage.addKeyboardMouseBinding("w","Toggle polygon fill mode between fill, line (wire frame) and points");
|
2003-02-19 18:43:02 +08:00
|
|
|
}
|
|
|
|
|
2002-05-09 18:31:03 +08:00
|
|
|
void StateSetManipulator::accept(GUIEventHandlerVisitor& gehv)
|
|
|
|
{
|
|
|
|
gehv.visit(*this);
|
|
|
|
}
|