OpenGL Frame Cache Objects (Fbo:frame buffer object)

Source: Internet
Author: User
Tags error code

OpenGL Frame Bufferobject (FBO)

Overview:

In the OpenGL rendering pipeline, the geometric data and textures are transformed and tested several times, and are displayed on the screen in two-dimensional pixels. The final rendering destination for OpenGL pipelines is called frame caching (framebuffer). Frame buffering is a collection of storage areas used by two-dimensional arrays and OpenG: Color caching, depth caching, template caching, and cumulative caching. In general, frame caching is generated and managed entirely by the window system and is used by OpenGL. This default frame cache is known as the Frame cache for Window System generation (window-system-provided).

In OpenGL extensions, Gl_ext_framebuffer_object provides an interface to create additional frame cache objects that cannot be displayed. This frame buffer becomes the application frame cache (Application-createdframebuffer) to distinguish it from the default frame cache for Window System generation. By using the Frame cache object (FBO), OpenGL can output the display to the referencing frame cache object rather than the traditional window system generation frame cache. Moreover, it is completely subject to OpenGL control.

Similar to the frame cache provided by the window System, a FBO also contains areas that store color, depth, and template data. (Note: no cumulative cache) We refer to these logical caches in FBO as "frame cache associative Images", which are two-dimensional array pixels that can be associated with a frame cache object.

There are two types of frame Cache association images: Texture images (texture images) and render cache images (Renderbuffer images). If the image data of the texture object is associated to the frame cache, OpenGL performs the render to texture (render to texture) operation. If the rendering cached image data is associated to the frame cache, OpenGL performs an offline rendering (offscreen rendering).

It is mentioned here that the render cache object is a new type of storage defined in the Gl_ext_framebuffer_object extension. It is used to store a single two-dimensional image during rendering.

The following illustration shows the relationship between a frame cache object, a texture object, and a render cache object. A lot of texture objects or render cache objects can be associated with a frame cache object through association points.


There are multiple color association points (Gl_color_attachment0_ext,..., gl_color_attachmentn_ext) in a Frame cache object, a depth association point (gl_depth_attachment_ext), and a template Association point (gl_stencil_attachment_ext). There is at least one color association point in each FBO, and the number is related to the entity video card. You can query the maximum number of color association points by Gl_max_color_attachments_ext. FBO has multiple color association points because it allows you to convert colors to render to multiple FBO associated areas at the same time. This "multiple render target" (multiple RENDERTARGETS,MRT) can be implemented through Gl_arb_draw_buffers extensions. It should be noted that FBO itself does not have any image storage, only a number of association points.

FBO provides an efficient switching mechanism, separating the previous frame cache associated image from the FBO, and then associating the new frame cache associated image with the FBO. Switching between frame cache associated images is much faster than switching between FBO. The FBO provides glframebuffertexture2dext () to switch between the 2D texture objects and glframebufferrenderbufferext () to toggle the render cache object.

Create FBO

Creating FBO and generating Vbo is similar.

Glgenframebuffersext ()

Void Gelgenframebuffersext (Glsizei n,gluint* IDs);

void Gldeleteframebuffersext (Glsizei n, const gluint* IDs);

Glgenframebuffersext () requires two parameters: the first is the number of frame caches to create, and the second is a pointer to a variable or array that stores one or more IDs. It returns the ID of the unused FBO. An ID of 0 indicates the default frame cache, which is the frame cache provided by the window System.

When FBO is no longer in use, FBO can be deleted by calling Gldeleteframebuffersext ().

Glbindframebufferext ()

Once a FBO is created, it must be bound before it is used.

void Glbindframebufferext (Glenum target, gluint ID)

The first parameter target should be Gl_framebuffer_ext, and the second parameter is the ID number of the FBO. Once FBO is bound, all OpenGL operations that follow will have an effect on the currently bound FBO. An ID number of 0 indicates the default frame cache, which is the default window-provided frame cache. Therefore, setting the ID number to 0 in Glbindframebufferext () can unbind the current FBO.

