您的位置:首页 > 其它

osg中拾取对象和拾取点

2016-06-07 16:10 267 查看
使用osgUtil::PolytopeIntersector来拾取对象,使用这个类可与方便与点、直线等对象进行碰撞求交计算

int COSGViewInternal:PickObject(int x, int y, CObject* object)
{
const osg::GraphicsContext::Traits* trait = m_viewer->getCamera()->getGraphicsContext()->getTraits();
y = trait->height - y;

double w(5.0f), h(5.0f);
osg::ref_ptr<osgUtil::PolytopeIntersector> picker = new osgUtil::PolytopeIntersector(osgUtil::Intersector::WINDOW, x-w, y-h, x+w, y+h);
osgUtil::IntersectionVisitor iv(picker);
m_viewer->getCamera()->accept(iv);
if(picker->containsIntersections())
{
typedef osgUtil::PolytopeIntersector::Intersections Inters;
for(Inters::iterator it = picker->getIntersections.begin();\
it != picker->getIntersections().end(); it++)
{
osg::NodePath nodepath = it->nodePath;
unsigned int idx = nodePath.size();

//查找交集节点路径中的最后一个节点
obj = dynamic_cast<CObject*>(nodePath[idx -1]);
}
}
}


使用osgUtil::LineSegmentIntersector类来创建与对象的交点

int COSGViewInternal:PickPoint(int x, int y, osg::vec3& pnt)
{
const osg::GraphicsContext::Traits* trait = m_viewer->getCamera()->getGraphicsContext()->getTraits();
y = trait->height - y;
float nx = (x - (trait->width/2.0))/(trait->width/2.0);
float nx = (x - (trait->width/2.0))/(trait->width/2.0);
osgUtil::LineSegmentIntersector* picker = new osgUtil::LineSegmentIntersector(osgUtil::Intersection::PROJECTION, nx, ny);
osgUtil::IntersectionVisitor iv(picker);
m_viewer->getCamera()->accept(iv);
if(picker->containsIntersections())
{
typedef osgUtil::LineSegmentIntersector::Intersections Inters;
for(Inters::iterator it = picker->getIntersections.begin();\
it != picker->getIntersections().end(); it++)
{
osg::NodePath nodepath = it->nodePath;
unsigned int idx = nodePath.size();

//查找交集节点路径中的最后一个节点
pnt = it->locakIntersectionPoint;
normal = it->localIntersectionNormal;
return true;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: