shadowmapping

shadow mapping

shadow mapping using glsl and fbo

This is a small tutorial to cover the shadow mapping technique.

    • Steps to follow

    • 1) Create a frame buffer object and attach it to texture

    • 2) Render the scene's depth information to the frame buffer object

    • 3) Send the light transformation information in texture matrix.

    • 4) Use texture matrix to transform the current vertex to light space.

    • 5) If the depth of current is less than or equal to the depth value in shadow map then this is shadow else it is lit pixel.

Here are the results of the above described method:

As you can see shadow map works pretty well but here are the things to keep in mind:

1) your graphics card should support vertex and pixel shaders.

2) Your driver should have support for FrameBuffer objects.

If your graphics card doesn't support FrameBuffer objects, you could use the glReadPixels to read the depth buffer and create texture from it. But doing it in real time with that technique would be a bad idea. Because glReadPixels is a very costly operation.

Vertex Shader

varying vec4 ShadowCoord;

void main()

{

ShadowCoord= gl_TextureMatrix[7] * gl_Vertex;

gl_Position = ftransform();

gl_FrontColor = gl_Color;

}

Fragment shader

uniform sampler2D ShadowMap;

varying vec4 ShadowCoord;

void main()

{

vec4 shadowCoordinateWdivide = ShadowCoord / ShadowCoord.w ;

// Used to lower moiré pattern and self-shadowing

shadowCoordinateWdivide.z += 0.0005;

float distanceFromLight = texture2D(ShadowMap,shadowCoordinateWdivide.st).z;

float shadow = 1.0;

if (ShadowCoord.w > 0.0)

shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.5 : 1.0 ;

gl_FragColor = shadow * gl_Color;

}

For detailed explanation of this method you might want to look at this link