Render Cache Object (Renderbuffer object)

In addition, rendering caching is a new introduction for offline rendering. It allows a scene to be rendered directly into a render cache object, rather than rendering it to a texture object. A render cache object is a data storage area that is used to store a single image. The image is stored in a rendered internal format. It is used to store OpenGL logical caches that do not have a related texture format, such as template caching or depth caching.

Glgenrenderbuffersext ()

void Glgenrenderbuffersext (Glsizei N, gluint* IDs)

void Gldeleterenderbuffersext (Glsizei n, const gluint* IDs)

Once a render cache is created, it returns a Non-zero positive integer. An ID of 0 is an OpenGL retention value.

Glbindrenderbufferext ()

void Glbindrenderbufferext (Glenum target, gluint ID)

As with other objects in OpenGL, you must bind the current render cache object before referencing the render cache. His target parameter should be gl_renderbuffer_ext.

Glrenderbufferstorageext ()

void Glrenderbufferstorageext (Glenum target, Glenum Internalformat,

Glsizei width, Glsizei height)

When a render cache is created, it does not have any data storage area, so we have to allocate space for him. This can be done by using Glrenderbufferstorageext (). The first argument must be gl_renderbuffer_ext. The second argument can be for color (gl_rgb,gl_rgba,etc.), for Depth (gl_depth_component), or for templates (Gl_stencil_index). Width and height are pixel dimensions of the rendering cache image.

Width and height must be smaller than gl_max_renderbuffer_size_ext, otherwise a gl_unvalid_value error will be generated.

Glgetrenderbufferparameterivext ()

void Glgetrenderbufferparameterivext (Glenum target, glenum param,glint* value);

We can also get some parameters for the currently bound render cache object. Target should be gl_renderbuffer_ext, and the second parameter is the name of the parameter to be obtained. The last one is a pointer to the integer that stores the return value. The name of the variable that renders the cache is as follows:

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

associating an image with a FBO

The FBO itself does not have an image storage area. We must correlate the frame cache associated image (texture or render object) to FBO. This mechanism allows FBO to quickly toggle (detach and correlate) frame cache associated images. Switching Frame Cache Association images is much faster than switching between FBO. Moreover, it saves unnecessary data copying and memory consumption. For example, a texture can be associated with multiple FBO, and the image store can be shared by multiple FBO.

associating a 2D texture image with a FBO

Glframebuffertexture2dext (Glenum Target,

Glenumattachmentpoint,

Glenum Texturetarget,

Gluint Textureid,

Glint level)

Glframebuffertexture2dext () associates a texture image with a FBO. The first argument must be Gl_framebuffer_ext, and the second parameter is the associated point of the associated texture image. The third parameter texturetarget is gl_texture_2d in most cases. The fourth parameter is the ID number of the texture object. The last parameter is the mipmap level of the texture to be associated

If the parameter Textureid is set to 0, the texture image will be detached from the FBO. If the texture object is deleted when it is still associated with the FBO, the texture object will automatically detach from the FBO of the current help. However, if it is associated with multiple FBO and then deleted, it will only be detached from the FBO of the binding and not from other unbound FBO.

associating the render cache object to the FBO

void Glframebufferrenderbufferext (Glenum target,

Glenum Attachmentpoint,

Glenum Renderbuffertarget,

Gluint Renderbufferid)

The render cache image can be associated by calling Glframebufferrenderbufferext (). The first two parameters are the same as Glframebuffertexture2dext (). The third parameter can only be Gl_renderbuffer_ext, and the last parameter is the ID number of the render cache object.

If the parameter renderbufferid is set to 0, the render cache image will be detached from the FBO Association point. If the render cache image is deleted when it is still associated with the FBO, the texture object will automatically detach from the FBO of the current binding and not from the other unbound FBO.

Check FBO status

