From df0fdadb59530f832b1a06c6292a1302ad4a0b8b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 11 Feb 2011 12:11:17 +0000 Subject: [PATCH] From Jean-Sebastien Guay, event handler for controlling the field of view of the viewer's camera using 0, - and = keys. --- examples/osgshadow/osgshadow.cpp | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/examples/osgshadow/osgshadow.cpp b/examples/osgshadow/osgshadow.cpp index a89dd9312..7e572aa2c 100644 --- a/examples/osgshadow/osgshadow.cpp +++ b/examples/osgshadow/osgshadow.cpp @@ -56,6 +56,57 @@ #include "IslandScene.h" +class ChangeFOVHandler : public osgGA::GUIEventHandler +{ +public: + ChangeFOVHandler(osg::Camera* camera) + : _camera(camera) + { + double fovy, aspectRatio, zNear, zFar; + _camera->getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); + std::cout << "FOV is " << fovy << std::endl; + } + + /** Deprecated, Handle events, return true if handled, false otherwise. */ + virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) + { + if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP) + { + if (ea.getKey() == '-' || ea.getKey() == '=' || ea.getKey() == '0') + { + double fovy, aspectRatio, zNear, zFar; + _camera->getProjectionMatrix().getPerspective(fovy, aspectRatio, zNear, zFar); + + if (ea.getKey() == '-') + { + fovy -= 5.0; + } + + if (ea.getKey() == '=') + { + fovy += 5.0; + } + + if (ea.getKey() == '0') + { + fovy = 45.0; + } + + std::cout << "Setting FOV to " << fovy << std::endl; + _camera->getProjectionMatrix().makePerspective(fovy, aspectRatio, zNear, zFar); + + return true; + } + } + + return false; + } + + osg::ref_ptr _camera; +}; + + + static int ReceivesShadowTraversalMask = 0x1; static int CastsShadowTraversalMask = 0x2; @@ -800,6 +851,8 @@ int main(int argc, char** argv) viewer.setSceneData(shadowedScene.get()); + viewer.addEventHandler(new ChangeFOVHandler(viewer.getCamera())); + // create the windows and run the threads. viewer.realize();