我正在为我的小 3d 引擎实现阴影映射。但是,深度纹理中似乎存在伪影,可以通过使用纹理并观察阴影,或者在四边形上绘制深度纹理来看到。
这是在四边形上绘制深度纹理时的样子:
是的,那些白色的小矩形不应该在那里。
那么,可能是什么原因造成的呢?
打开glfw窗口:
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);
创建深度帧缓冲区和纹理:
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);
渲染到深度纹理:
glBindFramebuffer(GL_FRAMEBUFFER, depth_framebuffer_);
glClear(GL_DEPTH_BUFFER_BIT);
render_objects();
glBindFramebuffer(GL_FRAMEBUFFER, 0);