我想使用OpenSceneGraph Pickhandler以便在用鼠标单击时打印节点的名称。我已经制作了一个PickHandler Header文件,并包含了我认为是实现此目的的正确代码。

运行完应用程序后,如果没有错误,则单击时不显示节点名称。我错过了重要的事情吗?

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;
}

最佳答案

您需要提取节点名称才能进行打印。如果不使用任何自定义节点,请使用intersection.drawable->getName()。确保为该特定名称的osg::Geometry设置名称,否则该名称默认为空。

您的案例的印刷代码如下:

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

08-05 01:48