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