Introduced new uniforms for tracking the modelview and project matrices in shaders using non built-ins.

This commit is contained in:
Robert Osfield 2009-10-09 13:39:11 +00:00
parent ba8d38b885
commit f6166d1119
5 changed files with 142 additions and 103 deletions

View File

@ -51,7 +51,7 @@ class ConvertToVertexAttibArrays : public osg::NodeVisitor
program.addBindAttribLocation(alias.second, alias.first);
}
void replaceAndBind(osg::Program& program, std::string& source, const std::string& originalStr, const AttributeAlias& alias, const std::string& declarationPrefix)
void replaceAndBindAttrib(osg::Program& program, std::string& source, const std::string& originalStr, const AttributeAlias& alias, const std::string& declarationPrefix)
{
if (replace(source, originalStr, alias.second))
{
@ -60,6 +60,14 @@ class ConvertToVertexAttibArrays : public osg::NodeVisitor
}
}
void replaceBuiltInUniform(std::string& source, const std::string& originalStr, const std::string& newStr, const std::string& declarationPrefix)
{
if (replace(source, originalStr, newStr))
{
source.insert(0, declarationPrefix + newStr + std::string(";\n"));
}
}
void convertVertexShader(osg::Program& program, osg::Shader& shader)
{
std::string source = shader.getShaderSource();
@ -67,20 +75,20 @@ class ConvertToVertexAttibArrays : public osg::NodeVisitor
// replace ftransform as it only works with built-ins
replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex");
replaceAndBind(program, source, "gl_Normal", _normalAlias, "attribute vec3 ");
replaceAndBind(program, source, "gl_Vertex", _vertexAlias, "attribute vec4 ");
replaceAndBind(program, source, "gl_Color", _colorAlias, "attribute vec4 ");
replaceAndBind(program, source, "gl_SecondaryColor", _secondaryColorAlias, "attribute vec4 ");
replaceAndBind(program, source, "gl_FogCoord", _fogCoordAlias, "attribute float ");
replaceAndBindAttrib(program, source, "gl_Normal", _normalAlias, "attribute vec3 ");
replaceAndBindAttrib(program, source, "gl_Vertex", _vertexAlias, "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_Color", _colorAlias, "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_SecondaryColor", _secondaryColorAlias, "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_FogCoord", _fogCoordAlias, "attribute float ");
replaceAndBind(program, source, "gl_MultiTexCoord0", _texCoordAlias[0], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord1", _texCoordAlias[1], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord2", _texCoordAlias[2], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord3", _texCoordAlias[3], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord4", _texCoordAlias[4], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord5", _texCoordAlias[5], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord6", _texCoordAlias[6], "attribute vec4 ");
replaceAndBind(program, source, "gl_MultiTexCoord7", _texCoordAlias[7], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord0", _texCoordAlias[0], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord1", _texCoordAlias[1], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord2", _texCoordAlias[2], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord3", _texCoordAlias[3], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord4", _texCoordAlias[4], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord5", _texCoordAlias[5], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord6", _texCoordAlias[6], "attribute vec4 ");
replaceAndBindAttrib(program, source, "gl_MultiTexCoord7", _texCoordAlias[7], "attribute vec4 ");
#if 0
@ -88,6 +96,11 @@ class ConvertToVertexAttibArrays : public osg::NodeVisitor
replace(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix");
replace(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix");
replace(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix");
#else
// replace built in uniform
replaceBuiltInUniform(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix", "uniform mat4 ");
replaceBuiltInUniform(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform mat4 ");
replaceBuiltInUniform(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform mat4 ");
#endif
shader.setShaderSource(source);
}

View File

@ -166,17 +166,20 @@ class OSG_EXPORT State : public Referenced, public Observer
{
if (_projection!=matrix)
{
glMatrixMode( GL_PROJECTION );
if (matrix)
{
_projection=matrix;
glLoadMatrix(matrix->ptr());
}
else
{
_projection=_identity;
glLoadIdentity();
}
if (_projectionMatrixUniform.valid()) _projectionMatrixUniform->set(*_projection);
if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection));
glMatrixMode( GL_PROJECTION );
glLoadMatrix(_projection->ptr());
glMatrixMode( GL_MODELVIEW );
}
}
@ -193,13 +196,16 @@ class OSG_EXPORT State : public Referenced, public Observer
if (matrix)
{
_modelView=matrix;
glLoadMatrix(matrix->ptr());
}
else
{
_modelView=_identity;
glLoadIdentity();
}
if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView);
if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection));
glLoadMatrix(_modelView->ptr());
}
}
@ -208,6 +214,12 @@ class OSG_EXPORT State : public Referenced, public Observer
return *_modelView;
}
void applyModelViewAndProjectionUniformsIfRequired();
osg::Uniform* getModelViewMatrixUniform() { return _modelViewMatrixUniform.get(); }
osg::Uniform* getProjectionMatrixUniform() { return _projectionMatrixUniform.get(); }
osg::Uniform* getModelViewProjectionMatrixUniform() { return _modelViewProjectionMatrixUniform.get(); }
Polytope getViewFrustum() const;
@ -1088,6 +1100,10 @@ class OSG_EXPORT State : public Referenced, public Observer
ref_ptr<const RefMatrix> _projection;
ref_ptr<const RefMatrix> _modelView;
ref_ptr<Uniform> _modelViewMatrixUniform;
ref_ptr<Uniform> _projectionMatrixUniform;
ref_ptr<Uniform> _modelViewProjectionMatrixUniform;
Matrix _initialInverseViewMatrix;
ref_ptr<DisplaySettings> _displaySettings;

