在屏幕上绘制对象是OpenGL最基本的功能,但是你还可以用其他Android图形框架类做到这一点,包括Canvas和Drawable。OpenGL ES 提供了在三维里移动和转换绘制对象的额外功能,或其他独一无二的令人惊叹的用户体验。
这一节,你将再向前迈进一步用OpenGL ES 去给图形添加motion。
1.Rotate a Shape
用OpenGL ES 2.0 做图片旋转相当简单,在你的渲染器里,创建另一个变换矩阵 (旋转矩阵) ,然后用你的投影和相机视图变换矩阵结合起来:
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);
}
如果做出这些改变后您的三角形没有转动,请确保您已注释掉GLSurfaceView.RENDERMODE_WHEN_DIRTY设置,如下一节中所述。
二.Enable Continuous Rendering
如果你确保照着示例代码那样,确保您注释掉设置渲染模式那一行只是在dirty时才画,否则OpenGL旋转的这个形状只会有一次,然后等待到requestRender()从GLSurfaceView容器调用:
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:
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
除非无任何用户交互的变化,开启这个标志是明智的。准备去掉这一代码注释,因为下一课应用只调用一次。