NodeVisitor的使用-遍历Geode节点并在它与父节点之间添加一个LOD节点

#include <osg\NodeVisitor>
#include <osg\MatrixTransform>
#include <osg\PagedLOD>
#include <osgDB\FileNameUtils>
#include <osg\Geode>
#include <strstream>

//只能处理osgExp插件导出的单个ive文件
class InsideLODVisitor : public osg::NodeVisitor
{
public:
InsideLODVisitor(float_rangeScale=50):rangeScale(_rangeScale),
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
{
}
virtual void apply(osg::Geode& geode)
{
//检测是否处理过
if (markedGeode.find(&geode)!=markedGeode.end())
{
return;
}
//记录已经处理
markedGeode.insert(&geode);
//在geode的父节点和geode之间插入一个LOD节点
unsigned int parentNum=geode.getNumParents();
osg::Node::ParentList parents=geode.getParents();
for (osg::Node::ParentList::iterator itr =parents.begin(); itr<parents.end(); itr++)
{
osg::ref_ptr<osg::LOD> lod=new osg::LOD;
lod->addChild(&geode, 0, geode.getBound().radius()*rangeScale);
//osg::ref_ptr<osg::Geode> tmpGeode=dynamic_cast<osg::Geode*>(geode.clone(osg::CopyOp::DEEP_COPY_ALL));
//osgUtil::Simplifier simplifier(0.5);
//tmpGeode->accept(simplifier);
//lod->addChild(tmpGeode, geode.getBound().radius()*rangeScale, FLT_MAX);
(*itr)->replaceChild(&geode, lod);
}
}
private:
std::set<osg::Geode*> markedGeode;
float rangeScale;
};

posted @ 2015-06-19 09:46  酷熊  阅读(1244)  评论(0编辑  收藏  举报