再谈 Framebuffers

https://learnopengl.com/Advanced-OpenGL/Framebuffers

so far we have used several types of screen buffers:
a color buffer for writing color values, a depth buffer to write depth information and finally
a stencil buffer that allows us to discard certain fragments based on some condition.
the combination of these buffers is called a framebuffer and is stored somewhere in memory.

opengl gives us the flexibility to define our own framebuffers and thus define our own color
and optionally a depth and stencil buffer.

the rendering operations we have done so far were all done on top of the render buffers attached to the default framebuffer.
the default framebuffer is created and configured when u create your window (GLFW does this for us).
by creating our own framebuffer we can get an additional means to render to.

the application fo framebuffers might not immediately make sense, but rendering your scene to a different framebuffer allows us to create mirrors in a scene or do cool post-processing effects for example.

1.creating a framebuffer

just like any other object in opengl we can create a framebuffer object (abbreviated to FBO) by using a function called glGenFramebuffers:

unsigned int fbo;
glGenFramebuffers(1, &fbo);

2. bind frambuffer

glBindFramebuffer(GL_FRAMEBUFFER, fbo);  

by binding to the GL_FRAMEBUFFER target all the next read and write framebuffer operations will
affect the currently bound framebuffer.
it is also possible to bind a framebuffer to a read or write target specifically by binding to GL_READ_FRAMEBUFFER or GL_DRAW_FRAMEBUFFER respectively. The framebuffer bound to GL_READ_FRAMEBUFFER is then used for all read operations like glReadPixels and the framebuffer bound to GL_DRAW_FRAMEBUFFER is used as the destination for rendering, clearing and other write operations. Most of the times you won’t need to make this distinction though and you generally bind to both with GL_FRAMEBUFFER.

unfortunately, we can not use our framebuffer yet because it is not complete.
for a framebuffer to be complete the following requirements have to be satisfied:

  1. We have to attach at least one buffer (color, depth or stencil buffer).
  2. There should be at least one color attachment.
  3. All attachments should be complete as well (reserved memory).
  4. Each buffer should have the same number of samples.

3. 检测绑定状态

if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
  // execute victory dance

all subsequent rendering operations will now render to the attachments of the currently bound framebuffer.
since our framebuffer is not the defautl framebuffer, the rendering commands will have no impact on the visual output of your window.
for this reason it is called off-screen rendering while rendering to a different framebuffer.
to make sure all rendering operations will have a visual impact on the main window we need to make the default framebuffer active again by binding to 0:

glBindFramebuffer(GL_FRAMEBUFFER, 0);   

4. 删除帧对象

glDeleteFramebuffers(1, &fbo);  

now before the completeness check is executed we need to attach one or more attachments to the framebuffer.
an attachment is a memory location that can act as a buffer for the framebuffer, think of it as an image. when creating an attachemnt we have two operations to take: textures or renderbuffer objects.

发布了618 篇原创文章 · 获赞 96 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/wodownload2/article/details/104439368