osg中拾取对象和拾取点
2016-06-07 16:10
267 查看
使用osgUtil::PolytopeIntersector来拾取对象,使用这个类可与方便与点、直线等对象进行碰撞求交计算
使用osgUtil::LineSegmentIntersector类来创建与对象的交点
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; } } }
相关文章推荐
- saltstack批量修改root密码
- SIM卡的PIN码(CHV)及对应的APDU命令
- using namespace [ns]与 using [ns]区别
- PHPWEB后台用Burp Suite拿webshell
- 提取用Bounding box label标记后的误标文件
- php mongodb 链接失败,提示Authentication failed on database...[坑]
- alt和title的用法区别
- Grunt 之通配符
- Grunt之watch详解
- Entity Framework 无法对没有主键的视图映射实体的解决办法
- LeetCode—354. Russian Doll Envelopes
- Python中Mysql的操作方法
- ARM寄存器介绍
- 使用Gradle第一个Java项目
- 常用的HTTP状态码
- Apache Flink:流处理中Window的概念
- js正则表达式基础
- iOS 直播-闪光灯的使用
- PID原理
- 导航栏渐变透明&下拉image放大