gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:50:00 38 4
gpt4 key购买 nike

我想使用 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/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com