From 9e2567cb88eeb55328473025f81545e6d99da4a7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Sun, 11 Oct 2009 06:05:19 +0000 Subject: [PATCH] Made the use of the new projection and modelview matrix uniforms optional --- .../osgvertexattributes.cpp | 15 +++++++++++++++ include/osg/State | 18 ++++++++++++++---- src/osg/State.cpp | 1 + src/osgUtil/RenderLeaf.cpp | 4 ++-- src/osgWrappers/osg/State.cpp | 13 +++++++++++++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/examples/osgvertexattributes/osgvertexattributes.cpp b/examples/osgvertexattributes/osgvertexattributes.cpp index c8b5407ad..a0bb3ce4e 100644 --- a/examples/osgvertexattributes/osgvertexattributes.cpp +++ b/examples/osgvertexattributes/osgvertexattributes.cpp @@ -20,6 +20,7 @@ #include #include #include +#include class ConvertToVertexAttibArrays : public osg::NodeVisitor { @@ -329,5 +330,19 @@ int main(int argc, char *argv[]) // add a viewport to the viewer and attach the scene graph. viewer.setSceneData(loadedModel.get()); + viewer.setCameraManipulator(new osgGA::TrackballManipulator()); + + viewer.realize(); + + // switch on the uniforms that track the modelview and projection matrices + osgViewer::Viewer::Windows windows; + viewer.getWindows(windows); + for(osgViewer::Viewer::Windows::iterator itr = windows.begin(); + itr != windows.end(); + ++itr) + { + (*itr)->getState()->setUseModelViewAndProjectionUniforms(true); + } + return viewer.run(); } diff --git a/include/osg/State b/include/osg/State index 3baaa917a..d0c562e5a 100644 --- a/include/osg/State +++ b/include/osg/State @@ -175,8 +175,11 @@ class OSG_EXPORT State : public Referenced, public Observer _projection=_identity; } - if (_projectionMatrixUniform.valid()) _projectionMatrixUniform->set(*_projection); - if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); + if (_useModelViewAndProjectionUniforms) + { + if (_projectionMatrixUniform.valid()) _projectionMatrixUniform->set(*_projection); + if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); + } glMatrixMode( GL_PROJECTION ); glLoadMatrix(_projection->ptr()); @@ -202,8 +205,11 @@ class OSG_EXPORT State : public Referenced, public Observer _modelView=_identity; } - if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView); - if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); + if (_useModelViewAndProjectionUniforms) + { + if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView); + if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); + } glLoadMatrix(_modelView->ptr()); } @@ -214,6 +220,9 @@ class OSG_EXPORT State : public Referenced, public Observer return *_modelView; } + void setUseModelViewAndProjectionUniforms(bool flag) { _useModelViewAndProjectionUniforms = flag; } + bool getUseModelViewAndProjectionUniforms() const { return _useModelViewAndProjectionUniforms; } + void applyModelViewAndProjectionUniformsIfRequired(); osg::Uniform* getModelViewMatrixUniform() { return _modelViewMatrixUniform.get(); } @@ -1100,6 +1109,7 @@ class OSG_EXPORT State : public Referenced, public Observer ref_ptr _projection; ref_ptr _modelView; + bool _useModelViewAndProjectionUniforms; ref_ptr _modelViewMatrixUniform; ref_ptr _projectionMatrixUniform; ref_ptr _modelViewProjectionMatrixUniform; diff --git a/src/osg/State.cpp b/src/osg/State.cpp index 700a56495..1a2acb18a 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -44,6 +44,7 @@ State::State(): _projection = _identity; _modelView = _identity; + _useModelViewAndProjectionUniforms = false; _modelViewMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewMatrix"); _projectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ProjectionMatrix"); _modelViewProjectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewProjectionMatrix"); diff --git a/src/osgUtil/RenderLeaf.cpp b/src/osgUtil/RenderLeaf.cpp index 65d63b485..3bb2bc708 100644 --- a/src/osgUtil/RenderLeaf.cpp +++ b/src/osgUtil/RenderLeaf.cpp @@ -57,7 +57,7 @@ void RenderLeaf::render(osg::RenderInfo& renderInfo,RenderLeaf* previous) // if we are using osg::Program which requires OSG's generated uniforms to track // modelview and projection matrices then apply them now. - state.applyModelViewAndProjectionUniformsIfRequired(); + if (state.getUseModelViewAndProjectionUniforms()) state.applyModelViewAndProjectionUniformsIfRequired(); // draw the drawable _drawable->draw(renderInfo); @@ -75,7 +75,7 @@ void RenderLeaf::render(osg::RenderInfo& renderInfo,RenderLeaf* previous) // if we are using osg::Program which requires OSG's generated uniforms to track // modelview and projection matrices then apply them now. - state.applyModelViewAndProjectionUniformsIfRequired(); + if (state.getUseModelViewAndProjectionUniforms()) state.applyModelViewAndProjectionUniformsIfRequired(); // draw the drawable _drawable->draw(renderInfo); diff --git a/src/osgWrappers/osg/State.cpp b/src/osgWrappers/osg/State.cpp index 2a7a66b80..5637e29f5 100644 --- a/src/osgWrappers/osg/State.cpp +++ b/src/osgWrappers/osg/State.cpp @@ -167,6 +167,16 @@ BEGIN_OBJECT_REFLECTOR(osg::State) __C5_osg_Matrix_R1__getModelViewMatrix, "", ""); + I_Method1(void, setUseModelViewAndProjectionUniforms, IN, bool, flag, + Properties::NON_VIRTUAL, + __void__setUseModelViewAndProjectionUniforms__bool, + "", + ""); + I_Method0(bool, getUseModelViewAndProjectionUniforms, + Properties::NON_VIRTUAL, + __bool__getUseModelViewAndProjectionUniforms, + "", + ""); I_Method0(void, applyModelViewAndProjectionUniformsIfRequired, Properties::NON_VIRTUAL, __void__applyModelViewAndProjectionUniformsIfRequired, @@ -928,6 +938,9 @@ BEGIN_OBJECT_REFLECTOR(osg::State) I_SimpleProperty(unsigned int, StateSetStackSize, __unsigned_int__getStateSetStackSize, 0); + I_SimpleProperty(bool, UseModelViewAndProjectionUniforms, + __bool__getUseModelViewAndProjectionUniforms, + __void__setUseModelViewAndProjectionUniforms__bool); I_SimpleProperty(const osg::Array *, VertexPointer, 0, __void__setVertexPointer__C5_Array_P1);