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

@ -59,14 +59,14 @@ public:
if ( pagedLOD->getDatabasePath().empty() )
{
nv->getDatabaseRequestHandler()->requestNodeFile(
pagedLOD->getFileName(numChildren), pagedLOD,
pagedLOD->getFileName(numChildren), nv->getNodePath(),
1.0, nv->getFrameStamp(),
pagedLOD->getDatabaseRequest(numChildren), pagedLOD->getDatabaseOptions() );
}
else
{
nv->getDatabaseRequestHandler()->requestNodeFile(
pagedLOD->getDatabasePath()+pagedLOD->getFileName(numChildren), pagedLOD,
pagedLOD->getDatabasePath()+pagedLOD->getFileName(numChildren), nv->getNodePath(),
1.0, nv->getFrameStamp(),
pagedLOD->getDatabaseRequest(numChildren), pagedLOD->getDatabaseOptions() );
}

View File

@ -23,8 +23,15 @@
#include <string>
#include <vector>
// forward declare osgTerrrain::Terrain to enable declaration of asTerrain() method.
namespace osgTerrain {
class Terrain;
}
namespace osg {
// forcing declare classes to enable declaration of as*() methods.
class NodeVisitor;
class Group;
class Transform;
@ -114,6 +121,15 @@ class OSG_EXPORT Node : public Object
* Equivalent to dynamic_cast<const Geode*>(this).*/
virtual const Geode* asGeode() const { return 0; }
/** Convert 'this' into a Transform pointer if Node is a Terrain, otherwise return 0.
* Equivalent to dynamic_cast<Terrrain*>(this).*/
virtual osgTerrain::Terrain* asTerrain() { return 0; }
/** convert 'const this' into a const Terrain pointer if Node is a Terrain, otherwise return 0.
* Equivalent to dynamic_cast<const Terrain*>(this).*/
virtual const osgTerrain::Terrain* asTerrain() const { return 0; }
/** Visitor Pattern : calls the apply method of a NodeVisitor with this node's type.*/
virtual void accept(NodeVisitor& nv);
/** Traverse upwards : calls parents' accept method with NodeVisitor.*/

View File

@ -280,7 +280,7 @@ class OSG_EXPORT NodeVisitor : public virtual Referenced
DatabaseRequestHandler():
Referenced(true) {}
virtual void requestNodeFile(const std::string& fileName,osg::Group* group, float priority, const FrameStamp* framestamp, osg::ref_ptr<osg::Referenced>& databaseRequest, const osg::Referenced* options=0) = 0;
virtual void requestNodeFile(const std::string& fileName, osg::NodePath& nodePath, float priority, const FrameStamp* framestamp, osg::ref_ptr<osg::Referenced>& databaseRequest, const osg::Referenced* options=0) = 0;
protected:
virtual ~DatabaseRequestHandler() {}

View File

@ -70,7 +70,7 @@ class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandl
/** Add a request to load a node file to end the the database request list.*/
virtual void requestNodeFile(const std::string& fileName,osg::Group* group,
virtual void requestNodeFile(const std::string& fileName, osg::NodePath& nodePath,
float priority, const osg::FrameStamp* framestamp,
osg::ref_ptr<osg::Referenced>& databaseRequest,
const osg::Referenced* options);
@ -316,9 +316,12 @@ class OSGDB_EXPORT DatabasePager : public osg::NodeVisitor::DatabaseRequestHandl
double _timestampLastRequest;
float _priorityLastRequest;
unsigned int _numOfRequests;
osg::ObserverNodePath _observerNodePath;
osg::ref_ptr<osg::Node> _loadedModel;
osg::ref_ptr<Options> _loadOptions;
osg::observer_ptr<osg::Node> _terrain;
osg::observer_ptr<osg::Group> _group;
osg::ref_ptr<osg::Node> _loadedModel;
osg::ref_ptr<Options> _loadOptions;
osg::observer_ptr<osgUtil::IncrementalCompileOperation::CompileSet> _compileSet;

View File

@ -25,6 +25,7 @@
#include <osg/PrimitiveSet>
#include <osgDB/ReaderWriter>
#include <osgDB/StreamOperator>
#include <osgDB/Options>
#include <iostream>
#include <sstream>

View File

@ -15,6 +15,7 @@
#define OSGDB_OPTIONS 1
#include <osgDB/Callbacks>
#include <osg/ObserverNodePath>
#include <deque>
#include <list>
@ -227,7 +228,6 @@ class OSGDB_EXPORT Options : public osg::Object
/** Get the callback to use inform the DatabasePager whether a file is located on local or remote file system.*/
FileLocationCallback* getFileLocationCallback() const { return _fileLocationCallback.get(); }
/** Set the FileCache that is used to manage local storage of files downloaded from the internet.*/
void setFileCache(FileCache* fileCache) { _fileCache = fileCache; }
@ -235,6 +235,13 @@ class OSGDB_EXPORT Options : public osg::Object
FileCache* getFileCache() const { return _fileCache.get(); }
/** Set the terrain observer_ptr, use to decorate any osgTerrain subgraphs.*/
void setTerrain(osg::observer_ptr<osg::Node>& terrain) { _terrain = terrain; }
/** Get the terrain observer_ptr, use to decorate any osgTerrain subgraphs.*/
const osg::observer_ptr<osg::Node>& getTerrain() const { return _terrain; }
protected:
virtual ~Options() {}
@ -257,6 +264,8 @@ class OSGDB_EXPORT Options : public osg::Object
osg::ref_ptr<FileLocationCallback> _fileLocationCallback;
osg::ref_ptr<FileCache> _fileCache;
osg::observer_ptr<osg::Node> _terrain;
};
}

View File

@ -34,7 +34,11 @@ class OSGTERRAIN_EXPORT Terrain : public osg::CoordinateSystemNode
META_Node(osgTerrain, Terrain);
virtual void traverse(osg::NodeVisitor& nv);
virtual osgTerrain::Terrain* asTerrain() { return this; }
virtual const osgTerrain::Terrain* asTerrain() const { return this; }
/** Set the sample ratio hint that TerrainTile should use when building geometry.
* Defaults to 1.0, which means use all original sample points.*/
void setSampleRatio(float ratio);

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() );
}