VBO, PbO and FBO (III)

Source: Internet
Author: User
VBO, PbO and FBO (III) (22:52:28)

Tags:FBO Framebuffer OpenGL Extension It

FBO, frame buffer object

On the OpenGL rendering assembly line, geometric data and texture data are converted and tested multiple times, and finally displayed on the screen in the form of two-dimensional pixels. The pixel location of the final display phase on the OpenGL assembly line is called frame cache. Frame cache can be regarded as a two-dimensional array or a storage area used by OpenGL. It includes: Color cache, deep cache, template cache, and cumulative cache.
Generally, the frame cache is generated and managed by the window system for OpenGL. This default frame cache is called"Window System Generation(Window-system-provided) frame cache.

In OpenGL extension, gl_ext_framebuffer_object provides another frame cache interface that cannot be displayed, namely, the frame cache object (FBO ). This frame cache is called"Application GenerationFrame cache, which is different from the frame cache generated by the window system. By using the frame cache object (FBO), OpenGL applications can output the displayed content to the "Application Generated" frame cache instead of the traditional "window system generated" frame cache. This process is all controlled by OpenGL.

Like the frame cache provided by the window system, FBO also has a set of cache regions for storing color, depth, and template (note that no data is accumulated. The region in FBO that stores the data is called the framebuffer-attached image ). It is completely controlled by OpenGL.

Cache associated images are classified into texture cache and rendering (Display) cache (renderbuffer ). If the image data of the texture object is associated with the frame cache, OpenGL performs the render to texture operation. If the image data of the rendered cache object is associated with the frame cache, OpenGL performs offline rendering (OFFSCREEN rendering ).

The rendered cache object is a new storage type defined in gl_ext_framebuffer_object. This is used to store a single 2-dimensional image during rendering. Figure 1 below describes the relationship between FBO, texture object, and rendering cache object.

                               Figure 1 Relationship Between FBO, texture object, and rendering cache object

 It can be seen that FBO has multiple color associations: (gl_color_attachment0_ext,..., gl_color_attachmentN_ Ext), a deep Association (gl_depth_attachment_ext) and a template Association (gl_stencil_attachment_ext ). You can query gl_max_color_attachments_ext to obtain the minimum number of color associations. FBO uses multiple color associations, so that multiple colors can be cached and rendered (drawn) to multiple FBO associated storage areas at the same time, that is, "multiple rendering targets" MRT (multiple render target ). Available for MRTGl_arb_draw_buffersComplete.

Note: FBO itself does not have an image data storage zone, and there are only multiple associations.

 

 FBO provides an efficient switching mechanism: the former frame cache is associated with the image from FBO, and a new correlated frame cache image is associated with FBO. The switching of the image associated with the frame cache by FBO is faster than that of FBO. UseGlframebuffertexture2dext ()To switch between two-dimensional texture objects.GlframebufferrenderbufferextYou can switch the rendering cache object.

----------------------------------------------

FBO generation

The FBO generation process is similar to the VBO generation process.

Glgenframebuffersext ()
void glGenFramebuffersEXT(GLsizei n, GLuint* ids) void glDeleteFramebuffersEXT(GLsizei n, const GLuint* ids)

Glgenframebuffersext requires two parameters: one is the number of cached frames to be generated, and the other is the address pointer used to store the cached IDs of frames. If the frame cache ID is 0, it indicates the default frame cache, that is, the frame cache generated by the window system.

Gldeleteframebuffersext can be used to delete FBO that are no longer in use.


Glbindframebufferext ()

Void glbindframebufferext (glenum target, gluint ID)

 Once FBO is generated, use glbindframebufferext to enable it. The first parameter target should be gl_framebuffer_ext, and the second parameter is the ID of FBO. If FBO is no longer used, call this function. The parameter ID is set to 0.

 -------------------------------

