1page.title=Adding Motion
2parent.title=Displaying Graphics with OpenGL ES
3parent.link=index.html
4
5trainingnavtop=true
6previous.title=Applying Projection and Camera Views
7previous.link=projection.html
8next.title=Responding to Touch Events
9next.link=touch.html
10
11@jd:body
12
13<div id="tb-wrapper">
14<div id="tb">
15
16<h2>This lesson teaches you to</h2>
17<ol>
18  <li><a href="#rotate-gl1">Rotate a Shape</a></li>
19  <li><a href="#cont-render">Enable Continuous Rendering</a></li>
20</ol>
21
22<h2>You should also read</h2>
23<ul>
24  <li><a href="{@docRoot}guide/topics/graphics/opengl.html">OpenGL</a></li>
25</ul>
26
27<div class="download-box">
28 <a href="http://developer.android.com/shareables/training/OpenGLES.zip"
29class="button">Download the sample</a>
30 <p class="filename">OpenGLES.zip</p>
31</div>
32
33</div>
34</div>
35
36<p>Drawing objects on screen is a pretty basic feature of OpenGL, but you can do this with other
37Android graphics framwork classes, including {@link android.graphics.Canvas} and
38{@link android.graphics.drawable.Drawable} objects. OpenGL ES provides additional capabilities for
39moving and transforming drawn objects in three dimensions or in other unique ways to create
40compelling user experiences.</p>
41
42<p>In this lesson, you take another step forward into using OpenGL ES by learning how to add motion
43to a shape with rotation.</p>
44
45
46<h2 id="rotate">Rotate a Shape</h2>
47
48<p>Rotating a drawing object with OpenGL ES 2.0 is relatively simple. In your renderer, create
49another transformation matrix (a rotation matrix) and then combine it with your projection and
50camera view transformation matrices:</p>
51
52<pre>
53private float[] mRotationMatrix = new float[16];
54public void onDrawFrame(GL10 gl) {
55    float[] scratch = new float[16];
56
57    ...
58
59    // Create a rotation transformation for the triangle
60    long time = SystemClock.uptimeMillis() % 4000L;
61    float angle = 0.090f * ((int) time);
62    Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);
63
64    // Combine the rotation matrix with the projection and camera view
65    // Note that the mMVPMatrix factor *must be first* in order
66    // for the matrix multiplication product to be correct.
67    Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
68
69    // Draw triangle
70    mTriangle.draw(scratch);
71}
72</pre>
73
74<p>If your triangle does not rotate after making these changes, make sure you have commented out the
75{@link android.opengl.GLSurfaceView#RENDERMODE_WHEN_DIRTY GLSurfaceView.RENDERMODE_WHEN_DIRTY}
76setting, as described in the next section.</p>
77
78
79<h2 id="cont-render">Enable Continuous Rendering</h2>
80
81<p>If you have diligently followed along with the example code in this class to this point, make
82sure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL
83rotates the shape only one increment and then waits for a call to {@link
84android.opengl.GLSurfaceView#requestRender requestRender()} from the {@link
85android.opengl.GLSurfaceView} container:</p>
86
87<pre>
88public MyGLSurfaceView(Context context) {
89    ...
90    // Render the view only when there is a change in the drawing data.
91    // To allow the triangle to rotate automatically, this line is commented out:
92    <strong>//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);</strong>
93}
94</pre>
95
96<p>Unless you have objects changing without any user interaction, it’s usually a good idea have this
97flag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable
98once again.</p>
99