|
发表于 2009-6-25 09:37:30
|
显示全部楼层
以下为osg2.8版本的代码,向场景合并新加载的节点
void DatabasePager::addLoadedDataToSceneGraph(const osg::FrameStamp &frameStamp)
{
double timeStamp = frameStamp.getReferenceTime();
int frameNumber = frameStamp.getFrameNumber();
osg::Timer_t before = osg::Timer::instance()->tick();
RequestQueue::RequestList localFileLoadedList;
// get the data for the _dataToCompileList, leaving it empty via a std::vector<>.swap.
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_dataToMergeList->_requestMutex); //这里是与分页数据库线程的同步!
localFileLoadedList.swap(_dataToMergeList->_requestList);
}
osg::Timer_t mid = osg::Timer::instance()->tick();
// add the loaded data into the scene graph.
for(RequestQueue::RequestList::iterator itr=localFileLoadedList.begin(); // 这个循环中真正把加载的节点合并入场景,似乎没有任何同步的代码,怎样保证与渲染线程的同步呢?
itr!=localFileLoadedList.end();
++itr)
{
DatabaseRequest* databaseRequest = itr->get();
// osg::notify(osg::NOTICE)<<"Merging "<<_frameNumber-(*itr)->_frameNumberLastRequest<<std::endl;
if (osgDB::Registry::instance()->getSharedStateManager())
osgDB::Registry::instance()->getSharedStateManager()->share(databaseRequest->_loadedModel.get());
registerPagedLODs(databaseRequest->_loadedModel.get(), frameStamp.getFrameNumber());
osg::ref_ptr<osg::Group> group = databaseRequest->_groupForAddingLoadedSubgraph.get();
if (group.valid())
{
osg:agedLOD* plod = dynamic_cast<osg::PagedLOD*>(group.get());
if (plod)
{
plod->setTimeStamp(plod->getNumChildren(), timeStamp);
plod->setFrameNumber(plod->getNumChildren(), frameNumber);
plod->getDatabaseRequest(plod->getNumChildren()) = 0;
}
else
{
osg::ProxyNode* proxyNode = dynamic_cast<osg::ProxyNode*>(group.get());
if (proxyNode)
{
proxyNode->getDatabaseRequest(proxyNode->getNumChildren()) = 0;
}
}
group->addChild(databaseRequest->_loadedModel.get()); // 这里调用addChild把节点加入场景
// osg::notify(osg::NOTICE)<<"merged subgraph"<<databaseRequest->_fileName<<" after "<<databaseRequest->_numOfRequests<<" requests and time="<<(timeStamp-databaseRequest->_timestampFirstRequest)*1000.0<<std::endl;
double timeToMerge = timeStamp-databaseRequest->_timestampFirstRequest;
if (timeToMerge<_minimumTimeToMergeTile) _minimumTimeToMergeTile = timeToMerge;
if (timeToMerge>_maximumTimeToMergeTile) _maximumTimeToMergeTile = timeToMerge;
_totalTimeToMergeTiles += timeToMerge;
++_numTilesMerges;
}
// reset the loadedModel pointer
databaseRequest->_loadedModel = 0;
// osg::notify(osg::NOTICE)<<"curr = "<<timeToMerge<<" min "<<getMinimumTimeToMergeTile()*1000.0<<" max = "<<getMaximumTimeToMergeTile()*1000.0<<" average = "<<getAverageTimToMergeTiles()*1000.0<<std::endl;
}
osg::Timer_t last = osg::Timer::instance()->tick();
osg::notify(osg:EBUG_INFO)<<"Done DatabasePager::addLoadedDataToSceneGraph"<<
osg::Timer::instance()->delta_m(before,mid)<<"ms,\t"<<
osg::Timer::instance()->delta_m(mid,last)<<"ms"<<
" objects"<<localFileLoadedList.size()<<std::endl<<std::endl;
} |
|