Mouse event picker openscenegraph

1.3k views Asked by At

I want to use OpenSceneGraph Pickhandler in order to print the name of a node when clicked on with a mouse. I have made a PickHandler Header file and included what I think is the correct code to make this happen.

After no errors upon running the application does not display the node name when clicked. Have I missed something important?

bool PickHandler::handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
  `if( ea.getEventType() != osgGA::GUIEventAdapter::RELEASE &&
  ea.getButton()    != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
  {
return false;
  }

  osgViewer::View* viewer = dynamic_cast<osgViewer::View*>( &aa );

  if( viewer )
  {
osgUtil::LineSegmentIntersector* intersector
    = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, ea.getX(), ea.getY() );`if( ea.getEventType() != osgGA::GUIEventAdapter::RELEASE &&
  ea.getButton()    != osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON )
 {
return false;
}

osgViewer::View* viewer = dynamic_cast<osgViewer::View*>( &aa );

if( viewer )
{
osgUtil::LineSegmentIntersector* intersector
    = new osgUtil::LineSegmentIntersector( osgUtil::Intersector::WINDOW, ea.getX(), ea.getY() );

osgUtil::IntersectionVisitor iv( intersector );

osg::Camera* camera = viewer->getCamera();
if( !camera )
  return false;

camera->accept( iv );

if( !intersector->containsIntersections() )
  return false;

auto intersections = intersector->getIntersections();

std::cout << "Got " << intersections.size() << " intersections:\n";

for( auto&& intersection : intersections )
  std::cout << "  - Local intersection point = " << intersection.localIntersectionPoint << "\n";
}

return true;
}
1

There are 1 answers

5
vicrucann On

You need to extract your node name in order to print it. If you do not use any custom nodes, then use intersection.drawable->getName(). Make sure you set up a name for that particular's osg::Geometry, otherwire the name is empty by default.

The printing code for your case would be something like:

for( auto&& intersection : intersections ) {
   std::cout << "  - Local intersection point = " << intersection.localIntersectionPoint << "\n";
   std::cout << "Intersection name = " << intersection.drawable->getName() << std::endl; 
}