Seems to fix wireframe crashes

c++11
scrawl 10 years ago
parent 2eedb3acec
commit 31a0bbcb23

@ -17,9 +17,8 @@
class WireframeKeyHandler : public osgGA::GUIEventHandler class WireframeKeyHandler : public osgGA::GUIEventHandler
{ {
public: public:
WireframeKeyHandler(osg::Node* node) WireframeKeyHandler()
: mWireframe(false) : mWireframe(false)
, mNode(node)
{ {
} }
@ -32,12 +31,7 @@ public:
if (adapter.getKey() == osgGA::GUIEventAdapter::KEY_W) if (adapter.getKey() == osgGA::GUIEventAdapter::KEY_W)
{ {
mWireframe = !mWireframe; mWireframe = !mWireframe;
osg::PolygonMode* mode = new osg::PolygonMode; // applying state from an event handler doesn't appear to be safe, so do it in the frame update
mode->setMode(osg::PolygonMode::FRONT_AND_BACK,
mWireframe ? osg::PolygonMode::LINE : osg::PolygonMode::FILL);
mNode->getOrCreateStateSet()->setAttributeAndModes(mode, osg::StateAttribute::ON);
mNode->getOrCreateStateSet()->setMode(GL_CULL_FACE, mWireframe ? osg::StateAttribute::OFF
: osg::StateAttribute::ON);
return true; return true;
} }
default: default:
@ -46,9 +40,13 @@ public:
return false; return false;
} }
bool getWireframe() const
{
return mWireframe;
}
private: private:
bool mWireframe; bool mWireframe;
osg::Node* mNode;
}; };
int main(int argc, char** argv) int main(int argc, char** argv)
@ -93,11 +91,28 @@ int main(int argc, char** argv)
viewer.setUpViewInWindow(0, 0, 800, 600); viewer.setUpViewInWindow(0, 0, 800, 600);
viewer.realize(); viewer.realize();
viewer.setCameraManipulator(new osgGA::TrackballManipulator()); viewer.setCameraManipulator(new osgGA::TrackballManipulator());
viewer.addEventHandler(new WireframeKeyHandler(root));
WireframeKeyHandler* keyHandler = new WireframeKeyHandler;
viewer.addEventHandler(keyHandler);
viewer.addEventHandler(new osgViewer::StatsHandler); viewer.addEventHandler(new osgViewer::StatsHandler);
bool wireframe = false;
while (!viewer.done()) while (!viewer.done())
{ {
if (wireframe != keyHandler->getWireframe())
{
wireframe = keyHandler->getWireframe();
osg::PolygonMode* mode = new osg::PolygonMode;
mode->setMode(osg::PolygonMode::FRONT_AND_BACK,
wireframe ? osg::PolygonMode::LINE : osg::PolygonMode::FILL);
root->getOrCreateStateSet()->setAttributeAndModes(mode, osg::StateAttribute::ON);
root->getOrCreateStateSet()->setMode(GL_CULL_FACE, wireframe ? osg::StateAttribute::OFF
: osg::StateAttribute::ON);
}
viewer.frame(); viewer.frame();
for (unsigned int i=0; i<controllers.size(); ++i) for (unsigned int i=0; i<controllers.size(); ++i)

Loading…
Cancel
Save