page.title=3D with OpenGL parent.title=Graphics parent.link=index.html @jd:body

In this document

  1. The Basics
  2. OpenGL Versions and Device Compatibility
    1. Texture Compression Support
    2. Declaring Use of Compressed Textures

Key classes

  1. {@link android.opengl.GLSurfaceView}
  2. {@link android.opengl.GLSurfaceView.Renderer}
  3. {@link javax.microedition.khronos.opengles}
  4. {@link android.opengl}

Related Samples

  1. GLSurfaceViewActivity
  2. GLES20Activity
  3. TouchRotateActivity
  4. Compressed Textures

See also

  1. Introducing GLSurfaceView
  2. OpenGL ES
  3. OpenGL ES 1.x Specification
  4. OpenGL ES 2.x specification

Android includes support for high performance 2D and 3D graphics with the Open Graphics Library (OpenGL) API—specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a standard software interface for 3D graphics processing hardware. OpenGL ES is a flavor of the OpenGL specification intended for embedded devices. The OpenGL ES 1.0 and 1.1 API specifications have been supported since Android 1.0. Beginning with Android 2.2 (API Level 8), the framework supports the OpenGL ES 2.0 API specification.

Note: The specific API provided by the Android framework is similar to the J2ME JSR239 OpenGL ES API, but is not identical. If you are familiar with J2ME JSR239 specification, be alert for variations.

The Basics

Android supports OpenGL both through its framework API and the Native Development Kit (NDK). This topic focuses on the Android framework interfaces. For more information about the NDK, see the Android NDK.

There are two foundational classes in the Android framework that let you create and manipulate graphics with the OpenGL ES API: {@link android.opengl.GLSurfaceView} and {@link android.opengl.GLSurfaceView.Renderer}. If your goal is to use OpenGL in your Android application, understanding how to implement these classes in an activity should be your first objective.

{@link android.opengl.GLSurfaceView}
This class is a container on which you can draw and manipulate objects using OpenGL API calls. This class is similar in function to a {@link android.view.SurfaceView}, except that it is specifically for use with OpenGL. You can use this class by simply creating an instance of {@link android.opengl.GLSurfaceView} and adding your {@link android.opengl.GLSurfaceView.Renderer Renderer} to it. However, if you want to capture touch screen events, you should extend the {@link android.opengl.GLSurfaceView} class to implement the touch listeners, as shown in the TouchRotateActivity sample.
{@link android.opengl.GLSurfaceView.Renderer}
This interface defines the methods required for drawing graphics in an OpenGL {@link android.opengl.GLSurfaceView}. You must provide an implementation of this interface as a separate class and attach it to your {@link android.opengl.GLSurfaceView} instance using {@link android.opengl.GLSurfaceView#setRenderer(android.opengl.GLSurfaceView.Renderer) GLSurfaceView.setRenderer()}.

The {@link android.opengl.GLSurfaceView.Renderer} interface requires that you implement the following methods:

Once you have established a container view for OpenGL using {@link android.opengl.GLSurfaceView} and {@link android.opengl.GLSurfaceView.Renderer}, you can begin calling OpenGL APIs using the following classes:

OpenGL Versions and Device Compatibility

The OpenGL ES 1.0 and 1.1 API specifications have been supported since Android 1.0. Beginning with Android 2.2 (API Level 8), the framework supports the OpenGL ES 2.0 API specification. OpenGL ES 2.0 is supported by most Android devices and is recommended for new applications being developed with OpenGL. For information about the relative number of Android-powered devices that support a given version of OpenGL ES, see the OpenGL ES Versions Dashboard.

Texture compression support

Texture compression can significantly increase the performance of your OpenGL application by reducing memory requirements and making more efficient use of memory bandwidth. The Android framework provides support for the ETC1 compression format as a standard feature, including a {@link android.opengl.ETC1Util} utility class and the {@code etc1tool} compression tool (located in your Android SDK at {@code <sdk>/tools/}).

For an example of an Android application that uses texture compression, see the CompressedTextureActivity code sample.

To check if the ETC1 format is supported on a device, call the {@link android.opengl.ETC1Util#isETC1Supported() ETC1Util.isETC1Supported()} method.

Note: The ETC1 texture compression format does not support textures with an alpha channel. If your application requires textures with an alpha channel, you should investigate other texture compression formats available on your target devices.

Beyond the ETC1 format, Android devices have varied support for texture compression based on their GPU chipsets. You should investigate texture compression support on the the devices you are are targeting to determine what compression types your application should support.

To determine if texture compression formats other than ETC1 are supported on a particular device:

  1. Run the following code on your target devices to determine what texture compression formats are supported:
      String extensions = javax.microedition.khronos.opengles.GL10.glGetString(GL10.GL_EXTENSIONS);
    

    Warning: The results of this call vary by device! You must run this call on several target devices to determine what compression types are commonly supported on your target devices.

  2. Review the output of this method to determine what extensions are supported on the device.

Declaring compressed textures

Once you have decided which texture compression types your application will support, you must declare them in your manifest file using <supports-gl-texture>. Declaring this information in your manifest file hides your application from users with devices that do not support at least one of your declared compression types. For more information on how Android Market filtering works for texture compressions, see the Android Market and texture compression filtering section of the {@code <supports-gl-texture>} documentation.