/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or * (at your option) any later version. The full license is in LICENSE file * included with this distribution, and on the openscenegraph.org website. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * OpenSceneGraph Public License for more details. */ /* osgpick sample * demonstrate use of osgUtil/PickVisitor for picking in a HUD or * in a 3d scene, */ #include #include #include #include #include #include #include #include #include #include #include #include #include // class to handle events with a pick class PickHandler : public osgGA::GUIEventHandler { public: PickHandler(osgText::Text* updateText): _updateText(updateText) {} ~PickHandler() {} bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); virtual void pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea); void setLabel(const std::string& name) { if (_updateText.get()) _updateText->setText(name); } protected: osg::ref_ptr _updateText; }; bool PickHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) { switch(ea.getEventType()) { case(osgGA::GUIEventAdapter::FRAME): { osgViewer::Viewer* viewer = dynamic_cast(&aa); if (viewer) pick(viewer,ea); return false; } default: return false; } } void PickHandler::pick(osgViewer::Viewer* viewer, const osgGA::GUIEventAdapter& ea) { osgUtil::LineSegmentIntersector::Intersections intersections; std::string gdlist=""; if (viewer->computeIntersections(ea.getX(),ea.getY(),intersections)) { for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin(); hitr != intersections.end(); ++hitr) { std::ostringstream os; if (!hitr->nodePath.empty() && !(hitr->nodePath.back()->getName().empty())) { // the geodes are identified by name. os<<"Object \""<nodePath.back()->getName()<<"\""<drawable.valid()) { os<<"Object \""<drawable->className()<<"\""<getLocalIntersectPoint()<<")"<<" normal("<getLocalIntersectNormal()<<")"<getWorldIntersectPoint()<<")"<<" normal("<getWorldIntersectNormal()<<")"<indexList; for(unsigned int i=0;igetOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); geode->setName("simple"); hudCamera->addChild(geode); osgText::Text* text = new osgText::Text; geode->addDrawable( text ); text->setFont(timesFont); text->setText("Picking in Head Up Displays is simple!"); text->setPosition(position); position += delta; } for (int i=0; i<5; i++) { osg::Vec3 dy(0.0f,-30.0f,0.0f); osg::Vec3 dx(120.0f,0.0f,0.0f); osg::Geode* geode = new osg::Geode(); osg::StateSet* stateset = geode->getOrCreateStateSet(); const char *opts[]={"One", "Two", "Three", "January", "Feb", "2003"}; osg::Geometry *quad=new osg::Geometry; stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); std::string name="subOption"; name += " "; name += std::string(opts[i]); geode->setName(name); osg::Vec3Array* vertices = new osg::Vec3Array(4); // 1 quad osg::Vec4Array* colors = new osg::Vec4Array; colors = new osg::Vec4Array; colors->push_back(osg::Vec4(0.8-0.1*i,0.1*i,0.2*i, 1.0)); quad->setColorArray(colors); quad->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); (*vertices)[0]=position; (*vertices)[1]=position+dx; (*vertices)[2]=position+dx+dy; (*vertices)[3]=position+dy; quad->setVertexArray(vertices); quad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); geode->addDrawable(quad); hudCamera->addChild(geode); position += delta; } { // this displays what has been selected osg::Geode* geode = new osg::Geode(); osg::StateSet* stateset = geode->getOrCreateStateSet(); stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF); stateset->setMode(GL_DEPTH_TEST,osg::StateAttribute::OFF); geode->setName("The text label"); geode->addDrawable( updateText ); hudCamera->addChild(geode); updateText->setCharacterSize(20.0f); updateText->setFont(timesFont); updateText->setColor(osg::Vec4(1.0f,1.0f,0.0f,1.0f)); updateText->setText(""); updateText->setPosition(position); updateText->setDataVariance(osg::Object::DYNAMIC); position += delta; } return hudCamera; } 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); osg::ref_ptr group = dynamic_cast(scene.get()); if (!group) { group = new osg::Group; group->addChild(scene.get()); } osg::ref_ptr updateText = new osgText::Text; // add the HUD subgraph. group->addChild(createHUD(updateText.get())); // 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(); }