2001-10-19 22:22:02 +08:00
|
|
|
#include <osgUtil/Optimizer>
|
|
|
|
|
|
|
|
#include <osg/Transform>
|
|
|
|
#include <osg/LOD>
|
|
|
|
#include <osg/Impostor>
|
|
|
|
#include <osg/Billboard>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
|
|
|
#include <typeinfo>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace osgUtil;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Overall Optimizetion function.
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Optimizer::optimize(osg::Node* node, unsigned int options)
|
|
|
|
{
|
|
|
|
|
2001-12-14 22:25:38 +08:00
|
|
|
if (options & COMBINE_ADJACENT_LODS)
|
|
|
|
{
|
|
|
|
CombineLODsVisitor clv;
|
|
|
|
node->accept(clv);
|
|
|
|
clv.combineLODs();
|
|
|
|
}
|
2002-01-22 20:03:47 +08:00
|
|
|
/*
|
2001-10-19 22:22:02 +08:00
|
|
|
if (options & FLATTEN_STATIC_TRANSFORMS)
|
|
|
|
{
|
|
|
|
FlattenStaticTransformsVisitor fstv;
|
|
|
|
node->accept(fstv);
|
|
|
|
fstv.removeTransforms();
|
|
|
|
}
|
2002-01-22 20:03:47 +08:00
|
|
|
*/
|
2001-10-19 22:22:02 +08:00
|
|
|
if (options & REMOVE_REDUNDENT_NODES)
|
|
|
|
{
|
|
|
|
RemoveRedundentNodesVisitor rrnv;
|
|
|
|
node->accept(rrnv);
|
|
|
|
rrnv.removeRedundentNodes();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options & SHARE_DUPLICATE_STATE)
|
|
|
|
{
|
2001-10-19 23:30:54 +08:00
|
|
|
#if !defined(WIN32) || defined(_STLPORT_VERSION)
|
2001-10-19 22:22:02 +08:00
|
|
|
StateVisitor osv;
|
|
|
|
node->accept(osv);
|
|
|
|
osv.optimize();
|
2001-10-19 23:30:54 +08:00
|
|
|
#endif
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class TransformFunctor : public osg::Drawable::AttributeFunctor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
osg::Matrix _m;
|
|
|
|
osg::Matrix _im;
|
|
|
|
|
|
|
|
TransformFunctor(const osg::Matrix& m):
|
|
|
|
osg::Drawable::AttributeFunctor(osg::Drawable::COORDS|osg::Drawable::NORMALS)
|
|
|
|
{
|
|
|
|
_m = m;
|
|
|
|
_im.invert(_m);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~TransformFunctor() {}
|
|
|
|
|
|
|
|
virtual bool apply(osg::Drawable::AttributeBitMask abm,osg::Vec3* begin,osg::Vec3* end)
|
|
|
|
{
|
|
|
|
if (abm == osg::Drawable::COORDS)
|
|
|
|
{
|
|
|
|
for (osg::Vec3* itr=begin;itr<end;++itr)
|
|
|
|
{
|
|
|
|
(*itr) = (*itr)*_m;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (abm == osg::Drawable::NORMALS)
|
|
|
|
{
|
|
|
|
for (osg::Vec3* itr=begin;itr<end;++itr)
|
|
|
|
{
|
|
|
|
// note post mult by inverse for normals.
|
|
|
|
(*itr) = osg::Matrix::transform3x3(_im,(*itr));
|
|
|
|
(*itr).normalize();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Optimize State Visitor
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct LessAttributeFunctor
|
|
|
|
{
|
|
|
|
bool operator () (const osg::StateAttribute* lhs,const osg::StateAttribute* rhs) const
|
|
|
|
{
|
|
|
|
return (*lhs<*rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct LessStateSetFunctor
|
|
|
|
{
|
|
|
|
bool operator () (const osg::StateSet* lhs,const osg::StateSet* rhs) const
|
|
|
|
{
|
|
|
|
return (*lhs<*rhs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Optimizer::StateVisitor::reset()
|
|
|
|
{
|
|
|
|
_statesets.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::StateVisitor::addStateSet(osg::StateSet* stateset,osg::Object* obj)
|
|
|
|
{
|
|
|
|
_statesets[stateset].insert(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::StateVisitor::apply(osg::Node& node)
|
|
|
|
{
|
|
|
|
osg::StateSet* ss = node.getStateSet();
|
|
|
|
if (ss) addStateSet(ss,&node);
|
|
|
|
|
|
|
|
traverse(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::StateVisitor::apply(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
osg::StateSet* ss = geode.getStateSet();
|
|
|
|
if (ss) addStateSet(ss,&geode);
|
|
|
|
for(int i=0;i<geode.getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
osg::Drawable* drawable = geode.getDrawable(i);
|
|
|
|
if (drawable)
|
|
|
|
{
|
|
|
|
ss = drawable->getStateSet();
|
|
|
|
if (ss) addStateSet(ss,drawable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::StateVisitor::optimize()
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << "Num of StateSet="<<_statesets.size()<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// create map from state attributes to stateset which contain them.
|
|
|
|
typedef std::set<osg::StateSet*> StateSetList;
|
|
|
|
typedef std::map<osg::StateAttribute*,StateSetList> AttributeToStateSetMap;
|
|
|
|
|
|
|
|
AttributeToStateSetMap _attributeToStateSetMap;
|
|
|
|
|
|
|
|
// NOTE will need to track state attribute override value too.
|
|
|
|
|
|
|
|
for(StateSetMap::iterator sitr=_statesets.begin();
|
|
|
|
sitr!=_statesets.end();
|
|
|
|
++sitr)
|
|
|
|
{
|
|
|
|
osg::StateSet::AttributeList& attributes = sitr->first->getAttributeList();
|
|
|
|
for(osg::StateSet::AttributeList::iterator aitr= attributes.begin();
|
|
|
|
aitr!=attributes.end();
|
|
|
|
++aitr)
|
|
|
|
{
|
|
|
|
_attributeToStateSetMap[aitr->second.first.get()].insert(sitr->first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_attributeToStateSetMap.size()<2)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << "Too few state attributes to optimize."<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create unique set of state attribute pointers.
|
|
|
|
typedef std::vector<osg::StateAttribute*> AttributeList;
|
|
|
|
AttributeList _attributeList;
|
|
|
|
|
|
|
|
for(AttributeToStateSetMap::iterator aitr=_attributeToStateSetMap.begin();
|
|
|
|
aitr!=_attributeToStateSetMap.end();
|
|
|
|
++aitr)
|
|
|
|
{
|
|
|
|
_attributeList.push_back(aitr->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort the attributes so that equal attributes sit along side each
|
|
|
|
// other.
|
|
|
|
std::sort(_attributeList.begin(),_attributeList.end(),LessAttributeFunctor());
|
|
|
|
|
|
|
|
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << "state attribute list"<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
for(AttributeList::iterator aaitr = _attributeList.begin();
|
|
|
|
aaitr!=_attributeList.end();
|
|
|
|
++aaitr)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << " "<<*aaitr << " "<<(*aaitr)->className()<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << "searching for duplicate attributes"<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
// find the duplicates.
|
|
|
|
AttributeList::iterator first_unique = _attributeList.begin();
|
|
|
|
AttributeList::iterator current = first_unique; ++current;
|
|
|
|
for(; current!=_attributeList.end();++current)
|
|
|
|
{
|
|
|
|
if (**current==**first_unique)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << " found duplicate "<<(*current)->className()<<" first="<<*first_unique<<" current="<<*current<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
StateSetList& statesetlist = _attributeToStateSetMap[*current];
|
|
|
|
for(StateSetList::iterator sitr=statesetlist.begin();
|
|
|
|
sitr!=statesetlist.end();
|
|
|
|
++sitr)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << " replace duplicate "<<*current<<" with "<<*first_unique<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
osg::StateSet* stateset = *sitr;
|
|
|
|
stateset->setAttribute(*first_unique);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else first_unique = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// duplicate state attributes removed.
|
|
|
|
// now need to look at duplicate state sets.
|
|
|
|
|
|
|
|
{
|
|
|
|
// create the list of stateset's.
|
|
|
|
typedef std::vector<osg::StateSet*> StateSetSortList;
|
|
|
|
StateSetSortList _statesetSortList;
|
|
|
|
for(StateSetMap::iterator ssitr=_statesets.begin();
|
|
|
|
ssitr!=_statesets.end();
|
|
|
|
++ssitr)
|
|
|
|
{
|
|
|
|
_statesetSortList.push_back(ssitr->first);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sort the StateSet's so that equal StateSet's sit along side each
|
|
|
|
// other.
|
|
|
|
std::sort(_statesetSortList.begin(),_statesetSortList.end(),LessStateSetFunctor());
|
|
|
|
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << "searching for duplicate attributes"<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
// find the duplicates.
|
|
|
|
StateSetSortList::iterator first_unique = _statesetSortList.begin();
|
|
|
|
StateSetSortList::iterator current = first_unique; ++current;
|
|
|
|
for(; current!=_statesetSortList.end();++current)
|
|
|
|
{
|
|
|
|
if (**current==**first_unique)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << " found duplicate "<<(*current)->className()<<" first="<<*first_unique<<" current="<<*current<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
ObjectSet& objSet = _statesets[*current];
|
|
|
|
for(ObjectSet::iterator sitr=objSet.begin();
|
|
|
|
sitr!=objSet.end();
|
|
|
|
++sitr)
|
|
|
|
{
|
2001-12-15 18:00:43 +08:00
|
|
|
osg::notify(osg::INFO) << " replace duplicate "<<*current<<" with "<<*first_unique<< std::endl;
|
2001-10-19 22:22:02 +08:00
|
|
|
osg::Object* obj = *sitr;
|
|
|
|
osg::Drawable* drawable = dynamic_cast<osg::Drawable*>(obj);
|
|
|
|
if (drawable)
|
|
|
|
{
|
|
|
|
drawable->setStateSet(*first_unique);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
osg::Node* node = dynamic_cast<osg::Node*>(obj);
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
node->setStateSet(*first_unique);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else first_unique = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Flatten static transforms
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Geode& geode)
|
|
|
|
{
|
|
|
|
if (!_matrixStack.empty())
|
|
|
|
{
|
|
|
|
TransformFunctor tf(_matrixStack.back());
|
|
|
|
for(int i=0;i<geode.getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
geode.getDrawable(i)->applyAttributeOperation(tf);
|
2001-11-07 20:42:08 +08:00
|
|
|
geode.getDrawable(i)->dirtyBound();
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
2001-11-07 20:42:08 +08:00
|
|
|
geode.dirtyBound();
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Billboard& billboard)
|
|
|
|
{
|
|
|
|
if (!_matrixStack.empty())
|
|
|
|
{
|
|
|
|
osg::Matrix& matrix = _matrixStack.back();
|
2001-11-10 21:36:05 +08:00
|
|
|
osg::Matrix matrix_no_trans = matrix;
|
|
|
|
matrix_no_trans.setTrans(0.0f,0.0f,0.0f);
|
|
|
|
|
|
|
|
TransformFunctor tf(matrix_no_trans);
|
2001-10-19 22:22:02 +08:00
|
|
|
|
|
|
|
osg::Vec3 axis = osg::Matrix::transform3x3(tf._im,billboard.getAxis());
|
2001-11-10 21:36:05 +08:00
|
|
|
axis.normalize();
|
2001-10-19 22:22:02 +08:00
|
|
|
billboard.setAxis(axis);
|
|
|
|
|
|
|
|
for(int i=0;i<billboard.getNumDrawables();++i)
|
|
|
|
{
|
|
|
|
billboard.setPos(i,billboard.getPos(i)*matrix);
|
|
|
|
billboard.getDrawable(i)->applyAttributeOperation(tf);
|
|
|
|
}
|
2001-11-10 21:36:05 +08:00
|
|
|
|
|
|
|
billboard.dirtyBound();
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::FlattenStaticTransformsVisitor::apply(osg::LOD& lod)
|
|
|
|
{
|
|
|
|
if (!_matrixStack.empty())
|
|
|
|
{
|
2001-11-10 23:46:05 +08:00
|
|
|
osg::Matrix& matrix = _matrixStack.back();
|
|
|
|
osg::Matrix matrix_no_trans = matrix;
|
|
|
|
matrix_no_trans.setTrans(0.0f,0.0f,0.0f);
|
|
|
|
|
|
|
|
osg::Vec3 v111(1.0f,1.0f,1.0f);
|
|
|
|
osg::Vec3 new_v111 = v111*matrix_no_trans;
|
|
|
|
float ratio = new_v111.length()/v111.length();
|
|
|
|
|
|
|
|
// move center point.
|
|
|
|
lod.setCenter(lod.getCenter()*matrix);
|
|
|
|
|
|
|
|
// adjust ranges to new scale.
|
|
|
|
for(int i=0;i<lod.getNumRanges();++i)
|
|
|
|
{
|
|
|
|
lod.setRange(i,lod.getRange(i)*ratio);
|
|
|
|
}
|
|
|
|
|
2001-11-10 21:36:05 +08:00
|
|
|
lod.dirtyBound();
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
traverse(lod);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::FlattenStaticTransformsVisitor::apply(osg::Transform& transform)
|
|
|
|
{
|
2001-12-17 23:05:06 +08:00
|
|
|
if (_ignoreDynamicTransforms && transform.getType()==osg::Transform::DYNAMIC)
|
2001-10-19 22:22:02 +08:00
|
|
|
{
|
2001-12-17 23:05:06 +08:00
|
|
|
// simple traverse the children as if this Transform didn't exist.
|
|
|
|
traverse(transform);
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
else
|
2001-12-17 23:05:06 +08:00
|
|
|
{
|
|
|
|
if (_matrixStack.empty())
|
|
|
|
{
|
|
|
|
_matrixStack.push_back(transform.getMatrix());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_matrixStack.push_back(transform.getMatrix()*_matrixStack.back());
|
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2001-12-17 23:05:06 +08:00
|
|
|
_transformList.insert(&transform);
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2001-12-17 23:05:06 +08:00
|
|
|
// simple traverse the children as if this Transform didn't exist.
|
|
|
|
traverse(transform);
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2001-12-17 23:05:06 +08:00
|
|
|
// reset the matrix to identity.
|
|
|
|
transform.setMatrix(osg::Matrix::identity());
|
2001-10-19 22:22:02 +08:00
|
|
|
|
2001-12-17 23:05:06 +08:00
|
|
|
_matrixStack.pop_back();
|
|
|
|
}
|
2001-10-19 22:22:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Optimizer::FlattenStaticTransformsVisitor::removeTransforms()
|
|
|
|
{
|
|
|
|
for(TransformList::iterator itr=_transformList.begin();
|
|
|
|
itr!=_transformList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Transform> transform = *itr;
|
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for(i=0;i<transform->getNumChildren();++i)
|
|
|
|
{
|
|
|
|
for(int j=0;j<transform->getNumParents();++j)
|
|
|
|
{
|
|
|
|
group->addChild(transform->getChild(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=transform->getNumParents()-1;i>=0;--i)
|
|
|
|
{
|
|
|
|
transform->getParent(i)->replaceChild(transform.get(),group.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
_transformList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RemoveRedundentNodes.
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void Optimizer::RemoveRedundentNodesVisitor::apply(osg::Group& group)
|
|
|
|
{
|
|
|
|
if (typeid(group)==typeid(osg::Group))
|
|
|
|
{
|
|
|
|
if (group.getNumParents()>0 && group.getNumChildren()<=1)
|
|
|
|
{
|
|
|
|
_redundentNodeList.insert(&group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traverse(group);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Optimizer::RemoveRedundentNodesVisitor::removeRedundentNodes()
|
|
|
|
{
|
|
|
|
for(NodeList::iterator itr=_redundentNodeList.begin();
|
|
|
|
itr!=_redundentNodeList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::ref_ptr<osg::Group> group = dynamic_cast<osg::Group*>(*itr);
|
|
|
|
if (group.valid())
|
|
|
|
{
|
|
|
|
|
|
|
|
for(int j=group->getNumParents()-1;j>=0;--j)
|
|
|
|
{
|
|
|
|
for(int i=0;i<group->getNumChildren();++i)
|
|
|
|
{
|
|
|
|
group->getParent(j)->addChild(group->getChild(i));
|
|
|
|
}
|
|
|
|
group->getParent(j)->removeChild(group.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_redundentNodeList.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// combine LOD's.
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
void Optimizer::CombineLODsVisitor::apply(osg::LOD& lod)
|
|
|
|
{
|
|
|
|
for(int i=0;i<lod.getNumParents();++i)
|
|
|
|
{
|
|
|
|
if (typeid(*lod.getParent(i))==typeid(osg::Group))
|
|
|
|
{
|
|
|
|
_groupList.insert(lod.getParent(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
traverse(lod);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Optimizer::CombineLODsVisitor::combineLODs()
|
|
|
|
{
|
|
|
|
for(GroupList::iterator itr=_groupList.begin();
|
|
|
|
itr!=_groupList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
osg::Group* group = *itr;
|
|
|
|
|
|
|
|
typedef std::set<osg::LOD*> LODSet;
|
|
|
|
|
|
|
|
LODSet lodChildren;
|
|
|
|
|
|
|
|
for(int i=0;i<group->getNumChildren();++i)
|
|
|
|
{
|
|
|
|
osg::Node* child = group->getChild(i);
|
|
|
|
osg::LOD* lod = dynamic_cast<osg::LOD*>(child);
|
|
|
|
if (lod)
|
|
|
|
{
|
|
|
|
if (lod->getNumRanges()-1==lod->getNumChildren())
|
|
|
|
{
|
|
|
|
lodChildren.insert(lod);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// wonky LOD, numRanges should = numChildren+1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lodChildren.size()>=2)
|
|
|
|
{
|
|
|
|
osg::BoundingBox bb;
|
|
|
|
LODSet::iterator lod_itr;
|
|
|
|
for(lod_itr=lodChildren.begin();
|
|
|
|
lod_itr!=lodChildren.end();
|
|
|
|
++lod_itr)
|
|
|
|
{
|
|
|
|
|
|
|
|
bb.expandBy((*lod_itr)->getCenter());
|
|
|
|
}
|
|
|
|
if (bb.radius()<1e-2)
|
|
|
|
{
|
|
|
|
typedef std::pair<float,float> RangePair;
|
|
|
|
typedef std::multimap<RangePair,osg::Node*> RangeMap;
|
|
|
|
RangeMap rangeMap;
|
|
|
|
float maxRange = 0.0f;
|
|
|
|
for(lod_itr=lodChildren.begin();
|
|
|
|
lod_itr!=lodChildren.end();
|
|
|
|
++lod_itr)
|
|
|
|
{
|
|
|
|
|
|
|
|
osg::LOD* lod = *lod_itr;
|
|
|
|
for(int i=0;i<lod->getNumRanges()-1;++i)
|
|
|
|
{
|
|
|
|
if (maxRange<lod->getRange(i+1)) maxRange = lod->getRange(i+1);
|
|
|
|
rangeMap.insert(RangeMap::value_type(RangePair(lod->getRange(i),lod->getRange(i+1)),lod->getChild(i)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new LOD containing all other LOD's children.
|
|
|
|
osg::LOD* newLOD = new osg::LOD;
|
|
|
|
newLOD->setName("newLOD");
|
|
|
|
newLOD->setCenter(bb.center());
|
|
|
|
|
|
|
|
int i=0;
|
|
|
|
for(RangeMap::iterator c_itr=rangeMap.begin();
|
|
|
|
c_itr!=rangeMap.end();
|
|
|
|
++c_itr,++i)
|
|
|
|
{
|
|
|
|
newLOD->setRange(i,c_itr->first.first);
|
|
|
|
newLOD->addChild(c_itr->second);
|
|
|
|
}
|
|
|
|
newLOD->setRange(i,maxRange);
|
|
|
|
|
|
|
|
// add LOD into parent.
|
|
|
|
group->addChild(newLOD);
|
|
|
|
|
|
|
|
// remove all the old LOD's from group.
|
|
|
|
for(lod_itr=lodChildren.begin();
|
|
|
|
lod_itr!=lodChildren.end();
|
|
|
|
++lod_itr)
|
|
|
|
{
|
|
|
|
group->removeChild(*lod_itr);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_groupList.clear();
|
|
|
|
}
|
|
|
|
|