I am trying to get a debug display of a shadow-map, based on osgdepthshadow.
I've added a quad fixed with respect to the eye, and I can paint a regular texture on it. But when I try to do the same with the texture used for the shadow-map all I get is the clear color.
Right now I'm using a shader that uses a shadow sampler, but I get the same (bad) results with using a texture sampler. Swizzling some of the components in the shader has no effect for the GL_DEPTH_COMPONENT texture, but works fine with the RGBA texture.
I'm lost by now.
I have modified the code so that I create a ref_ptr to the texture objects before creating the shadowed scene, then pass it to the function that creates the shadowed scene:
//////////////////////////////////////////////////////////////////
// fragment shader
//
char fragmentShaderSource_dbg[] =
"uniform sampler2DShadow shadowTexture; \n"
"\n"
"void main(void) \n"
"{ \n"
" vec4 color = shadow2D( shadowTexture, gl_TexCoord[0].xy ); \n"
" gl_FragColor = color;\n"
"}\n";
class TextureDebugger: public osg::Projection
{
public:
TextureDebugger(osg::Texture2D *tex,int shdUnit)
{
setMatrix(osg::Matrix::ortho2D(0.0, 1.0, 0.0, 1.0 ));
osg::ref_ptr<osg::MatrixTransform> mva = new osg::MatrixTransform;
mva->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
mva->setMatrix(osg::Matrix::identity());
addChild( mva.get());
osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array;
osg::ref_ptr<osg::Vec2Array> tcoords = new osg::Vec2Array;
coords->push_back( osg::Vec3( 0.0, 0.0, 0.0 ));
coords->push_back( osg::Vec3( 0.2, 0.0, 0.0 ));
coords->push_back( osg::Vec3( 0.2, 0.2, 0.0 ));
coords->push_back( osg::Vec3( 0.0, 0.2, 0.0 ));
tcoords->push_back( osg::Vec2( 0.0, 0.0 ));
tcoords->push_back( osg::Vec2( 1.0, 0.0 ));
tcoords->push_back( osg::Vec2( 1.0, 1.0 ));
tcoords->push_back( osg::Vec2( 0.0, 1.0 ));
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(coords.get());
geom->setTexCoordArray( 0, tcoords.get() );
geom->addPrimitiveSet( new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, coords->size()));
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
geom->setColorArray(colors);
geom->setColorBinding(osg::Geometry::BIND_OVERALL);
osg::ref_ptr<osg::StateSet> sset = new osg::StateSet;
sset->setMode( GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
//tex->setShadowComparison(true);
sset->setTextureAttributeAndModes( shdUnit, tex );
// sset->setTextureMode( 1, GL_TEXTURE_2D, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
// sset->setTextureMode( 1, GL_TEXTURE_GEN_S, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
// sset->setTextureMode( 1, GL_TEXTURE_GEN_T, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
// sset->setTextureMode( 2, GL_TEXTURE_2D, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
// sset->setTextureMode( 2, GL_TEXTURE_GEN_S, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
// sset->setTextureMode( 2, GL_TEXTURE_GEN_T, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
sset->setMode( GL_DEPTH_TEST, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED );
osg::Program* program = new osg::Program;
sset->setAttribute(program);
osg::Shader* fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragmentShaderSource_dbg);
program->addShader(fragment_shader);
osg::Uniform* shadowTextureSampler = new osg::Uniform("shadowTexture",(int)shdUnit);
sset->addUniform(shadowTextureSampler);
sset->setRenderBinDetails(110,"RenderBin");
geom->setStateSet( sset.get() );
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable( geom.get() );
mva->addChild( geode.get() );
}
};