Writing to renderbuffer AND display with OpenGL with single rendercall

Viewed 134

Is there a way to render to both, a renderbuffer and to the display without have to call multiple times the draw function? I wanted someting like this:

glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE2D, tbo, 0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
GLenum* attachments[2] = {GL_COLOR_ATTACHMENT0, ...} //with the second I want to render to screen which would have the same value like the first
glDrawBuffer(2, attachments);

glDrawElements(GL_TRIANGLES, 12*3, GL_UNSIGNED_INT, 0);

So I can use what I drawed in the same time as a texture for another rendercall. I could just write

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDrawElements(GL_TRIANGLES, 12*3, GL_UNSIGNED_INT, 0);

but this would require to draw the scene again, which would be slow. Another solution would be just to draw the framebuffer, but I thought there would be a smarter solution.

1 Answers

You can copy a frame buffer to another one using glBlitFramebuffer().

Something like:

glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(
    0, 0, fboWidth, fboHeight,
    0, 0, screenWidth, screenHeight,
    GL_COLOR_BUFFER_BIT, GL_NEAREST);

should do the trick.

Related