c++ - 鼠标事件选择器openscenegraph

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

最佳答案

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

关于c++ - 鼠标事件选择器openscenegraph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41467251/

相关文章:

c++ - 如何使用 cv::dnn::Net 执行深层复制?

c++ - 模板元编程或运算

.net - 为什么RegisterAllAreas 中区域的顺序随着Visual Studio 2015 发生变化?

c++ - 在 Visual Studio 2017 中使用 Cmake 构建 ssh.dll

c++ - 使用 gcc c++ 获取 Windows 版本

c++ - 如何在 OS X 上使用 QSettings 存储 INI 文件

css - VS2010 - 编写CSS时自动添加冒号

C# System.Windows.Forms.Timer EventHandler 未被调用

delphi - 将私有(private)/ protected /公共(public)方法设置为事件处理程序是否安全?

jquery动态绑定(bind).on()选择 parent 还是 child ?