为什么我无法使用 FBO 从深度纹理获取值?
我无法从 FBO 的深度纹理获取深度值。程序的工作流程如下所示:
设置
1.制作pass1 FBO(颜色纹理+深度纹理)
2.进行pass2 RBO(颜色渲染缓冲区+深度渲染缓冲区)
渲染
1.仅顶点处理(pass1)
2. 从pass0 FBO的深度纹理中获取深度值
3. 使用先前的深度值进行顶点和片段处理
Setup.1 代码(创建 pass0 fbo)
glActiveTexture(GL_TEXTURE0);
glGenTextures(2, &pass1_tex);
glBindTexture(GL_TEXTURE_2D, pass1_tex[0]);
glTexImage2D(GL_TEXTURE_2D, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pass1_tex[1]);
glTexImage2D(GL_TEXTURE_2D, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
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);
glGenFramebuffers(1, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMEMNT0, GL_TEXTURE_2D< pass1_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMEMNT, GL_TEXTURE_2D< pass1_tex[0], 0);
为了在渲染中获取深度值,我做了如下操作:
glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
// vertex processing..
glActiveTexture(GL_TEXTURE1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, &debug_buf[0]);
但是,debug_buf 数组中只有 1.0 或 0.0。当然,所有物体的距离(NDC)都在1.0和-1.0之间,gluPerspective()中的近和远值为1.0和2.0。
为什么我无法从 FBO 的深度纹理获取正确的深度值?
I can't get depth values from FBO's depth texture.. Program's workflow is like below :
Setup
1. Make pass1 FBO (color texture + depth texture)
2. Make pass2 RBO (color renderbuffer + depth renderbuffer)
Rendering
1. only vertex processing (pass1)
2. get depth values from pass0 FBO's depth texture
3. vertex and fragment processing using a previous depth values
Setup.1 code (create pass0 fbo)
glActiveTexture(GL_TEXTURE0);
glGenTextures(2, &pass1_tex);
glBindTexture(GL_TEXTURE_2D, pass1_tex[0]);
glTexImage2D(GL_TEXTURE_2D, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, pass1_tex[1]);
glTexImage2D(GL_TEXTURE_2D, GL_DEPTH_COMPONENT, w, h, 0, GL_DEPTH_COMPONENT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
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);
glGenFramebuffers(1, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMEMNT0, GL_TEXTURE_2D< pass1_tex[0], 0);
glBindFramebuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMEMNT, GL_TEXTURE_2D< pass1_tex[0], 0);
And to get depth values in rendering, I did like below :
glBindFramebuffer(GL_FRAMEBUFFER, pass1_fbo);
// vertex processing..
glActiveTexture(GL_TEXTURE1);
glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, &debug_buf[0]);
But, there are only 1.0 or 0.0 in debug_buf array. Of course, all objects are between 1.0 and -1.0 distance (NDC), a near and far value is 1.0 and 2.0 in gluPerspective().
Why can't I get proper depth values from FBO's depth texture?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在本文中找到问题的答案。使用 GL_COMPARE_R_TO_TEXTURE 时,对深度纹理进行采样会返回深度纹理值与参考值(即插值的 r 纹理坐标)的比较结果。您想要做的是指定以下纹理参数:
You can find the answer to your problem in this article. When using GL_COMPARE_R_TO_TEXTURE, sampling the depth texture returns a comparison result of the depth texture values against a reference value (which is the interpolated r texture coordinate). What you want to do is to specify the following texture parameters instead:
您使用了颜色纹理 pass1_tex[0] 作为颜色和深度缓冲区
you used the color texture, pass1_tex[0] for both the color and depth buffers