Ping-Pong Buffer OpenGL

时间:2018-01-27 16:22:24

标签: opengl glsl shader

我正在尝试实现一个DoF着色器,而且我一直在使用高斯着色器时遇到问题。 我正在尝试为我的高斯模糊着色器实现乒乓缓冲区,但我无法理解为什么它不起作用。

这是Ping-Pong的循环

bool b = true;
for (int i = 0; i < 10; i++) {

    glBindFramebuffer(GL_FRAMEBUFFER, dof_FBO);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, b ? tmp_texture : blur_texture, 0);

    glViewport(0, 0, g_Width, g_Height);

    glUseProgram(gaussProgram);

    glUniformMatrix4fv(locUniformMVPM2, 1, GL_FALSE, &mvp2[0][0]);
    glUniform1i(locColorTexture, b ? 3 : 4);
    glUniform2f(glGetUniformLocation(gaussProgram, "pixelSize"), 1.0 / g_Width, 1.0 / g_Height);
    glUniform2fv(glGetUniformLocation(gaussProgram, "direccion"), 1, b ? &x[0] : &y[0]);

    drawQuad();
    b = !b;
}

tmp_texture链接到GL_TEXTURE4,blur_texture链接到GL_TEXTURE3

这就是我初始化FBO的方式

glGenFramebuffers(1, &dof_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, dof_FBO);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, dpth_texture);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color_texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, dof_texture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, blur_texture, 0);

GLenum DrawBuffers[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, DrawBuffers);

这是我的着色器的代码。

#version330
uniform sampler2D uColorTexture;

in vec2 vTexCoord;

layout (location = 2) out vec4 fBlur;

uniform vec2 pixelSize;
uniform vec2 direccion;
uniform int kernel = 4;
const float values[9] = {0.05,0.09,0.11,0.15,0.2,0.15,0.11,0.09,0.05};
void main(){
    vec2 tc = vTexCoord;
    vec4 color = vec4(0);
    vec2 pos = -kernel * direccion * pixelSize;

    for(int i = 0; i < 1 + kernel * 2; i++){
        color += texture2D(uColorTexture, pos+tc) * values[i];
        pos += direccion * pixelSize;
    }

    fBlur = color;  
}

我已经尝试了一切,没有结果希望有人能看到我不能做到的事情。

主要问题是屏幕上显示的结果意味着它只进行了一次乒乓循环迭代。

感谢阅读。

由于您的FBO附加了深度缓冲区,您必须在绘制四边形之前禁用深度测试或清除深度缓冲区。

1 个答案:

答案 0 :(得分:0)

由于您的FBO附加了深度缓冲区,您必须在绘制四边形之前禁用深度测试,或清除深度缓冲区。