opengl.jd revision d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03
1d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lypage.title=OpenGL
2c918e4a86d42d89cb893b0150358cfd01ef7d532Scott Mainparent.title=Graphics
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectparent.link=index.html
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project@jd:body
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<div id="qv-wrapper">
7a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  <div id="qv">
8a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <h2>In this document</h2>
9d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
10a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ol>
110664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li><a href="#basics">The Basics</a>
120664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        <ol>
130664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez           <li><a href="#packages">OpenGL packages</a></li>
140664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        </ol>
150664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li><a href="#manifest">Declaring OpenGL Requirements</a></li>
160664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      </li>
17d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly      <li><a href="#coordinate-mapping">Mapping Coordinates for Drawn Objects</a>
180664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        <ol>
190664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          <li><a href="#proj-es1">Projection and camera in ES 1.0</a></li>
200664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          <li><a href="#proj-es1">Projection and camera in ES 2.0</a></li>
210664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        </ol>
220664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      </li>
23a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="#compatibility">OpenGL Versions and Device Compatibility</a>
24a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        <ol>
250664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          <li><a href="#textures">Texture compression support</a></li>
260664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          <li><a href="#gl-extension-query">Determining OpenGL Extensions</a></li>
27a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        </ol>
28a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      </li>
290664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li><a href="#choosing-version">Choosing an OpenGL API Version</a></li>
30a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ol>
31a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <h2>Key classes</h2>
32a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ol>
33a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>{@link android.opengl.GLSurfaceView}</li>
34a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>{@link android.opengl.GLSurfaceView.Renderer}</li>
35a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ol>
360664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <h2>Related tutorials</h2>
370664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <ol>
380664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li><a href="{@docRoot}resources/tutorials/opengl/opengl-es10.html">OpenGL ES 1.0</a></li>
390664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li><a href="{@docRoot}resources/tutorials/opengl/opengl-es20.html">OpenGL ES 2.0</a></li>
400664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    </ol>
410664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <h2>Related samples</h2>
42a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ol>
43a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/
44a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezGLSurfaceViewActivity.html">GLSurfaceViewActivity</a></li>
45a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/
46a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezGLES20Activity.html">GLES20Activity</a></li>
47a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/
48a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezTouchRotateActivity.html">TouchRotateActivity</a></li>
49a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/
50a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezCompressedTextureActivity.html">Compressed Textures</a></li>
51a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ol>
52a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <h2>See also</h2>
53a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ol>
54a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="{@docRoot}resources/articles/glsurfaceview.html">Introducing
55a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezGLSurfaceView</a></li>
56a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="http://www.khronos.org/opengles/">OpenGL ES</a></li>
57a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="http://www.khronos.org/opengles/1_X/">OpenGL ES 1.x Specification</a></li>
58a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li><a href="http://www.khronos.org/opengles/2_X/">OpenGL ES 2.x specification</a></li>
59a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ol>
60a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  </div>
61a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</div>
629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
63a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>Android includes support for high performance 2D and 3D graphics with the Open Graphics Library
640664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez(OpenGL), specifically, the OpenGL ES API. OpenGL is a cross-platform graphics API that specifies a
650664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezstandard software interface for 3D graphics processing hardware. OpenGL ES is a flavor of the OpenGL
660664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezspecification intended for embedded devices. The OpenGL ES 1.0 and 1.1 API specifications have been
670664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezsupported since Android 1.0. Beginning with Android 2.2 (API Level 8), the framework supports the
680664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezOpenGL ES 2.0 API specification.</p>
699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
70a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p class="note"><b>Note:</b> The specific API provided by the Android framework is similar to the
71a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  J2ME JSR239 OpenGL ES  API, but is not identical. If you are familiar with J2ME JSR239
72a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  specification, be alert for variations.</p>
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
75a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<h2 id="basics">The Basics</h2>
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
77a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>Android supports OpenGL both through its framework API and the Native Development
78a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezKit (NDK). This topic focuses on the Android framework interfaces. For more information about the
79a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezNDK, see the <a href="{@docRoot}sdk/ndk/index.html">Android NDK</a>.
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
81d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly<p>There are two foundational classes in the Android framework that let you create and manipulate
82a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezgraphics with the OpenGL ES API: {@link android.opengl.GLSurfaceView} and {@link
83a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezandroid.opengl.GLSurfaceView.Renderer}. If your goal is to use OpenGL in your Android application,
84a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezunderstanding how to implement these classes in an activity should be your first objective.
85a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</p>
86a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez
87a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<dl>
880664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <dt><strong>{@link android.opengl.GLSurfaceView}</strong></dt>
890664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <dd>This class is a {@link android.view.View} where you can draw and manipulate objects using
900664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    OpenGL API calls and is similar in function to a {@link android.view.SurfaceView}. You can use
91d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    this class by creating an instance of {@link android.opengl.GLSurfaceView} and adding your
92a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    {@link android.opengl.GLSurfaceView.Renderer Renderer} to it. However, if you want to capture
93a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    touch screen events, you should extend the {@link android.opengl.GLSurfaceView} class to
94d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    implement the touch listeners, as shown in OpenGL Tutorials for
95d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    <a href="{@docRoot}resources/tutorials/opengl/opengl-es10.html#touch">ES 1.0</a>,
960664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <a href="{@docRoot}resources/tutorials/opengl/opengl-es20.html#touch">ES 2.0</a> and the <a
97a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezhref="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/TouchRotateActivity
98a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez.html">TouchRotateActivity</a> sample.</dd>
99d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
1000664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <dt><strong>{@link android.opengl.GLSurfaceView.Renderer}</strong></dt>
101a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  <dd>This interface defines the methods required for drawing graphics in an OpenGL {@link
102a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    android.opengl.GLSurfaceView}. You must provide an implementation of this interface as a
103a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    separate class and attach it to your {@link android.opengl.GLSurfaceView} instance using
104a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    {@link android.opengl.GLSurfaceView#setRenderer(android.opengl.GLSurfaceView.Renderer)
105a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    GLSurfaceView.setRenderer()}.
106d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
107a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <p>The {@link android.opengl.GLSurfaceView.Renderer} interface requires that you implement the
108a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      following methods:</p>
109a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ul>
110a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>
111a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        {@link
112a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
113a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    javax.microedition.khronos.egl.EGLConfig) onSurfaceCreated()}: The system calls this
114a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    method once, when creating the {@link android.opengl.GLSurfaceView}. Use this method to perform
115a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    actions that need to happen only once, such as setting OpenGL environment parameters or
116a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    initializing OpenGL graphic objects.
117a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      </li>
118a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>
119a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        {@link
120a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
121a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        onDrawFrame()}: The system calls this method on each redraw of the {@link
122a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        android.opengl.GLSurfaceView}. Use this method as the primary execution point for
123a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        drawing (and re-drawing) graphic objects.</li>
124a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>
125a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        {@link
126a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
127a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    int, int) onSurfaceChanged()}: The system calls this method when the {@link
128a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    android.opengl.GLSurfaceView} geometry changes, including changes in size of the {@link
129a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    android.opengl.GLSurfaceView} or orientation of the device screen. For example, the system calls
130a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    this method when the device changes from portrait to landscape orientation. Use this method to
131d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    respond to changes in the {@link android.opengl.GLSurfaceView} container.
132a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      </li>
133a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ul>
134a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </dd>
135a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</dl>
136a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez
1370664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h3 id="packages">OpenGL packages</h3>
138a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>Once you have established a container view for OpenGL using {@link
139a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezandroid.opengl.GLSurfaceView} and {@link android.opengl.GLSurfaceView.Renderer}, you can begin
140a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezcalling OpenGL APIs using the following classes:</p>
141a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez
142a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<ul>
143a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  <li>OpenGL ES 1.0/1.1 API Packages
144a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ul>
1450664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        <li>{@link android.opengl} - This package provides a static interface to the OpenGL ES
1460664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez1.0/1.1 classes and better performance than the javax.microedition.khronos package interfaces.
1470664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          <ul>
1480664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez            <li>{@link android.opengl.GLES10}</li>
1490664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez            <li>{@link android.opengl.GLES10Ext}</li>
1500664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez            <li>{@link android.opengl.GLES11}</li>
1510664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez            <li>{@link android.opengl.GLES10Ext}</li>
1520664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez          </ul>
1530664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        </li>
154a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      <li>{@link javax.microedition.khronos.opengles} - This package provides the standard
1550664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezimplementation of OpenGL ES 1.0/1.1.
156a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez          <ul>
157a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez            <li>{@link javax.microedition.khronos.opengles.GL10}</li>
158a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez            <li>{@link javax.microedition.khronos.opengles.GL10Ext}</li>
159a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez            <li>{@link javax.microedition.khronos.opengles.GL11}</li>
160a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez            <li>{@link javax.microedition.khronos.opengles.GL11Ext}</li>
161a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez            <li>{@link javax.microedition.khronos.opengles.GL11ExtensionPack}</li>
162a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez          </ul>
163a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez        </li>
164a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez      </ul>
165a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  </li>
166a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  <li>OpenGL ES 2.0 API Class
167a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    <ul>
1680664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@link android.opengl.GLES20 android.opengl.GLES20} - This package provides the
1690664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezinterface to OpenGL ES 2.0 and is available starting with Android 2.2 (API Level 8).</li>
170a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez    </ul>
171a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  </li>
172a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</ul>
1739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1740664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>If you'd like to start building an app with OpenGL right away, have a look at the tutorials for
175d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly<a href="{@docRoot}resources/tutorials/opengl/opengl-es10.html">OpenGL ES 1.0</a> or
1760664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<a href="{@docRoot}resources/tutorials/opengl/opengl-es20.html">OpenGL ES 2.0</a>!
1770664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</p>
1780664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
1790664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h2 id="manifest">Declaring OpenGL Requirements</h2>
1800664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>If your application uses OpenGL features that are not available on all devices, you must include
181d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lythese requirements in your <a
1820664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}guide/topics/manifest/manifest-intro.html">AndroidManifest.xml</a></code> file.
1830664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezHere are the most common OpenGL manifest declarations:</p>
1840664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
1850664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<ul>
1860664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>OpenGL ES version requirements</strong> - If your application only supports OpenGL ES
1870664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez2.0, you must declare that requirement by adding the following settings to your manifest as
1880664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezshown below.
1890664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
1900664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
1910664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    &lt;!-- Tell the system this app requires OpenGL ES 2.0. --&gt;
1920664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    &lt;uses-feature android:glEsVersion="0x00020000" android:required="true" /&gt;
1930664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
1940664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
1950664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <p>Adding this declaration causes the Android Market to restrict your application from being
1960664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  installed on devices that do not support OpenGL ES 2.0.</p>
1970664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  </li>
1980664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>Texture compression requirements</strong> - If your application uses texture
1998f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezcompression formats, you must declare the formats your application supports in your manifest file
2008f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezusing <a href="{@docRoot}guide/topics/manifest/supports-gl-texture-element.html">{@code
2018f134ea74badba7108989898117bb70c4226b5b5Joe Fernandez&lt;supports-gl-texture&gt;}</a>. For more information about available texture compression
202d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lyformats, see <a href="#textures">Texture compression support</a>.
2030664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2040664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>Declaring texture compression requirements in your manifest hides your application from users
2050664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezwith devices that do not support at least one of your declared compression types. For more
2060664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezinformation on how Android Market filtering works for texture compressions, see the <a
2070664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}guide/topics/manifest/supports-gl-texture-element.html#market-texture-filtering">
2080664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezAndroid Market and texture compression filtering</a> section of the {@code
209d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly&lt;supports-gl-texture&gt;} documentation.</p>
2100664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  </li>
2110664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</ul>
2120664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2130664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2148f134ea74badba7108989898117bb70c4226b5b5Joe Fernandez<h2 id="coordinate-mapping">Mapping Coordinates for Drawn Objects</h2>
2150664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2160664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>One of the basic problems in displaying graphics on Android devices is that their screens can
2170664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezvary in size and shape. OpenGL assumes a square, uniform coordinate system and, by default, happily
2180664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdraws those coordinates onto your typically non-square screen as if it is perfectly square.</p>
2190664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2200664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<img src="{@docRoot}images/opengl/coordinates.png">
2210664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p class="img-caption">
2220664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <strong>Figure 1.</strong> Default OpenGL coordinate system (left) mapped to a typical Android
2230664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdevice screen (right).
2240664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</p>
2250664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2260664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>The illustration above shows the uniform coordinate system assumed for an OpenGL frame on the
2270664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezleft, and how these coordinates actually map to a typical device screen in landscape orientation
2280664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezon the right. To solve this problem, you can apply OpenGL projection modes and camera views to
2290664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeztransform coordinates so your graphic objects have the correct proportions on any display.</p>
2300664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2310664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>In order to apply projection and camera views, you create a projection matrix and a camera view
2320664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmatrix and apply them to the OpenGL rendering pipeline. The projection matrix recalculates the
2330664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezcoordinates of your graphics so that they map correctly to Android device screens. The camera view
2340664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmatrix creates a transformation that renders objects from a specific eye position.</p>
2350664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2360664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h3 id="proj-es1">Projection and camera view in OpenGL ES 1.0</h3>
2370664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>In the ES 1.0 API, you apply projection and camera view by creating each matrix and then
2380664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezadding them to the OpenGL environment.</p>
239d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
2400664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<ol>
2410664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Projection matrix</strong> - Create a projection matrix using the geometry of the
2420664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdevice screen in order to recalculate object coordinates so they are drawn with correct proportions.
2438f134ea74badba7108989898117bb70c4226b5b5Joe FernandezThe following example code demonstrates how to modify the {@link
2448f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
2458f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezint, int) onSurfaceChanged()} method of a {@link android.opengl.GLSurfaceView.Renderer}
2468f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezimplementation to create a projection matrix based on the screen's aspect ratio and apply it to the
2478f134ea74badba7108989898117bb70c4226b5b5Joe FernandezOpenGL rendering environment.
2480664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2490664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
2500664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  public void onSurfaceChanged(GL10 gl, int width, int height) {
2510664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      gl.glViewport(0, 0, width, height);
252d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
2530664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      // make adjustments for screen ratio
2540664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      float ratio = (float) width / height;
2550664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
2560664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      gl.glLoadIdentity();                        // reset the matrix to its default state
2570664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);  // apply the projection matrix
258d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly  }
2590664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
2600664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
2610664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2620664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Camera transformation matrix</strong> - Once you have adjusted the coordinate system
2630664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezusing a projection matrix, you must also apply a camera view. The following example code shows how
264d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lyto modify the {@link
2658f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
2668f134ea74badba7108989898117bb70c4226b5b5Joe FernandezonDrawFrame()} method of a {@link android.opengl.GLSurfaceView.Renderer}
2678f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezimplementation to apply a model view and use the
2688f134ea74badba7108989898117bb70c4226b5b5Joe Fernandez{@link android.opengl.GLU#gluLookAt(javax.microedition.khronos.opengles.GL10, float, float, float,
2698f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezfloat, float, float, float, float, float) GLU.gluLookAt()} utility to create a viewing tranformation
2708f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezwhich simulates a camera position.
2710664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2720664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
2730664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    public void onDrawFrame(GL10 gl) {
2740664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
2750664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // Set GL_MODELVIEW transformation mode
2760664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        gl.glMatrixMode(GL10.GL_MODELVIEW);
2770664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        gl.glLoadIdentity();                      // reset the matrix to its default state
278d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
2790664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // When using GL_MODELVIEW, you must set the camera view
280d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
2810664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
2820664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    }
283d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly</pre>
2840664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
2850664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</ol>
2860664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2870664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>For a complete example of how to apply projection and camera views with OpenGL ES 1.0, see the <a
2880664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}resources/tutorials/opengl/opengl-es10.html#projection-and-views">OpenGL ES 1.0
2890664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeztutorial</a>.</p>
2900664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2910664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
2920664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h3 id="proj-es2">Projection and camera view in OpenGL ES 2.0</h3>
2930664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>In the ES 2.0 API, you apply projection and camera view by first adding a matrix member to
2940664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezthe vertex shaders of your graphics objects. With this matrix member added, you can then
2950664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezgenerate and apply projection and camera viewing matrices to your objects.</p>
296d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
2970664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<ol>
2980664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Add matrix to vertex shaders</strong> - Create a variable for the view projection matrix
2990664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezand include it as a multiplier of the shader's position. In the following example vertex shader
300d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lycode, the included {@code uMVPMatrix} member allows you to apply projection and camera viewing
3010664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmatrices to the coordinates of objects that use this shader.
3020664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
3030664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
304d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    private final String vertexShaderCode =
305d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3060664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // This matrix member variable provides a hook to manipulate
3070664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // the coordinates of objects that use this vertex shader
3080664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        "uniform mat4 uMVPMatrix;   \n" +
309d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3100664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        "attribute vec4 vPosition;  \n" +
3110664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        "void main(){               \n" +
312d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3130664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // the matrix must be included as part of gl_Position
3140664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        " gl_Position = uMVPMatrix * vPosition; \n" +
315d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3160664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        "}  \n";
3170664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
3180664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <p class="note"><strong>Note:</strong> The example above defines a single transformation matrix
3190664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmember in the vertex shader into which you apply a combined projection matrix and camera view
3200664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmatrix. Depending on your application requirements, you may want to define separate projection
3210664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmatrix and camera viewing matrix members in your vertex shaders so you can change them
3220664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezindependently.</p>
3230664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
3240664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Access the shader matrix</strong> - After creating a hook in your vertex shaders to
3250664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezapply projection and camera view, you can then access that variable to apply projection and
3268f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezcamera viewing matrices. The following code shows how to modify the {@link
3278f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
3288f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezjavax.microedition.khronos.egl.EGLConfig) onSurfaceCreated()} method of a {@link
3298f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer} implementation to access the matrix
3300664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezvariable defined in the vertex shader above.
3310664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
3320664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
3330664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
3340664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
3350664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
3360664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
3370664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    }
3380664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
3390664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
3400664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Create projection and camera viewing matrices</strong> - Generate the projection and
3410664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezviewing matrices to be applied the graphic objects. The following example code shows how to modify
342d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lythe {@link
3438f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10,
3448f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezjavax.microedition.khronos.egl.EGLConfig) onSurfaceCreated()} and {@link
3458f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10,
3468f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezint, int) onSurfaceChanged()} methods of a {@link android.opengl.GLSurfaceView.Renderer}
3478f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezimplementation to create camera view matrix and a projection matrix based on the screen aspect ratio
3488f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezof the device.
3490664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
3500664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
3510664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
3520664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
3530664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // Create a camera view matrix
3540664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
355d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    }
356d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3570664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    public void onSurfaceChanged(GL10 unused, int width, int height) {
3580664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        GLES20.glViewport(0, 0, width, height);
359d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3600664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        float ratio = (float) width / height;
361d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3620664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // create a projection matrix from device screen geometry
3630664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
364d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly    }
3650664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
3660664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
3670664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
3680664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<li><strong>Apply projection and camera viewing matrices</strong> - To apply the projection and
3690664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezcamera view transformations, multiply the matrices together and then set them into the vertex
3708f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezshader. The following example code shows how modify the {@link
3718f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezandroid.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
3728f134ea74badba7108989898117bb70c4226b5b5Joe FernandezonDrawFrame()} method of a {@link android.opengl.GLSurfaceView.Renderer} implementation to combine
3738f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezthe projection matrix and camera view created in the code above and then apply it to the graphic
3748f134ea74badba7108989898117bb70c4226b5b5Joe Fernandezobjects to be rendered by OpenGL.
375d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3760664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<pre>
3770664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    public void onDrawFrame(GL10 unused) {
3780664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
3790664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // Combine the projection and camera view matrices
3800664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
381d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3820664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // Apply the combined projection and camera view transformations
3830664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
384d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
3850664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        // Draw objects
3860664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez        ...
3870664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    }
3880664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</pre>
3890664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</li>
3900664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</ol>
3910664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>For a complete example of how to apply projection and camera view with OpenGL ES 2.0, see the <a
3920664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}resources/tutorials/opengl/opengl-es20.html#projection-and-views">OpenGL ES 2.0
3930664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeztutorial</a>.</p>
3940664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
3950664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
396a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<h2 id="compatibility">OpenGL Versions and Device Compatibility</h2>
397a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez
3980664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>The OpenGL ES 1.0 and 1.1 API specifications have been supported since Android 1.0.
399a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezBeginning with Android 2.2 (API Level 8), the framework supports the OpenGL ES 2.0 API
400a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezspecification. OpenGL ES 2.0 is supported by most Android devices and is recommended for new
401a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezapplications being developed with OpenGL. For information about the relative number of
402a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezAndroid-powered devices that support a given version of OpenGL ES, see the <a
403a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezhref="{@docRoot}resources/dashboard/opengl.html">OpenGL ES Versions Dashboard</a>.</p>
404a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez
4050664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
406a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<h3 id="textures">Texture compression support</h3>
407a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>Texture compression can significantly increase the performance of your OpenGL application by
408a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezreducing memory requirements and making more efficient use of memory bandwidth. The Android
409a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezframework provides support for the ETC1 compression format as a standard feature, including a {@link
4100664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezandroid.opengl.ETC1Util} utility class and the {@code etc1tool} compression tool (located in the
4110664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezAndroid SDK at {@code &lt;sdk&gt;/tools/}). For an example of an Android application that uses
4120664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeztexture compression, see the <a
413a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezhref="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/graphics/
414a53afedf783176d531e7cf8744f9b07ce60a5c92Joe FernandezCompressedTextureActivity.html">CompressedTextureActivity</a> code sample.
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project</p>
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
417a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>To check if the ETC1 format is supported on a device, call the {@link
418a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezandroid.opengl.ETC1Util#isETC1Supported() ETC1Util.isETC1Supported()} method.</p>
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
420a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p class="note"><b>Note:</b> The ETC1 texture compression format does not support textures with an
421a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezalpha channel. If your application requires textures with an alpha channel, you should
422a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezinvestigate other texture compression formats available on your target devices.</p>
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
424a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<p>Beyond the ETC1 format, Android devices have varied support for texture compression based on
4250664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeztheir GPU chipsets and OpenGL implementations. You should investigate texture compression support on
4260664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezthe the devices you are are targeting to determine what compression types your application should
4270664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezsupport. In order to determine what texture formats are supported on a given device, you must <a
4280664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="#gl-extension-query">query the device</a> and review the <em>OpenGL extension names</em>,
4290664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezwhich identify what texture compression formats (and other OpenGL features) are supported by the
4300664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdevice. Some commonly supported texture compression formats are as follows:</p>
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4320664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<ul>
4330664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>ATITC (ATC)</strong> - ATI texture compression (ATITC or ATC) is available on a
4340664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezwide variety of devices and supports fixed rate compression for RGB textures with and without
4350664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezan alpha channel. This format may be represented by several OpenGL extension names, for example:
4360664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <ul>
4370664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_AMD_compressed_ATC_texture}</li>
4380664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_ATI_texture_compression_atitc}</li>
4390664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    </ul>
4400664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  </li>
4410664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>PVRTC</strong> - PowerVR texture compression (PVRTC) is available on a wide
4420664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezvariety of devices and supports 2-bit and 4-bit per pixel textures with or without an alpha channel.
4430664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezThis format is represented by the following OpenGL extension name:
4440664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <ul>
4450664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_IMG_texture_compression_pvrtc}</li>
4460664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    </ul>
4470664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  </li>
4480664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>S3TC (DXT<em>n</em>/DXTC)</strong> - S3 texture compression (S3TC) has several
4490664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezformat variations (DXT1 to DXT5) and is less widely available. The format supports RGB textures with
4500664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez4-bit alpha or 8-bit alpha channels. This format may be represented by several OpenGL extension
4510664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandeznames, for example:
4520664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <ul>
4530664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_OES_texture_compression_S3TC}</li>
4540664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_EXT_texture_compression_s3tc}</li>
4550664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_EXT_texture_compression_dxt1}</li>
4560664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_EXT_texture_compression_dxt3}</li>
4570664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_EXT_texture_compression_dxt5}</li>
4580664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    </ul>
4590664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  </li>
4600664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>3DC</strong> - 3DC texture compression (3DC) is a less widely available format that
4610664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezsupports RGB textures with an an alpha channel. This format is represented by the following OpenGL
4620664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezextension name:</li>
4630664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    <ul>
4640664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez      <li>{@code GL_AMD_compressed_3DC_texture}</li>
4650664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez    </ul>
4660664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</ul>
4670664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
4680664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p class="warning"><strong>Warning:</strong> These texture compression formats are <em>not
4690664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezsupported</em> on all devices. Support for these formats can vary by manufacturer and device. For
4700664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezinformation on how to determine what texture compression formats are on a particular device, see
4710664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezthe next section.
4720664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</p>
4730664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
4740664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p class="note"><strong>Note:</strong> Once you decide which texture compression formats your
4750664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezapplication will support, make sure you declare them in your manifest using <a
4760664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}guide/topics/manifest/supports-gl-texture-element.html">&lt;supports-gl-texture&gt;
4770664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</a>. Using this declaration enables filtering by external services such as Android Market, so that
4780664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezyour app is installed only on devices that support the formats your app requires. For details, see
4790664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<a
4800664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezhref="{@docRoot}guide/topics/graphics/opengl.html#manifest">OpenGL manifest declarations</a>.</p>
4810664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
4820664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h3 id="gl-extension-query">Determining OpenGL extensions</h3>
4830664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>Implementations of OpenGL vary by Android device in terms of the extensions to the OpenGL ES API
4840664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezthat are supported. These extensions include texture compressions, but typically also include other
4850664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezextensions to the OpenGL feature set.</p>
4860664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
4870664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>To determine what texture compression formats, and other OpenGL extensions, are supported on a
4880664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezparticular device:</p>
489a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<ol>
490a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  <li>Run the following code on your target devices to determine what texture compression
491a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandezformats are supported:
492a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez<pre>
493a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  String extensions = javax.microedition.khronos.opengles.GL10.glGetString(GL10.GL_EXTENSIONS);
494a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</pre>
4950664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <p class="warning"><b>Warning:</b> The results of this call <em>vary by device!</em> You
4960664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezmust run this call on several target devices to determine what compression types are commonly
4970664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezsupported.</p>
498a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez  </li>
4990664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li>Review the output of this method to determine what OpenGL extensions are supported on the
500d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Lydevice.</li>
501a53afedf783176d531e7cf8744f9b07ce60a5c92Joe Fernandez</ol>
5029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5040664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<h2 id="choosing-version">Choosing an OpenGL API Version</h2>
5050664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
5060664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>OpenGL ES API version 1.0 (and the 1.1 extensions) and version 2.0 both provide high
5070664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezperformance graphics interfaces for creating 3D games, visualizations and user interfaces. Graphics
5080664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezprogramming for the OpenGL ES 1.0/1.1 API versus ES 2.0 differs significantly, and so developers
5090664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezshould carefully consider the following factors before starting development with either API:</p>
5100664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez
5110664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<ul>
5120664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>Performance</strong> - In general, OpenGL ES 2.0 provides faster graphics performance
5130664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezthan the ES 1.0/1.1 APIs. However, the performance difference can vary depending on the Android
5140664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdevice your OpenGL application is running on, due to differences in the implementation of the OpenGL
5150664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezgraphics pipeline.</li>
516d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly  <li><strong>Device Compatibility</strong> - Developers should consider the types of devices,
5170664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezAndroid versions and the OpenGL ES versions available to their customers. For more information
5180664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezon OpenGL compatibility across devices, see the <a href="#compatibility">OpenGL Versions and Device
5190664a8f661e15991e20cab1c0daa41f5e21977abJoe FernandezCompatibility</a> section.</li>
5200664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>Coding Convenience</strong> - The OpenGL ES 1.0/1.1 API provides a fixed function
5210664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezpipeline and convenience functions which are not available in the ES 2.0 API. Developers who are new
5220664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezto OpenGL may find coding for OpenGL ES 1.0/1.1 faster and more convenient.</li>
5230664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez  <li><strong>Graphics Control</strong> - The OpenGL ES 2.0 API provides a higher degree
5240664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezof control by providing a fully programmable pipeline through the use of shaders. With more
5250664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdirect control of the graphics processing pipeline, developers can create effects that would be
5260664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezvery difficult to generate using the 1.0/1.1 API.</li>
5270664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez</ul>
528d2a4e69f3dffb85c256629f5ee9005d8b9d8ac03Robert Ly
5290664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandez<p>While performance, compatibility, convenience, control and other factors may influence your
5300664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezdecision, you should pick an OpenGL API version based on what you think provides the best experience
5310664a8f661e15991e20cab1c0daa41f5e21977abJoe Fernandezfor your users.</p>
532