• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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. You create another
49transformation matrix (a rotation matrix) and then combine it with your projection and
50camera view tranformation matrices:</p>
51
52<pre>
53private float[] mRotationMatrix = new float[16];
54public void onDrawFrame(GL10 gl) {
55    ...
56    // Create a rotation transformation for the triangle
57    long time = SystemClock.uptimeMillis() % 4000L;
58    float angle = 0.090f * ((int) time);
59    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);
60
61    // Combine the rotation matrix with the projection and camera view
62    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);
63
64    // Draw triangle
65    mTriangle.draw(mMVPMatrix);
66}
67</pre>
68
69<p>If your triangle does not rotate after making these changes, make sure you have commented out the
70{@link android.opengl.GLSurfaceView#RENDERMODE_WHEN_DIRTY GLSurfaceView.RENDERMODE_WHEN_DIRTY}
71setting, as described in the next section.</p>
72
73
74<h2 id="cont-render">Enable Continuous Rendering</h2>
75
76<p>If you have diligently followed along with the example code in this class to this point, make
77sure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL
78rotates the shape only one increment and then waits for a call to {@link
79android.opengl.GLSurfaceView#requestRender requestRender()} from the {@link
80android.opengl.GLSurfaceView} container:</p>
81
82<pre>
83public MyGLSurfaceView(Context context) {
84    ...
85    // Render the view only when there is a change in the drawing data
86    //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // comment out for auto-rotation
87}
88</pre>
89
90<p>Unless you have objects changing without any user interaction, it’s usually a good idea have this
91flag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable
92once again.</p>
93