Imagine 3 different balls, each one stored on three different textures. There is no background, so I can draw and overlay (on the front buffer) all of them.
The problem is: how can I make this draw on a fourth texture, keeping the NO background?
I'm dealing with multiple layers and want to save all of them in a single png file. Saving what's in the GL_COLOR_ATTACHMENT0 buffer won't work great, because it'll only save what's being seen. Instead, there is a need of saving huge images, which can't be seen (in it's original scale) at one time.
Layer fourthLayer(0, 0, w, h, 1.0f); // creates a Layer object, which contains a fbo and a texture
glBindFramebuffer(GL_FRAMEBUFFER, fourthLayer.fbo); // selects the fourth layer's fbo
render(layers, n); // draw all the three textures
glBindFramebuffer(GL_FRAMEBUFFER, 0);
std::vector<unsigned char> pixels(4 * w * h);
glBindTexture(GL_TEXTURE_2D, fourthLayer.texId); // selects the fourth layer's texture, so I can read it's bytes
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
glBindTexture(GL_TEXTURE_2D, 0);
lodepng::encode(filename.append(".png").data(), pixels, w, h); // save