Once the associated image (texture and render cache) is associated to the FBO, you must check the state of the FBO before performing the FBO operation, which can be achieved by calling Glcheckframebufferstatusext (). If this fbobuilding is complete, any drawing and reading commands (Glbegin (), glcopyteximage2d (), etc) will fail.

Glenum Glcheckframebufferstatusext (glenum target)

Glcheckframebufferstatusext () checks the associated image and frame cache parameters for the current frame cache. This function cannot be invoked between Glbegin ()/glend (). The target parameter must be gl_framebuffer_ext. It returns a value other than 0. If all requirements and guidelines are met, it returns to Gl_framebuffer_complete_ext. Otherwise, the return of a related error code tells us which rule is not satisfied.

The FBO integrity guidelines are:

(1) Frame cache associated image width and height must be non-zero.

(2) If an image is associated with a color association point, the image must have a color-rendering internal format (Gl_rgba, gl_depth_component, gl_luminance, etc).

(3) If a picture is associated with the Gl_depth_attachment_ext, the image must have a depth-rendered internal format (Gl_depth_component,gl_depth_component24_ext, etc).

(4) If a picture is associated with the Gl_stencil_attachment_ext, the image must have an internal format (Gl_stencil_index,gl_stencil_index8_ext, etc) that the template can render.

(5) FBO has at least one image associated with it.

(6) The miniature image associated with the FBO must have the same width and height.

(7) All images associated to a color association point must have the same internal format.

Note: Even if all of the above conditions are met, your OpenGL driver may not support some combination of formatting and parameters. If a particular implementation is not supported by OpenGL drivers, then Glcheckframebufferstatusext () returns GL_FRAMEBUFFER_UNSUPPORTED_EXT.


example: rendering to Textures

Source code Download: http://www.songho.ca/opengl/gl_fbo.html

Includes rendering to texture, rendering only to depth caching, and using template caching to render objects ' outlines.

Sometimes, you need to generate dynamic textures. A more common example is the effect of specular reflection, dynamic environment mapping, and shading. Dynamic textures can be implemented by rendering the scene to a texture. A traditional way of rendering to textures is to draw the scene to the normal frame cache, and then call Glcopytexsubimage2d () to copy the frame cache image to the texture.

With FBO, we were able to render the scene directly to the texture, so we didn't have to use the frame cache provided by the window System. Also, we are able to remove additional copies of the data (from the frame cache to the texture);.

This demo implements rendering to textures using FBO and not using FBO two, and compares performance differences. In addition to getting a performance boost, there is another advantage to using FBO. In traditional rendering to texture mode (without FBO), if the texture resolution is larger than the size of the render window, parts that exceed the window area are clipped. Then, using FBO would not have this problem. You can generate a frame cache rendering image larger than the display window.

The following code initializes the FBO and frame cache associated images before the rendering loop begins. Note that only one texture image is associated with the FBO, however, a depth rendering image is associated to the FBO depth Association point. We're not actually using this depth cache, but FBO itself needs it for depth testing. If we do not associate this depth-rendering image with the FBO, it is incorrect to render the output result for lack of depth testing. If template testing is also necessary during FBO rendering, you also need to associate additional rendering images with Gl_stencil_attachment_ext.[CPP]  View plain copy print?  create a texture object   gluint textureid;   glgentextures (1,  &textureid);   glbindtexture (Gl_texture_2d, textureid);   GlTexParameterf (GL_ Texture_2d, gl_texture_mag_filter, gl_linear);   Gltexparameterf (GL_TEXTURE_2D, GL_ Texture_min_filter, gl_linear_mipmap_linear);   Gltexparameterf (GL_TEXTURE_2D, GL_TEXTURE_ Wrap_s, gl_clamp_to_edge);   Gltexparameterf (gl_texture_2d, gl_texture_wrap_t, gl_ Clamp_to_edge);   Gltexparameteri (gl_texture_2d, gl_generate_mipmap, gl_true);  //  automatic mipmap   glteximage2d (Gl_texture_2d, 0, gl_rgba8, texture_width,  TEXTURE_HEIGHT, 0,  

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.