[osg-users] How to select or pick an object in OSG?

Alberto Luaces aluaces at udc.es
Thu Sep 6 08:00:17 PDT 2007


El Jueves 06 Septiembre 2007 16:05, hesicong2006 escribió:
> Hi,
> I'm new to OSG and now I 'm trying to do picking (or say selecting) in
> OSG. I create a scene by 3DSMAX. It contains a teapot, a box. And then
> it is exported by OSGExp to IVE format.
> I loaded this scene in my program. I create an PickHandler just like the
> osgPick does.
> The core pick function is:
>
> void PickHandler::pick(osgViewer::Viewer* viewer, const
> osgGA::GUIEventAdapter& ea)
> {
>     osgUtil::LineSegmentIntersector::Intersections intersections;
>
>     if(viewer->computeIntersections(ea.getX(),ea.getY(), intersections))
>     {
>         for(osgUtil::LineSegmentIntersector::Intersections::iterator
> hitr =intersections.begin();
>             hitr!=intersections.end();
>             ++hitr)
>         {
>             if(!hitr->nodePath.empty() &&
> !(hitr->nodePath.back()->getName().empty()))
>             {
>                 std::cout<<"Hit:
> "<<hitr->nodePath.back()->getName()<<std::endl;
>             }
>             else if (hitr->drawable.valid())
>             {
>                 std::cout<<"Geo: name="<<hitr->drawable->getName()
> <<std::endl;
>             }
>         }
>     }
> }
>
> I notice that the osgUtil::LineSegmentIntersector::Intersections gives
> me only the drawable list but the object list. My aim is to pick the
> "object" (teapot or the box), but it pick the drawable only. And how can
> I pick the object?

You have it just rigth there! :) "nodePath" is a vector of nodes describing 
the hierarchy from the picked node to its subsequent parents. nodePath.back() 
is the last node in the chain, so it is the one you are looking for. In 
addition, see how in your example the name of the node(s) being picked is/are 
displayed.

Alberto



More information about the osg-users mailing list