使用另一个FBO渲染的Ping Pong渲染

时间:2017-05-24 14:11:32

标签: c++ opengl rendering opengl-3

我的目标是使用1个FBO(带有2个颜色附件)使用另一个FBO的渲染进行乒乓渲染。

我有一个带有5个颜色缓冲区的FBO。 然后,我想使用这5种颜色缓冲区的基础1创建一个乒乓球渲染。 所以,我用2缓冲区颜色创建另一个FBO,然后在for循环中,如果它是第一次迭代,我从另一个FBO生成我的纹理,否则我写入另一个颜色缓冲区。 最后我走出FBO并渲染到屏幕上。

FBOPingPong->bind();

    GLboolean first_iter = true;
    GLboolean ColToUse = 0;
    GLuint amount = 10;

    pingPongShader.Use();
    for (GLuint i = 0; i < amount; ++i) {
        glUniform1i(glGetUniformLocation(pingPongShader.Program, "mod"), ColToUse);
        glUniform1f(glGetUniformLocation(pingPongShader.Program, "screenWidth"), (float)w);
        glUniform1f(glGetUniformLocation(pingPongShader.Program, "screenHeight"), (float)h);
        if (first_iter) {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, FBO->getColorTextures(4)); //It's the texture from the other FBO I want to use
            glDrawBuffer(GL_COLOR_ATTACHMENT0 + 1);
            glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 1);

        }
        else {
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, FBOPingPong->getColorTextures(ColToUse?0:1));
            glDrawBuffer(GL_COLOR_ATTACHMENT0 + ColToUse ? 1 : 0);
            glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 1);

        }


        renderQuad(); // render the texture to a quad
        ColToUse = !ColToUse;
        if (first_iter) first_iter = false;
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    renderQuad();

和我的片段着色器

版本330核心

//I just want first to make the rendering darker and darker just to know if it works.
in vec2 TexCoords;

uniform sampler2D PingPongTex;
uniform bool mod;

out vec4 color;

vec3 position() {
    return texture(PingPongTex, TexCoords).xyz;
}

void main()
{
    color = vec4(position(), 1.0f);
}

但似乎FBO->getColorTextures(4)总是给我相同的结果,而不是我想要的颜色缓冲区。它为我提供了在FBO->getColorTextures(1)中呈现的法线的渲染。

而且我认为它只出现在for循环中,因为无论数量多少,结果都是一样的。

这个错误可能是愚蠢的,但是我无法解决这个问题:/

编辑: 使用glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 0);代替glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 1)制作了关于从其他FBO使用哪种颜色纹理的事情。这实际上是愚蠢的,因为我试图写一些不存在的东西。 但现在,只有一次通过后,屏幕变黑,没有任何显示。这可能是我使用的布尔问题。

EDIT2: 可能这样的事情会更好!

GLboolean first_iter = true;
GLuint amount = 2;

pingPongShader.Use();
for (GLuint i = 0; i < amount; ++i) {
    glUniform1i(glGetUniformLocation(pingPongShader.Program, "mod"), i%2);
    glUniform1f(glGetUniformLocation(pingPongShader.Program, "screenWidth"), (float)w);
    glUniform1f(glGetUniformLocation(pingPongShader.Program, "screenHeight"), (float)h);
    if (first_iter) { // pass 0
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, FBO->getColorTextures(4));
        glDrawBuffer(GL_COLOR_ATTACHMENT0 + 1);
        glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 0);
    }
    else {
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, FBOPingPong->getColorTextures(i%2); // For pass1 I need to read from texture 1
        glUniform1i(glGetUniformLocation(pingPongShader.Program, "PingPongTex"), 0);
        glDrawBuffer(GL_COLOR_ATTACHMENT0 + ((i+1)%2)); // And I need to write it in texture 0

    }


    renderQuad(); // render the texture to a quad
    if (first_iter) first_iter = false;

0 个答案:

没有答案