Hello!
I have spent last days trying to implement simple shadow mapping. Unfortunately no success so far. I have read many threads and looked at sources. I don’t have any screenshots about the problem because only pure black color is shown but I can show some code.
Shaders:
uniform mat4 texture;
varying vec4 ShadowCoord;
void main() {
gl_Position = gl_ModelViewMatrix * gl_Vertex;
ShadowCoord = texture * gl_ModelViewMatrix * gl_Vertex;
ShadowCoord = ShadowCoord / ShadowCoord.w;
}
uniform sampler2DShadow shadow_buffer;
varying vec4 ShadowCoord;
...
float shadow = shadow2DProj(shadow_buffer, ShadowCoord).r;
gl_FragColor = vec4(vec3(shadow * color), 1.0);
Shadow fbo:
glActiveTexture(GL_TEXTURE7);
glEnable(GL_TEXTURE_2D);
glGenFramebuffers(1, &id);
glBindFramebuffer(GL_FRAMEBUFFER, id);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glGenTextures(1, &depth);
glBindTexture(GL_TEXTURE_2D, depth);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, resolution.x, resolution.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
glBindTexture(GL_TEXTURE_2D, 0);
Before rendering from light’s point of view (before()):
glBindFramebuffer(GL_FRAMEBUFFER, id);
gluLookAt(5, 5, 5, 4, 1, 4, 0, 1, 0); // tested
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
After rendering from light’s point of view (after():
GLfloat temp[16] = {
0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0};
bias = mat4(temp);
glGetFloatv(GL_PROJECTION_MATRIX, temp);
projection = mat4(temp);
glGetFloatv(GL_MODELVIEW_MATRIX, temp);
modelview = mat4(temp);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, depth);
glCullFace(GL_BACK);
Texture matrix:
texture = bias * projection * modelview * inv_cam_mv;
Main rendering algorithm:
- clear scene and reset matrices
- before()
- draw stuff from light’s point of view
- after()
- clear scene and reset matrices
- bind fbo with mrt
- navigate camera
- get inverse of camera’s modelview matrix, send it to shadow class and multiply with texture matrix (see above)
- draw stuff from cameras’s point of view
- unbind fbo with mrt
- bind lighting pass shader and send all needed data via uniforms (including shadow texture id and texture matrix)
- unbind shader
- swap buffers
Any suggestions appreciated
Just in case: I am using Intel 4500 (OGL 2.1/GLSL 1.2) and deferred rendering without shadows works well.
EDIT: Interesting…
gl_FragColor = vec4(vec3((shadow + 1.0) * color), 1.0);
gives also only black color.