Added some debugging to investigate issue of global default ShaderAttribute not being automatically assigned.

This commit is contained in:
Robert Osfield
2010-07-07 11:02:15 +00:00
parent 46b221a832
commit d50bf88bc0
3 changed files with 109 additions and 29 deletions

View File

@@ -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 ************ "<<std::endl<<std::endl;
osg::ref_ptr<osg::Node> scenegraph = createSceneGraph(arguments);
if (!scenegraph) return 1;
viewer.setSceneData(scenegraph.get());
OSG_NOTICE<<std::endl<<"********** Finished constructing scene graph ************ "<<std::endl<<std::endl;
viewer.realize();
// enable shader composition

View File

@@ -23,6 +23,7 @@ ShaderAttribute::ShaderAttribute():
_type(osg::StateAttribute::Type(-1))
{
_shaderComponent = new osg::ShaderComponent;
OSG_NOTICE<<"Creating default constructed ShaderAttribute() "<<this<<std::endl;
}
ShaderAttribute::ShaderAttribute(const ShaderAttribute& sa,const CopyOp& copyop):
@@ -30,6 +31,7 @@ ShaderAttribute::ShaderAttribute(const ShaderAttribute& sa,const CopyOp& copyop)
_type(sa._type),
_uniforms(sa._uniforms)
{
OSG_NOTICE<<"Creating copy constructed ShaderAttribute("<<&sa<<") "<<this<<std::endl;
}
ShaderAttribute::~ShaderAttribute()

View File

@@ -184,6 +184,11 @@ osg::Shader* ShaderComposer::composeMain(const Shaders& shaders)
ref_ptr<Shader> mainShader = new Shader(type, full_source);
OSG_NOTICE<<"type =="<<type<<std::endl;
OSG_NOTICE<<"full_source == "<<std::endl<<full_source<<std::endl;
OSG_NOTICE<<"end of ShaderComposer::composeMain(Shaders)"<<std::endl<<std::endl;
_shaderMainMap[shaders] = mainShader;
return mainShader.get();