Shadows from texture shader

53 views
Skip to first unread message

Diclehan Ulucan

unread,
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;
scene->addChild(Obj1.get());
osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);

vertShader->loadShaderSourceFromFile("source.vert");
fragShader->loadShaderSourceFromFile("source.frag");

osg::ref_ptr<osg::Program> program = new osg::Program;
program->addShader(vertShader.get());
program->addShader(fragShader.get());

osg::Vec4 lightPos(0.0,75.0,75.0,1.0);
osg::ref_ptr<osg::LightSource> ls = new osg::LightSource;
ls->getLight()->setPosition(lightPos);
osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
sm->setLight(ls->getLight());
scene->setShadowTechnique(sm.get());

osg::ref_ptr<osg::StateSet> stateset= Obj1->getOrCreateStateSet();
stateset->setAttributeAndModes(program.get());
osg::ref_ptr<osg::Image> imagetex=osgDB::readImageFile("texture1.jpg");
osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D;
texture->setImage(imagetex.get());
stateset->setTextureAttributeAndModes(0,texture.get());

....

viewer.getCamera()->setClearColor(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
viewer.setSceneData(scene.get());
viewer.run();

//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

unread,
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.

Sincerely,
Diclehan Ulucan
Example.jpg

Trajce Nikolov NICK

unread,
Nov 6, 2021, 10:50:09 PM11/6/21
to osg-...@googlegroups.com
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 (github.com).

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

Cheers!
Nick


--
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 osg-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osg-users/16adfecd-7be6-4de5-a608-0e2c3e4115cbn%40googlegroups.com.


--
trajce nikolov nick
Reply all
Reply to author
Forward
0 new messages