天天看点

Android Training - 使用OpenGL ES(5) - 添加动作

绘制对象在屏幕上是OpenGL最基本的功能,不过你也可以使用Canvas和Drawable对象实现。OpenGL ES提供额外的能力来让你在三维空间中移动和转换绘制对象,提供用户体验。

旋转一个形状 为了旋转形状,你需要创建另外一个转换矩阵,然后合并到投影和摄像视图转换矩阵中:

private float[] mRotationMatrix = new float[16];
public void onDrawFrame(GL10 gl) {
    ...
    // Create a rotation transformation for the triangle
    long time = SystemClock.uptimeMillis() % 4000L;
    float angle = 0.090f * ((int) time);
    Matrix.setRotateM(mRotationMatrix, 0, mAngle, 0, 0, -1.0f);

    // Combine the rotation matrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    // Draw triangle
    mTriangle.draw(mMVPMatrix);
}      

开启持续渲染 如果你跟随教程做的练习,那么你要确保注释掉了下面的语句,不然的话旋转时只有一个增量,然后等待requestRender()的调用:

public MyGLSurfaceView(Context context) {
    ...
    // Render the view only when there is a change in the drawing data
    //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // comment out for auto-rotation
}      

除非你的程序没有任何用户交合,不然,通常情况下这个标示是需要打开的。