0
\$\begingroup\$

I'm currently trying to implement post processing into my project.

First I configure my framebuffer:

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

glGenTextures(1, &textureColorbuffer);
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, _width, _height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureColorbuffer, 0);

glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, _width, _height); // use a single renderbuffer object for both a depth AND stencil buffer.
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); // now actually attach it
                                                                                              // now that we actually created the framebuffer and added all attachments we want to check if it is actually complete now
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << endl;

float quadVertices[] = { // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
                         // positions   // texCoords
    -1.0f,  1.0f,  0.0f, 1.0f,
    -1.0f, -1.0f,  0.0f, 0.0f,
    1.0f, -1.0f,  1.0f, 0.0f,

    -1.0f,  1.0f,  0.0f, 1.0f,
    1.0f, -1.0f,  1.0f, 0.0f,
    1.0f,  1.0f,  1.0f, 1.0f
};

glGenVertexArrays(1, &quadVAO);
glGenBuffers(1, &quadVBO);
glBindVertexArray(quadVAO);
glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
glBindFramebuffer(GL_FRAMEBUFFER, 0);

Then I initialize the shader:

Vertex Shader:

    #version 430 core
    layout (location = 0) in vec2 aPos;
    layout (location = 1) in vec2 aTexCoords;
    out vec2 TexCoords;
    void main()
    {
        gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); 
        TexCoords = aTexCoords;
    } 

Fragment Shader:

    #version 430 core
    out vec4 FragColor;
    in vec2 TexCoords;
    uniform sampler2D screenTexture;
    void main()
    { 
        FragColor = vec4(vec3(1.0 - texture(screenTexture, TexCoords)),1.0);
    }

I then set the uniform for the texture:

    shader.get()->use();
    shader.get()->setUniform("screenTexture", 0);

Then before drawing my Scene I set to draw to the framebuffer:

    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glEnable(GL_DEPTH_TEST);

Then I draw my scene. After that I try to draw the framebuffer (after some post processing, just inverting the color to test)

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glDisable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    shader.get()->use();
    glBindVertexArray(quadVAO);
    glBindTexture(GL_TEXTURE_2D, textureColorbuffer);
    glDrawArrays(GL_TRIANGLES, 0, 6);

Unfortunately the whole screen is drawn full white. When I turn on wire-frame mode, i can see that 2 triangles were drawn, which is correct.. but I don't know why it doesn't work.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You missed to clear the color buffer and especially the depth buffer of the framebuffer (framebuffer) after the framebuffer is bound and before the scene is rendered. Since the depth test is enabled when the scene is rendered, the depth buffer has to be cleared right before:

// bind the frambuffer
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); 

// clear the color attachmnet (texture) and depth buffer
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // <------ you missed this

// enable the depth test
glEnable(GL_DEPTH_TEST);

// render the scene
// ....

// bind the defualt frambuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// celar the default frambuffer
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT );

// disable the depth test 
glDisable(GL_DEPTH_TEST);

// install current program and set the texture sampler uniform
shader.get()->use();
shader.get()->setUniform("screenTexture", 0);

// bind the texture
glBindTexture(GL_TEXTURE_2D, textureColorbuffer);

// draw a quad (2 triangles ) on to the entire viewport
glBindVertexArray(quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .