|
有哪位大侠在mfc下做过osg的拾取,小弟在osgMFC那个例子里增加拾取功能,老是报错,请指点一二,不胜感激!!!
下面是CPickHandler的代码
//////h文件
#pragma once
#include <osgGA/GUIEventHandler>
#include <osgViewer/View>
class CPickHandler :
public osgGA::GUIEventHandler
{
public:
CPickHandler(void);
virtual ~CPickHandler(void);
public:
float m_fMouseX;
float m_fMouseY;
public:
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa);
void pick( osg::ref_ptr<osgViewer::View> view, float x, float y);
};
/////////cpp文件
#include "StdAfx.h"
#include "ickHandler.h"
#include <osgUtil/LineSegmentIntersector>
CPickHandler::CPickHandler(void)
{
m_fMouseX = 0;
m_fMouseY = 0;
}
CPickHandler::~CPickHandler(void)
{
}
bool CPickHandler::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa)
{
osg::ref_ptr<osgViewer::View> view = dynamic_cast<osgViewer::View*>(&aa);
if ( !view )
{
return false;
}
switch(ea.getEventType())
{
case osgGA::GUIEventAdapter:USH:
{
m_fMouseX = ea.getX();
m_fMouseY = ea.getY();
break;
}
case osgGA::GUIEventAdapter::RELEASE:
{
if ( m_fMouseX==ea.getX() && m_fMouseY == ea.getY() )
{
pick(view.get(), m_fMouseX, m_fMouseY);
}
break;
}
default:
break;
}
return false;
}
void CPickHandler::pick( osg::ref_ptr<osgViewer::View> view, float x, float y)
{
osg::ref_ptr<osg::Node> node = new osg::Node();
osg::ref_ptr<osg::Group> parent = new osg::Group();
osgUtil:ineSegmentIntersector::Intersections intersections;
if ( view->computeIntersections(x,y,intersections))
{
osgUtil::LineSegmentIntersector::Intersection intersection = *intersections.begin();
osg::NodePath& nodepath = intersection.nodePath;
node = (nodepath.size()>=1)?nodepath[nodepath.size()-1]:0;
parent = (nodepath.size()>=2)?dynamic_cast<osg::Group*>(nodepath[nodepath.size()-2]):0;
CString ss;
std::string str = node->getName();
ss = str.c_str();
AfxMessageBox(ss);
}
}
在osg类中增加事件处理器
void cOSG::InitCameraConfig(void)
{
// Local Variable to hold window size data
RECT rect;
// Create the viewer for this window
mViewer = new osgViewer::Viewer();
//这句注释掉就不报错,打开就报错
mViewer->addEventHandler(new CPickHandler());
// Add a Stats Handler to the viewer
//mViewer->addEventHandler(new osgViewer::StatsHandler);
// Get the current window size
::GetWindowRect(m_hWnd, &rect);
………………以下省略……………………
}
|
-
|