Usually, we use an FBO for rendering without problems, such as:
// Store the current frame buffer
glGenTextures(1, &offscreen_fbo_texture);
glBindTexture(GL_TEXTURE_2D, offscreen_fbo_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);
//Depth buffer
glGenRenderbuffers(1, &offscreen_fbo_depth);
glBindRenderbuffer(GL_RENDERBUFFER, offscreen_fbo_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
//Framebuffer to link everything together
glGenFramebuffers(1, &offscreen_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, offscreen_fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, offscreen_fbo_texture, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, offscreen_fbo_depth);
if ((status = glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE) {
fprintf(stderr, "glCheckFramebufferStatus returned error %d", status);
return -1;
}
// Bind back original frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// To draw to offscreen fbo, bind offscreen_fbo
glBindFramebuffer(GL_FRAMEBUFFER, offscreen_fbo);
// DrawA()
// DrawB()
// ....
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Draw offscreen_fbo_texture
Draw(offscreen_fbo_texture)
But now I want to nest another FBO inside one FBO, but report 0x506 error GL_INVALID_FRAMEBUFFER_OPERATION. The specific process is as follows:
- Initialize and bind an FBO, temporarily name it FBO_A, texture Tex_A
- Drawing and rendering function FuncA:
2.1. Initialize and bind another FBO, named FBO_B for the time being, texture Tex_B
2.2, Draw1()
Draw2()
2.3, glBindFramebuffer(GL_FRAMEBUFFER, 0);
2.4. Render Tex_B after processing - glBindFramebuffer(GL_FRAMEBUFFER, 0);
- Render Tex_A
How to achieve similar FBO? Or is there any other way to achieve this?