View File

@ -2114,7 +2114,16 @@ void Geometry::accept(AttributeFunctor& af)
{
AttributeFunctorArrayVisitor afav(af);
afav.applyArray(VERTICES,_vertexData.array.get());
if (_vertexData.array.valid())
{
afav.applyArray(VERTICES,_vertexData.array.get());
}
else if (_vertexAttribList.size()>0)
{
osg::notify(osg::INFO)<<"Geometry::accept(AttributeFunctor& af): Using vertex attribute instead"<<std::endl;
afav.applyArray(VERTICES,_vertexAttribList[0].array.get());
}
afav.applyArray(NORMALS,_normalData.array.get());
afav.applyArray(COLORS,_colorData.array.get());
afav.applyArray(SECONDARY_COLORS,_secondaryColorData.array.get());
@ -2178,7 +2187,16 @@ void Geometry::accept(ConstAttributeFunctor& af) const
{
ConstAttributeFunctorArrayVisitor afav(af);
afav.applyArray(VERTICES,_vertexData.array.get());
if (_vertexData.array.valid())
{
afav.applyArray(VERTICES,_vertexData.array.get());
}
else if (_vertexAttribList.size()>0)
{
osg::notify(osg::INFO)<<"Geometry::accept(ConstAttributeFunctor& af): Using vertex attribute instead"<<std::endl;
afav.applyArray(VERTICES,_vertexAttribList[0].array.get());
}
afav.applyArray(NORMALS,_normalData.array.get());
afav.applyArray(COLORS,_colorData.array.get());
afav.applyArray(SECONDARY_COLORS,_secondaryColorData.array.get());
@ -2197,32 +2215,42 @@ void Geometry::accept(ConstAttributeFunctor& af) const
void Geometry::accept(PrimitiveFunctor& functor) const
{
if (!_vertexData.array.valid() || _vertexData.array->getNumElements()==0) return;
const osg::Array* vertices = _vertexData.array.get();
const osg::IndexArray* indices = _vertexData.indices.get();
if (!_vertexData.indices.valid())
if (!vertices && _vertexAttribList.size()>0)
{
switch(_vertexData.array->getType())
osg::notify(osg::INFO)<<"Using vertex attribute instead"<<std::endl;
vertices = _vertexAttribList[0].array.get();
indices = _vertexAttribList[0].indices.get();
}
if (!vertices || vertices->getNumElements()==0) return;
if (!indices)
{
switch(vertices->getType())
{
case(Array::Vec2ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec2*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2*>(vertices->getDataPointer()));
break;
case(Array::Vec3ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec3*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3*>(vertices->getDataPointer()));
break;
case(Array::Vec4ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec4*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4*>(vertices->getDataPointer()));
break;
case(Array::Vec2dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec2d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2d*>(vertices->getDataPointer()));
break;
case(Array::Vec3dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec3d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3d*>(vertices->getDataPointer()));
break;
case(Array::Vec4dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec4d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4d*>(vertices->getDataPointer()));
break;
default:
notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<_vertexData.array->getType()<<std::endl;
notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl;
return;
}
@ -2241,33 +2269,32 @@ void Geometry::accept(PrimitiveFunctor& functor) const
const Vec2d* vec2dArray = 0;
const Vec3d* vec3dArray = 0;
const Vec4d* vec4dArray = 0;
Array::Type type = _vertexData.array->getType();
Array::Type type = vertices->getType();
switch(type)
{
case(Array::Vec2ArrayType):
vec2Array = static_cast<const Vec2*>(_vertexData.array->getDataPointer());
vec2Array = static_cast<const Vec2*>(vertices->getDataPointer());
break;
case(Array::Vec3ArrayType):
vec3Array = static_cast<const Vec3*>(_vertexData.array->getDataPointer());
vec3Array = static_cast<const Vec3*>(vertices->getDataPointer());
break;
case(Array::Vec4ArrayType):
vec4Array = static_cast<const Vec4*>(_vertexData.array->getDataPointer());
vec4Array = static_cast<const Vec4*>(vertices->getDataPointer());
break;
case(Array::Vec2dArrayType):
vec2dArray = static_cast<const Vec2d*>(_vertexData.array->getDataPointer());
vec2dArray = static_cast<const Vec2d*>(vertices->getDataPointer());
break;
case(Array::Vec3dArrayType):
vec3dArray = static_cast<const Vec3d*>(_vertexData.array->getDataPointer());
vec3dArray = static_cast<const Vec3d*>(vertices->getDataPointer());
break;
case(Array::Vec4dArrayType):
vec4dArray = static_cast<const Vec4d*>(_vertexData.array->getDataPointer());
vec4dArray = static_cast<const Vec4d*>(vertices->getDataPointer());
break;
default:
notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<_vertexData.array->getType()<<std::endl;
notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl;
return;
}
for(PrimitiveSetList::const_iterator itr=_primitives.begin();
itr!=_primitives.end();
++itr)
@ -2311,7 +2338,7 @@ void Geometry::accept(PrimitiveFunctor& functor) const
}
}
functor.end();
break;
}
@ -2354,7 +2381,7 @@ void Geometry::accept(PrimitiveFunctor& functor) const
}
++vindex;
}
functor.end();
}
@ -2487,30 +2514,40 @@ void Geometry::accept(PrimitiveFunctor& functor) const
void Geometry::accept(PrimitiveIndexFunctor& functor) const
{
if (!_vertexData.array.valid() || _vertexData.array->getNumElements()==0) return;
const osg::Array* vertices = _vertexData.array.get();
const osg::IndexArray* indices = _vertexData.indices.get();
switch(_vertexData.array->getType())
if (!vertices && _vertexAttribList.size()>0)
{
osg::notify(osg::INFO)<<"Geometry::accept(PrimitiveIndexFunctor& functor): Using vertex attribute instead"<<std::endl;
vertices = _vertexAttribList[0].array.get();
indices = _vertexAttribList[0].indices.get();
}
if (!vertices || vertices->getNumElements()==0) return;
switch(vertices->getType())
{
case(Array::Vec2ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec2*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2*>(vertices->getDataPointer()));
break;
case(Array::Vec3ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec3*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3*>(vertices->getDataPointer()));
break;
case(Array::Vec4ArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec4*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4*>(vertices->getDataPointer()));
break;
case(Array::Vec2dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec2d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2d*>(vertices->getDataPointer()));
break;
case(Array::Vec3dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec3d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3d*>(vertices->getDataPointer()));
break;
case(Array::Vec4dArrayType):
functor.setVertexArray(_vertexData.array->getNumElements(),static_cast<const Vec4d*>(_vertexData.array->getDataPointer()));
functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4d*>(vertices->getDataPointer()));
break;
default:
notify(WARN)<<"Warning: Geometry::accept(PrimitiveIndexFunctor&) cannot handle Vertex Array type"<<_vertexData.array->getType()<<std::endl;
notify(WARN)<<"Warning: Geometry::accept(PrimitiveIndexFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl;
return;
}

