鼠标事件选择器openscenegraph

时间:2017-01-04 15:19:46

标签: c++ visual-studio eventhandler openscenegraph

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

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

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 个答案:

答案 0 :(得分:0)

您需要提取节点名称才能打印它。如果您不使用任何自定义节点,请使用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; 
}