在OpenGL中模糊深度缓冲区-如何在片段着色器中访问Mipmap级别? [英] Blurring the depth buffer in OpenGL - how to access mipmap levels in a fragment shader?

查看:259
本文介绍了在OpenGL中模糊深度缓冲区-如何在片段着色器中访问Mipmap级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过模糊化&来模糊深度纹理.在片段着色器中混合mipmap级别.

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

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

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

将场景渲染到彩色帧缓冲区​​对象后,我便将其拖到深度缓冲区对象,然后可以成功渲染(输出是GL_LUMINANCE深度纹理).

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).

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

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);

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

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));
}

我不确定mipmapping如何与glBlitFramebuffer()一起工作,但是我的问题是设置程序的正确方法是什么,以便对Texture2D/texture2DLod的调用能够获得预期的结果?

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?

谢谢丹尼斯

推荐答案

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

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);

完成此操作后,增加 texture2D(采样器,坐标,偏差)中的偏差会按预期提供来自mipmap级别的片段.

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

这篇关于在OpenGL中模糊深度缓冲区-如何在片段着色器中访问Mipmap级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