Hi Robert,

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.

I don't know if there are other concerns that force us put the user
traversal before updating DatabasePager. But I think it will be easier for
users to manage the scenegraph, especially paged objects, if swap these two
processes. What's your opinion? :)

Cheers,

Wang Rui
/* -*-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>
#include <osgGA/EventVisitor>

using namespace osgViewer;

typedef std::vector< osg::observer_ptr<Scene> >  SceneCache;
static OpenThreads::Mutex s_sceneCacheMutex;
static SceneCache s_sceneCache;

Scene::Scene():
    osg::Referenced(true)
{
    setDatabasePager(osgDB::DatabasePager::create());
    setImagePager(new osgDB::ImagePager);
    
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
    s_sceneCache.push_back(this);
}

Scene::~Scene()
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
    for(SceneCache::iterator itr = s_sceneCache.begin();
        itr != s_sceneCache.end();
        ++itr)
    {
        Scene* scene = itr->get();
        if (scene==this)
        {
            s_sceneCache.erase(itr);
            break;
        }
    }
}

void Scene::setSceneData(osg::Node* node)
{
    _sceneData = node;
    
    if (_databasePager.valid())
    {    
        // register any PagedLOD that need to be tracked in the scene graph
        if (node) _databasePager->registerPagedLODs(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;
}

void Scene::setImagePager(osgDB::ImagePager* ip)
{
    _imagePager = ip;
}

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 (getSceneData())
    {
        updateVisitor.setImageRequestHandler(getImagePager());
        getSceneData()->accept(updateVisitor);
    }

    if (getImagePager())
    {
        // synchronize changes required by the DatabasePager thread to the 
scene graph
        getImagePager()->updateSceneGraph(*(updateVisitor.getFrameStamp()));
    }

}


Scene* Scene::getScene(osg::Node* node)
{
    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_sceneCacheMutex);
    for(SceneCache::iterator itr = s_sceneCache.begin();
        itr != s_sceneCache.end();
        ++itr)
    {
        Scene* scene = itr->get();
        if (scene && scene->getSceneData()==node) return scene;
    }
    return 0;
}

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;
}
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org

Reply via email to