One of the Android OpenGL ES drawing tutorials: Building an OpenGL ES environment

Source: Internet
Author: User

In order to use OpenGL ES drawing in an Android app, you first have to create a view container. One of the simplest methods is to implement Glsurfaceview and Glsurfaceview.renderer. Glsurfaceview A view container to display OpenGL-drawn graphics, glsurfaceview.renderer to control the drawing of graphics inside the Glsurfaceview. For more information, please refer to the OpenGL ES Development wizard.
Glsurfaceview is a way to use OpenGL ES graphics for Applications, which is a good choice for full-screen or near-full-screen graphical view. But if developers want to display OpenGL ES graphics in a small area of layout, they should use Textureview. It is also possible to use Surfaceview to create an OpenGL ES view, but this requires a lot of extra code.
This tutorial describes how to implement the basic parts of Glsurfaceview and Glsurfaceview.renderer in a simple application activity.

    1. Declaring the use of OpenGL ES in manifest
In order to use the OpenGL ES 2.0 API in your app, you must add the following declaration in manifest:
<uses-feature android:glesversion= "0x00020000" android:required= "true"/>
If texture compression is used in your app, you must declare the application's supported compression format to ensure that the app is installed only on compatible devices.
<supports-gl-texture android:name= "Gl_oes_compressed_etc1_rgb8_texture"/>
<supports-gl-texture android:name= "Gl_oes_compressed_paletted_texture"/>

For more information on texture compression formats, refer to the OpenGL Development Guide.

2. Create an activity that displays OpenGL ES graphics

Android apps that display OpenGL ES, like other apps with a user interface, have activity, and one of the main differences with other apps is the content placed in the activity's layout. Textview,button and ListView may be used in most applications, and you can use Glsurfaceview in applications that use OpenGL ES.
The following code shows an activity that uses the Glsurface view as its main display:
Public
Class Opengles20activity extends Activity {

private Glsurfaceview Mglview;

@Override
Public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);

//Create a Glsurfaceview instance and set it
//
as the Contentview for this Activity.
Mglview = new Myglsurfaceview (this);
Setcontentview (mglview);
    }
}
Note: OpenGL ES 2.0 requires Android 2.2 or higher, so make sure the app's target API meets the requirements.

3. Create a Glsurfaceview object
Glsurfaceview is a dedicated view used to draw OpenGL ES Graphics, which itself does not do much work. The real drawing work is controlled by glsurfaceview.renderer, in fact, Glsurfaceview is the code used to draw the OpenGL object is so few, you might try to skip inheriting it, just create an unmodified Glsurfaceview instance, but don't do it, you need to This class captures the touch event, which is described in the responding to touch events tutorial.
The key code for Glsurfaceview is very small, so a faster implementation is to create an anonymous inner class only in the activity:
class Myglsurfaceview extends Glsurfaceview {

Public
Myglsurfaceview (context context) {
super (context);

//Set the Renderer for drawing
on the Glsurfaceview
Setrenderer (New Myrenderer ());
    }
}
When using OpenGL ES 2.0, you must add a declaration in the Glsurfaceview constructor that specifies that you want to use the 2.0 API:
//Create an OpenGL ES 2.0 context
Seteglcontextclientversion (2);
Another option to add to the Glsurfaceview implementation is to set the rendering mode to draw the view only when the drawing data changes, using the Glsurfaceview.rendermode_when_dirty settings:
//Render The view only if there is a change in the drawing data
Setrendermode (glsurfaceview.rendermode_when_dirty);
This option avoids glsurfaceview being redrawn and redraws only when you call Requestrender (), which increases the efficiency of your application.


4. Create a renderer class

In an application that uses OpenGL ES, Glsurfaceview.renderer or renderer is the place where things get interesting, and this class controls what Glsurfaceview above, and there are three ways in renderer that are called by the Android system to determine what to draw in Glsurfaceview and how to draw it:

onsurfacecreated ()-will be called once when setting the OpenGL ES environment for the view
Ondrawframe ()-called every time the view is redrawn
Onsurfacechanged ()-Called when the geometry of the view changes, such as when the device rotates the screen.

Here is a simple OpenGL ES render implementation that draws a gray background in Glsurfaceview:


Public
class Myglrenderer implements Glsurfaceview.renderer {

Public
void onsurfacecreated (GL10 unused, EGLConfig config) {
//Set the background frame color
Gles20.glclearcolor (0.0f, 0.0f, 0.0f, 1.0f);
    }

Public
void Ondrawframe (GL10 unused) {
//Redraw background color
gles20.glclear (gles20.gl_color_buffer_bit);
    }

Public
void Onsurfacechanged (GL10 unused, int width, int height) {
gles20.glviewport (0, 0, width, height);
    }

}


That's all! The example code above creates a simple Android application that uses OpenGL to display a gray screen. Although the code does not do something interesting, by creating these classes, you have laid the groundwork needed to draw graphical elements using OpenGL.
Note: When you use OpenGL ES 2.0API, you may wonder why these methods have a GL10 parameter. In fact, the purpose of this is to allow the Android framework to be easily compatible with all versions of OpenGL ES.
If you compare the API to OpenGL ES, you should now be able to set up an OpenGL ES environment and start drawing, but if you need more OpenGL help, learn the next tutorial to get more help.

One of the Android OpenGL ES drawing tutorials: Building an OpenGL ES environment

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.