OpenGL ES basics for Android Development

Source: Internet
Author: User

OpenGL ES (OpenGL for Embedded Systems) is a subset of OpenGL 3D graphics APIs. It is designed for Embedded devices such as mobile phones, PDAs, and game hosts.

This API was defined and promoted by Khronos group, a graphic software and hardware industry association that focuses on open standards in graphics and multimedia.

Build a basic framework for 3D development in Android:

1. Import opengl es Library:

[Java]

Import javax. microedition. khronos. egl. EGLConfig;

Import javax. microedition. khronos. opengles. GL10;

Import javax. microedition. khronos. egl. EGLConfig;

Import javax. microedition. khronos. opengles. GL10;

2. Add rendering support:

In Android, GLSurfaceView contains an interface Renderer dedicated to 3D rendering to display OpenGL views:

1. Import support libraries:

[Java]

Import android. opengl. GLSurfaceView;

Import android. opengl. GLSurfaceView. Renderer;

Import android. opengl. GLSurfaceView;

Import android. opengl. GLSurfaceView. Renderer;

2. Create a GLRender class to implement the Renderer interface:

[Java] view plaincopyprint? Public class GLRender implements Renderer

{

}

Public class GLRender implements Renderer

{

}

3. implement three abstract methods in the GLRender class:

[Java]

Public void onDrawFrame (GL10 gl)

Public void onSurfaceChanged (GL10 gl, int width, int height)

Public void onSurfaceCreated (GL10 gl, EGLConfig config)

Public void onDrawFrame (GL10 gl)

Public void onSurfaceChanged (GL10 gl, int width, int height)

Public void onSurfaceCreated (GL10 gl, EGLConfig config)

① OnSurfaceCreated is called when the window is created. You need to initialize the window as follows:

[Java] view plaincopyprint? Public void onSurfaceCreated (GL10 gl, EGLConfig config)

{

// Enable shadow Smoothing

Gl. glShadeModel (GL10.GL _ SMOOTH );

// Black background, set the color used to clear the screen --> (R, G, B, A); Value: 0.0f-1.0f.

Gl. glClearColor (0, 0, 0, 0 );

// Set the depth cache --> to determine which object to draw first.

Gl. glClearDepthf (1.0f );

// Enable deep Test

Gl. glable (GL10.GL _ DEPTH_TEST );

// Type of the deep Test

Gl. glDepthFunc (GL10.GL _ LEQUAL );

// Notify the system to correct the Perspective

Gl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST );

}

Public void onSurfaceCreated (GL10 gl, EGLConfig config)

{

// Enable shadow Smoothing

Gl. glShadeModel (GL10.GL _ SMOOTH );

// Black background, set the color used to clear the screen --> (R, G, B, A); Value: 0.0f-1.0f.

Gl. glClearColor (0, 0, 0, 0 );

// Set the depth cache --> to determine which object to draw first.

Gl. glClearDepthf (1.0f );

// Enable deep Test

Gl. glable (GL10.GL _ DEPTH_TEST );

// Type of the deep Test

Gl. glDepthFunc (GL10.GL _ LEQUAL );

// Notify the system to correct the Perspective

Gl. glHint (GL10.GL _ PERSPECTIVE_CORRECTION_HINT, GL10.GL _ FASTEST );

} ② OnSurfaceChanged is called when the window size changes, no matter whether the window size has changed or not, it should be run at least once at the beginning of the program.

In this example, we need to set the OpenGL scenario size:

[Java]

Public void onSurfaceChanged (GL10 gl, int width, int height)

{

Float ratio = (float) width/height;

// Set the size of the OpenGL scenario

Gl. glViewport (0, 0, width, height );

// Set the projection matrix --> Add perspective.

Gl. glMatrixMode (GL10.GL _ PROJECTION );

// Reset the projection matrix --> to restore to the original state.

Gl. glLoadIdentity ();

// Set the preview size --> the first four parameters to determine the size of the window. The last two parameters are the start and end points of the depth that can be drawn in the scene.

Gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10 );

// Select the model observation matrix

Gl. glMatrixMode (GL10.GL _ MODELVIEW );

// Reset the model observation matrix

Gl. glLoadIdentity ();

}

Public void onSurfaceChanged (GL10 gl, int width, int height)

{

Float ratio = (float) width/height;

// Set the size of the OpenGL scenario

Gl. glViewport (0, 0, width, height );

// Set the projection matrix --> Add perspective.

Gl. glMatrixMode (GL10.GL _ PROJECTION );

// Reset the projection matrix --> to restore to the original state.

Gl. glLoadIdentity ();

// Set the preview size --> the first four parameters to determine the size of the window. The last two parameters are the start and end points of the depth that can be drawn in the scene.

Gl. glFrustumf (-ratio, ratio,-1, 1, 1, 10 );

// Select the model observation matrix

Gl. glMatrixMode (GL10.GL _ MODELVIEW );

// Reset the model observation matrix

Gl. glLoadIdentity ();

} ③ OnDrawFrame plot in the window. Before drawing, you need to clear the screen into the color specified above, clear the depth cache and reset the scenario,

Then you can draw the image:

[Java]

Public void onDrawFrame (GL10 gl)

{

// Clear screen and deep Cache

Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// The Specific Drawing operation starts...

}

Public void onDrawFrame (GL10 gl)

{

// Clear screen and deep Cache

Gl. glClear (GL10.GL _ COLOR_BUFFER_BIT | GL10.GL _ DEPTH_BUFFER_BIT );

// Reset the current model observation matrix

Gl. glLoadIdentity ();

// The Specific Drawing operation starts...

}

3. Call the interface we have written in the main program:

Call the setRenderer method of the GLSurfaceView class to set our own GLRender class to the default Renderer,

Then, use the setContentView method to display a GLSurfaceView for the Activity.

[Java]

Renderer render = new GLRender ();

GLSurfaceView glView = new GLSurfaceView (this );

GlView. setRenderer (render );

SetContentView (glView );

Renderer render = new GLRender ();

GLSurfaceView glView = new GLSurfaceView (this );

GlView. setRenderer (render );

SetContentView (glView );

At this point, our basic framework has been set up.

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.