首先建立事件响应类,继承自
osgGA::GUIEventHandler
在handle()函数中做相关处理如下:
//创建一个线段交集检测对象
bool handle( const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa ){ osgUtil::LineSegmentIntersector::Intersections intersections;
float x = ea.getX();
float y = ea.getY();
if (viewer->computeIntersections(x,y,intersections))
{
//得到相交交集的交点
/*for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
hitr != intersections.end();
++hitr)*/
osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();//找到第一个交接点即可
(*hitr).drawable->getBound().center()找到这个几何体的中心位置
/*若为双击事件,为避免响应两次,可添加如下代码:
//m_CB为类成员变量,初始化为NULL
if (m_CB)
{
// 双击消息
m_CB->pickCB(ostr.str().c_str(),88);
// qDebug()<<"DoubleClick"<<endl;
}
*/}