Made the use of the new projection and modelview matrix uniforms optional

This commit is contained in:
Robert Osfield 2009-10-11 06:05:19 +00:00
parent f479eb174c
commit 9e2567cb88
5 changed files with 45 additions and 6 deletions

View File

@ -20,6 +20,7 @@
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
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();
}

View File

@ -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<const RefMatrix> _projection;
ref_ptr<const RefMatrix> _modelView;
bool _useModelViewAndProjectionUniforms;
ref_ptr<Uniform> _modelViewMatrixUniform;
ref_ptr<Uniform> _projectionMatrixUniform;
ref_ptr<Uniform> _modelViewProjectionMatrixUniform;

View File

@ -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");

View File

@ -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);

View File

@ -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);