Changeset 10620
- Timestamp:
- 10/09/09 12:39:55 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgvertexattributes/osgvertexattributes.cpp
r10619 r10620 47 47 } 48 48 49 std::string convertShader(std::string source) 50 { 49 void bindAttribute(osg::Program& program, const AttributeAlias& alias) 50 { 51 program.addBindAttribLocation(alias.second, alias.first); 52 } 53 54 void replaceAndBind(osg::Program& program, std::string& source, const std::string& originalStr, const AttributeAlias& alias, const std::string& declarationPrefix) 55 { 56 if (replace(source, originalStr, alias.second)) 57 { 58 source.insert(0, declarationPrefix + alias.second + std::string(";\n")); 59 bindAttribute(program, alias); 60 } 61 } 62 63 void convertVertexShader(osg::Program& program, osg::Shader& shader) 64 { 65 std::string source = shader.getShaderSource(); 66 51 67 // replace ftransform as it only works with built-ins 52 68 replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex"); 53 69 54 // replace the vertex attributes 55 replace(source, "gl_Vertex", "osg_Vertex"); 56 replace(source, "gl_Normal", "osg_Normal"); 57 replace(source, "gl_Color", "osg_Color"); 58 replace(source, "gl_SecondaryColor", "osg_SecondaryColor"); 59 replace(source, "gl_FogCoord", "osg_FogCoord"); 60 replace(source, "gl_MultiTexCoord0", "osg_MultiTexCoord0"); 61 replace(source, "gl_MultiTexCoord1", "osg_MultiTexCoord1"); 62 replace(source, "gl_MultiTexCoord2", "osg_MultiTexCoord2"); 63 replace(source, "gl_MultiTexCoord3", "osg_MultiTexCoord3"); 64 replace(source, "gl_MultiTexCoord4", "osg_MultiTexCoord4"); 65 replace(source, "gl_MultiTexCoord5", "osg_MultiTexCoord5"); 66 replace(source, "gl_MultiTexCoord6", "osg_MultiTexCoord6"); 67 replace(source, "gl_MultiTexCoord7", "osg_MultiTexCoord7"); 68 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 "); 84 85 86 #if 0 69 87 // replace the modelview and project matrices 70 88 replace(source, "gl_ModelViewMatrix", "osg_ModeViewMatrix"); 71 89 replace(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix"); 72 90 replace(source, "gl_ProjectionMatrix", "osg_ProjectionMatrix"); 73 74 return source; 91 #endif 92 shader.setShaderSource(source); 93 } 94 95 void convertFragmentShader(osg::Program& program, osg::Shader& shader) 96 { 75 97 } 76 98 … … 105 127 } 106 128 107 void replace(std::string& str, const std::string& original_phrase, const std::string& new_phrase) 108 { 129 bool replace(std::string& str, const std::string& original_phrase, const std::string& new_phrase) 130 { 131 bool replacedStr = false; 109 132 std::string::size_type pos = 0; 110 133 while((pos=str.find(original_phrase, pos))!=std::string::npos) … … 123 146 } 124 147 148 replacedStr = true; 125 149 str.replace(pos, original_phrase.size(), new_phrase); 126 150 } 127 } 128 129 void apply(osg::Shader& shader) 151 return replacedStr; 152 } 153 154 void apply(osg::Program& program, osg::Shader& shader) 130 155 { 131 156 if (_visited.count(&shader)!=0) return; … … 134 159 osg::notify(osg::NOTICE)<<"Shader "<<shader.getTypename()<<" ----before-----------"<<std::endl; 135 160 osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; 136 shader.setShaderSource(convertShader(shader.getShaderSource())); 161 162 if (shader.getType()==osg::Shader::VERTEX) convertVertexShader(program, shader); 163 else if (shader.getType()==osg::Shader::FRAGMENT) convertFragmentShader(program, shader); 164 137 165 osg::notify(osg::NOTICE)<<"--after-----------"<<std::endl; 138 166 osg::notify(osg::NOTICE)<<shader.getShaderSource()<<std::endl; … … 152 180 for(unsigned int i=0; i<program->getNumShaders(); ++i) 153 181 { 154 apply(* (program->getShader(i)));182 apply(*program, *(program->getShader(i))); 155 183 } 184 156 185 } 157 186 } … … 159 188 void apply(osg::Geometry& geom) 160 189 { 190 geom.setUseDisplayList(false); 191 161 192 osg::notify(osg::NOTICE)<<"Found geometry "<<&geom<<std::endl; 162 193 if (geom.getVertexArray()) … … 168 199 if (geom.getNormalArray()) 169 200 { 170 setVertexAttrib(geom, _normalAlias, geom.getNormalArray(), true, geom.getNormalBinding());201 setVertexAttrib(geom, _normalAlias, geom.getNormalArray(), false, geom.getNormalBinding()); 171 202 geom.setNormalArray(0); 172 203 } … … 174 205 if (geom.getColorArray()) 175 206 { 176 setVertexAttrib(geom, _colorAlias, geom.getColorArray(), true, geom.getColorBinding());207 setVertexAttrib(geom, _colorAlias, geom.getColorArray(), false, geom.getColorBinding()); 177 208 geom.setColorArray(0); 178 209 } … … 180 211 if (geom.getSecondaryColorArray()) 181 212 { 182 setVertexAttrib(geom, _secondaryColorAlias, geom.getSecondaryColorArray(), true, geom.getSecondaryColorBinding());213 setVertexAttrib(geom, _secondaryColorAlias, geom.getSecondaryColorArray(), false, geom.getSecondaryColorBinding()); 183 214 geom.setSecondaryColorArray(0); 184 215 } … … 201 232 if (geom.getTexCoordArray(i)) 202 233 { 203 setVertexAttrib(geom, _texCoordAlias[i], geom.getTexCoordArray(i), true, geom.getSecondaryColorBinding());234 setVertexAttrib(geom, _texCoordAlias[i], geom.getTexCoordArray(i), false, osg::Geometry::BIND_PER_VERTEX); 204 235 geom.setTexCoordArray(i,0); 205 236 }
