2003-07-08 22:44:00 +08:00
|
|
|
#include <osg/PagedLOD>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
2003-07-10 22:53:07 +08:00
|
|
|
PagedLOD::PagedLOD()
|
|
|
|
{
|
|
|
|
_centerMode = USER_DEFINED_CENTER;
|
2003-12-09 20:08:27 +08:00
|
|
|
_radius = -1;
|
|
|
|
_numChildrenThatCannotBeExpired = 0;
|
2003-07-10 22:53:07 +08:00
|
|
|
}
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
PagedLOD::PagedLOD(const PagedLOD& plod,const CopyOp& copyop):
|
2003-12-09 20:08:27 +08:00
|
|
|
LOD(plod,copyop),
|
|
|
|
_radius(plod._radius),
|
|
|
|
_numChildrenThatCannotBeExpired(plod._numChildrenThatCannotBeExpired),
|
2004-01-06 04:45:28 +08:00
|
|
|
_perRangeDataList(plod._perRangeDataList)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2003-12-09 20:08:27 +08:00
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
void PagedLOD::traverse(NodeVisitor& nv)
|
|
|
|
{
|
|
|
|
|
|
|
|
double timeStamp = nv.getFrameStamp()?nv.getFrameStamp()->getReferenceTime():0.0;
|
|
|
|
bool updateTimeStamp = nv.getVisitorType()==osg::NodeVisitor::CULL_VISITOR;
|
|
|
|
|
|
|
|
switch(nv.getTraversalMode())
|
|
|
|
{
|
|
|
|
case(NodeVisitor::TRAVERSE_ALL_CHILDREN):
|
|
|
|
std::for_each(_children.begin(),_children.end(),NodeAcceptOp(nv));
|
|
|
|
break;
|
|
|
|
case(NodeVisitor::TRAVERSE_ACTIVE_CHILDREN):
|
|
|
|
{
|
|
|
|
float distance = nv.getDistanceToEyePoint(getCenter(),true);
|
2003-12-21 07:25:05 +08:00
|
|
|
|
|
|
|
int lastChildTraversed = -1;
|
|
|
|
bool needToLoadChild = false;
|
|
|
|
for(unsigned int i=0;i<_rangeList.size();++i)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
if (_rangeList[i].first<=distance && distance<_rangeList[i].second)
|
|
|
|
{
|
2003-12-21 07:25:05 +08:00
|
|
|
if (i<_children.size())
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
if (updateTimeStamp) _perRangeDataList[i]._timeStamp=timeStamp;
|
2003-07-08 22:44:00 +08:00
|
|
|
|
2003-12-21 07:25:05 +08:00
|
|
|
//std::cout<<"PagedLOD::traverse() - Selecting child "<<i<<std::endl;
|
|
|
|
_children[i]->accept(nv);
|
|
|
|
lastChildTraversed = (int)i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
needToLoadChild = true;
|
|
|
|
}
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-21 07:25:05 +08:00
|
|
|
if (needToLoadChild)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2003-12-21 07:25:05 +08:00
|
|
|
unsigned int numChildren = _children.size();
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
//std::cout<<"PagedLOD::traverse() - falling back "<<std::endl;
|
|
|
|
// select the last valid child.
|
2003-12-21 07:25:05 +08:00
|
|
|
if (numChildren>0 && ((int)numChildren-1)!=lastChildTraversed)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
//std::cout<<" to child "<<numChildren-1<<std::endl;
|
2004-01-06 04:45:28 +08:00
|
|
|
if (updateTimeStamp) _perRangeDataList[numChildren-1]._timeStamp=timeStamp;
|
2003-07-08 22:44:00 +08:00
|
|
|
_children[numChildren-1]->accept(nv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now request the loading of the next unload child.
|
2004-01-06 04:45:28 +08:00
|
|
|
if (nv.getDatabaseRequestHandler() && numChildren<_perRangeDataList.size())
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2003-10-11 03:25:14 +08:00
|
|
|
float priority = (_rangeList[numChildren].second-distance)/(_rangeList[numChildren].second-_rangeList[numChildren].first);
|
|
|
|
//std::cout<<" requesting child "<<_fileNameList[numChildren]<<" priotity = "<<priority<<std::endl;
|
2004-01-06 04:45:28 +08:00
|
|
|
nv.getDatabaseRequestHandler()->requestNodeFile(_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp());
|
2003-07-08 22:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-09 20:08:27 +08:00
|
|
|
bool PagedLOD::computeBound() const
|
|
|
|
{
|
|
|
|
if (_centerMode==USER_DEFINED_CENTER && _radius>=0.0f)
|
|
|
|
{
|
|
|
|
_bsphere._center = _userDefinedCenter;
|
|
|
|
_bsphere._radius = _radius;
|
|
|
|
_bsphere_computed = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return LOD::computeBound();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
void PagedLOD::childRemoved(unsigned int pos, unsigned int numChildrenToRemove)
|
|
|
|
{
|
|
|
|
LOD::childRemoved(pos, numChildrenToRemove);
|
|
|
|
//std::cout<<"PagedLOD::childRemoved("<<pos<<","<<numChildrenToRemove<<")"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::childInserted(unsigned int pos)
|
|
|
|
{
|
|
|
|
LOD::childInserted(pos);
|
|
|
|
//std::cout<<"PagedLOD::childInserted("<<pos<<")"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::rangeRemoved(unsigned int pos, unsigned int numChildrenToRemove)
|
|
|
|
{
|
|
|
|
LOD::rangeRemoved(pos, numChildrenToRemove);
|
|
|
|
std::cout<<"PagedLOD::rangeRemoved("<<pos<<","<<numChildrenToRemove<<")"<<std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::rangeInserted(unsigned int pos)
|
|
|
|
{
|
|
|
|
LOD::rangeInserted(pos);
|
|
|
|
std::cout<<"PagedLOD::rangeInserted("<<pos<<")"<<std::endl;
|
|
|
|
expandPerRangeDataTo(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PagedLOD::expandPerRangeDataTo(unsigned int pos)
|
|
|
|
{
|
|
|
|
if (pos>=_perRangeDataList.size()) _perRangeDataList.resize(pos+1);
|
|
|
|
}
|
|
|
|
|
2003-07-08 22:44:00 +08:00
|
|
|
bool PagedLOD::addChild( Node *child )
|
|
|
|
{
|
|
|
|
if (LOD::addChild(child))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
expandPerRangeDataTo(_children.size());
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PagedLOD::addChild(Node *child, float min, float max)
|
|
|
|
{
|
|
|
|
if (LOD::addChild(child,min,max))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
expandPerRangeDataTo(_children.size());
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
bool PagedLOD::addChild(Node *child, float min, float max,const std::string& filename, float priorityOffset, float priorityScale)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
if (LOD::addChild(child,min,max))
|
|
|
|
{
|
2004-01-06 04:45:28 +08:00
|
|
|
setFileName(_children.size()-1,filename);
|
|
|
|
setPriorityOffset(_children.size()-1,priorityOffset);
|
|
|
|
setPriorityScale(_children.size()-1,priorityScale);
|
2003-07-08 22:44:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PagedLOD::removeChild( Node *child )
|
|
|
|
{
|
|
|
|
// find the child's position.
|
|
|
|
unsigned int pos=getChildIndex(child);
|
|
|
|
if (pos==_children.size()) return false;
|
|
|
|
|
2004-01-06 04:45:28 +08:00
|
|
|
if (pos<_rangeList.size()) _rangeList.erase(_rangeList.begin()+pos);
|
|
|
|
if (pos<_perRangeDataList.size()) _perRangeDataList.erase(_perRangeDataList.begin()+pos);
|
2003-07-08 22:44:00 +08:00
|
|
|
|
|
|
|
return Group::removeChild(child);
|
|
|
|
}
|
|
|
|
|
2003-07-09 22:55:39 +08:00
|
|
|
void PagedLOD::removeExpiredChildren(double expiryTime,NodeList& removedChildren)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
2003-12-09 20:08:27 +08:00
|
|
|
for(unsigned int i=_children.size();i>_numChildrenThatCannotBeExpired;)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
--i;
|
2004-01-06 04:45:28 +08:00
|
|
|
if (!_perRangeDataList[i]._filename.empty() && _perRangeDataList[i]._timeStamp<expiryTime)
|
2003-07-08 22:44:00 +08:00
|
|
|
{
|
|
|
|
//std::cout<<"Removing child "<<_children[i].get()<<std::endl;
|
2003-07-09 22:55:39 +08:00
|
|
|
removedChildren.push_back(_children[i]);
|
2003-07-08 22:44:00 +08:00
|
|
|
Group::removeChild(_children[i].get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|