osg已经实现了通过屏幕坐标直接与物体求交的方法。
能得到pick的物体,也能得到坐标。
我希望点击大地形,根据点击的点画出直线。
屏幕坐标与世界坐标的转换都会了,随后实现这个功能。 屏幕坐标与世界坐标转换 1.配置MD
#pragma once #include <osgGA/GUIEventHandler> #include <osgUtil/LineSegmentIntersector> #include <osgViewer/Viewer> class CPickHandler : public osgGA::GUIEventHandler { public: CPickHandler(osgViewer::Viewer *viewer); ~CPickHandler( void); osgViewer::Viewer *m_pViewer; virtual bool handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); void pick( float fX, float fY); };
#include " StdAfx.h " #include " PickHandler.h " CPickHandler::CPickHandler(osgViewer::Viewer *viewer):m_pViewer(viewer) { } CPickHandler::~CPickHandler( void) { } bool CPickHandler::handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa ) { switch (ea.getEventType()) { case(osgGA::GUIEventAdapter::PUSH): { if (ea.getButton() == 1) { pick(ea.getX(),ea.getY()); } return true; } } return false; } void CPickHandler::pick( float fX, float fY ) { osgUtil::LineSegmentIntersector::Intersections intersections; if (m_pViewer->computeIntersections(fX,fY,intersections)) { for (auto itr=intersections.begin();itr!=intersections.end();++itr) { if (!itr->nodePath.empty()) { const osg::NodePath& np = itr->nodePath; for ( int i=np.size()- 1;i>= 0;--i) { osg::ref_ptr<osg::Node> node = dynamic_cast<osg::Node *>(np[i]); if (NULL != node && node->getName().compare( " cow ") == 0) { node->setNodeMask( 0); // itr->getWorldIntersectPoint(); // 得到坐标 } } } } } }
osg::Vec3 DrawCallback::getPoint() { osg::Vec3 vec3; osg::ref_ptr<osgViewer::Viewer> viewer = dynamic_cast<osgViewer::Viewer *>(m_pAA); osg::ref_ptr<osg::Camera> camera = viewer->getCamera(); osgViewer::Renderer *render = dynamic_cast<osgViewer::Renderer *>(camera->getRenderer()); osgUtil::SceneView *sceneView = render->getSceneView( 0); osg::Vec3 vScreen(m_pEA->getX(),m_pEA->getY(), 0); osg::Vec3 window; sceneView->projectWindowIntoObject(vScreen,window); osg::Matrix mVPW = camera->getViewMatrix() * camera->getProjectionMatrix() * camera->getViewport()->computeWindowMatrix(); osg::Matrix invertVPW; invertVPW.invert(mVPW); vec3 = window * invertVPW; return vec3; }
2.配置x64
3.找到mfc example
4.写callback
5.创建一个节点挂到根节点下,为这个节点设置一个回调器。
响应事件改变节点。 url: