Android OpenGL ES: First Program

Source: Internet
Author: User

1. Overview

OpenGL is a desktop environment for drawing, rendering three-dimensional graphics API.

OpenGL ES is OpenGL in the Android environment.

OpenGL in Android needs to be rendered in glsurfaceview, rendering control functions in Glsurfaceview.renderer. Next you'll learn how to create your first OpenGL program


2. Configure Androidmanifest

(1) declaring OpenGL ES API
<uses-feature android:glesversion= "0x00020000" android:required= "true"/>
(2) If the texture is used in the app, you need to declare the texture format
<supports-gl-texture android:name= "Gl_oes_compressed_etc1_rgb8_texture"/>
<supports-gl-texture android:name= "Gl_oes_compressed_paletted_texture"/>


<manifest xmlns:android= "http://schemas.android.com/apk/res/android"    package= " Com.example.android.firstglsurfaceview ">    <uses-feature android:glesversion=" 0x00020000 "/>    < USES-SDK android:targetsdkversion= "One"/>    <application            android:label= "@string/app_name" >        <activity android:name= "firstglactivity"                android:theme= "@android: Style/theme.notitlebar.fullscreen"                Android:launchmode= "Singletask"                android:configchanges= "Orientation|keyboardhidden" >            < intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android:name= "Android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </ Application></manifest>

3. Create an OpenGL Project3.1 Creating an activity

You only need to set a glsurfaceview in the activity.

FirstGlsurfaceview inherits from Glsurfaceview.

public class Firstglactivity extends Activity {    private firstglsurfaceview mglview;    @Override    protected void onCreate (Bundle icicle) {        super.oncreate (icicle);        Mglview = new Firstglsurfaceview (Getapplication ());        Setcontentview (Mglview);    }    @Override    protected void OnPause () {        super.onpause ();        Mglview.onpause ();    }    @Override    protected void Onresume () {        super.onresume ();        Mglview.onresume ();    }}

3.2 Creating Glsurfaceview

This section needs to declare the OpenGL version, set the renderer

Class Firstglsurfaceview extends Glsurfaceview {    private final firstglrenderer mrenderer;    Public Myglsurfaceview (Context context) {        super (context);        Create an OpenGL ES 2.0 context        seteglcontextclientversion (2);        Mrenderer = new Myglrenderer ();        Set the Renderer for drawing on the Glsurfaceview        setrenderer (mrenderer);    }}

3.3 Creating a render

Glsurfaceview.renderer is the most important part of OpenGL ES, setting up the OpenGL environment and plotting OpenGL is done in renderer.

Render implements the Glsurfaceview.renderer interface and requires the implementation of 3 functions:
Onsurfacecreated ()-This event is triggered when surface is created, and the initialization of OpenGL needs to be done in this event.
Ondrawframe ()-Update OpenGL-drawn content
Onsurfacechanged ()-Triggers this event when surface size changes, such as direction changes.

If you understand OpenGL programming in a desktop environment, then we can match it to the OpenGL program created by GLUT (see First OpenGL program ).

onsurfacecreated ---Init

ondrawframe ---Display


?

public class Firstglrenderer 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);}    }



4. References

[1] Google Android API, http://developer.android.com/training/graphics/opengl/environment.html

Android OpenGL ES: First Program

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.