OpenGL ES 3.0 Fragment buffer Objects (FBO) detailed (i)

Source: Internet
Author: User

Fragment Operation diagram

  

This article describes how to buffer from the write frame and read the frame buffer.

Buffers (buffered)

OpenGL ES supports three types of buffering:

OpenGL ES

Color Buffer color buffer

Depth Buffer depth buffering

Stencil Buffer template buffering

Creating buffers

OpenGL ES is an interactive rendering system that, assuming the start of each frame, you want to initialize the default values for all the data in the buffer. Call the Glclear function to clear the buffer contents, and the parameter mask specifies the cleared buffer.

  

You may not want to purge each buffer, not to clear them at the same time. But if you want to clear all the buffers at once, you can get better performance by calling only one glclear at a time.

  

  

  

If you have more than one draw buffer in your frame buffer, you can also clear the specified buffer as follows:

  

To reduce the number of calls to the function, you can clear both the depth and template buffer contents, as follows:

  

Using masks to control write buffers

Generally you can control the operation of the buffer, which part can be written, which part is not writable. In the color buffer, the portion of the glcolormask specified color buffer can be updated. If mask is set to Gl_false, this component cannot be updated, and the default value is Gl_true. For the same depth

  

  

  

  

Read and write pixel-to-frame buffers

  

  

  

Multi-render target MRTs

MRTs allows applications to render multiple color buffers at the same time. Here is the process

1. Create a Frame Buffer object Framebuffer objects, using

Glgenframebuffers (1, &FBO); Creating a Frame Buffer object
Glbindframebuffer (Gl_framebuffer, FBO); Binds to the currently used frame buffer object.

  

2. Create a Texture object

Glgentextures (4,textureid); create

Glbindtexture (gl_texture_2d, textureid); binding
Glteximage2d (gl_texture_2d, 0, Gl_rgba,
Texturewidth, Textureheight,
0, Gl_rgba, Gl_unsigned_byte, NULL);
Set the filtering mode
Gltexparameteri (gl_texture_2d, Gl_texture_min_filter,
Gl_nearest);
Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter,
Gl_nearest);

  

3. Frame buffer object binding related textures.

Glframebuffertexture2d (Gl_draw_framebuffer,
Gl_color_attachment0,
gl_texture_2d,
Textureid, 0);

  

4. Specify additional colors for rendering

Gldrawbuffers (glsizei N, const glenum* BUFS)

  

For example, you can use 4 color outputs to create a FBO object

  

Const Glenum attachments[4] = {gl_color_attachment0,                                                   gl_color_attachment1,                                                   Gl_color_attachment2,                                                   Gl_color_attachment3                                                    4, attachments);

With gl_max_color_attachments you can query the maximum number of colors supported, the minimum number is 4.

5. Declaring and using the output of the shader

0  out   1 out 2 off 3 out VEC4 fragData3;

Full code

  

intINITFBO (Escontext *Escontext) {UserData*userdata = escontext->UserData; inti; Glint Defaultframebuffer=0; ConstGlenum attachments[4] ={gl_color_attachment0, gl_color_attachment1, Gl_color_attachment2, Gl_color_attachment3}   ; Glgetintegerv (gl_framebuffer_binding,&Defaultframebuffer); //Setup FBOGlgenframebuffers (1, &userData->FBO); Glbindframebuffer (Gl_framebuffer, UserData-FBO); //Setup Four output buffers and attach to FBOUserdata->textureheight = Userdata->texturewidth = -; Glgentextures (4, &userdata->colortexid[0] );  for(i =0; I <4; ++i) {glbindtexture (gl_texture_2d, UserData-Colortexid[i]); Glteximage2d (gl_texture_2d,0, Gl_rgba, UserData->texturewidth, userdata->Textureheight,0, Gl_rgba, Gl_unsigned_byte, NULL); //Set the filtering modeGltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest);      Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, gl_nearest); Glframebuffertexture2d (Gl_draw_framebuffer, Attachments[i], gl_texture_2d, UserData->colortexid[i],0 ); } gldrawbuffers (4, attachments); if(Gl_framebuffer_complete! =Glcheckframebufferstatus (Gl_framebuffer)) {      returnFALSE; }   //Restore the original framebufferGlbindframebuffer (Gl_framebuffer, Defaultframebuffer); returnTRUE;}

FSH Code

#version -esprecision Mediumpfloat; layout ( location=0) outvec4 fragdata0;layout ( location=1) outvec4 fragdata1;layout ( location=2) outvec4 fragdata2;layout ( location=3) outVEC4 fragData3;voidMain () {//First buffer would contain red colorFRAGDATA0 = VEC4 (1,0,0,1 ); //second buffer would contain green colorFragData1 = VEC4 (0,1,0,1 ); //third buffer would contain blue colorFragData2 = VEC4 (0,0,1,1 ); //fourth buffer would contain gray colorFRAGDATA3 = VEC4 (0.5,0.5,0.5,1 );}

OpenGL ES 3.0 Fragment buffer Objects (FBO) detailed (i)

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.