Android OpenGL ES-Frame Buffer Object

Source: Internet
Author: User

For the concept of Frame Buffer object, see the previous Android OpenGL ES development tutorial (23): FrameBuffer.

Compared with 2D images, if Frame Buffer is mapped to a two-dimensional graphic environment, it is a 2D memory array space. By default, it is the display space of the screen. You can also create an Offscreen memory space, in this case, Frame Buffer can be a two-dimensional array. Each element in the array represents a pixel color.

For a 3D image, in addition to a two-dimensional array (Color Buffer) that represents the Color, a two-dimensional array (Depth Buffer) or an array (stencel Buffer) are also required ), therefore, the Frame Buffer in OpenGL is a set of the preceding Color Buffer, Depth Buffer, and stencel Buffer. If the mobile phone has a GPU, the default Frame Buffer is also the 3D display area.

With Opengl ES extension support, applications can also create Frame Buffer objects in memory (not used for Screen Display ). Through the FrameBuffer object created by this application, the OpenGL application can redirect the Image Display output to this non-Screen Display in FrameBuffer object, similar to the Offscreen technology commonly used in 2D drawing.

Like the default FrameBuffer on the screen, the FrameBuffer object created by the application is also composed of a set of Color Buffer, Depth Buffer, and stencel Buffer (optional. These buffers can be called FrameBuffer-attachable images in FrameBuffer objects. FrameBuffer defines some access points that can be used to connect these Buffer arrays.

OpenGL ES defines two types of FrameBuffer-attachable images: Texture and renderbuffer. In simple terms, Texture can be understood as a Color buffer or a 2D image, and render buffer corresponds to depth buffer.

Represents the relationship between Texture, Renderbuffer object and Frame Buffer object:

However, after linking Texture and Render Buffer to the access points (ATTACHMENT) of FrameBuffer, the output results of all OpenGL drawing commands will be written to these memory Buffer, rather than the default screen display.

The OpenGL ES extensions supported by different Android devices may be different. Therefore, if you need to use an application to create a FrameBuffer object, you must check whether the mobile phone supports Framebuffer extensions. In this example

Private boolean checkifcontextsuppsexsextension (GL10 gl, String extension) {String extensions = "" + gl. glGetString (GL10.GL _ EXTENSIONS) + ""; // The extensions string is padded with spaces between extensions, but not // necessarily at the beginning or end.
For simplicity, add spaces at the // beginning and end of the extensions string and the extension string. // This means we can avoid special-case checks for the first or last // extension, as well as avoid special-case checks when an extension name // is
Same as the first part of another extension name. return extensions. indexOf ("" + extension + "")> = 0 ;}

The basic steps for using FrameBuffer are as follows:

1. Use glGenFramebuffersOES to create a FrameBuffer object

Int [] framebuffers = new int [1]; gl11ep. glGenFramebuffersOES (1, framebuffers, 0); framebuffer = framebuffers [0];

2. After the FrameBuffer is created, you must bind the FrameBuffer to OpenGL,

Gl11ep. glBindFramebufferOES (GL11ExtensionPack. GL_FRAMEBUFFER_OES, framebuffer );

The first parameter type must be GL_FRAMEBUFFER_OES, and the second parameter is the ID of FrameBuffer. If the Id is 0, it indicates binding to the default screen FrameBuffer.

After binding, subsequent OpenGL rendering results will be directed to FrameBuffer, rather than displayed on the screen.

3. Use glGenRenderbuffersOES to create the RenderBuffer

Int depthbuffer; int [] renderbuffers = new int [1]; gl11ep. glGenRenderbuffersOES (1, renderbuffers, 0); depthbuffer = renderbuffers [0];

4. Similar to FrameBuffer, you must bind the RenderBuffer object to the OpengL library.

Gl11ep. glBindRenderbufferOES (GL11ExtensionPack. GL_RENDERBUFFER_OES, depthbuffer );

5. allocate memory to renderBuffer.

The created renderBuffer itself does not contain memory space, so it must be allocated with memory space, which is achieved through glRenderbufferStorageOES.

Gl11ep. glRenderbufferStorageOES (GL11ExtensionPack. GL_RENDERBUFFER_OES, GL11ExtensionPack. GL_DEPTH_COMPONENT16, width, height );

The second parameter is the internal format type of the created RenderBuffer.

6. Create a Texture object. For details, see Android ApiDemos example parsing (200): Graphics-> OpenGL ES-> Textured Triangle.

7. After creating FrameBuffer, Texture, and renderBuffer objects, link Texture, RenderBuffer object, and Attachment Point in FrameBuffer.

Link Texture object

Gl11ep. glFramebufferTexture2DOES (GL11ExtensionPack. GL_FRAMEBUFFER_OES, GL11ExtensionPack. GL_COLOR_ATTACHMENT0_OES, GL10.GL _ TEXTURE_2D, targetTextureId, 0 );

Link the renderBuffer object

L11ep. glFramebufferRenderbufferOES (GL11ExtensionPack. GL_FRAMEBUFFER_OES, GL11ExtensionPack. GL_DEPTH_ATTACHMENT_OES, GL11ExtensionPack. GL_RENDERBUFFER_OES, depthbuffer );

Let's take a look at the onDrawFrame method in this example.

Private Static final Boolean success = false; Public void ondrawframe (gl10 GL) {checkglerror (GL); If (mcontextsupportsframebufferobject) {gl11extensionpack gl11ep = (gl11extensionpack) GL; If (fail) {drawoffscreenimage (GL,
Msurfacewidth, msurfaceheight);} else {gl11ep. glbindframebufferoes (gl11extensionpack. gl_framebuffer_oes, mframebuffer); drawoffscreenimage (GL, mframebufferwidth, mframebufferheight); gl11ep. glbindframebufferoes (gl11extensionpack. gl_framebuffer_oes, 0); drawonscreen (GL,
Msurfacewidth, msurfaceheight);} else {// current context doesn't support frame buffer objects. // indicate this by drawing a red background. GL. glclearcolor (1, 0, 0); GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit );}}

In this example, a cube is drawn in the created FrameBuffer. The drawing instruction is the same as the parsing of the example Android ApiDemos (203): Graphics-> OpenGL ES-> GLSurfaceView. However, the displayed results are stored in mFramebuffer (you can set DEBUG_RENDER_OFFSCREEN_ONSCREEN to True to see the content of FrameBuffer ). Then, the memory displayed by mFramebuffer is used as the Triangle material to draw a Triangle.

Related Article

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.