From d50bf88bc0d1248b133a4b0c8bdd388240c2bb69 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 7 Jul 2010 11:02:15 +0000 Subject: [PATCH] Added some debugging to investigate issue of global default ShaderAttribute not being automatically assigned. --- .../osgshadercomposition.cpp | 131 ++++++++++++++---- src/osg/ShaderAttribute.cpp | 2 + src/osg/ShaderComposer.cpp | 5 + 3 files changed, 109 insertions(+), 29 deletions(-) diff --git a/examples/osgshadercomposition/osgshadercomposition.cpp b/examples/osgshadercomposition/osgshadercomposition.cpp index 3e517fb69..34ee98fd1 100644 --- a/examples/osgshadercomposition/osgshadercomposition.cpp +++ b/examples/osgshadercomposition/osgshadercomposition.cpp @@ -33,6 +33,55 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments) osg::ShaderAttribute* sa1 = NULL; + { + osg::StateSet* stateset = group->getOrCreateStateSet(); + osg::ShaderAttribute* sa = new osg::ShaderAttribute; + sa->setType(osg::StateAttribute::Type(10000)); + sa1 = sa; + stateset->setAttribute(sa); + + { + osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX); + vertex_shader->addCodeInjection(-1,"varying vec4 color;\n"); + vertex_shader->addCodeInjection(-1,"varying vec4 texcoord;\n"); + vertex_shader->addCodeInjection(0,"color = gl_Color;\n"); + vertex_shader->addCodeInjection(0,"texcoord = gl_MultiTexCoord0;\n"); + vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n"); + sa->addShader(vertex_shader); + } + + { + osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT); + fragment_shader->addCodeInjection(-1,"varying vec4 color;\n"); + fragment_shader->addCodeInjection(-1,"varying vec4 texcoord;\n"); + fragment_shader->addCodeInjection(-1,"uniform sampler2D baseTexture; \n"); + fragment_shader->addCodeInjection(0,"gl_FragColor = color * texture2DProj( baseTexture, texcoord );\n"); + + sa->addShader(fragment_shader); + } + + sa->addUniform(new osg::Uniform("baseTexture",0)); + +#if 1 + osg::ShaderAttribute* sa_dummy = new osg::ShaderAttribute; + sa_dummy->setType(osg::StateAttribute::Type(10001)); + stateset->setAttribute(sa_dummy); +#endif + + } + + // inherit the ShaderComponents entirely from above + { + osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform; + pat->setPosition(position); + pat->addChild(node); + + position.x() += spacing; + + group->addChild(pat); + + } + { osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform; pat->setPosition(position); @@ -41,42 +90,45 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments) position.x() += spacing; osg::StateSet* stateset = pat->getOrCreateStateSet(); + stateset->setMode(GL_BLEND, osg::StateAttribute::ON); + +#if 1 osg::ShaderAttribute* sa = new osg::ShaderAttribute; - //sa->setType(osg::StateAttribute::Type(10000)); - sa1 = sa; + sa->setType(osg::StateAttribute::Type(10001)); stateset->setAttribute(sa); - { - const char shader_str[] = - "uniform vec4 myColour;\n" - "vec4 colour()\n" - "{\n" - " return myColour;\n" - "}\n"; - - osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX, shader_str); - vertex_shader->addCodeInjection(-1,"varying vec4 c;\n"); - vertex_shader->addCodeInjection(-1,"vec4 colour();\n"); - vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n"); - vertex_shader->addCodeInjection(0,"c = colour();\n"); - - sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,0.5f,0.0f,1.0f))); - - sa->addShader(vertex_shader); - } - { osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT); - fragment_shader->addCodeInjection(-1,"varying vec4 c;\n"); - fragment_shader->addCodeInjection(0,"gl_FragColor = c;\n"); + fragment_shader->addCodeInjection(0.9f,"gl_FragColor.a = gl_FragColor.a*0.5;\n"); sa->addShader(fragment_shader); } +#endif + + group->addChild(pat); + } + + // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform + { + osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform; + pat->setPosition(position); + pat->addChild(node); + + position.x() += spacing; + + osg::StateSet* stateset = pat->getOrCreateStateSet(); + osg::ShaderAttribute* sa = new osg::ShaderAttribute(*sa1); + stateset->setAttribute(sa); + + // reuse the same ShaderComponent as the first branch + sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f))); group->addChild(pat); } -#if 1 + + + // resuse the first ShaderAttribute's type and ShaderComponent, just use new uniform { osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform; pat->setPosition(position); @@ -86,17 +138,34 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments) osg::StateSet* stateset = pat->getOrCreateStateSet(); osg::ShaderAttribute* sa = new osg::ShaderAttribute; - //sa->setType(osg::StateAttribute::Type(10000)); + sa->setType(osg::StateAttribute::Type(10000)); stateset->setAttribute(sa); + stateset->setMode(GL_BLEND, osg::StateAttribute::ON); + stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); - // reuse the same ShaderComponent as the first branch - sa->setShaderComponent(sa1->getShaderComponent()); - sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f))); + { + osg::Shader* vertex_shader = new osg::Shader(osg::Shader::VERTEX); + vertex_shader->addCodeInjection(0,"gl_Position = ftransform();\n"); + + sa->addShader(vertex_shader); + } + + { + osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT); + fragment_shader->addCodeInjection(-1,"uniform vec4 newColour;\n"); + fragment_shader->addCodeInjection(-1,"uniform float osg_FrameTime;\n"); + fragment_shader->addCodeInjection(0,"gl_FragColor = vec4(newColour.r,newColour.g,newColour.b, 0.5+sin(osg_FrameTime*2.0)*0.5);\n"); + + sa->addShader(fragment_shader); + sa->addUniform(new osg::Uniform("newColour",osg::Vec4(1.0f,1.0f,1.0f,0.5f))); + } + + group->addChild(pat); group->addChild(pat); } -#endif + return group; } @@ -106,11 +175,15 @@ int main( int argc, char **argv ) osgViewer::Viewer viewer(arguments); + OSG_NOTICE<<"********** Constructing scene graph ************ "< scenegraph = createSceneGraph(arguments); if (!scenegraph) return 1; viewer.setSceneData(scenegraph.get()); + OSG_NOTICE< mainShader = new Shader(type, full_source); + OSG_NOTICE<<"type =="<