Rendering cache objects

 Rendering cache objects are newly added and used for offline rendering. It allows a scenario to be rendered directly to a rendering cache object instead of a texture object. Rendering cache objects are simple storage objects used to store a single image. The image is stored in a rendered internal format. It can be said that the rendering cache object stores the logic cache of OpenGL and does not have a format like texture.

_______________________________________________________________________________

Glgenrenderbuffersext () void glgenrenderbufferext (glsizei target, gluint * IDs) void gldeleterenderbuffersext (Glsizei N, const gluint * IDs)

Glgenrenderbuffersext: Once a rendering cache is generated,Returns a non-zero integer.

_____________________________________________________________________ Glbindrenderbufferext () void glbindrenderbufferext (glenum target, gluint ID)

Like other OpenGL objects, You need to bind rendering cache objects before using them. The target parameter should be gl_renderbuffer_ext.

__________________________________________________________________________________

Glrenderbufferstorageext () void glbindrenderbufferext (glenum target, gluint ID)

After a rendering object is generated, you also need to allocate space to store data. GlrenderbufferstorageextUsed to complete this task. The first parameter of this function must be gl_renderbuffer_ext. The second parameter can be used for colors (such as gl_rgb and gl_rgba), gl_depth_compoent, and gl_stencil_index ). The size of the image stored in the rendering cache is calculated based on the pixel width x height. It must not be greater than the constant gl_max_renderbuffer_size_ext; otherwise, the gl_invalid_value error will occur.

_________________________________________________________________________________

Glgetrenderbufferparameterivext () void glgetrenderbufferparamet Erivext (glenum target, glenum Param,
Glint * value );

 Use glgetrenderbufferparameterivext to obtain the attribute parameters of the current rendered object. Target must be gl_renderbuffer_ext. The second parameter Param is taken from the following constant, and value is the pointer to get the returned value.

 Gl_renderbuffer_width_ext
 Gl_renderbuffer_height_ext
 Gl_renderbuffer_internal_format_ext
 Gl_renderbuffer_red_size_ext
 Gl_renderbuffer_green_size_ext
 Gl_renderbuffer_blue_size_ext
 Gl_renderbuffer_alpha_size_ext
 Gl_renderbuffer_depth_size_ext
 Gl_renderbuffer_stencil_size_ext
----------------------------------------

Associate images with FBO

FBO itself does not store any images. We only associate the frame cache associated images (textures and rendering objects) with FBO. This mechanism allows us to quickly switch between frame caches and correlated images in FBO. In this way, unnecessary copying is unnecessary and memory consumption is reduced. For example, a texture can be associated with multiple fbrs so that its image storage area can share multiple fbrs.

   ______________________________________________________________________________________

    Associate a texture with FBOGlframebuffertexture2dex T (glenum target,
                                                 
Glenum attachmentpoint,
                                                 
Glenum texturetarget,
                                                 
Gluint textureid,
                                                 
Glint Level)

Glframebuffertexture2dexT can associate 2D texture images to FBO. The first target parameter must be gl_framebuffer_ext. The attachment parameter is the join point on FBO. There are multiple color correlated points (gl_color_attachment0_ext,..., gl_color_attachmentN_ Ext), a gl_depth_attachment_ext and gl_stencil_attachment_ext. The parameter texturetarget is gl_texture_2d when many values exist. The textureid parameter is the ID of the texture object. The parameter level is the level of the mipmap associated with the texture.

If textureid is set to 0, the associated texture is separated from FBO. If a texture object is deleted and associated with an FBO object, the texture image is automatically separated. If it is associated with multiple FBO, It is deleted only from the current FBO and will not be separated from other FBO.

______________________________________________________________________________________

    Associate a rendered cache image to FBOGlframebufferrenderbuffe RExt (glenum target,
                                                 
Glenum attachmentpoint,
                                                 
Glenum renderbuffertarget,
                                                 
Gluint renderbufferid)

Use glframebufferrenderbuffe to render cached imagesRExt to be associated with FBO. The target and attachmentpoint parameters and the previous function glframebuffertexture2dexT. The third function must be gl_renderbuffer_ext. The renderbufferid parameter is the ID of the rendered object.

