diff --git a/VisualStudio/OpenSceneGraph.dsw b/VisualStudio/OpenSceneGraph.dsw index 713ab81d3..59661a304 100644 --- a/VisualStudio/OpenSceneGraph.dsw +++ b/VisualStudio/OpenSceneGraph.dsw @@ -1350,6 +1350,9 @@ Package=<4> Project_Dep_Name Core osgViewer End Project Dependency Begin Project Dependency + Project_Dep_Name Core osgFX + End Project Dependency + Begin Project Dependency Project_Dep_Name Core osgUtil End Project Dependency }}} diff --git a/examples/osgmultiplecameras/GNUmakefile b/examples/osgmultiplecameras/GNUmakefile index b162479bb..74e2ee0db 100644 --- a/examples/osgmultiplecameras/GNUmakefile +++ b/examples/osgmultiplecameras/GNUmakefile @@ -4,7 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ osgmultiplecameras.cpp\ -LIBS += -losgViewer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) +LIBS += -losgViewer -losgFX -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) INSTFILES = \ $(CXXFILES)\ diff --git a/examples/osgmultiplecameras/GNUmakefile.inst b/examples/osgmultiplecameras/GNUmakefile.inst index adb3ce35d..a3e5a6768 100644 --- a/examples/osgmultiplecameras/GNUmakefile.inst +++ b/examples/osgmultiplecameras/GNUmakefile.inst @@ -4,7 +4,7 @@ include $(TOPDIR)/Make/makedefs CXXFILES =\ osgmultiplecameras.cpp\ -LIBS += -losgViewer -losgText-losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) +LIBS += -losgViewer -losgFX -losgText -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS) EXEC = osgmultiplecameras diff --git a/examples/osgmultiplecameras/osgmultiplecameras.cpp b/examples/osgmultiplecameras/osgmultiplecameras.cpp index e0375ced5..bf94a9ad1 100644 --- a/examples/osgmultiplecameras/osgmultiplecameras.cpp +++ b/examples/osgmultiplecameras/osgmultiplecameras.cpp @@ -32,6 +32,92 @@ #include +#include + +#include + +// class to handle events with a pick +class PickHandler : public osgGA::GUIEventHandler { +public: + + PickHandler(): + _mx(0.0f), + _my(0.0f) {} + + ~PickHandler() {} + + bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) + { + osgViewer::View* view = dynamic_cast(&aa); + if (!view) return false; + + switch(ea.getEventType()) + { + case(osgGA::GUIEventAdapter::PUSH): + { + _mx = ea.getX(); + _my = ea.getY(); + break; + } + case(osgGA::GUIEventAdapter::RELEASE): + { + if (_mx==ea.getX() && _my==ea.getY()) + { + pick(view, ea.getX(), ea.getY()); + } + break; + } + default: + break; + } + return false; + } + + void pick(osgViewer::View* view, float x, float y) + { + osg::Node* node = 0; + osg::Group* parent = 0; + + osgUtil::LineSegmentIntersector::Intersections intersections; + if (view->computeIntersections(x, y, intersections)) + { + osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin(); + osg::NodePath& nodePath = intersection.nodePath; + node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0; + parent = (nodePath.size()>=2)?dynamic_cast(nodePath[nodePath.size()-2]):0; + } + + // now we try to decorate the hit node by the osgFX::Scribe to show that its been "picked" + if (parent && node) + { + + osgFX::Scribe* parentAsScribe = dynamic_cast(parent); + if (!parentAsScribe) + { + // node not already picked, so highlight it with an osgFX::Scribe + osgFX::Scribe* scribe = new osgFX::Scribe(); + scribe->addChild(node); + parent->replaceChild(node,scribe); + } + else + { + // node already picked so we want to remove scribe to unpick it. + osg::Node::ParentList parentList = parentAsScribe->getParents(); + for(osg::Node::ParentList::iterator itr=parentList.begin(); + itr!=parentList.end(); + ++itr) + { + (*itr)->replaceChild(parentAsScribe,node); + } + } + } + + } + + float _mx, _my; + +}; + int main( int argc, char **argv ) { @@ -47,105 +133,20 @@ int main( int argc, char **argv ) // construct the viewer. osgViewer::CompositeViewer viewer; + if (arguments.read("-1")) - { - - osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); - if (!wsi) - { - osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); - - osg::ref_ptr traits = new osg::GraphicsContext::Traits; - traits->x = 0; - traits->y = 0; - traits->width = width; - traits->height = height; - traits->windowDecoration = true; - traits->doubleBuffer = true; - traits->sharedContext = 0; - - osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get()); - if (gc.valid()) - { - osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); - gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } - else - { - osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<setSceneData(scene.get()); - view->getCamera()->setViewport(new osg::Viewport(0,0, width/2, height/2)); - view->getCamera()->setGraphicsContext(gc.get()); - view->setCameraManipulator(new osgGA::TrackballManipulator); - viewer.addView(view); - - // add the state manipulator - osg::ref_ptr statesetManipulator = new osgGA::StateSetManipulator; - statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet()); - - view->addEventHandler( statesetManipulator.get() ); - } - - // view two - { - osgViewer::View* view = new osgViewer::View; - view->setSceneData(scene.get()); - view->getCamera()->setViewport(new osg::Viewport(width/2,0, width/2, height/2)); - view->getCamera()->setGraphicsContext(gc.get()); - view->setCameraManipulator(new osgGA::TrackballManipulator); - viewer.addView(view); - } - - // view three - if (false) - { - osgViewer::View* view = new osgViewer::View; - view->setSceneData(osgDB::readNodeFile("town.ive")); - - view->setUpViewAcrossAllScreens(); - viewer.addView(view); - } - else - { - osgViewer::View* view = new osgViewer::View; - view->setSceneData(osgDB::readNodeFile("town.ive")); - - view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(width) / double(height/2), 1.0, 1000.0); - view->getCamera()->setViewport(new osg::Viewport(0, height/2, width, height/2)); - view->getCamera()->setGraphicsContext(gc.get()); - view->setCameraManipulator(new osgGA::FlightManipulator); - viewer.addView(view); - } - - } - - if (arguments.read("-2")) { { osgViewer::View* view = new osgViewer::View; - view->setSceneData(osgDB::readNodeFile("town.ive")); + view->setSceneData(osgDB::readNodeFile("fountain.osg")); view->setUpViewAcrossAllScreens(); - view->setCameraManipulator(new osgGA::FlightManipulator); + view->setCameraManipulator(new osgGA::TrackballManipulator); viewer.addView(view); } } - if (arguments.read("-3") || viewer.getNumViews()==0) + if (arguments.read("-2")) { // view one @@ -172,9 +173,95 @@ int main( int argc, char **argv ) view->setUpViewOnSingleScreen(1); view->setSceneData(scene.get()); view->setCameraManipulator(new osgGA::TrackballManipulator); + + // add the handler for doing the picking + view->addEventHandler(new PickHandler()); } } + + if (arguments.read("-3") || viewer.getNumViews()==0) + { + + osg::GraphicsContext::WindowingSystemInterface* wsi = osg::GraphicsContext::getWindowingSystemInterface(); + if (!wsi) + { + osg::notify(osg::NOTICE)<<"Error, no WindowSystemInterface available, cannot create windows."<getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), width, height); + + osg::ref_ptr traits = new osg::GraphicsContext::Traits; + traits->x = 100; + traits->y = 100; + traits->width = 1000; + traits->height = 800; + traits->windowDecoration = true; + traits->doubleBuffer = true; + traits->sharedContext = 0; + + osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits.get()); + if (gc.valid()) + { + osg::notify(osg::INFO)<<" GraphicsWindow has been created successfully."<setClearColor(osg::Vec4f(0.2f,0.2f,0.6f,1.0f)); + gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + else + { + osg::notify(osg::NOTICE)<<" GraphicsWindow has not been created successfully."<setSceneData(scene.get()); + view->getCamera()->setViewport(new osg::Viewport(0,0, traits->width/2, traits->height/2)); + view->getCamera()->setGraphicsContext(gc.get()); + view->setCameraManipulator(new osgGA::TrackballManipulator); + + // add the state manipulator + osg::ref_ptr statesetManipulator = new osgGA::StateSetManipulator; + statesetManipulator->setStateSet(view->getCamera()->getOrCreateStateSet()); + + view->addEventHandler( statesetManipulator.get() ); + } + + // view two + { + osgViewer::View* view = new osgViewer::View; + viewer.addView(view); + + view->setSceneData(scene.get()); + view->getCamera()->setViewport(new osg::Viewport(traits->width/2,0, traits->width/2, traits->height/2)); + view->getCamera()->setGraphicsContext(gc.get()); + view->setCameraManipulator(new osgGA::TrackballManipulator); + + // add the handler for doing the picking + view->addEventHandler(new PickHandler()); + + } + + { + osgViewer::View* view = new osgViewer::View; + viewer.addView(view); + + view->setSceneData(osgDB::readNodeFile("cessnafire.osg")); + + view->getCamera()->setProjectionMatrixAsPerspective(30.0, double(traits->width) / double(traits->height/2), 1.0, 1000.0); + view->getCamera()->setViewport(new osg::Viewport(0, traits->height/2, traits->width, traits->height/2)); + view->getCamera()->setGraphicsContext(gc.get()); + view->setCameraManipulator(new osgGA::TrackballManipulator); + } + + } while (arguments.read("-s")) { viewer.setThreadingModel(osgViewer::CompositeViewer::SingleThreaded); } while (arguments.read("-g")) { viewer.setThreadingModel(osgViewer::CompositeViewer::ThreadPerContext); }