Shadow Mapping GLSL

Hi, I’m trying to implement shadow mapping with a GLSL code to have shadows with GLSL custom materials.

The method uses a video texture getting the scene depth rendering from the light point of view. The image is then passed to fragment shader as sampler2D to make distance computation as in simple shadow mapping technique.

Unfortunately I get a very bad result (saw tooth shape) on the in-shadow face of the cube. I tried to raise the size of videotexture but it seems upper limited to 1024 pixel

What could it be the reason?


shadowMappingGLSL.blend (698 KB)

I would recommend trying to grab the texture that the BGE makes for shadow maps. If you use PyOpenGL, you can iterate through the textures on the GPU. If you’re using VSMs, then there should be two RG32F texture per shadow caster, you’ll want the larger one. You can check a texture’s internal format with glGetTexLevelParameter:

Not sure if it’s possible, but a nice feature to add in would be to allow one to use Node materials to mix a shadow-influenced blank material with the custom material with a shader. Unfortunately, you can’t do this currently as the BGE only can access the first, initial node material, not any materials that it is interacting with. Could be a nice, easy solution compared to grabbing the shadow map via OpenGL.

Adding the shadow textures to be accessible in the shaders could be nice as well. Again, I’m not knowledgeable about the subject, though, so I don’t know if it’s indeed possible.

Better custom shader support is in the works, but for now we just have hacky workarounds.

Thank you very much Moguri! I will try to use PyOpenGL to get the shadow map texture on GPU

Ok, I successfully tried to get the GL_TEXTURE_2D created from Blender (for this case bgl is sufficient) but the problem persists. Furthermore, I had to transform the depth texture (widthheight1) to an RGB one (widthheight3) as ImageBuff.load raised an error telling the format didn’t correspond (the texture slot, in fact, had a generated RGB image). Is there is any other way to pass directly the depth texture to the shader?

Is this the only method to code a custom GLSL shader and to get dynamic shadows?


shadowMappingGLSL1.blend (698 KB)