If renderbufferid is set to 0, the rendered image in the current FBO will be separated from it. If the rendering object is also associated with FBO when it is deleted, it is automatically separated from the current FBO, but not from other associated FBO.

 ----------------------------------------

Check FBO status

 Before associating an image (texture or rendering buffer) with FBO, you must check the status of FBO before entertaining FBO operations. The function for getting the FBO status is glcheckframebufferstatus.Ext. If FBO is incomplete, the draw and read commands (glbegin (), glcopyteximage2d (), and so on) will fail.

Glenum glcheckframebufferstatus EXT (glenum target );

GlcheckframebufferstatusEXT verifies the parameters of the current frame cache and the associated images. Note that this function cannot be used in the center of the glbegin ()/glend () function pair. The target parameter must be gl_framebuffer_ext. It returns a non-zero status value. If FBO passes the check, the system returnsGl_framebuffer_complete_ext.

  FBO must meet the following conditions:

  • The width and height of the associated image cannot be 0;
  • If the associated image is associated with a color correlation point, the image must be in an internal color rendering format (such as gl_rgba,Gl_depth_component, gl_luminance, etc );
  • If the associated image is associated with gl_depth_attachment_ext, the image must be in an internal deep rendering format (gl_depth_component, gl_depth_component24_ext, etc ).
  • If the associated image is gl_stencil_attachment_ext, the image must be in the internal format that can be rendered by the template (gl_stencil_index, gl_stencil_index8_ext, etc ).
  • FBO must have at least one image association;
  • All images associated with the color correlated points must use the same internal format.

Note that even if all the conditions are met, the OpenGL driver may not support combinations of some internal formats and parameters. In this case, glcheckframebufferstatusext returns gl_framebuffer_unsupported_ext.

   ----------------------------------------

Example: rendering to texture

 Sample source code:Fbo.zip

Sometimes, you may need to generate dynamic textures, such as mirroring effects, dynamic cube/environment textures, shadows, and other effects. Dynamic texture

You can do this by drawing the rendered field shadows to the texture. The traditional method to render the image to the texture is to draw the scene to the normal frame cache, and then copy the cached image to the texture using glcopytexsubimage2d.

With FBO, we can directly plot the scene to the texture without the frame cache provided by the window system. In addition, we can reduce the process of data copying (from frame cache to texture.

The routine provides code that uses FBO and does not use FBO to render the texture, so as to compare their performance. In addition to the performance advantages, if the texture resolution is greater than the rendering window size in the FBO mode, the part outside the window area will be cropped, but FBO will not have this problem. You can generate a frame cache image that is much larger than the display window.

  The following code sets (initializes) the correlated image of FBO and frame cache before the rendering loop starts ). In the program, not only texture images are associated with FBO, but a rendering cache image is also associated with the FBO depth Association Point. We didn't actually use this deep cache, but FBO itself needs to use it for deep testing. If we do not associate a deep rendered image with FBO, we may not be able to perform a successful rendering because we cannot perform a deep test. If a template test is required during rendering, you can add the rendering image to the program and associate it with gl_stencil_attachment_ext.

The process of rendering to texture is the same as that of normal rendering. We only need to place the rendering destination in FBO.

Note,Glgeneratemipmapext ()As part of the FBO extension, it is used to explicitly generate the mipmap after the base level of the texture image is modified. IfGl_generate_mipmapIf it is set to gl_true, gltex {sub} image2d () and glcopytex {sub} image2d () will activate automatic mipmap generation. However, after the texture base level is modified, FBO does not automatically generate its mipmap. This is because FBO does not call glcopytex {sub} image2d () to modify the texture. Therefore, glgeneratemipmapext () must be explicitly called to generate the mipmap.

 

References

Http://www.gamedev.net/reference/articles/article2331.asp

Http://www.physdev.com/articles/

Http://www.builder.com.cn/2008/0703/963021.shtml

Http://www.songho.ca/opengl/gl_fbo.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.