Refactored the way that the DatabasePager passes the Terrain decorator node onto the TerrainTile.

The DatabasePager now passes the Terrain pointer into the ReaderWriter's via osgDB::Options object,
rather than pushing a NodePath containing the Terrain onto NodeVisitor.  This
change means that the DatabasePager nolonger needs to observer the whole NodePath and
will be lighter and quicker for it.

The change also means that ReadFileCallback can now run custom NodeVisitor's on the scene graph without
having to worry about TerrainTile's constructing scene graphs prior to the Terrain being assigned.

Also changed is the NodeVisitor::DatabaseRequestHandler which now requires a NodePath to the node that you wish
to add to rather than just the pointer to the node you wish to add to.  This is more robust when handling scenes
with multiple parental paths, whereas previously errors could have occurred due to the default of picking the first
available parental path.  This change means that subclasses of DatabasePager will need to be updated to use this new
function entry point.
This commit is contained in:
Robert Osfield
2011-01-12 19:29:24 +00:00
parent 762f8d5360
commit f61a6aa4e7
15 changed files with 119 additions and 48 deletions

View File

@@ -218,12 +218,12 @@ void PagedLOD::traverse(NodeVisitor& nv)
if (_databasePath.empty())
{
nv.getDatabaseRequestHandler()->requestNodeFile(_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp(), _perRangeDataList[numChildren]._databaseRequest, _databaseOptions.get());
nv.getDatabaseRequestHandler()->requestNodeFile(_perRangeDataList[numChildren]._filename,nv.getNodePath(),priority,nv.getFrameStamp(), _perRangeDataList[numChildren]._databaseRequest, _databaseOptions.get());
}
else
{
// prepend the databasePath to the child's filename.
nv.getDatabaseRequestHandler()->requestNodeFile(_databasePath+_perRangeDataList[numChildren]._filename,this,priority,nv.getFrameStamp(), _perRangeDataList[numChildren]._databaseRequest, _databaseOptions.get());
nv.getDatabaseRequestHandler()->requestNodeFile(_databasePath+_perRangeDataList[numChildren]._filename,nv.getNodePath(),priority,nv.getFrameStamp(), _perRangeDataList[numChildren]._databaseRequest, _databaseOptions.get());
}
}

View File

@@ -63,7 +63,7 @@ void ProxyNode::traverse(NodeVisitor& nv)
{
for(unsigned int i=_children.size(); i<_filenameList.size(); ++i)
{
nv.getDatabaseRequestHandler()->requestNodeFile(_databasePath+_filenameList[i].first, this, 1.0f, nv.getFrameStamp(), _filenameList[i].second, _databaseOptions.get());
nv.getDatabaseRequestHandler()->requestNodeFile(_databasePath+_filenameList[i].first, nv.getNodePath(), 1.0f, nv.getFrameStamp(), _filenameList[i].second, _databaseOptions.get());
}
}
else

View File

