root/OpenSceneGraph/trunk/src/osgViewer/Scene.cpp @ 13041

Revision 13041, 3.2 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osgViewer/Scene>
15#include <osgGA/EventVisitor>
16
17using namespace osgViewer;
18
19typedef std::vector< osg::observer_ptr<Scene> >  SceneCache;
20
21static SceneCache s_sceneCache;
22static SceneCache& getSceneCache()
23{
24    return s_sceneCache;
25}
26
27static OpenThreads::Mutex s_sceneCacheMutex;
28static OpenThreads::Mutex& getSceneCacheMutex()
29{
30    return s_sceneCacheMutex;
31}
32
33Scene::Scene():
34    osg::Referenced(true)
35{
36    setDatabasePager(osgDB::DatabasePager::create());
37    setImagePager(new osgDB::ImagePager);
38
39    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
40    getSceneCache().push_back(this);
41}
42
43Scene::~Scene()
44{
45    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
46    for(SceneCache::iterator itr = getSceneCache().begin();
47        itr != getSceneCache().end();
48        ++itr)
49    {
50        Scene* scene = itr->get();
51        if (scene==this)
52        {
53            getSceneCache().erase(itr);
54            break;
55        }
56    }
57}
58
59void Scene::setSceneData(osg::Node* node)
60{
61    _sceneData = node;
62}
63
64osg::Node* Scene::getSceneData()
65{
66    return _sceneData.get();
67}
68
69const osg::Node* Scene::getSceneData() const
70{
71    return _sceneData.get();
72}
73
74void Scene::setDatabasePager(osgDB::DatabasePager* dp)
75{
76    _databasePager = dp;
77}
78
79void Scene::setImagePager(osgDB::ImagePager* ip)
80{
81    _imagePager = ip;
82}
83
84void Scene::updateSceneGraph(osg::NodeVisitor& updateVisitor)
85{
86    if (!_sceneData) return;
87
88    if (getDatabasePager())
89    {
90        // synchronize changes required by the DatabasePager thread to the scene graph
91        getDatabasePager()->updateSceneGraph((*updateVisitor.getFrameStamp()));
92    }
93
94    if (getImagePager())
95    {
96        // synchronize changes required by the DatabasePager thread to the scene graph
97        getImagePager()->updateSceneGraph(*(updateVisitor.getFrameStamp()));
98    }
99
100    if (getSceneData())
101    {
102        updateVisitor.setImageRequestHandler(getImagePager());
103        getSceneData()->accept(updateVisitor);
104    }
105}
106
107
108Scene* Scene::getScene(osg::Node* node)
109{
110    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getSceneCacheMutex());
111    for(SceneCache::iterator itr = getSceneCache().begin();
112        itr != getSceneCache().end();
113        ++itr)
114    {
115        Scene* scene = itr->get();
116        if (scene && scene->getSceneData()==node) return scene;
117    }
118    return 0;
119}
120
121Scene* Scene::getOrCreateScene(osg::Node* node)
122{
123    if (!node) return 0;
124
125    osgViewer::Scene* scene = getScene(node);
126    if (!scene)
127    {
128        scene = new Scene;
129        scene->setSceneData(node);
130    }
131
132    return scene;
133}
Note: See TracBrowser for help on using the browser.