|
在《OSG三维渲染引擎编程指南》上的pick例子中
node = (nodePath.size()>=1)?nodePath[nodePath.size()-1]:0;
parent = (nodePath.size()>=2)?dynamic_cast<osg::Group*>(nodePath[nodePath.size()-2]):0;这两行代码是什么意思呢?有劳各位提点一下!谢谢。
整个函数的代码是:
//对象选取事件处理器
void 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::LineSegmentIntersector::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; }
//用一种高亮显示物体已经被选中
if(parent.get()&&node.get())
{
osg::ref_ptr<osgFX::Scribe> parentAsScribe = dynamic_cast<osgFX::Scribe*>(parent.get());
if(!parentAsScribe)
{
osg::ref_ptr<osgFX::Scribe> scribe = new osgFX::Scribe();
scribe->addChild(node.get());
parent->replaceChild(node.get(),scribe.get());
}
else
{
//如果没有选择到,则移除高亮显示的对象
osg::Node::ParentList parentList = parentAsScribe->getParents();
for(osg::Node::ParentList::iterator itr = parentList.begin();
itr!= parentList.end(); ++itr)
{
(*itr)->replaceChild(parentAsScribe.get(),node.get());
}
}
}
} |
|