diff --git a/apps/nifosgtest/test.cpp b/apps/nifosgtest/test.cpp index 668bda373..16d1d58a3 100644 --- a/apps/nifosgtest/test.cpp +++ b/apps/nifosgtest/test.cpp @@ -17,9 +17,8 @@ class WireframeKeyHandler : public osgGA::GUIEventHandler { public: - WireframeKeyHandler(osg::Node* node) + WireframeKeyHandler() : mWireframe(false) - , mNode(node) { } @@ -32,12 +31,7 @@ public: if (adapter.getKey() == osgGA::GUIEventAdapter::KEY_W) { mWireframe = !mWireframe; - osg::PolygonMode* mode = new osg::PolygonMode; - 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); + // applying state from an event handler doesn't appear to be safe, so do it in the frame update return true; } default: @@ -46,9 +40,13 @@ public: return false; } + bool getWireframe() const + { + return mWireframe; + } + private: bool mWireframe; - osg::Node* mNode; }; int main(int argc, char** argv) @@ -93,11 +91,28 @@ int main(int argc, char** argv) viewer.setUpViewInWindow(0, 0, 800, 600); viewer.realize(); viewer.setCameraManipulator(new osgGA::TrackballManipulator()); - viewer.addEventHandler(new WireframeKeyHandler(root)); + + WireframeKeyHandler* keyHandler = new WireframeKeyHandler; + + viewer.addEventHandler(keyHandler); viewer.addEventHandler(new osgViewer::StatsHandler); + bool wireframe = false; + 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(); for (unsigned int i=0; i