diff --git a/include/osgViewer/Renderer b/include/osgViewer/Renderer index d04e9ab63..23e9bae21 100644 --- a/include/osgViewer/Renderer +++ b/include/osgViewer/Renderer @@ -66,10 +66,16 @@ class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQu void setGraphicsThreadDoesCull(bool flag); bool getGraphicsThreadDoesCull() const { return _graphicsThreadDoesCull; } + virtual void cull(); virtual void draw(); virtual void cull_draw(); + virtual void compile(); + + void setCompileOnNextDraw(bool flag) { _compileOnNextDraw = flag; } + bool getCompileOnNextDraw() const { return _compileOnNextDraw; } + virtual void operator () (osg::Object* object); virtual void operator () (osg::GraphicsContext* context); @@ -136,6 +142,7 @@ class OSGVIEWER_EXPORT Renderer : public osg::GraphicsOperation, public OpenGLQu bool _done; bool _graphicsThreadDoesCull; + bool _compileOnNextDraw; osg::ref_ptr _sceneView[2]; diff --git a/src/osgViewer/Renderer.cpp b/src/osgViewer/Renderer.cpp index 66636c2b5..be5113870 100644 --- a/src/osgViewer/Renderer.cpp +++ b/src/osgViewer/Renderer.cpp @@ -167,7 +167,8 @@ Renderer::Renderer(osg::Camera* camera): _conservativeTimeRatio(0.5), _camera(camera), _done(false), - _graphicsThreadDoesCull(true) + _graphicsThreadDoesCull(true), + _compileOnNextDraw(true) { DEBUG_MESSAGE<<"Render::Render() "<getStartTick(); } +void Renderer::compile() +{ + DEBUG_MESSAGE<<"Renderer::compile()"<getSceneData()) + { + osgUtil::GLObjectsVisitor glov; + glov.setState(sceneView->getState()); + sceneView->getSceneData()->accept(glov); + } +} void Renderer::cull() { @@ -322,8 +339,13 @@ void Renderer::draw() osg::GraphicsContext* compileContext = sceneView ? osg::GraphicsContext::getCompileContext(sceneView->getState()->getContextID()) : 0; osg::GraphicsThread* compileThread = compileContext ? compileContext->getGraphicsThread() : 0; - if (sceneView || _done) + if (sceneView && !_done) { + if (_compileOnNextDraw) + { + compile(); + } + osgViewer::View* view = dynamic_cast(_camera->getView()); osgDB::DatabasePager* databasePager = view ? view->getDatabasePager() : 0; @@ -431,6 +453,11 @@ void Renderer::cull_draw() osgUtil::SceneView* sceneView = _sceneView[0].get(); if (!sceneView || _done) return; + if (_compileOnNextDraw) + { + compile(); + } + updateSceneView(sceneView); osgViewer::View* view = dynamic_cast(_camera->getView()); diff --git a/src/osgViewer/View.cpp b/src/osgViewer/View.cpp index 687c60383..02c74387c 100644 --- a/src/osgViewer/View.cpp +++ b/src/osgViewer/View.cpp @@ -1634,6 +1634,10 @@ void View::assignSceneDataToCameras() { _camera->removeChildren(0,_camera->getNumChildren()); if (sceneData) _camera->addChild(sceneData); + + Renderer* renderer = dynamic_cast(_camera->getRenderer()); + if (renderer) renderer->setCompileOnNextDraw(true); + } for(unsigned i=0; iremoveChildren(0,slave._camera->getNumChildren()); if (sceneData) slave._camera->addChild(sceneData); + + Renderer* renderer = dynamic_cast(slave._camera->getRenderer()); + if (renderer) renderer->setCompileOnNextDraw(true); } } } diff --git a/src/osgViewer/ViewerBase.cpp b/src/osgViewer/ViewerBase.cpp index a5bfbfe07..0d7a19f3c 100644 --- a/src/osgViewer/ViewerBase.cpp +++ b/src/osgViewer/ViewerBase.cpp @@ -26,8 +26,6 @@ #include #include -#include -#include static osg::ApplicationUsageProxy ViewerBase_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_CONFIG_FILE ","Specify a viewer configuration file to load by default."); static osg::ApplicationUsageProxy ViewerBase_e1(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_THREADING ","Set the threading model using by Viewer, can be SingleThreaded, CullDrawThreadPerContext, DrawThreadPerContext or CullThreadPerCameraDrawThreadPerContext."); @@ -281,7 +279,6 @@ void ViewerBase::startThreading() // using multi-threading so make sure that new objects are allocated with thread safe ref/unref osg::Referenced::setThreadSafeReferenceCounting(true); - Scenes scenes; getScenes(scenes); for(Scenes::iterator scitr = scenes.begin(); @@ -375,8 +372,6 @@ void ViewerBase::startThreading() if (affinity) gc->getGraphicsThread()->setProcessorAffinity(processNum % numProcessors); threadAffinityMap[gc->getGraphicsThread()] = processNum % numProcessors; - gc->getGraphicsThread()->add(new osgUtil::GLObjectsOperation()); - // add the startRenderingBarrier if (_threadingModel==CullDrawThreadPerContext && _startRenderingBarrier.valid()) gc->getGraphicsThread()->add(_startRenderingBarrier.get());