1
votes

I'm trying to implement deferred rendering in my engine but I'm having some problems. It seems that there is some static in the triangles being drawn.

Here is the drawing code:

    // GEOMETRY PASS

    gbuffer->BindForWriting(); // glBind(GL_DRAW_FRAMEBUFFER, fbo);
    window->Clear(); // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable(GL_DEPTH_TEST); // Before it was glDisable(GL_DEPTH_TEST); (I was testing)

    ... // Render all objects (they are rendering well when not using deferred rendering)

    // LIGHTING PASS

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    window->Clear(); // Clear buffer again (same thing above)

    gbuffer->BindForReading(); // glBind(GL_READ_FRAMEBUFFER, fbo);

    GLsizei HalfWidth = (GLsizei)(window->GetSize().x / 2.0f);
    GLsizei HalfHeight = (GLsizei)(window->GetSize().y / 2.0f); // Get half the size of the window

    gbuffer->SetReadBuffer(GBUFFER_POSITION); // glReadBuffer(GL_COLOR_ATTACHMENT0);
    glBlitFramebuffer(0, 0, window->GetSize().x, window->GetSize().y, 0, 0, HalfWidth, HalfHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

    gbuffer->SetReadBuffer(GBUFFER_DIFFUSE); // glReadBuffer(GL_COLOR_ATTACHMENT1);
    glBlitFramebuffer(0, 0, window->GetSize().x, window->GetSize().y, 0, HalfHeight, HalfWidth, window->GetSize().y, GL_COLOR_BUFFER_BIT, GL_LINEAR);

    gbuffer->SetReadBuffer(GBUFFER_NORMAL); // glReadBuffer(GL_COLOR_ATTACHMENT2);
    glBlitFramebuffer(0, 0, window->GetSize().x, window->GetSize().y, HalfWidth, HalfHeight, window->GetSize().x, window->GetSize().y, GL_COLOR_BUFFER_BIT, GL_LINEAR);

    gbuffer->SetReadBuffer(GBUFFER_TEXCOORD); // glReadBuffer(GL_COLOR_ATTACHMENT3);
    glBlitFramebuffer(0, 0, window->GetSize().x, window->GetSize().y, HalfWidth, 0, window->GetSize().x, HalfHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

(upleft is diffuse, upright is normals, downleft is position and downright is texture coords)

Image showing static in triangles

EDIT: New screenshot after i enabled GL_DEPTH_TEST:

enter image description here

EDIT 2 Here is the GBuffer setup:

glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);

glGenTextures(4, textures);
glGenTextures(1, &depthTexture);

for (int i = 0; i < 4; i++) {
    glBindTexture(GL_TEXTURE_2D, textures[i]);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, windowSize.x, windowSize.y, 0, GL_RGB, GL_FLOAT, NULL);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i], 0);
}

glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, windowSize.x, windowSize.y, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);

GLenum drawBuffers[] = {
    GL_COLOR_ATTACHMENT0,
    GL_COLOR_ATTACHMENT1,
    GL_COLOR_ATTACHMENT2,
    GL_COLOR_ATTACHMENT3
};
glDrawBuffers(4, drawBuffers);

GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE) {
    // PRINT ERROR TO LOG ( No errors here i've already checked)
    return;
}

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

EDIT 3 I'm following this tutorial if it helps in anything

1
Is there a reason for disabling the depth-test before writing to the gbuffers? The results look to me as if triangles from the back are drawn over triangles from the front side.BDL
Oh i just had it on before, but i tried putting it off so i could check if it worked that way but the result was pratically the sameRicardo Antunes
I will edit the answer and update with a new imageRicardo Antunes
Can you also add the gbuffer setup?BDL
Oh sure, i've already updaten the questionRicardo Antunes

1 Answers

1
votes

I forgot to disable GL_BLEND before drawing the objects in the buffer.

Now it's working fine.

glDisable(GL_BLEND);
(Deferred rendering...)
glEnable(GL_BLEND);