2006-11-27 22:52:07 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osgViewer/Scene>
|
2007-01-10 18:09:05 +08:00
|
|
|
#include <osgGA/EventVisitor>
|
2006-11-27 22:52:07 +08:00
|
|
|
|
|
|
|
using namespace osgViewer;
|
|
|
|
|
2007-08-10 18:52:35 +08:00
|
|
|
typedef std::vector< osg::observer_ptr<Scene> > SceneCache;
|
2010-11-12 17:48:21 +08:00
|
|
|
|
|
|
|
static SceneCache& getSceneCache()
|
|
|
|
{
|
|
|
|
static SceneCache s_sceneCache;
|
|
|
|
return s_sceneCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
static OpenThreads::Mutex& getSceneCacheMutex()
|
|
|
|
{
|
|
|
|
static OpenThreads::Mutex s_sceneCacheMutex;
|
|
|
|
return s_sceneCacheMutex;
|
|
|
|
}
|
2007-08-10 18:52:35 +08:00
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
Scene::Scene():
|
2007-08-10 18:52:35 +08:00
|
|
|
osg::Referenced(true)
|
2006-11-27 22:52:07 +08:00
|
|
|
{
|
2007-08-08 16:10:38 +08:00
|
|
|
setDatabasePager(osgDB::DatabasePager::create());
|
2008-07-21 17:47:39 +08:00
|
|
|
setImagePager(new osgDB::ImagePager);
|
2007-08-10 18:52:35 +08:00
|
|
|
|
2010-11-12 17:48:21 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
|
|
|
|
getSceneCache().push_back(this);
|
2006-11-27 22:52:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Scene::~Scene()
|
|
|
|
{
|
2010-11-12 17:48:21 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
|
|
|
|
for(SceneCache::iterator itr = getSceneCache().begin();
|
|
|
|
itr != getSceneCache().end();
|
2007-08-10 18:52:35 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
Scene* scene = itr->get();
|
|
|
|
if (scene==this)
|
|
|
|
{
|
2010-11-12 17:48:21 +08:00
|
|
|
getSceneCache().erase(itr);
|
2007-08-10 18:52:35 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-11-27 22:52:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::setSceneData(osg::Node* node)
|
|
|
|
{
|
|
|
|
_sceneData = node;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* Scene::getSceneData()
|
|
|
|
{
|
|
|
|
return _sceneData.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
const osg::Node* Scene::getSceneData() const
|
|
|
|
{
|
|
|
|
return _sceneData.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scene::setDatabasePager(osgDB::DatabasePager* dp)
|
|
|
|
{
|
|
|
|
_databasePager = dp;
|
|
|
|
}
|
|
|
|
|
2008-07-21 17:47:39 +08:00
|
|
|
void Scene::setImagePager(osgDB::ImagePager* ip)
|
|
|
|
{
|
|
|
|
_imagePager = ip;
|
|
|
|
}
|
|
|
|
|
2009-08-05 19:06:53 +08:00
|
|
|
void Scene::updateSceneGraph(osg::NodeVisitor& updateVisitor)
|
|
|
|
{
|
|
|
|
if (!_sceneData) return;
|
|
|
|
|
|
|
|
if (getDatabasePager())
|
|
|
|
{
|
|
|
|
// synchronize changes required by the DatabasePager thread to the scene graph
|
|
|
|
getDatabasePager()->updateSceneGraph((*updateVisitor.getFrameStamp()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getImagePager())
|
|
|
|
{
|
|
|
|
// synchronize changes required by the DatabasePager thread to the scene graph
|
|
|
|
getImagePager()->updateSceneGraph(*(updateVisitor.getFrameStamp()));
|
|
|
|
}
|
|
|
|
|
2009-11-20 22:40:58 +08:00
|
|
|
if (getSceneData())
|
|
|
|
{
|
|
|
|
updateVisitor.setImageRequestHandler(getImagePager());
|
|
|
|
getSceneData()->accept(updateVisitor);
|
|
|
|
}
|
2009-08-05 19:06:53 +08:00
|
|
|
}
|
|
|
|
|
2006-11-27 22:52:07 +08:00
|
|
|
|
2007-08-10 18:52:35 +08:00
|
|
|
Scene* Scene::getScene(osg::Node* node)
|
2006-11-27 22:52:07 +08:00
|
|
|
{
|
2010-11-12 17:48:21 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
|
|
|
|
for(SceneCache::iterator itr = getSceneCache().begin();
|
|
|
|
itr != getSceneCache().end();
|
2007-08-10 18:52:35 +08:00
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
Scene* scene = itr->get();
|
|
|
|
if (scene && scene->getSceneData()==node) return scene;
|
2006-11-27 22:52:07 +08:00
|
|
|
}
|
2007-08-10 18:52:35 +08:00
|
|
|
return 0;
|
2006-11-27 22:52:07 +08:00
|
|
|
}
|
|
|
|
|
2007-09-23 00:50:45 +08:00
|
|
|
Scene* Scene::getOrCreateScene(osg::Node* node)
|
|
|
|
{
|
|
|
|
if (!node) return 0;
|
|
|
|
|
|
|
|
osgViewer::Scene* scene = getScene(node);
|
|
|
|
if (!scene)
|
|
|
|
{
|
|
|
|
scene = new Scene;
|
|
|
|
scene->setSceneData(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
return scene;
|
|
|
|
}
|