The Framebuffer Object, or FBO, is the final Framebuffer Object on which the image is drawn, usually the default FBO, which is our screen.

In addition, you can also create your own FBO, which can be used as the carrier for drawing. When you have finished drawing on your OWN FBO, you can display the drawing content on the screen to achieve a double-buffered drawing.

FBO is actually composed of color attachment, depth attachment, template attachment, as the shader aspects (generally including color, depth, depth value) to draw the result store logical object.

Renderbuffer Object, or RBO, is an application-allocated 2D image buffer that can be used to assign and store depth and template values. It can also be used as an FBO depth or template attachment. Texture can also be used as an FBO color and depth attachment.

Frame buffering is related to render buffering and textures as follows:

Use an overview

To use the frame buffer, first create the corresponding frame buffer object, and then add the corresponding attachment to it, such as color attachment or depth attachment.

The next step is to switch to the frame buffer rendering and draw in the frame buffer. At this point, the drawing content is recorded on the color attachment or depth attachment added in the previous step.

Then switch to the screen buffer, at which point you can pull out the color or depth information recorded in the frame buffer and draw them onto the screen.

The use of frame buffering seems simple, but it is very common. Frame buffering can be used in some camera applications for beauty treatments, filters, stickers and other effects.

Using the step

Create FBO

Follow the steps above to create the FBO first.

        int[] framebuffers = new int[1];
        GLES20.glGenFramebuffers(1, framebuffers, 0);
Copy the code

Like most objects created in OpenGL, they’re represented by an int.

Now you can add some attachments to the FBO.

Binding texture

// Bind the texture to the color attachment
GLES20.glFramebufferTexture2D(
    GLES20.GL_FRAMEBUFFER, 
    GLES20.GL_COLOR_ATTACHMENT0, // as a color attachment
    GLES20.GL_TEXTURE_2D, 
    fboTextureId, 
    0);
Copy the code

The glFramebufferTexture2D function binds the texture to the FBO as an attachment, and you can choose from a variety of attachment options when binding.

  • GL_COLOR_ATTACHMENT0
    • Color in attachment
  • GL_DEPTH_ATTACHMENT
    • The depth of the attachment
  • GL_STENCIL_ATTACHMENT
    • The template in attachment

Of course, as a texture, there are only two options: color and depth.

If you are using OpenGL 3.x, you can also choose whether to bind read-only or write-only Fbos when binding them.

  • GL_READ_FRAMEBUFFER
    • Read-only FBO
  • GL_DRAW_FRAMEBUFFER
    • Just write FBO

If GL_FRAMEBUFFER is used, both read and write are allowed.

Bind render buffer

In addition to textures, you can also bind to render buffers.

First, create a render buffer object:

        int[] renderbuffers = new int[1];
        GLES20.glGenRenderbuffers(1, renderbuffers, 0);
Copy the code

After that, bind to the current render buffer object, initialize the render buffer object’s data store, and add it to the FBO.

// Bind to the current render buffer object
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderbufferId);
// Initialize the render datastore
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_ATTACHMENT, width, height);
// Add the render buffer to the FBO
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_RENDERBUFFER, renderbufferId);
Copy the code

Binding the render buffer object and initializing the data store is similar to creating a texture and initializing it.

        // Bind the texture
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        // Initialize texture data
        GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, width, height,
                0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_SHORT_5_6_5, null);
Copy the code

Apply colours to a drawing

When rendering, first render to FBO, then render to screen.

First, switch to FBO for rendering:

GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboId);
Copy the code

If the fboId parameter is 0, the default screen is switched to for drawing.

At this point, normal drawing can be carried out, by adding attachments to record the drawing information.

// Load the texture
int textureId = TextureHelper.loadTexture(context, R.drawable.lgq);
// Draw the texture onto FBO
mTextureRect.drawSelf(textureId);
Copy the code

When you draw a texture map on FBO, the color attachment attached to the FBO will record all the color content of the texture map.

In other words, the fBO-bound texture is used as a color attachment, it is already colored, and the color is the content we draw, and then we can continue to draw using the FBO-bound texture.

        // Switch to the screen buffer
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
        // Draw with the texture bound to FBO
        mTextureRect.drawSelf(fboTextureId);
Copy the code

After switching to the screen buffer and drawing directly with the fBO-bound texture, you will see the same effect as if you did not use FBO.

But the drawing inside is completely different.

In this article, you can refer to my Github project. Welcome Star.

Github.com/glumes/Andr…

reference

  1. Blog.csdn.net/cauchyweier…
  2. OpenGL ES 3.X Game Development volume 2

If you are also interested in OpenGL, please pay attention to wechat official account: [Paper Talk] and get the latest article push ~~~