我想使用OpenSceneGraph Pickhandler來打印用鼠標點擊時節點的名稱。我製作了一個PickHandler頭文件,幷包含了我認爲是正確的代碼來實現此目的。鼠標事件選取器openscenegraph
運行應用程序時沒有錯誤,單擊時不顯示節點名稱。我錯過了重要的事情嗎?
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;
}
看起來你在你的代碼錯過了名的打印。它是否在代碼中顯示交叉點大小和本地交點? – vicrucann
是的,它會打印以下信息,但不知道如何獲取該點的節點名稱。輸出的 例子: 了2十字路口: - 本地交點= -0.148942 -0.512957 0.357376 - 本地交點= -0.159801 0.489366 0.363891 –