This lesson teaches you to

You should also read

Try it out

In order to draw graphics with OpenGL ES in your Android application, you must create a
view container for them. One of the more straight-forward ways to do this is to implement both a
GLSurfaceView and a GLSurfaceView.Renderer. A GLSurfaceView is a view container for graphics drawn with OpenGL and GLSurfaceView.Renderer controls what is drawn within that view. For more information
about these classes, see the OpenGL ES
developer guide.

GLSurfaceView is just one way to incorporate OpenGL ES graphics into your
application. For a full-screen or near-full screen graphics view, it is a reasonable choice.
Developers who want to incorporate OpenGL ES graphics in a small portion of their layouts should
take a look at TextureView. For real, do-it-yourself developers, it is also
possible to build up an OpenGL ES view using SurfaceView, but this requires
writing quite a bit of additional code.

For more information about texture compression formats, see the
OpenGL developer guide.

Create an Activity for OpenGL ES Graphics

Android applications that use OpenGL ES have activities just like any other application that has
a user interface. The main difference from other applications is what you put in the layout for your
activity. While in many applications you might use TextView, Button and ListView, in an app that uses OpenGL ES, you can
also add a GLSurfaceView.

The following code example shows a minimal implementation of an activity that uses a
GLSurfaceView as its primary view:

Build a GLSurfaceView Object

A GLSurfaceView is a specialized view where you can draw OpenGL ES
graphics.
It does not do much by itself. The actual drawing of objects is controlled in the GLSurfaceView.Renderer that you set on this view. In fact, the code for this object
is so thin, you may be tempted to skip extending it and just create an unmodified GLSurfaceView instance, but don’t do that. You need to extend this class in
order to capture touch events, which is covered in the Responding to Touch
Events lesson.

The essential code for a GLSurfaceView is minimal, so for a quick
implementation, it is common to
just create an inner class in the activity that uses it:

// Render the view only when there is a change in the drawing data
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

This setting prevents the GLSurfaceView frame from being redrawn until you
call requestRender(), which is more
efficient for this sample app.

Build a Renderer Class

The implementation of the GLSurfaceView.Renderer class, or renderer,
within an application that uses OpenGL ES is where things start to get interesting. This class
controls
what gets drawn on the GLSurfaceView with which it is associated. There are
three methods in a renderer that are called by the Android system in order to figure out what and
how to draw on a GLSurfaceView:

That’s all there is to it! The code examples above create a simple Android application that
displays a black screen using OpenGL. While this code does not do anything very interesting, by
creating these classes, you have laid the foundation you need to start drawing graphic elements with
OpenGL.

Note: You may wonder why these methods have a GL10 parameter, when you are using the OpengGL ES 2.0 APIs.
These method signatures are simply reused for the 2.0 APIs to keep the Android framework code
simpler.

If you are familiar with the OpenGL ES APIs, you should now be able to set up a OpenGL ES
environment in your app and start drawing graphics. However, if you need a bit more help getting
started with OpenGL, head on to the next lessons for a few more hints.