OpenGL中的Frame Buffer、Depth Buffer、Color Buffer、Stencil Buffer

Frame Buffer

Frame Buffer: 是一塊buffer(即內存),存儲了一幀的buffer數據。

從數據結構的角度來看,此處的Frame Buffer並不是一個真正意義上的buffer,其存了一些指針,分別指向Depth Buffer、Color Buffer、Stencil Buffer、Texture、RBO(Render Buffer Object)等。

Wiki中的解釋:https://www.khronos.org/opengl/wiki/Framebuffer_Object

Framebuffer objects are a collection of attachments. To help explain lets explicitly define certain terminology.

Image
For the purposes of this article, an image is a single 2D array of pixels. It has a specific format for these pixels.
Layered Image
For the purposes of this article, a layered image is a sequence of images of a particular size and format. Layered images come from single mipmap levels of certain Texture types.
Texture
For the purposes of this article, a texture is an object that contains some number of images, as defined above. All of the images have the same format, but they do not have to have the same size (different mip-maps, for example). Textures can be accessed from Shaders via various methods.
Renderbuffer
A renderbuffer is an object that contains a single image. Renderbuffers cannot be accessed by Shaders in any way. The only way to work with a renderbuffer, besides creating it, is to put it into an FBO.
Attach
To connect one object to another. This term is used across all of OpenGL, but FBOs make the most use of the concept. Attachment is different from binding. Objects are bound to the context; objects are attached to one another.
Attachment point
A named location within a framebuffer object that a framebuffer-attachable image or layered image can be attached to. Attachment points restrict the general kind of Image Format for images attached to them.
Framebuffer-attachable image
Any image, as previously described, that can be attached to a framebuffer object.
Framebuffer-attachable layered image
Any layered image, as previously described, that can be attached to a framebuffer object.

Framebuffer Object Structure

As standard OpenGL Objects, FBOs have the usual glGenFramebuffers and glDeleteFramebuffers functions. As expected, it also has the usual glBindFramebuffer function, to bind an FBO to the context.

The target​ parameter for this object can take one of 3 values: GL_FRAMEBUFFER, GL_READ_FRAMEBUFFER, or GL_DRAW_FRAMEBUFFER. The last two allow you to bind an FBO so that reading commands (glReadPixels, etc) and writing commands (all rendering commands) can happen to two different framebuffers. The GL_FRAMEBUFFER binding target simply sets both the read and the write to the same FBO.

The default framebuffer has buffer names like GL_FRONT, GL_BACK, GL_AUXi, GL_ACCUM, and so forth. FBOs do not use these.

Instead, FBOs have a different set of image names. Each FBO image represents an attachment point, a location in the FBO where an image can be attached. FBOs have the following attachment points:

  • GL_COLOR_ATTACHMENTi: These are an implementation-dependent number of attachment points. You can query GL_MAX_COLOR_ATTACHMENTS to determine the number of color attachments that an implementation will allow. The minimum value for this is 8, so you are guaranteed to be able to have at least color attachments 0-7. These attachment points can only have images bound to them with color-renderable formats. All compressed image formats are not color-renderable, and thus cannot be attached to an FBO.
  • GL_DEPTH_ATTACHMENT: This attachment point can only have images with depth formats bound to it. The image attached becomes the Depth Buffer for the FBO. Note that if no depth image is attached, Depth Testing will be disabled when rendering to this FBO.
  • GL_STENCIL_ATTACHMENT: This attachment point can only have images with stencil formats bound to it. The image attached becomes the stencil buffer for the FBO.
  • GL_DEPTH_STENCIL_ATTACHMENT: This is shorthand for "both depth and stencil". The image attached becomes both the depth and stencil buffers.
Note: If you use GL_DEPTH_STENCIL_ATTACHMENT, you should use a packed depth-stencil internal format for the texture or renderbuffer you are attaching.

Depth Buffer

Depth Buffer:深度緩存區。

工作原理如下:

  1. 在圖形渲染過程中,對於每個像素,渲染管線會計算出其在觀察空間中的深度值。
  2. 當一個像素需要進行渲染時,會將其深度值與深度緩衝區中對應像素的深度值進行比較。
  3. 如果當前像素的深度值小於深度緩衝區中的深度值,說明當前像素位於前面,它將被認爲是可見的,並將其深度值寫入深度緩衝區。
  4. 如果當前像素的深度值大於深度緩衝區中的深度值,說明當前像素被前面的物體遮擋,它將被認爲是不可見的,深度緩衝區中的深度值不做更新。

深度緩衝區的主要作用是進行深度測試(Depth Test),即通過比較像素的深度值來確定像素的可見性。它可以有效解決遮擋關係,避免對被遮擋像素進行不必要的渲染,從而提高圖形渲染的效率。

Color Buffer

Color Buffer:顏色緩存區

OpenGL中的顏色緩衝區(Color Buffer)是一個用於存儲渲染結果的內存區域。當我們通過OpenGL渲染場景時,渲染的結果會被存儲到顏色緩衝區中。通常情況下,顏色緩衝區是一個二維數組,每個元素代表屏幕上的一個像素點,存儲着該像素點的顏色信息。

在OpenGL中,顏色緩衝區可以包括一個或多個顏色緩衝區。例如,我們可以創建一個雙緩衝區(Double Buffer),其中包括前緩衝區和後緩衝區,用於實現平滑的動畫效果。

當我們完成一次渲染後,OpenGL會將顏色緩衝區中的像素值發送到幀緩衝區(Frame Buffer),並在屏幕上顯示出來。我們也可以使用OpenGL的一些API函數對顏色緩衝區進行讀寫操作,例如修改像素的顏色、清空顏色緩衝區等。

 

Stencil Buffer

Stencil Buffer:模板緩存區

爲屏幕上的每一個像素保存一個8位的無符號整數,跟模板緩衝區進行比較並決定是否保留像素稱爲模板測試

模板測試發生在透明度測試之後,深度測試之前

img

模板緩衝區默認值爲0(測試得到),並且我推測模板緩衝區每幀執行完會進行一個刷新

要加模板測試,就在Shader的Pass開頭寫Stencil{ }結構體。如果每個Pass都用,則可以提到外面。

 

參考連接:

Stencil Buffer:https://www.cnblogs.com/FlyingZiming/p/12937642.html

Stencil Buffer:https://www.jianshu.com/p/f79f0da90103

Depth Buffer:https://zhuanlan.zhihu.com/p/638266570

Color Buffer: https://www.cnblogs.com/errorman/p/17222536.html

Frame Buffer: https://blog.csdn.net/alexhu2010q/article/details/101436270

OpenGL Frame Buffer: https://zhuanlan.zhihu.com/p/631073748

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章