|
用EventHandler来实现点击选中并且高亮节点,拾取函数如下。
在多次点击拾取模型中的节点之后可能报错,需要的点击次数不固定,有时5次就报错,有时20次左右才报错;只在Debug模式下报错,Release下从来不报错。
报错信息见下图:
- void CPickHandler::Pick(float x, float y)
- {
- static osg::ref_ptr<osg::Group> m_selected = new osg::Group();
- static osg::ref_ptr<osg::Material> m_material = new osg::Material();
- m_material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.f, 0.f, 0.f, 1.f));
-
- while(m_selected->getNumChildren() > 0)
- {
- osg::ref_ptr<osg::Node> node = m_selected->getChild(0);
- node->getOrCreateStateSet()->removeAttribute(m_material.get());
- m_selected->removeChild(node.get());
- }
-
- osgUtil::LineSegmentIntersector::Intersections intersections;
- if(m_Viewer->computeIntersections(x, y, intersections))
- {
- osgUtil::LineSegmentIntersector::Intersections::iterator hitr = intersections.begin();
- if (hitr != intersections.end() && !hitr->nodePath.empty())
- {
- osg::ref_ptr<osg::Node> node = hitr->nodePath.back();
-
- osg::StateSet * stateset = node->getOrCreateStateSet();
- stateset->setAttribute(m_material.get(), osg::StateAttribute::ON/* | osg::StateAttribute::OVERRIDE*/);
- m_selected->addChild(node.get());
- }
- }
- }
复制代码
|
-
|