写入渲染缓冲区并使用单个渲染调用使用 OpenGL 显示



有没有办法同时渲染到渲染缓冲区和显示器,而不必多次调用绘制函数?我想要这样的东西:

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);

因此,我可以将绘制的内容与另一个渲染调用的纹理同时使用。我可以写

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

但这需要再次绘制场景,这会很慢。另一种解决方案是绘制帧缓冲,但我认为会有一个更智能的解决方案。

您可以使用glBlitFramebuffer()将帧缓冲区复制到另一个帧缓冲区。

像这样:

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);

应该做这个伎俩。

最新更新