OpenSceneGraph/src/osg/LOD.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

#include <osg/LOD>
2001-01-11 00:32:10 +08:00
#include <algorithm>
using namespace osg;
LOD::LOD(const LOD& lod,const CopyOp& copyop):
Group(lod,copyop),
_rangeList(lod._rangeList),
_rangeList2(lod._rangeList2),
_center(lod._center)
{
}
2001-01-11 00:32:10 +08:00
void LOD::traverse(NodeVisitor& nv)
{
switch(nv.getTraversalMode())
2001-01-11 00:32:10 +08:00
{
case(NodeVisitor::TRAVERSE_ALL_CHILDREN):
std::for_each(_children.begin(),_children.end(),NodeAcceptOp(nv));
break;
case(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
if (_children.size()!=0) _children.front()->accept(nv);
break;
default:
break;
2001-01-11 00:32:10 +08:00
}
}
void LOD::setRange(unsigned int index, float range)
2001-01-11 00:32:10 +08:00
{
if (index<_rangeList.size()) _rangeList[index] = range;
else while (index>=_rangeList.size()) _rangeList.push_back(range);
if (index<_rangeList2.size()) _rangeList2[index] = range*range;
else while (index>=_rangeList2.size()) _rangeList2.push_back(range*range);
}
int LOD::evaluateLODChild(const Vec3& eye_local, float bias) const
2001-01-11 00:32:10 +08:00
{
// For cache coherency, use _rangeList2 exclusively
if (_rangeList2.empty()) return -1;
2001-01-11 00:32:10 +08:00
// Test distance-squared against the stored array of squared ranges
2001-01-11 00:32:10 +08:00
float LODRange = (eye_local-_center).length2()*bias;
if (LODRange<_rangeList2[0]) return -1;
unsigned int end_marker = _rangeList2.size()-1;
if (end_marker>_children.size()) end_marker=_children.size();
for(unsigned int i=0;i<end_marker;++i)
2001-01-11 00:32:10 +08:00
{
if (LODRange<_rangeList2[i+1])
{
2001-01-11 00:32:10 +08:00
return i;
}
}
return -1;
}