再谈glBindRenderbuffer

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

步骤:
创建渲染缓冲对象-》绑定渲染缓冲对象-》为渲染缓冲对象分配内存-》帧缓冲和渲染缓冲绑定

Renderbuffer objects were introduced to OpenGL after textures
as a possible type of framebuffer attachments,
so textures were the only attachments used in the good old days.

just like a texture image, a renderbuffer object is an actural buffer e.g. an array of bytes, integers, pixels or whatever.

a renderbuffer object has the added advantage though that
it stores its data in OpenGL’s native rendering format making it optimized for off-screen rendering to a framebuffer. 用于离屏渲染

renderbuffer objects store all the render data directly into their buffer
without any conversions to texture-specific formats,
thus, making them faster as a writeable storage medium.
however, renderbuffer objects are generally wirte-only,
thus u can not read from them (like with texture-access).
it is possible to read from them via glReadPixels though that returns specified area of pixels
from the currently bound framebuffer, but not directly from the attachment itself.

优点:
becuase their data is already in its native format they are quite fast when writing data or simply copying their data to other buffers.
operations like switching buffers are thus quite fast when using renderbuffer objects.

glfwSwapBuffers function we have been using at the end of each render iteration might as well be implemented with renderbuffer objects:
we simply write to a renderbuffer image, and swap to the other one at the end.
renderbuffer objects are perfect for these kind of operations.

  1. 创建renderbuffer object对象方法:
unsigned int rbo;
glGenRenderbuffers(1, &rbo);
  1. 绑定渲染缓冲对象:
glBindRenderbuffer(GL_RENDERBUFFER, rbo);

since renderbuffer objects are generally write-only they are often used as depth and stencil attachments,
since most of the time we do not really need to read values from the depth and stencil buffers but still care about depth and stencil testing.
we need the depth and stencil values for testing, but do not need to sample these values
so a renderbuffer object suits this perfectly.
when we are not sampling from these buffers,
a renderbuffer object is generally preferred since it’s more optimized.

  1. 为renderbuffer object对象分配内存,分配多大用来干什么的
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 800, 600);

我们可以怎么理解呢?
GL_RENDERBUFFER——说明这是一个renderbuffer的对象相关的存储
GL_DEPTH24_STENCIL8——说明这块内存使用了存储深度和模板的
800和600——说明buffer的大小是多大

creating a renderbuffer object is similar to textures objects, the difference being that this object is
specifically designed to be used as an image,
instead of a general purpose data buffer like a texture.
here we have chosen the GL_DEPTH24_STENCIL8
as the intenal format, which holds both the depth and stencil with 24 and 8 bits respectively.

  1. 最后是和帧对象关联起来:
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); 

何时用render buffer object:

Renderbuffer objects could provide some optimizations in your framebuffer projects, but it is important to realize when to use renderbuffer objects and when to use textures. The general rule is that

  1. if you never need to sample data from a specific buffer, it is wise to use a renderbuffer object for that specific buffer.
  2. If you need to someday sample data from a specific buffer like colors or depth values, you should use a texture attachment instead. Performance-wise it doesn’t have an enormous impact though.

实际举例:

 //帧缓冲区对象
 glGenFramebuffers(1& fboId); 
 glBindFramebuffer(GL_FRAMEBUFFER,fboId); 


 //1.渲染缓冲区作为颜色缓冲区
 glGenRenderbuffers(1& colorBuffer); 
 glBindRenderbuffer(GL_RENDERBUFFER,colorBuffer); 
 glRenderbufferStorage(GL_RENDERBUFFER,GL_RGBA,width,height); 
 glBindRenderbuffer(GL_RENDERBUFFER,0; 
 //将渲染缓冲区作为颜色缓冲区附加到fbo 
 glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_RENDERBUFFER,colorBuffer); 

 

 //2.将缓冲区渲染为深度缓冲区
 glGenRenderbuffers(1& depthBuffer); 
 glBindRenderbuffer(GL_RENDERBUFFER,depthBuffer); 
 glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,width,height); 
 glBindRenderbuffer(GL_RENDERBUFFER,0; 
 //将渲染缓冲区作为深度缓冲区附加到fbo 
 glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,depthBuffer); 

 
 //3.将缓冲区渲染为模板缓冲区
 glGenRenderbuffers(1& stencilBuffer); 
 glBindRenderbuffer(GL_RENDERBUFFER,stencilBuffer); 
 glRenderbufferStorage(GL_RENDERBUFFER,GL_STENCIL_INDEX,width,height); 
 glBindRenderbuffer(GL_RENDERBUFFER,0; 
 //将渲染缓冲区附加到fbo作为模板缓冲区
 glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_STENCIL_BUFFER,GL_RENDERBUFFER,stencilBuffer); 
发布了610 篇原创文章 · 获赞 96 · 访问量 33万+

猜你喜欢

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