From 77d4705182a3fe89bb6195a3052f9f6836eba853 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 6 Sep 2017 16:53:54 +0100 Subject: [PATCH] Added KeyHandler for toggling "SIGNED_DISTANCE_FIELD" and "OUTLINE" #pragma(tic) shader defines to control the different shader paths. Keys to press are 'd' for toggle SIGNED_DISTANCE_FIELD and 'o' for OUTLINE. --- examples/osgfont/osgfont.cpp | 57 +++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/examples/osgfont/osgfont.cpp b/examples/osgfont/osgfont.cpp index 55f893f36..3e70be384 100644 --- a/examples/osgfont/osgfont.cpp +++ b/examples/osgfont/osgfont.cpp @@ -182,7 +182,61 @@ osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigne return label; } +class KeyHandler : public osgGA::GUIEventHandler +{ +public: + KeyHandler() {} + + ~KeyHandler() {} + + bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) + { + osgViewer::View* view = dynamic_cast(&aa); + if (!view) return false; + +#if 1 + osg::StateSet* stateset = view->getSceneData()->getOrCreateStateSet(); +#else + osg::StateSet* stateset = view->getCamera()->getOrCreateStateSet(); +#endif + switch(ea.getEventType()) + { + case(osgGA::GUIEventAdapter::KEYUP): + { + if (ea.getKey()=='d') + { + toggleDefine(stateset, "SIGNED_DISTANCE_FIELD"); + return true; + } + else if (ea.getKey()=='o') + { + toggleDefine(stateset, "OUTLINE"); + return true; + } + break; + } + default: + break; + } + return false; + } + + void toggleDefine(osg::StateSet* stateset, const std::string& define) + { + osg::StateSet::DefinePair* dp = stateset->getDefinePair(define); + if (dp) + { + OSG_NOTICE<<"Disabling "<removeDefine(define); + } + else + { + OSG_NOTICE<<"Enabling "<setDefine(define); + } + } +}; int main(int argc, char** argv) @@ -191,9 +245,10 @@ int main(int argc, char** argv) osgViewer::Viewer viewer(args); - viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) ); + viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet())); viewer.addEventHandler(new osgViewer::StatsHandler()); viewer.addEventHandler(new osgViewer::WindowSizeHandler()); + viewer.addEventHandler(new KeyHandler()); TextSettings settings; settings.backgroundColor = viewer.getCamera()->getClearColor();