天天看點

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
}      

除非你的程式沒有任何使用者交合,不然,通常情況下這個标示是需要打開的。