I'm implementing shadow mapping for my little 3d engine. However, there seem to be artifacts in the depth texture, which can be seen by using the texture and observing the shadows, or drawing the depth texture on a quad.
Here is what the depth texture looks like when drawn on a quad:
Yes, those little white rectangles shouldn't be there.
So, what could be causing it?
Opening the glfw window:
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindow(width, height,
8, 8, 8, 8,
24, 0, // 24 = depth buffer bits
GLFW_WINDOW);
Creating the depth framebuffer and texture:
glGenFramebuffers(1, &depth_framebuffer_);
glGenTextures(1, &depth_texture_);
glBindTexture(GL_TEXTURE_2D, depth_texture_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glBindFramebuffer(GL_FRAMEBUFFER, depth_framebuffer_);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth_texture_, 0);
glDrawBuffer(GL_NONE);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
exit(-1);
Rendering to the depth texture:
glBindFramebuffer(GL_FRAMEBUFFER, depth_framebuffer_);
glClear(GL_DEPTH_BUFFER_BIT);
render_objects();
glBindFramebuffer(GL_FRAMEBUFFER, 0);