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

Revision 10810, 3.2 kB (checked in by robert, 5 years ago)

From Wang Rui, "I've made a small fix to osgViewer/Scene.cpp, which is already attached. I would like to explain what I've done and why.

In Scene::updateSceneGraph(), change:

if (getSceneData())
{

updateVisitor.setImageRequestHandler(getImagePager());
getSceneData()->accept(updateVisitor);

}
if (getDatabasePager())
{

// synchronize changes required by the DatabasePager? thread to the scene graph
getDatabasePager()->updateSceneGraph((*updateVisitor.getFrameStamp()));

}

to

if (getDatabasePager())
{

// synchronize changes required by the DatabasePager? thread to the scene graph
getDatabasePager()->updateSceneGraph((*updateVisitor.getFrameStamp()));

}
if (getSceneData())
{

updateVisitor.setImageRequestHandler(getImagePager());
getSceneData()->accept(updateVisitor);

}

That is, just swap the positions of two 'if () {...}' segments.

While working on a paged terrain, I need to collect every newly allocated PagedLODs and make them temporarily unrenderable in the next frame, which are all done in a update callback. But I found that these PagedLODs will always be shown before collecting them, because of the unsuitable sequence in Scene::updateSceneGraph(). DatabasePager? is synchronized AFTER the user updating traversal, that is, user cannot IMMEDIATELY find out changes made by DatabasePager?.

"

  • 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;
20static OpenThreads::Mutex s_sceneCacheMutex;
21static SceneCache s_sceneCache;
22
23Scene::Scene():
24    osg::Referenced(true)
25{
26    setDatabasePager(osgDB::DatabasePager::create());
27    setImagePager(new osgDB::ImagePager);
28   
29    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
30    s_sceneCache.push_back(this);
31}
32
33Scene::~Scene()
34{
35    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
36    for(SceneCache::iterator itr = s_sceneCache.begin();
37        itr != s_sceneCache.end();
38        ++itr)
39    {
40        Scene* scene = itr->get();
41        if (scene==this)
42        {
43            s_sceneCache.erase(itr);
44            break;
45        }
46    }
47}
48
49void Scene::setSceneData(osg::Node* node)
50{
51    _sceneData = node;
52   
53    if (_databasePager.valid())
54    {   
55        // register any PagedLOD that need to be tracked in the scene graph
56        if (node) _databasePager->registerPagedLODs(node);
57    }
58}
59
60osg::Node* Scene::getSceneData()
61{
62    return _sceneData.get();
63}
64
65const osg::Node* Scene::getSceneData() const
66{
67    return _sceneData.get();
68}
69
70void Scene::setDatabasePager(osgDB::DatabasePager* dp)
71{
72    _databasePager = dp;
73}
74
75void Scene::setImagePager(osgDB::ImagePager* ip)
76{
77    _imagePager = ip;
78}
79
80void Scene::updateSceneGraph(osg::NodeVisitor& updateVisitor)
81{
82    if (!_sceneData) return;
83
84    if (getDatabasePager())
85    {
86        // synchronize changes required by the DatabasePager thread to the scene graph
87        getDatabasePager()->updateSceneGraph((*updateVisitor.getFrameStamp()));
88    }
89
90    if (getSceneData())
91    {
92        updateVisitor.setImageRequestHandler(getImagePager());
93        getSceneData()->accept(updateVisitor);
94    }
95
96    if (getImagePager())
97    {
98        // synchronize changes required by the DatabasePager thread to the scene graph
99        getImagePager()->updateSceneGraph(*(updateVisitor.getFrameStamp()));
100    }
101
102}
103
104
105Scene* Scene::getScene(osg::Node* node)
106{
107    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
108    for(SceneCache::iterator itr = s_sceneCache.begin();
109        itr != s_sceneCache.end();
110        ++itr)
111    {
112        Scene* scene = itr->get();
113        if (scene && scene->getSceneData()==node) return scene;
114    }
115    return 0;
116}
117
118Scene* Scene::getOrCreateScene(osg::Node* node)
119{
120    if (!node) return 0;
121
122    osgViewer::Scene* scene = getScene(node);
123    if (!scene)
124    {
125        scene = new Scene;
126        scene->setSceneData(node);
127    }
128
129    return scene;
130}
Note: See TracBrowser for help on using the browser.