From d8f674a59d7f16c7b13069293075f20b64620e0a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 4 Mar 2008 14:19:03 +0000 Subject: [PATCH] From Jean-Sebastien Guay and Robert Osfield, added optional --CompositeViewer path into osgpick to illustrate how to do picking in both viewers and as unit test for picking. --- examples/osgpick/osgpick.cpp | 52 +++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/examples/osgpick/osgpick.cpp b/examples/osgpick/osgpick.cpp index 0c48b1e7f..c829ff4f6 100644 --- a/examples/osgpick/osgpick.cpp +++ b/examples/osgpick/osgpick.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -49,7 +50,7 @@ public: bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); - virtual void pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea); + virtual void pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea); void setLabel(const std::string& name) { @@ -65,10 +66,10 @@ bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapte { switch(ea.getEventType()) { - case(osgGA::GUIEventAdapter::FRAME): + case(osgGA::GUIEventAdapter::PUSH): { - osgViewer::Viewer* viewer = dynamic_cast(&aa); - if (viewer) pick(viewer,ea); + osgViewer::View* view = dynamic_cast(&aa); + if (view) pick(view,ea); return false; } default: @@ -76,12 +77,14 @@ bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapte } } -void PickHandler::pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea) +void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea) { osgUtil::LineSegmentIntersector::Intersections intersections; std::string gdlist=""; - if (viewer->computeIntersections(ea.getX(),ea.getY(),intersections)) + float x = ea.getX(); + float y = ea.getY(); + if (view->computeIntersections(x,y,intersections)) { for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); hitr != intersections.end(); @@ -210,13 +213,9 @@ osg::Node* createHUD(osgText::Text* updateText) int main( int argc, char **argv ) { - // use an ArgumentParser object to manage the program arguments. osg::ArgumentParser arguments(&argc,argv); - // construct the viewer. - osgViewer::Viewer viewer; - // read the scene from the list of file specified commandline args. osg::ref_ptr scene = osgDB::readNodeFiles(arguments); @@ -235,11 +234,34 @@ int main( int argc, char **argv ) // add the HUD subgraph. group->addChild(createHUD(updateText.get())); - // add the handler for doing the picking - viewer.addEventHandler(new PickHandler(updateText.get())); + if (arguments.read("--CompositeViewer")) + { + osg::ref_ptr view = new osgViewer::View; + // add the handler for doing the picking + view->addEventHandler(new PickHandler(updateText.get())); - // set the scene to render - viewer.setSceneData(group.get()); + // set the scene to render + view->setSceneData(group.get()); + + view->setUpViewAcrossAllScreens(); + + osgViewer::CompositeViewer viewer; + viewer.addView(view.get()); + + return viewer.run(); + + } + else + { + osgViewer::Viewer viewer; + + // add the handler for doing the picking + viewer.addEventHandler(new PickHandler(updateText.get())); + + // set the scene to render + viewer.setSceneData(group.get()); + + return viewer.run(); + } - return viewer.run(); }