How can I draw two textures using the same FBO?
As seen bellow, I have a initDesktop()
where I initialize my both textures (previously loaded using SOIL).
On my other class, I want to execute the drawDesktop()
which will draw my background rectangle with a texture, and a footer rectangle...
I only see the footer... What I am missing here?
EDITED
void Desktop::initDesktop ()
{
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_READ_FRAMEBUFFER, fboId);
//now with GL_COLOR_ATTACHMENT0
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, m_background.getId(), 0);
//now with GL_COLOR_ATTACHMENT1
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT1,GL_TEXTURE_2D, m_footer.getId(), 0);
}
void Desktop::drawDesktop ()
{
drawBackground ();
drawFooter ();
}
void Desktop::drawBackground ()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glReadBuffer(GL_COLOR_ATTACHMENT0); //<-- ?
glBlitFramebuffer(0, 0, 1200, 800, 0, 0, 1200, 800, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
void Desktop::drawFooter ()
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glReadBuffer(GL_COLOR_ATTACHMENT1); //<-- ?
glBlitFramebuffer(0, 0, 1200, 100, 0, 0, 1200, 100, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
Thanks!
You are trying to attach two textures to the same attachment point:
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, m_background.getId(), 0);
glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, m_footer.getId(), 0);
the latter call will override the former one. So you have the footer texture bound to this FBO. To fix this, you can attach both textures to different attachment points, like GL_COLOR_ATTACHMENT0
and GL_COLOR_ATTACHMENT1
and can use
glReadBuffer()
to select which one glBlitFramebuffer()
will read from. Don't confuse the currently selected read and draw buffers with the currenlty bound read and rsw framebuffers. These are different concepts. Drawing will always go into the currently selected drawbuffer(s) of the currently bound GL_DRAW_FRAMEBUFFER
, and read operations on the color buffer source the pixel data from the currently selected read framebuffer of the currently bound GL_READ_FRAMEBUFFER
.
Another option would be to just attach one texture at a time, and switch between them. Or even using two FBOs. But switching the read buffer alone is probably the cheapest.