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)
EDIT: New screenshot after i enabled GL_DEPTH_TEST:
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
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);