添加動畫
在屏幕上繪製物體是OpenGL最基本的特性,但是你也可用通過使用Android graphics 框架,包括Canvas和Drawable來完成這件事.OpenGL ES 爲三維空間的物體移動和變換提供了更多的能力或者以另外的獨特方式提供超乎想象的用戶體驗.
在這一課中,通過使用OpenGL ES添加運動讓物體旋轉,你又向前邁出了一步!
旋轉圖形
使用OpenGL ES2.0旋轉繪製圖相對比較簡單.在renderer中,創建另外一個變換矩陣(旋轉矩陣)並且將他與投影和相機視角矩陣組合:
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 設置,下面會講到.
打開連續渲染
到這裏爲止,如果你代碼與教程中的示例代碼一樣,確保你已經註釋掉"只在無效時渲染"的設置,否則OpenGL只會渲染圖形一次,然後等待GLSurfaceView的requestRender()方法的調用:
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);
}
除非你的繪製對象的變換不依賴用戶交互,否則通常情況下你需要打開這個設置.準備好取消這行代碼的註釋 ,下一課中會再次調用這個方法
下一篇: OpenGL ES|用戶交互