Show
Ignore:
Timestamp:
10/09/09 15:39:11 (5 years ago)
Author:
robert
Message:

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/examples/osgvertexattributes/osgvertexattributes.cpp

    r10620 r10621  
    5252        } 
    5353 
    54         void replaceAndBind(osg::Program& program, std::string& source, const std::string& originalStr, const AttributeAlias& alias, const std::string& declarationPrefix) 
     54        void replaceAndBindAttrib(osg::Program& program, std::string& source, const std::string& originalStr, const AttributeAlias& alias, const std::string& declarationPrefix) 
    5555        { 
    5656            if (replace(source, originalStr, alias.second)) 
     
    6161        } 
    6262 
     63        void replaceBuiltInUniform(std::string& source, const std::string& originalStr, const std::string& newStr, const std::string& declarationPrefix) 
     64        { 
     65            if (replace(source, originalStr, newStr)) 
     66            { 
     67                source.insert(0, declarationPrefix + newStr + std::string(";\n")); 
     68            } 
     69        } 
     70 
    6371        void convertVertexShader(osg::Program& program, osg::Shader& shader) 
    6472        { 
     
    6876            replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex"); 
    6977 
    70             replaceAndBind(program, source, "gl_Normal", _normalAlias, "attribute vec3 "); 
    71             replaceAndBind(program, source, "gl_Vertex", _vertexAlias, "attribute vec4 "); 
    72             replaceAndBind(program, source, "gl_Color", _colorAlias, "attribute vec4 "); 
    73             replaceAndBind(program, source, "gl_SecondaryColor", _secondaryColorAlias, "attribute vec4 "); 
    74             replaceAndBind(program, source, "gl_FogCoord", _fogCoordAlias, "attribute float "); 
    75  
    76             replaceAndBind(program, source, "gl_MultiTexCoord0", _texCoordAlias[0], "attribute vec4 "); 
    77             replaceAndBind(program, source, "gl_MultiTexCoord1", _texCoordAlias[1], "attribute vec4 "); 
    78             replaceAndBind(program, source, "gl_MultiTexCoord2", _texCoordAlias[2], "attribute vec4 "); 
    79             replaceAndBind(program, source, "gl_MultiTexCoord3", _texCoordAlias[3], "attribute vec4 "); 
    80             replaceAndBind(program, source, "gl_MultiTexCoord4", _texCoordAlias[4], "attribute vec4 "); 
    81             replaceAndBind(program, source, "gl_MultiTexCoord5", _texCoordAlias[5], "attribute vec4 "); 
    82             replaceAndBind(program, source, "gl_MultiTexCoord6", _texCoordAlias[6], "attribute vec4 "); 
    83             replaceAndBind(program, source, "gl_MultiTexCoord7", _texCoordAlias[7], "attribute vec4 "); 
     78            replaceAndBindAttrib(program, source, "gl_Normal", _normalAlias, "attribute vec3 "); 
     79            replaceAndBindAttrib(program, source, "gl_Vertex", _vertexAlias, "attribute vec4 "); 
     80            replaceAndBindAttrib(program, source, "gl_Color", _colorAlias, "attribute vec4 "); 
     81            replaceAndBindAttrib(program, source, "gl_SecondaryColor", _secondaryColorAlias, "attribute vec4 "); 
     82            replaceAndBindAttrib(program, source, "gl_FogCoord", _fogCoordAlias, "attribute float "); 
     83 
     84            replaceAndBindAttrib(program, source, "gl_MultiTexCoord0", _texCoordAlias[0], "attribute vec4 "); 
     85            replaceAndBindAttrib(program, source, "gl_MultiTexCoord1", _texCoordAlias[1], "attribute vec4 "); 
     86            replaceAndBindAttrib(program, source, "gl_MultiTexCoord2", _texCoordAlias[2], "attribute vec4 "); 
     87            replaceAndBindAttrib(program, source, "gl_MultiTexCoord3", _texCoordAlias[3], "attribute vec4 "); 
     88            replaceAndBindAttrib(program, source, "gl_MultiTexCoord4", _texCoordAlias[4], "attribute vec4 "); 
     89            replaceAndBindAttrib(program, source, "gl_MultiTexCoord5", _texCoordAlias[5], "attribute vec4 "); 
     90            replaceAndBindAttrib(program, source, "gl_MultiTexCoord6", _texCoordAlias[6], "attribute vec4 "); 
     91            replaceAndBindAttrib(program, source, "gl_MultiTexCoord7", _texCoordAlias[7], "attribute vec4 "); 
    8492 
    8593 
     
    8997            replace(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix"); 
    9098            replace(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix"); 
     99#else 
     100            // replace built in uniform 
     101            replaceBuiltInUniform(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix", "uniform mat4 "); 
     102            replaceBuiltInUniform(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix", "uniform mat4 "); 
     103            replaceBuiltInUniform(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix", "uniform mat4 "); 
    91104#endif 
    92105            shader.setShaderSource(source);