View File

@ -44,6 +44,10 @@ State::State():
_projection = _identity;
_modelView = _identity;
_modelViewMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewMatrix");
_projectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ProjectionMatrix");
_modelViewProjectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewProjectionMatrix");
_abortRenderingPtr = false;
_checkGLErrors = ONCE_PER_FRAME;
@ -90,6 +94,8 @@ State::State():
_maxTexturePoolSize = 0;
_maxBufferObjectPoolSize = 0;
}
State::~State()
@ -420,7 +426,7 @@ void State::apply(const StateSet* dstate)
//apply();
//popStateSet();
//return;
if (dstate)
{
@ -440,41 +446,13 @@ void State::apply(const StateSet* dstate)
{
if (unit<ds_textureModeList.size()) applyModeList(getOrCreateTextureModeMap(unit),ds_textureModeList[unit]);
else if (unit<_textureModeMapList.size()) applyModeMap(_textureModeMapList[unit]);
if (unit<ds_textureAttributeList.size()) applyAttributeList(getOrCreateTextureAttributeMap(unit),ds_textureAttributeList[unit]);
else if (unit<_textureAttributeMapList.size()) applyAttributeMap(_textureAttributeMapList[unit]);
}
}
#if 1
applyUniformList(_uniformMap,dstate->getUniformList());
#else
if (_lastAppliedProgramObject)
{
for(StateSetStack::iterator sitr=_stateStateStack.begin();
sitr!=_stateStateStack.end();
++sitr)
{
const StateSet* stateset = *sitr;
const StateSet::UniformList& uniformList = stateset->getUniformList();
for(StateSet::UniformList::const_iterator itr=uniformList.begin();
itr!=uniformList.end();
++itr)
{
_lastAppliedProgramObject->apply(*(itr->second.first));
}
}
const StateSet::UniformList& uniformList = dstate->getUniformList();
for(StateSet::UniformList::const_iterator itr=uniformList.begin();
itr!=uniformList.end();
++itr)
{
_lastAppliedProgramObject->apply(*(itr->second.first));
}
}
#endif
}
else
{
@ -508,27 +486,7 @@ void State::apply()
}
}
#if 1
applyUniformMap(_uniformMap);
#else
if (_lastAppliedProgramObject && !_stateStateStack.empty())
{
for(StateSetStack::iterator sitr=_stateStateStack.begin();
sitr!=_stateStateStack.end();
++sitr)
{
const StateSet* stateset = *sitr;
const StateSet::UniformList& uniformList = stateset->getUniformList();
for(StateSet::UniformList::const_iterator itr=uniformList.begin();
itr!=uniformList.end();
++itr)
{
_lastAppliedProgramObject->apply(*(itr->second.first));
}
}
}
#endif
if (_checkGLErrors==ONCE_PER_ATTRIBUTE) checkGLErrors("end of State::apply()");
}
@ -1033,3 +991,11 @@ bool State::checkGLErrors(const StateAttribute* attribute) const
}
void State::applyModelViewAndProjectionUniformsIfRequired()
{
if (!_lastAppliedProgramObject) return;
if (_modelViewMatrixUniform.valid()) _lastAppliedProgramObject->apply(*_modelViewMatrixUniform);
if (_projectionMatrixUniform) _lastAppliedProgramObject->apply(*_projectionMatrixUniform);
if (_modelViewProjectionMatrixUniform) _lastAppliedProgramObject->apply(*_modelViewProjectionMatrixUniform);
}

View File

@ -54,7 +54,10 @@ void RenderLeaf::render(osg::RenderInfo& renderInfo,RenderLeaf* previous)
state.apply(rg->getStateSet());
}
// if we are using osg::Program which requires OSG's generated uniforms to track
// modelview and projection matrices then apply them now.
state.applyModelViewAndProjectionUniformsIfRequired();
// draw the drawable
_drawable->draw(renderInfo);
@ -70,6 +73,10 @@ void RenderLeaf::render(osg::RenderInfo& renderInfo,RenderLeaf* previous)
state.apply(_parent->getStateSet());
// if we are using osg::Program which requires OSG's generated uniforms to track
// modelview and projection matrices then apply them now.
state.applyModelViewAndProjectionUniformsIfRequired();
// draw the drawable
_drawable->draw(renderInfo);
}