@@ -799,7 +799,15 @@ void DatabasePager::DatabaseThread::run()
{
if (dr_loadOptions->getFileCache()) fileCache = dr_loadOptions->getFileCache();
if (dr_loadOptions->getFileLocationCallback()) fileLocationCallback = dr_loadOptions->getFileLocationCallback();
dr_loadOptions = dr_loadOptions->cloneOptions();
}
else
{
dr_loadOptions = new osgDB::Options;
}
dr_loadOptions->setTerrain(databaseRequest->_terrain);
// disable the FileCache if the fileLocationCallback tells us that it isn't required for this request.
if (fileLocationCallback.valid() && !fileLocationCallback->useFileCache()) fileCache = 0;
@@ -893,7 +901,7 @@ void DatabasePager::DatabaseThread::run()
{
fileCache->writeNode(*(loadedModel), fileName, dr_loadOptions.get());
}
osg::RefNodePath refNodePath;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> drLock(_pager->_dr_mutex);
if ((_pager->_frameNumber-databaseRequest->_frameNumberLastRequest)>1)
@@ -901,16 +909,6 @@ void DatabasePager::DatabaseThread::run()
OSG_INFO<<_name<<": Warning DatabaseRquest no longer required."<<std::endl;
loadedModel = 0;
}
else
{
// take a refNodePath to ensure that none of the nodes
// go out of scope while we are using them.
if (!databaseRequest->_observerNodePath.getRefNodePath(refNodePath))
{
OSG_INFO<<_name<<": Warning node in parental chain has been deleted, discarding load."<<std::endl;
loadedModel = 0;
}
}
}
//OSG_NOTICE<<" node read in "<<osg::Timer::instance()->delta_m(before,osg::Timer::instance()->tick())<<" ms"<<std::endl;
@@ -925,17 +923,6 @@ void DatabasePager::DatabaseThread::run()
_pager->_drawablePolicy,
_pager);
// push the soon to be parent on the nodepath of the NodeVisitor so that
// during traversal one can test for where it'll be in the overall scene graph
// This is the benefit for osgTerrain::TerrainTile, so
// that it will find its Terrain node on its first traversal.
for(osg::RefNodePath::iterator rnp_itr = refNodePath.begin();
rnp_itr != refNodePath.end();
++rnp_itr)
{
frov.pushOntoNodePath(rnp_itr->get());
}
loadedModel->accept(frov);
bool loadedObjectsNeedToBeCompiled = (_pager->_doPreCompile && frov.requiresCompilation() && _pager->_incrementalCompileOperation.valid());
@@ -1362,7 +1349,7 @@ bool DatabasePager::getRequestsInProgress() const
}
void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* group,
void DatabasePager::requestNodeFile(const std::string& fileName, osg::NodePath& nodePath,
float priority, const osg::FrameStamp* framestamp,
osg::ref_ptr<osg::Referenced>& databaseRequestRef,
const osg::Referenced* options)
@@ -1381,7 +1368,28 @@ void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* grou
if (!_acceptNewRequests) return;
if (nodePath.empty())
{
OSG_NOTICE<<"Warning: DatabasePager::requestNodeFile(..) passed empty NodePath, so nowhere to attach new subgraph to."<<std::endl;
return;
}
osg::Group* group = nodePath.back()->asGroup();
if (!group)
{
OSG_NOTICE<<"Warning: DatabasePager::requestNodeFile(..) passed NodePath without group as last node in path, so nowhere to attach new subgraph to."<<std::endl;
return;
}
osg::Node* terrain = 0;
for(osg::NodePath::reverse_iterator itr = nodePath.rbegin();
itr != nodePath.rend();
++itr)
{
if ((*itr)->asTerrain()) terrain = *itr;
}
double timestamp = framestamp?framestamp->getReferenceTime():0.0;
unsigned int frameNumber = framestamp?framestamp->getFrameNumber():static_cast<unsigned int>(_frameNumber);
@@ -1410,7 +1418,7 @@ void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* grou
bool requeue = false;
if (databaseRequest)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> drLock(_dr_mutex);
OpenThreads::ScopedLock<OpenThreads::Mutex> drLock(_dr_mutex);
if (!(databaseRequest->valid()))
{
OSG_INFO<<"DatabaseRequest has been previously invalidated whilst still attached to scene graph."<<std::endl;
@@ -1436,7 +1444,8 @@ void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* grou
databaseRequest->_frameNumberLastRequest = frameNumber;
databaseRequest->_timestampLastRequest = timestamp;
databaseRequest->_priorityLastRequest = priority;
databaseRequest->_observerNodePath.setNodePathTo(group);
databaseRequest->_group = group;
databaseRequest->_terrain = terrain;
databaseRequest->_loadOptions = loadOptions;
requeue = true;
}
@@ -1467,7 +1476,8 @@ void DatabasePager::requestNodeFile(const std::string& fileName,osg::Group* grou
databaseRequest->_frameNumberLastRequest = frameNumber;
databaseRequest->_timestampLastRequest = timestamp;
databaseRequest->_priorityLastRequest = priority;
databaseRequest->_observerNodePath.setNodePathTo(group);
databaseRequest->_group = group;
databaseRequest->_terrain = terrain;
databaseRequest->_loadOptions = loadOptions;
_fileRequestQueue->addNoLock(databaseRequest.get());
@@ -1608,19 +1618,17 @@ void DatabasePager::addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp)
++itr)
{
DatabaseRequest* databaseRequest = itr->get();
// No need to take _dr_mutex. The pager threads are done with
// the request; the cull traversal -- which might redispatch
// the request -- can't run at the sametime as this update traversal.
osg::RefNodePath refNodePath;
if (databaseRequest->_observerNodePath.getRefNodePath(refNodePath))
osg::ref_ptr<osg::Group> group;
if (databaseRequest->_group.lock(group))
{
// OSG_NOTICE<<"Merging "<<_frameNumber-(*itr)->_frameNumberLastRequest<<std::endl;
osg::Group* group = static_cast<osg::Group*>(refNodePath.back().get());
if (osgDB::Registry::instance()->getSharedStateManager())
osgDB::Registry::instance()->getSharedStateManager()->share(databaseRequest->_loadedModel.get());
osg::PagedLOD* plod = dynamic_cast<osg::PagedLOD*>(group);
osg::PagedLOD* plod = dynamic_cast<osg::PagedLOD*>(group.get());
if (plod)
{
plod->setTimeStamp(plod->getNumChildren(), timeStamp);
@@ -1629,7 +1637,7 @@ void DatabasePager::addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp)
}
else
{
osg::ProxyNode* proxyNode = dynamic_cast<osg::ProxyNode*>(group);
osg::ProxyNode* proxyNode = dynamic_cast<osg::ProxyNode*>(group.get());
if (proxyNode)
{
proxyNode->getDatabaseRequest(proxyNode->getNumChildren()) = 0;

View File

@@ -29,7 +29,8 @@ Options::Options(const Options& options,const osg::CopyOp& copyop):
_readFileCallback(options._readFileCallback),
_writeFileCallback(options._writeFileCallback),
_fileLocationCallback(options._fileLocationCallback),
_fileCache(options._fileCache) {}
_fileCache(options._fileCache),
_terrain(options._terrain) {}
void Options::parsePluginStringData(const std::string& str, char separator1, char separator2)
{

View File

@@ -16,7 +16,9 @@
#include "Group.h"
#include "Layer.h"
#include <osgDB/Options>
#include <osgTerrain/GeometryTechnique>
#include <osgTerrain/Terrain>
using namespace ive;
@@ -132,6 +134,15 @@ void TerrainTile::read(DataInputStream* in)
setTerrainTechnique(readTerrainTechnique(in));
if (in->getOptions())
{
osg::ref_ptr<osg::Node> node;
if (in->getOptions()->getTerrain().lock(node))
{
setTerrain(node->asTerrain());
}
}
if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(this, in->getOptions());
}

View File

@@ -105,7 +105,7 @@ void TXPPagedLOD::traverse(osg::NodeVisitor& nv)
//std::cout<<" requesting child "<<_fileNameList[numChildren]<<" priotity = "<<priority<<std::endl;
nv.getDatabaseRequestHandler()->requestNodeFile(_perRangeDataList[numChildren]._filename,
this,
nv.getNodePath(),
priority,
nv.getFrameStamp(),
_perRangeDataList[numChildren]._databaseRequest);

View File

@@ -188,6 +188,14 @@ bool TerrainTile_readLocalData(osg::Object& obj, osgDB::Input &fr)
itrAdvanced = true;
}
if (fr.getOptions())
{
osg::ref_ptr<osg::Node> node;
if (fr.getOptions()->getTerrain().lock(node))
{
terrainTile.setTerrain(node->asTerrain());
}
}
if (osgTerrain::TerrainTile::getTileLoadedCallback().valid())
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded(&terrainTile, fr.getOptions());

View File

@@ -65,6 +65,16 @@ struct TerrainTileFinishedObjectReadCallback : public osgDB::FinishedObjectReadC
virtual void objectRead(osgDB::InputStream& is, osg::Object& obj)
{
osgTerrain::TerrainTile& tile = static_cast<osgTerrain::TerrainTile&>(obj);
if (is.getOptions())
{
osg::ref_ptr<osg::Node> node;
if (is.getOptions()->getTerrain().lock(node))
{
tile.setTerrain(node->asTerrain());
}
}
if ( osgTerrain::TerrainTile::getTileLoadedCallback().valid() )
osgTerrain::TerrainTile::getTileLoadedCallback()->loaded( &tile, is.getOptions() );
}