OSG学习笔记19——再看pick点选物体
2017-03-22 09:53
120 查看
之前一直使用的pick方法就是将第一个点选到的节点保存下来,如果场景中只有一个物体,当然是没有问题的。当场景中出现多个物体,并且每个物体设置了标签,是可以被修改还是不能被修改,当两个物体重合的时候就不能得到正确的结果。
正确的做法是:用迭代器遍历所有获取到的节点并再遍历nodePath,找到需要的节点,并根据标签判断其是否可以被选中来执行操作。
这里我对物体标签设置的方法应该是不够科学的,我用了一个全局变量,一个map来保存了模型的名字和一个布尔值
extern ModelIndex modelIndex;主函数中是
std::list<ModelManage> modelList;
model1->setName("aorta");
modelIndex[model1->getName()] = true;
其中 ModelIndex是 std::map < std::string, bool >的别名,每添加一个模型,就给map中以模型节点为名字,bool为值向map中新增节点。
暂时想不到更好的方法。如果你们有好一点的想法,请务必告诉我!
下面贴出pick的实现。其实和之前的没有多大的变化,详细介绍见 <OSG学习笔记16>
void PQEventHandler::pick(double x, double y)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
if (viewer->computeIntersections(x, y, intersections))
{
std::cout << " trying to pick sth" << "x: " << x << " y: " << y << std::endl;
osgUtil::LineSegmentIntersector::Intersections::iterator hitr;
for (hitr = intersections.begin(); hitr != intersections.end(); ++hitr)
{
if (!hitr->nodePath.empty() )
{
const osg::NodePath& np = hitr->nodePath;
for (int i = np.size() - 1; i >= 0;--i)
{
osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(np[i]);
if (mt != NULL)
{
std::cout << mt->getName() << std::endl;
ModelIndex::iterator miitr;
miitr = modelIndex.find(mt->getName());
if (miitr == modelIndex.end())
{
continue;
}
else
{
if (modelIndex[mt->getName()])
{
PickObject = true;
picked = mt;
rotateCenter =hitr->getWorldIntersectPoint();
}
else
{
continue;
}
}
}
}
}
}
}
else
{
PickObject = false;
picked = NULL;
}
}
正确的做法是:用迭代器遍历所有获取到的节点并再遍历nodePath,找到需要的节点,并根据标签判断其是否可以被选中来执行操作。
这里我对物体标签设置的方法应该是不够科学的,我用了一个全局变量,一个map来保存了模型的名字和一个布尔值
extern ModelIndex modelIndex;主函数中是
std::list<ModelManage> modelList;
model1->setName("aorta");
modelIndex[model1->getName()] = true;
其中 ModelIndex是 std::map < std::string, bool >的别名,每添加一个模型,就给map中以模型节点为名字,bool为值向map中新增节点。
暂时想不到更好的方法。如果你们有好一点的想法,请务必告诉我!
下面贴出pick的实现。其实和之前的没有多大的变化,详细介绍见 <OSG学习笔记16>
void PQEventHandler::pick(double x, double y)
{
osgUtil::LineSegmentIntersector::Intersections intersections;
if (viewer->computeIntersections(x, y, intersections))
{
std::cout << " trying to pick sth" << "x: " << x << " y: " << y << std::endl;
osgUtil::LineSegmentIntersector::Intersections::iterator hitr;
for (hitr = intersections.begin(); hitr != intersections.end(); ++hitr)
{
if (!hitr->nodePath.empty() )
{
const osg::NodePath& np = hitr->nodePath;
for (int i = np.size() - 1; i >= 0;--i)
{
osg::MatrixTransform* mt = dynamic_cast<osg::MatrixTransform*>(np[i]);
if (mt != NULL)
{
std::cout << mt->getName() << std::endl;
ModelIndex::iterator miitr;
miitr = modelIndex.find(mt->getName());
if (miitr == modelIndex.end())
{
continue;
}
else
{
if (modelIndex[mt->getName()])
{
PickObject = true;
picked = mt;
rotateCenter =hitr->getWorldIntersectPoint();
}
else
{
continue;
}
}
}
}
}
}
}
else
{
PickObject = false;
picked = NULL;
}
}
相关文章推荐
- OSG学习笔记13Pick点选物体
- OSG学习笔记24——根据鼠标平移的位置正确设置物体平移量
- OSG学习笔记16 - 对点选物体平移(鼠标点选物体)(物体随鼠标移动)(屏幕坐标转世界坐标)
- OpenCV 2 学习笔记(19): 利用均值漂移(Meanshift)寻找物体
- OSG学习笔记18 - 关于物体绕指定点旋转的问题(非坐标轴原点)
- OSG学习笔记22——设置物体透明,再恢复
- 《Javascript高级程序设计》(第2版)学习笔记19--最佳实践
- ExtJs学习笔记(19)_复杂Form示例
- 31 天重构学习笔记19. 提取工厂类
- 传智播客Hibernate视频教程学习笔记19
- 传智播客Spring视频教程学习笔记19
- GCC-3.4.6源代码学习笔记(19)
- Asp.Net Ajax 学习笔记19 关注ASP.NET AJAX的核心-ScriptManager
- 【django 学习笔记】19-安全
- 31天重构学习笔记19. 提取工厂类
- 强大的DataGrid组件[11]_主从(Master-Details)的实现——Silverlight学习笔记[19]
- C#学习笔记和年轻真好 08/01/19
- 学习笔记:App-V测试错误代码4505CD-19D06A0A-10000004
- 美国海军研究生学校(NPS)OSG教程学习笔记——第一课
- 操作系统学习笔记(19)--贴一个内存设置的代码