1
\$\begingroup\$

I am trying to implement deferred rendering in my engine but things don't work like i thought they would do. Then, we have the next case:

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

    // Color texture
    glGenTextures(1, &m_colorTexture);
    glBindTexture(GL_TEXTURE_2D, m_colorTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colorTexture, 0);

    // Position texture
    glGenTextures(1, &m_positionTexture);
    glBindTexture(GL_TEXTURE_2D, m_positionTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, m_positionTexture, 0);

    // Normal texture
    glGenTextures(1, &m_normalTexture);
    glBindTexture(GL_TEXTURE_2D, m_normalTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, m_width, m_height, 0, GL_RGBA, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, m_normalTexture, 0);

    // Depth texture
    glGenTextures(1, &m_depthTexture);
    glBindTexture(GL_TEXTURE_2D, m_depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depthTexture, 0);

Nothing fancy, it is just a framebuffer creation with FrameBuffersTexture2Ds attached to the first 3 color channels and one last for the depth. Then, after drawing the scene, this frame buffers textures will have the normal, diffuse and position map. Like you will see in the next image.

enter image description here

For draw this, i used the next code:

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glBindFramebuffer(GL_READ_FRAMEBUFFER, m_fbo);

GLint HalfWidth = (GLint)(m_width / 2.0f);
GLint HalfHeight = (GLint)(m_height / 2.0f);

glReadBuffer(GL_COLOR_ATTACHMENT0);
glBlitFramebuffer(0, 0, m_width, m_height, 0, 0, HalfWidth, HalfHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

glReadBuffer(GL_COLOR_ATTACHMENT1);
glBlitFramebuffer(0, 0, m_width, m_height, 0, HalfHeight, HalfWidth, m_height, GL_COLOR_BUFFER_BIT, GL_LINEAR);

glReadBuffer(GL_COLOR_ATTACHMENT2);
glBlitFramebuffer(0, 0, m_width, m_height, HalfWidth, HalfHeight, m_width, m_height, GL_COLOR_BUFFER_BIT, GL_LINEAR);

But i don't want to read this channels like this, i want to use them in other shaders to combine them and draw on them(light volumes). Then i made a simple test, i took one of this channels (the diffuse one) and i used it with a basic shader to draw it in a quad which size is the screen size. In other words, i draw the frame buffer texture in the screen.

Or it was what i wanted to do, the result instead of being the diffuse map in fullscreen, it was this:

enter image description here

This is the code that i used to draw this... fail.

// We want to draw on the screen
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// We want to active a texture and bind it to the m_colorTexture which is the textureID of the diffuse map
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_colorTexture);

// We enable the shader
m_showTextureTech->Enable();

// This one set the sampler of the shader to 0, because we are using GL_TEXTURE0...
m_showTextureTech->SetTextureToDisplay();

// We bind a simple quad
glBindVertexArray(m_vertexArray);

glViewport(0, 0, 800, 600);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

// Simple clean up
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);

Then, why is it happening? I thought that i understood how this things work, but it looks like i don't.

\$\endgroup\$
2
  • \$\begingroup\$ Haven't you asked this question some time ago? \$\endgroup\$ – Bálint Oct 15 '16 at 14:38
  • \$\begingroup\$ @Bálint im making tests to solve my other question, and in the test process i found this issue. I mean, it doesn't suppose to work like this, and it can be the thing that is making the whole process of my last question to doesn't work properly. Then if i solve this i'll be closer to solve the problem that i exposed in my last question \$\endgroup\$ – Haruko Oct 15 '16 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.