Home | History | Annotate | Download | only in opengl
      1 page.title=Adding Motion
      2 parent.title=Displaying Graphics with OpenGL ES
      3 parent.link=index.html
      4 
      5 trainingnavtop=true
      6 previous.title=Applying Projection and Camera Views
      7 previous.link=projection.html
      8 next.title=Responding to Touch Events
      9 next.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"
     29 class="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
     37 Android graphics framwork classes, including {@link android.graphics.Canvas} and
     38 {@link android.graphics.drawable.Drawable} objects. OpenGL ES provides additional capabilities for
     39 moving and transforming drawn objects in three dimensions or in other unique ways to create
     40 compelling user experiences.</p>
     41 
     42 <p>In this lesson, you take another step forward into using OpenGL ES by learning how to add motion
     43 to 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
     49 transformation matrix (a rotation matrix) and then combine it with your projection and
     50 camera view transformation matrices:</p>
     51 
     52 <pre>
     53 private float[] mRotationMatrix = new float[16];
     54 public void onDrawFrame(GL10 gl) {
     55     ...
     56     float[] scratch = new float[16];
     57 
     58     // Create a rotation transformation for the triangle
     59     long time = SystemClock.uptimeMillis() % 4000L;
     60     float angle = 0.090f * ((int) time);
     61     Matrix.setRotateM(mRotationMatrix, 0, angle, 0, 0, -1.0f);
     62 
     63     // Combine the rotation matrix with the projection and camera view
     64     // Note that the mMVPMatrix factor *must be first* in order
     65     // for the matrix multiplication product to be correct.
     66     Matrix.multiplyMM(scratch, 0, mMVPMatrix, 0, mRotationMatrix, 0);
     67 
     68     // Draw triangle
     69     mTriangle.draw(scratch);
     70 }
     71 </pre>
     72 
     73 <p>If your triangle does not rotate after making these changes, make sure you have commented out the
     74 {@link android.opengl.GLSurfaceView#RENDERMODE_WHEN_DIRTY GLSurfaceView.RENDERMODE_WHEN_DIRTY}
     75 setting, as described in the next section.</p>
     76 
     77 
     78 <h2 id="cont-render">Enable Continuous Rendering</h2>
     79 
     80 <p>If you have diligently followed along with the example code in this class to this point, make
     81 sure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL
     82 rotates the shape only one increment and then waits for a call to {@link
     83 android.opengl.GLSurfaceView#requestRender requestRender()} from the {@link
     84 android.opengl.GLSurfaceView} container:</p>
     85 
     86 <pre>
     87 public MyGLSurfaceView(Context context) {
     88     ...
     89     // Render the view only when there is a change in the drawing data.
     90     // To allow the triangle to rotate automatically, this line is commented out:
     91     <strong>//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);</strong>
     92 }
     93 </pre>
     94 
     95 <p>Unless you have objects changing without any user interaction, its usually a good idea have this
     96 flag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable
     97 once again.</p>
     98