LearnOpenGL16-frame buffer

Transfer from
https://blog.csdn.net/qq_36696486/article/details/104425880
https://learnopengl-cn.github.io/04%20Advanced%20OpenGL/05%20Framebuffers/#_2

The concept of frame buffer

Up to the present position, a variety of caches (data) have been introduced

  1. Color buffer for writing color values
  2. Depth buffer for writing depth information
  3. Allows us to discard the template buffer of specific fragments based on some conditions

The combination of these buffers is called the frame buffer (Framebuffer)

The role of frame buffer

  • Used to save rendering result data
  • Used to implement custom after-effects, which can achieve very cool after-effects

Custom frame buffer

Create an object:

unsigned int fbo;
//初始化值
glGenFramebuffers(1, &fbo);
//将fbo绑定到GL_FRAMEBUFFER上,所有的读取和写入帧缓冲的操作将会影响当前绑定的帧缓冲
glBindFramebuffer(GL_FRAMEBUFFER, fbo);

Optional parameters of glBindFramebuffer:

  • GL_READ_FRAMEBUFFER:
    Bind a frame buffer to the read target. The frame buffer bound to GL_READ_FRAMEBUFFER will be used in all read operations like glReadPixels
  • GL_DRAW_FRAMEBUFFER:
    Bind a frame buffer to the write target. The frame buffer bound to GL_DRAW_FRAMEBUFFER will be used as the target of write operations such as rendering and clearing.

In most cases, you don't need to distinguish between them, usually GL_FRAMEBUFFER is used to bind to two.

Our frame buffer cannot be used yet, because it is not yet complete (Complete). A complete frame buffer needs to meet the following conditions:

  • Attach at least one buffer (color, depth or stencil buffer).
  • At least one color attachment (Attachment).
  • All attachments must be complete (memory reserved).
  • Each buffer should have the same number of samples.

Function to check integrity:

glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE

Delete framebuffer object

glDeleteFramebuffers(1, &fbo);

Allocate space for the frame buffer

There are two ways to allocate space: texture or Renderbuffer Object.

  • The texture saves the content in the frame buffer to a texture map, which is convenient for reading and writing, and can be passed into the shader for post-processing.
  • Rendering buffer object
    Put the data in the frame buffer in the rendering buffer object, because the object is the original data, the rendering buffer object is a real buffer, that is, a series of bytes, integers, pixels, etc. The additional benefit of the rendering buffer object is that it will store the data in the OpenGL native rendering format, which can quickly transfer the value of an array. The disadvantage is that it is only writeable and not readable. It is suitable for processing depth buffers and template buffers.

Render to texture:

  1. Create texture, use graphics library stb_image.h
  2. Attach the texture to the frame buffer
unsigned int framebuffer;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
// 生成纹理
unsigned int texColorBuffer;
glGenTextures(1, &texColorBuffer);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 800, 600, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

// 将它附加到当前绑定的帧缓冲对象,颜色缓存
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);  


glFrameBufferTexture2D has the following parameters:

  1. target: The target of the frame buffer (drawing, reading or both)
  2. attachment: The type of attachment we want to attach. We are currently attaching a color attachment. Note that the 0 at the end means that we can attach multiple color attachments. We will mention it in a later tutorial.
  3. textarget: the type of texture you wish to attach
  4. texture: the texture itself to be attached
  5. level: The level of the multi-level fade texture. We will leave it at 0.

Render to Renderbuffer Object:

  1. Create Renderbuffer Object
  2. Allocate space
unsigned int rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo); 
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);  
glBindRenderbuffer(GL_RENDERBUFFER, 0);
//深度和模板缓存
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);

Guess you like

Origin blog.csdn.net/J_avaSmallWhite/article/details/113846716