Shadows from texture shader

Skip to first unread message

Diclehan Ulucan

Oct 26, 2021, 1:02:08 PM10/26/21
to OpenSceneGraph Users
Hello all, 
I am facing a problem with the ShadowMap. I would like to add shadows to a scene which includes different textures for objects. When I add the shader for ShadowMap I get an all black scene. As far as I know this happens due to Ambient Bias, however setting it to some value in range [0,1] did not help me. Has anyone else faced this kind of problem or has an idea that might help? I would appreciate your help. My code is as follows:

osg::ref_ptr<osgShadow::ShadowedScene> scene = new osgShadow::ShadowedScene;
osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);


osg::ref_ptr<osg::Program> program = new osg::Program;

osg::Vec4 lightPos(0.0,75.0,75.0,1.0);
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;

osg::ref_ptr<osg::StateSet> stateset= Obj1->getOrCreateStateSet();
osg::ref_ptr<osg::Image> imagetex=osgDB::readImageFile("texture1.jpg");
osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D;



//fragment shader
uniform sampler2D osgShadow_baseTexture; 
uniform sampler2DShadow osgShadow_shadowTexture; 
uniform vec2 osgShadow_ambientBias; 
void main() 
     vec4 color = gl_Color * texture2D( osgShadow_baseTexture, gl_TexCoord[0].xy ); 
     gl_FragColor = color * (osgShadow_ambientBias.x+ shadow2DProj( osgShadow_shadowTexture, gl_TexCoord[1] ) * osgShadow_ambientBias.y);

Best Regards,
Diclehan Ulucan

Diclehan Ulucan

Nov 4, 2021, 11:54:39 AM11/4/21
to OpenSceneGraph Users

Hello all,
I worked on the problem and now I get the objects which will cast a shadow in black. Does anyone have an idea about the reason of this or an idea how to obtain shadows from shaders with another approach? I have attached an example to this e-mail.

Thank you in advance.

Diclehan Ulucan

Trajce Nikolov NICK

Nov 6, 2021, 10:50:09 PM11/6/21
Hi Diclehan,

I have been working with OSG shadows for years and I can say almost all of the implementations are buggy. Recently I figured out that the Parallel Split Shadow Maps are working the best so far, however their shader implementation is encapsulated in the technique itself and there are no shader hooks to inject your own shader code there. I did some changes that are submitted to the master repo (not yet accepted) and you can find them in my fork trajcenikolovnick/OpenSceneGraph: OpenSceneGraph git repository (

I went further and I havn't uploaded my changes yet, but you can enhance this Shadow Map Technique with user shader hooks and submit another change. The idea will be:

- In the header you put something like: void setUserFunctionVertexShader(const std::string& myShaderCode); Same for fragment shader
- make hooks of these strings and call them in the PSSM shaders

Easy. You might want first to give this PSSM a shot, and then try the hooks. If you still struggle, I will help with uploading my changes to my fork and post a PR again

Good luck and drop a line how it goes


You received this message because you are subscribed to the Google Groups "OpenSceneGraph Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

trajce nikolov nick
Reply all
Reply to author
0 new messages