模糊 OpenGL 中的深度缓冲区 - 如何访问片段着色器中的 mipmap 级别?

发布于 2024-09-06 08:18:01 字数 1591 浏览 11 评论 0原文

我试图通过模糊和模糊来模糊深度纹理在片段着色器中混合 mipmap 级别。

我有两个 frambuffer 对象:
1) 带有深度渲染对象的彩色帧缓冲区​​。
2) 附加了深度纹理的 z 帧缓冲区。

一旦我将场景渲染到颜色帧缓冲区​​对象,我就会将其传输到深度缓冲区对象,并且可以成功渲染它(输出是 GL_LUMINANCE 深度纹理)。

我可以通过在绘制深度缓冲区之前选择它来成功访问任何给定的 mipmap 级别,例如,我可以按如下方式渲染 mipmap 级别 3:

// FBO setup - all buffer objects created successfully and are complete and the color
// framebuffer has been rendered to (it has a depth renderbuffer attached), and no
// OpenGL errors are issued:
glBindFramebuffer(GL_READ_FRAMEBUFFER, _fbo_color);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_z);
glBlitFramebuffer(0,0,_w, _h, 0, 0, _w, _h, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

// This works:
// Select mipmap level 3
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 3);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);

draw_depth_texture_on_screen_aligned_quad();

// Reset mipmap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);

作为替代方案,我想将偏差参数添加到texture2D() GLSL 函数,或使用texture2DLod()并使用单个纹理采样器进行操作,但每当我选择超过0的级别时,似乎尚未生成mipmap:

// fragment shader (Both texture2DLod and texture2D(sampler, tcoord, bias)
// are behaving the same.
uniform sampler2D zbuffer;
uniform int mipmap_level;

void main()
{
    gl_FragColor = texture2DLod(zbuffer, gl_TexCoord[0].st, float(mipmap_level));
}

我不确定mipmapping如何与glBlitFramebuffer()一起使用,但是我的问题是设置程序的正确方法是什么,以便对texture2D/texture2DLod 的调用给出预期的结果?

谢谢,丹尼斯

I'm trying to blur a depth texture by blurring & blending mipmap levels in a fragment shader.

I have two frambuffer objects:
1) A color frambuffer with a depth renderobject attached.
2) A z framebuffer with a depth texture attached.

Once I render the scene to the color framebuffer object, I then blit to the depth buffer object, and can successfully render that (output is a GL_LUMINANCE depth texture).

I can successfully access any given mipmap level by selecting it prior to drawing the depth buffer, for example, I can render mipmap level 3 as follows:

// FBO setup - all buffer objects created successfully and are complete and the color
// framebuffer has been rendered to (it has a depth renderbuffer attached), and no
// OpenGL errors are issued:
glBindFramebuffer(GL_READ_FRAMEBUFFER, _fbo_color);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_z);
glBlitFramebuffer(0,0,_w, _h, 0, 0, _w, _h, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glGenerateMipmap(GL_TEXTURE_2D);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

// This works:
// Select mipmap level 3
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 3);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);

draw_depth_texture_on_screen_aligned_quad();

// Reset mipmap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 1000);

As an alternative, I'd like to add the bias parameter to the texture2D() GLSL function, or use texture2DLod() and operate with a single texture sampler, but whenever I choose a level over than 0, it appears that the mipmap hasn't been generated:

// fragment shader (Both texture2DLod and texture2D(sampler, tcoord, bias)
// are behaving the same.
uniform sampler2D zbuffer;
uniform int mipmap_level;

void main()
{
    gl_FragColor = texture2DLod(zbuffer, gl_TexCoord[0].st, float(mipmap_level));
}

I am not sure how the mipmapping works with the glBlitFramebuffer(), but my question is what is the proper way to setup the program such that calls made to texture2D/texture2DLod give the expected results?

Thanks, Dennis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好的 - 我想我已经明白了...我的深度缓冲区没有生成 mipmap 级别。我正在使用多重纹理,并且在渲染过程中,我为颜色帧缓冲区​​纹理激活纹理单元 0,为深度缓冲区纹理激活纹理单元 1。当我激活/绑定纹理时,我调用 glGenerateMipmap(GL_TEXTURE_2D),如下所示:

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, _color_texture);
glGenerateMipmap(GL_TEXTURE_2D);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, _zbuffer_texture);
glGenerateMipmap(GL_TEXTURE_2D);

完成此操作后,增加 texture2D(sampler, coord,bias) 中的偏差会按预期提供来自 mipmap 级别的片段。

Ok - I think I've got it... My depth buffer didn't have the mipmap levels generated. I'm using multi-texturing, and during rendering, I am activating texture unit 0 for the color framebuffer texture, and texture unit 1 for the depth buffer texture. When I activate/bind the textures, I call glGenerateMipmap(GL_TEXTURE_2D) as follows:

glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, _color_texture);
glGenerateMipmap(GL_TEXTURE_2D);

glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, _zbuffer_texture);
glGenerateMipmap(GL_TEXTURE_2D);

When this is done, increasing the bias in the texture2D(sampler, coord, bias) gives fragments from the mipmap level as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文