I'm having problems while trying to project the shadow map onto the terrain.
Although I can render the depth buffer from the lights point of view as illustrated in the lower left corner of the screenshot I can't project it onto the terrain for some reason. As you can see there are some black artifacts present. I have no idea what they are or where they come from as they seem to be completely random.
I have followed two shady "tutorials"
http://www.deor.org/~gulgi/shadowmappingdoc.html
http://www.ampoff.org/modules.php?name=Forums&file=viewtopic&t=15
But both of them expect something that I'm missing and due to their failure to provide source code I can't find out. Anyway, Let me illustrate the problem code:
In the rendering loop the following is done:
render begin
shadows begin
//renders lights point of view
render trees
render terrain
shadows calculate
render lit trees
render lit terrain
shadows end
render shadows on trees
render shadows on terrain
render end
everything works well until the shadows end, the part which the next code deals with
void
shadows_end()
{
static mat4 bias = { 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f};
set_texture(&ter_shader, "shadow_map", 2);
/*
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
const GLdouble x[] = {1.0, 0.0, 0.0, 0.0};
const GLdouble y[] = {0.0, 1.0, 0.0, 0.0};
const GLdouble z[] = {0.0, 0.0, 1.0, 0.0};
const GLdouble w[] = {0.0, 0.0, 0.0, 1.0};
glTexGendv(GL_S, GL_EYE_PLANE, x );
glTexGendv(GL_T, GL_EYE_PLANE, y );
glTexGendv(GL_R, GL_EYE_PLANE, z );
glTexGendv(GL_Q, GL_EYE_PLANE, w );
*/
glMatrixMode(GL_TEXTURE);
glLoadMatrixf(bias);
glMultMatrixf(light_proj_matrix);
glMultMatrixf(light_matrix);
glMatrixMode(GL_MODELVIEW);
//Bind & enable shadow map texture
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
//render using shadow vp and fp
set_uniform1i(&ter_shader, "state", SHADOW_PASS);
}
Another thing is that after using shadows if I don't set GL_TEXTURE_COMPARE_MODE_ARB to GL_NONE then the shadow texture will be either loaded as empty or render as empty. This doesn't worry me too much, except for the fact that I'm afraid it isn't working.
Here are the SHADOW_PASS GLSL programs
VP
proj_shadow = gl_TextureMatrix[0] * gl_Vertex;
gl_Position = ftransform();
fp
vec4 color = vec4(1.0);
// Do the Z comparison:
color *= shadow2DProj(shadow_map, proj_shadow).r;
// Output final color:
gl_FragColor = color;
So does anyone have working glsl shadow maps? I'd really appreciate some pointers.