Changeset 10621
- Timestamp:
- 10/09/09 15:39:11 (4 years ago)
- Location:
- OpenSceneGraph/trunk
- Files:
-
- 5 modified
-
examples/osgvertexattributes/osgvertexattributes.cpp (modified) (4 diffs)
-
include/osg/State (modified) (3 diffs)
-
src/osg/Geometry.cpp (modified) (7 diffs)
-
src/osg/State.cpp (modified) (6 diffs)
-
src/osgUtil/RenderLeaf.cpp (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/examples/osgvertexattributes/osgvertexattributes.cpp
r10620 r10621 52 52 } 53 53 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) 55 55 { 56 56 if (replace(source, originalStr, alias.second)) … … 61 61 } 62 62 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 63 71 void convertVertexShader(osg::Program& program, osg::Shader& shader) 64 72 { … … 68 76 replace(source, "ftransform()", "gl_ModelViewProjectionMatrix * gl_Vertex"); 69 77 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 "); 84 92 85 93 … … 89 97 replace(source, "gl_ModelViewProjectionMatrix", "osg_ModelViewProjectionMatrix"); 90 98 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 "); 91 104 #endif 92 105 shader.setShaderSource(source); -
OpenSceneGraph/trunk/include/osg/State
r10601 r10621 167 167 if (_projection!=matrix) 168 168 { 169 if (matrix) 170 { 171 _projection=matrix; 172 } 173 else 174 { 175 _projection=_identity; 176 } 177 178 if (_projectionMatrixUniform.valid()) _projectionMatrixUniform->set(*_projection); 179 if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); 180 169 181 glMatrixMode( GL_PROJECTION ); 182 glLoadMatrix(_projection->ptr()); 183 glMatrixMode( GL_MODELVIEW ); 184 } 185 } 186 187 inline const osg::Matrix& getProjectionMatrix() const 188 { 189 return *_projection; 190 } 191 192 inline void applyModelViewMatrix(const osg::RefMatrix* matrix) 193 { 194 if (_modelView!=matrix) 195 { 170 196 if (matrix) 171 197 { 172 _projection=matrix; 173 glLoadMatrix(matrix->ptr()); 198 _modelView=matrix; 174 199 } 175 200 else 176 201 { 177 _projection=_identity;178 glLoadIdentity();179 }180 glMatrixMode( GL_MODELVIEW );181 }182 }183 184 inline const osg::Matrix& getProjectionMatrix() const185 {186 return *_projection;187 }188 189 inline void applyModelViewMatrix(const osg::RefMatrix* matrix)190 {191 if (_modelView!=matrix)192 {193 if (matrix)194 {195 _modelView=matrix;196 glLoadMatrix(matrix->ptr());197 }198 else199 {200 202 _modelView=_identity; 201 glLoadIdentity(); 202 } 203 } 204 205 if (_modelViewMatrixUniform.valid()) _modelViewMatrixUniform->set(*_modelView); 206 if (_modelViewProjectionMatrixUniform.valid()) _modelViewProjectionMatrixUniform->set((*_modelView) * (*_projection)); 207 208 glLoadMatrix(_modelView->ptr()); 203 209 } 204 210 } … … 208 214 return *_modelView; 209 215 } 216 217 void applyModelViewAndProjectionUniformsIfRequired(); 218 219 osg::Uniform* getModelViewMatrixUniform() { return _modelViewMatrixUniform.get(); } 220 osg::Uniform* getProjectionMatrixUniform() { return _projectionMatrixUniform.get(); } 221 osg::Uniform* getModelViewProjectionMatrixUniform() { return _modelViewProjectionMatrixUniform.get(); } 210 222 211 223 … … 1088 1100 ref_ptr<const RefMatrix> _projection; 1089 1101 ref_ptr<const RefMatrix> _modelView; 1102 1103 ref_ptr<Uniform> _modelViewMatrixUniform; 1104 ref_ptr<Uniform> _projectionMatrixUniform; 1105 ref_ptr<Uniform> _modelViewProjectionMatrixUniform; 1090 1106 1091 1107 Matrix _initialInverseViewMatrix; -
OpenSceneGraph/trunk/src/osg/Geometry.cpp
r10615 r10621 2115 2115 AttributeFunctorArrayVisitor afav(af); 2116 2116 2117 afav.applyArray(VERTICES,_vertexData.array.get()); 2117 if (_vertexData.array.valid()) 2118 { 2119 afav.applyArray(VERTICES,_vertexData.array.get()); 2120 } 2121 else if (_vertexAttribList.size()>0) 2122 { 2123 osg::notify(osg::INFO)<<"Geometry::accept(AttributeFunctor& af): Using vertex attribute instead"<<std::endl; 2124 afav.applyArray(VERTICES,_vertexAttribList[0].array.get()); 2125 } 2126 2118 2127 afav.applyArray(NORMALS,_normalData.array.get()); 2119 2128 afav.applyArray(COLORS,_colorData.array.get()); … … 2179 2188 ConstAttributeFunctorArrayVisitor afav(af); 2180 2189 2181 afav.applyArray(VERTICES,_vertexData.array.get()); 2190 if (_vertexData.array.valid()) 2191 { 2192 afav.applyArray(VERTICES,_vertexData.array.get()); 2193 } 2194 else if (_vertexAttribList.size()>0) 2195 { 2196 osg::notify(osg::INFO)<<"Geometry::accept(ConstAttributeFunctor& af): Using vertex attribute instead"<<std::endl; 2197 afav.applyArray(VERTICES,_vertexAttribList[0].array.get()); 2198 } 2199 2182 2200 afav.applyArray(NORMALS,_normalData.array.get()); 2183 2201 afav.applyArray(COLORS,_colorData.array.get()); … … 2198 2216 void Geometry::accept(PrimitiveFunctor& functor) const 2199 2217 { 2200 if (!_vertexData.array.valid() || _vertexData.array->getNumElements()==0) return; 2201 2202 if (!_vertexData.indices.valid()) 2203 { 2204 switch(_vertexData.array->getType()) 2218 const osg::Array* vertices = _vertexData.array.get(); 2219 const osg::IndexArray* indices = _vertexData.indices.get(); 2220 2221 if (!vertices && _vertexAttribList.size()>0) 2222 { 2223 osg::notify(osg::INFO)<<"Using vertex attribute instead"<<std::endl; 2224 vertices = _vertexAttribList[0].array.get(); 2225 indices = _vertexAttribList[0].indices.get(); 2226 } 2227 2228 if (!vertices || vertices->getNumElements()==0) return; 2229 2230 if (!indices) 2231 { 2232 switch(vertices->getType()) 2205 2233 { 2206 2234 case(Array::Vec2ArrayType): 2207 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec2*>(_vertexData.array->getDataPointer()));2235 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2*>(vertices->getDataPointer())); 2208 2236 break; 2209 2237 case(Array::Vec3ArrayType): 2210 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec3*>(_vertexData.array->getDataPointer()));2238 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3*>(vertices->getDataPointer())); 2211 2239 break; 2212 2240 case(Array::Vec4ArrayType): 2213 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec4*>(_vertexData.array->getDataPointer()));2241 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4*>(vertices->getDataPointer())); 2214 2242 break; 2215 2243 case(Array::Vec2dArrayType): 2216 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec2d*>(_vertexData.array->getDataPointer()));2244 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2d*>(vertices->getDataPointer())); 2217 2245 break; 2218 2246 case(Array::Vec3dArrayType): 2219 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec3d*>(_vertexData.array->getDataPointer()));2247 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3d*>(vertices->getDataPointer())); 2220 2248 break; 2221 2249 case(Array::Vec4dArrayType): 2222 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec4d*>(_vertexData.array->getDataPointer()));2250 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4d*>(vertices->getDataPointer())); 2223 2251 break; 2224 2252 default: 2225 notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<< _vertexData.array->getType()<<std::endl;2253 notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl; 2226 2254 return; 2227 2255 } … … 2242 2270 const Vec3d* vec3dArray = 0; 2243 2271 const Vec4d* vec4dArray = 0; 2244 Array::Type type = _vertexData.array->getType();2272 Array::Type type = vertices->getType(); 2245 2273 switch(type) 2246 2274 { 2247 2275 case(Array::Vec2ArrayType): 2248 vec2Array = static_cast<const Vec2*>( _vertexData.array->getDataPointer());2276 vec2Array = static_cast<const Vec2*>(vertices->getDataPointer()); 2249 2277 break; 2250 2278 case(Array::Vec3ArrayType): 2251 vec3Array = static_cast<const Vec3*>( _vertexData.array->getDataPointer());2279 vec3Array = static_cast<const Vec3*>(vertices->getDataPointer()); 2252 2280 break; 2253 2281 case(Array::Vec4ArrayType): 2254 vec4Array = static_cast<const Vec4*>( _vertexData.array->getDataPointer());2282 vec4Array = static_cast<const Vec4*>(vertices->getDataPointer()); 2255 2283 break; 2256 2284 case(Array::Vec2dArrayType): 2257 vec2dArray = static_cast<const Vec2d*>( _vertexData.array->getDataPointer());2285 vec2dArray = static_cast<const Vec2d*>(vertices->getDataPointer()); 2258 2286 break; 2259 2287 case(Array::Vec3dArrayType): 2260 vec3dArray = static_cast<const Vec3d*>( _vertexData.array->getDataPointer());2288 vec3dArray = static_cast<const Vec3d*>(vertices->getDataPointer()); 2261 2289 break; 2262 2290 case(Array::Vec4dArrayType): 2263 vec4dArray = static_cast<const Vec4d*>( _vertexData.array->getDataPointer());2291 vec4dArray = static_cast<const Vec4d*>(vertices->getDataPointer()); 2264 2292 break; 2265 2293 default: 2266 notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<< _vertexData.array->getType()<<std::endl;2294 notify(WARN)<<"Warning: Geometry::accept(PrimitiveFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl; 2267 2295 return; 2268 2296 } 2269 2297 2270 2271 2298 for(PrimitiveSetList::const_iterator itr=_primitives.begin(); 2272 2299 itr!=_primitives.end(); … … 2312 2339 2313 2340 } 2314 2341 2315 2342 functor.end(); 2316 2343 break; … … 2355 2382 ++vindex; 2356 2383 } 2357 2384 2358 2385 functor.end(); 2359 2386 … … 2488 2515 void Geometry::accept(PrimitiveIndexFunctor& functor) const 2489 2516 { 2490 if (!_vertexData.array.valid() || _vertexData.array->getNumElements()==0) return; 2491 2492 switch(_vertexData.array->getType()) 2517 const osg::Array* vertices = _vertexData.array.get(); 2518 const osg::IndexArray* indices = _vertexData.indices.get(); 2519 2520 if (!vertices && _vertexAttribList.size()>0) 2521 { 2522 osg::notify(osg::INFO)<<"Geometry::accept(PrimitiveIndexFunctor& functor): Using vertex attribute instead"<<std::endl; 2523 vertices = _vertexAttribList[0].array.get(); 2524 indices = _vertexAttribList[0].indices.get(); 2525 } 2526 2527 if (!vertices || vertices->getNumElements()==0) return; 2528 2529 switch(vertices->getType()) 2493 2530 { 2494 2531 case(Array::Vec2ArrayType): 2495 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec2*>(_vertexData.array->getDataPointer()));2532 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2*>(vertices->getDataPointer())); 2496 2533 break; 2497 2534 case(Array::Vec3ArrayType): 2498 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec3*>(_vertexData.array->getDataPointer()));2535 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3*>(vertices->getDataPointer())); 2499 2536 break; 2500 2537 case(Array::Vec4ArrayType): 2501 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec4*>(_vertexData.array->getDataPointer()));2538 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4*>(vertices->getDataPointer())); 2502 2539 break; 2503 2540 case(Array::Vec2dArrayType): 2504 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec2d*>(_vertexData.array->getDataPointer()));2541 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec2d*>(vertices->getDataPointer())); 2505 2542 break; 2506 2543 case(Array::Vec3dArrayType): 2507 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec3d*>(_vertexData.array->getDataPointer()));2544 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec3d*>(vertices->getDataPointer())); 2508 2545 break; 2509 2546 case(Array::Vec4dArrayType): 2510 functor.setVertexArray( _vertexData.array->getNumElements(),static_cast<const Vec4d*>(_vertexData.array->getDataPointer()));2547 functor.setVertexArray(vertices->getNumElements(),static_cast<const Vec4d*>(vertices->getDataPointer())); 2511 2548 break; 2512 2549 default: 2513 notify(WARN)<<"Warning: Geometry::accept(PrimitiveIndexFunctor&) cannot handle Vertex Array type"<< _vertexData.array->getType()<<std::endl;2550 notify(WARN)<<"Warning: Geometry::accept(PrimitiveIndexFunctor&) cannot handle Vertex Array type"<<vertices->getType()<<std::endl; 2514 2551 return; 2515 2552 } -
OpenSceneGraph/trunk/src/osg/State.cpp
r10614 r10621 45 45 _modelView = _identity; 46 46 47 _modelViewMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewMatrix"); 48 _projectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ProjectionMatrix"); 49 _modelViewProjectionMatrixUniform = new Uniform(Uniform::FLOAT_MAT4,"osg_ModelViewProjectionMatrix"); 50 47 51 _abortRenderingPtr = false; 48 52 … … 91 95 _maxTexturePoolSize = 0; 92 96 _maxBufferObjectPoolSize = 0; 97 98 93 99 } 94 100 … … 421 427 //popStateSet(); 422 428 //return; 423 429 424 430 if (dstate) 425 431 { … … 441 447 if (unit<ds_textureModeList.size()) applyModeList(getOrCreateTextureModeMap(unit),ds_textureModeList[unit]); 442 448 else if (unit<_textureModeMapList.size()) applyModeMap(_textureModeMapList[unit]); 443 449 444 450 if (unit<ds_textureAttributeList.size()) applyAttributeList(getOrCreateTextureAttributeMap(unit),ds_textureAttributeList[unit]); 445 451 else if (unit<_textureAttributeMapList.size()) applyAttributeMap(_textureAttributeMapList[unit]); 446 452 } 447 453 } 448 449 #if 1 454 450 455 applyUniformList(_uniformMap,dstate->getUniformList()); 451 #else452 if (_lastAppliedProgramObject)453 {454 for(StateSetStack::iterator sitr=_stateStateStack.begin();455 sitr!=_stateStateStack.end();456 ++sitr)457 {458 const StateSet* stateset = *sitr;459 const StateSet::UniformList& uniformList = stateset->getUniformList();460 for(StateSet::UniformList::const_iterator itr=uniformList.begin();461 itr!=uniformList.end();462 ++itr)463 {464 _lastAppliedProgramObject->apply(*(itr->second.first));465 }466 }467 468 const StateSet::UniformList& uniformList = dstate->getUniformList();469 for(StateSet::UniformList::const_iterator itr=uniformList.begin();470 itr!=uniformList.end();471 ++itr)472 {473 _lastAppliedProgramObject->apply(*(itr->second.first));474 }475 }476 #endif477 478 456 } 479 457 else … … 509 487 } 510 488 511 #if 1512 489 applyUniformMap(_uniformMap); 513 #else514 if (_lastAppliedProgramObject && !_stateStateStack.empty())515 {516 for(StateSetStack::iterator sitr=_stateStateStack.begin();517 sitr!=_stateStateStack.end();518 ++sitr)519 {520 const StateSet* stateset = *sitr;521 const StateSet::UniformList& uniformList = stateset->getUniformList();522 for(StateSet::UniformList::const_iterator itr=uniformList.begin();523 itr!=uniformList.end();524 ++itr)525 {526 _lastAppliedProgramObject->apply(*(itr->second.first));527 }528 }529 }530 #endif531 532 490 533 491 if (_checkGLErrors==ONCE_PER_ATTRIBUTE) checkGLErrors("end of State::apply()"); … … 1034 992 1035 993 994 void State::applyModelViewAndProjectionUniformsIfRequired() 995 { 996 if (!_lastAppliedProgramObject) return; 997 998 if (_modelViewMatrixUniform.valid()) _lastAppliedProgramObject->apply(*_modelViewMatrixUniform); 999 if (_projectionMatrixUniform) _lastAppliedProgramObject->apply(*_projectionMatrixUniform); 1000 if (_modelViewProjectionMatrixUniform) _lastAppliedProgramObject->apply(*_modelViewProjectionMatrixUniform); 1001 } -
OpenSceneGraph/trunk/src/osgUtil/RenderLeaf.cpp
r7328 r10621 55 55 56 56 } 57 57 58 // if we are using osg::Program which requires OSG's generated uniforms to track 59 // modelview and projection matrices then apply them now. 60 state.applyModelViewAndProjectionUniformsIfRequired(); 58 61 59 62 // draw the drawable … … 71 74 state.apply(_parent->getStateSet()); 72 75 76 // if we are using osg::Program which requires OSG's generated uniforms to track 77 // modelview and projection matrices then apply them now. 78 state.applyModelViewAndProjectionUniformsIfRequired(); 79 73 80 // draw the drawable 74 81 _drawable->draw(renderInfo);
