From b1658ee6dfc986a90b705b6c10bdea2091cf5cbc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 19 Apr 2010 14:49:42 +0000 Subject: [PATCH] From Farshid Lashkari, "I noticed that osg::State::getInitialViewMatrix returns the monocular view matrix when rendering in stereo. I've made some changes to osgUtil::RenderStage & SceneView so that it will return the correct view matrix depending on which eye is currently being rendered." & "I made a small change to the previous patch so that osg::State::getInitialViewMatrix works correctly with pre/post render stages as well." --- include/osgUtil/RenderStage | 7 +++++++ src/osgUtil/CullVisitor.cpp | 3 ++- src/osgUtil/RenderStage.cpp | 2 ++ src/osgUtil/SceneView.cpp | 3 +-- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/include/osgUtil/RenderStage b/include/osgUtil/RenderStage index 6b50d46c5..f356c4814 100644 --- a/include/osgUtil/RenderStage +++ b/include/osgUtil/RenderStage @@ -87,6 +87,12 @@ class OSGUTIL_EXPORT RenderStage : public RenderBin /** Get the viewport. */ osg::Viewport* getViewport() { return _viewport.get(); } + + /** Set the initial view matrix.*/ + void setInitialViewMatrix(const osg::RefMatrix* matrix) { _initialViewMatrix = matrix; } + + /** Get the initial view matrix.*/ + const osg::RefMatrix* getInitialViewMatrix() { return _initialViewMatrix.get(); } /** Set the clear mask used in glClear(..). * Defaults to GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT. */ @@ -263,6 +269,7 @@ class OSGUTIL_EXPORT RenderStage : public RenderBin // viewport x,y,width,height. osg::ref_ptr _viewport; + osg::ref_ptr _initialViewMatrix; GLenum _drawBuffer; bool _drawBufferApplyMask; diff --git a/src/osgUtil/CullVisitor.cpp b/src/osgUtil/CullVisitor.cpp index 53650c630..a45ca1cc8 100644 --- a/src/osgUtil/CullVisitor.cpp +++ b/src/osgUtil/CullVisitor.cpp @@ -1323,7 +1323,8 @@ void CullVisitor::apply(osg::Camera& camera) osg::Viewport* viewport = camera.getViewport()!=0 ? camera.getViewport() : previous_stage->getViewport(); rtts->setViewport( viewport ); - + // set initial view matrix + rtts->setInitialViewMatrix(modelview); // set up to charge the same PositionalStateContainer is the parent previous stage. osg::Matrix inheritedMVtolocalMV; diff --git a/src/osgUtil/RenderStage.cpp b/src/osgUtil/RenderStage.cpp index 534d42679..15a3e9902 100644 --- a/src/osgUtil/RenderStage.cpp +++ b/src/osgUtil/RenderStage.cpp @@ -1096,6 +1096,8 @@ void RenderStage::draw(osg::RenderInfo& renderInfo,RenderLeaf*& previous) { if (_stageDrawnThisFrame) return; + if(_initialViewMatrix.valid()) renderInfo.getState()->setInitialViewMatrix(_initialViewMatrix.get()); + // push the stages camera so that drawing code can query it if (_camera) renderInfo.pushCamera(_camera); diff --git a/src/osgUtil/SceneView.cpp b/src/osgUtil/SceneView.cpp index d21db6f39..ce977a3a4 100644 --- a/src/osgUtil/SceneView.cpp +++ b/src/osgUtil/SceneView.cpp @@ -932,6 +932,7 @@ bool SceneView::cullStage(const osg::Matrixd& projection,const osg::Matrixd& mod // achieves a certain amount of frame cohereancy of memory allocation. rendergraph->clean(); + renderStage->setInitialViewMatrix(mv.get()); renderStage->setViewport(viewport); renderStage->setClearColor(_camera->getClearColor()); renderStage->setClearDepth(_camera->getClearDepth()); @@ -1062,8 +1063,6 @@ void SceneView::draw() // assume the the draw which is about to happen could generate GL objects that need flushing in the next frame. _requiresFlush = true; - state->setInitialViewMatrix(new osg::RefMatrix(getViewMatrix())); - RenderLeaf* previous = NULL; if (_displaySettings.valid() && _displaySettings->getStereo()) {