Learn Android OpenGL es2.0 programming step by step (1)

Source: Internet
Author: User

(Welcome to join the android technology exchange QQ group: 209796692)


Create an OpenGL ES Environment

To use opengles painting in your Android app, you must create a view as a container. The most direct way isGLSurfaceViewAndGLSurfaceView.RendererA class is derived.GLSurfaceViewAs the container where OpenGL is drawn, the actual drawing action is inGLSurfaceView.Renderer.

UseGLSurfaceViewIt is almost the only way to integrate opengles into your application. It is the best choice for a full screen or near-full screen graphicsview. You can considerTextureView. If you are abnormal, you can useSurfaceViewCreateOpengles view.

This tutorial demonstrates how to complete a minimum-implemented opengles2.0 application.

Declare the use of opengles in manifest

To use opengles 2.0 API, you must add the following statement to your manifest:

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

If your application uses the texture compression function, you must declare what compression formats the device needs to support:

<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" /><supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />
Create an activity for opengles graphics

This activity is no different from the activity of other types of applications. To put it in a different view, you only need to put it in the Layout View of the activity.GLSurfaceView.

The following code demonstrates how to useGLSurfaceViewAs the main viewAcitivityMinimum Code implementation:

Public class opengles20 extends activity {private glsurfaceview mglview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // create a glsurfaceview instance and set it to the contentview of the activity. mglview = new myglsurfaceview (this); setcontentview (mglview );}}

Note:OpenGL ES 2.0 requires Android (API Level 8) and later versions.

Creates a glsurfaceview object.

GLSurfaceViewIn fact, do not need to do too much work, the actual drawing tasks areGLSurfaceView.RendererIn progress. SoGLSurfaceViewThere are very few codes in, and you can even use them directly.GLSurfaceViewHowever, do not. Because you need to extend this class to respond to touch events.

ExtensionGLSurfaceViewThe class can be written like this:

Class myglsurfaceview extends glsurfaceview {public myglsurfaceview (context) {super (context); // set Renderer to glsurfaceview setrenderer (New myrenderer ());}}

When using opengles 2.0, you mustGLSurfaceViewThe constructor calls another function, which indicates that you will use the 2.0 API:

// Create an OpenGL ES 2.0 contextseteglcontextclientversion (2 );

Another one can add yourGLSurfaceViewThe optional operation is to set the render mode to draw a view only when the data to be drawn changes. UseGLSurfaceView.RENDERMODE_WHEN_DIRTY:

// Viewsetrendermode (glsurfaceview. rendermode_when_dirty) is drawn only when the drawing data changes );

This setting prevents plotting.GLSurfaceViewUntil you callrequestRender().

Build a Renderer class

This type of control directionGLSurfaceView. It has three methods called by the Android system to calculateGLSurfaceViewWhat to draw and how to draw.

  • onSurfaceCreated()-It is called only once to set the opengles environment of the view.

  • onDrawFrame()-The view is called every time it is re-painted.

  • onSurfaceChanged()-If the number and shape of the view change, call the function, for example, when the portrait screen changes to a landscape screen.

The following is the most basic implementation of an opengles Renderer.GLSurfaceViewUpperPainted a gray background:

Public class mygl20renderer implements glsurfaceview. renderer {public void onsurfacecreated (gl10 unused, eglconfig config) {// set the background color gles?glclearcolor (0.5f, 0.5f, 0.5f, 1.0f);} public void ondrawframe (gl10 unused) {// re-paint the background color gles‑glclear (gles‑gl _ color_buffer_bit);} public void onsurfacechanged (gl10 unused, int width, int height) {gles‑glviewport (0, 0, width, height );}}

The above is what we need to do! The above Code creates a simple Android Application, which uses OpenGL to display a gray screen. However, this code does not do anything interesting, but is ready to use OpenGL for plotting.

Note:You don't understand why these methods all haveGL10But you are using openggles 2.0 APIs. This is actually done to make the android framework simple and compatible with various opengles versions.

If you are familiar with the opengles API, you can start plotting now. However, if you are familiar with it, continue to the next chapter.

Next lecture

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.