Android OpenGL ES 開發(六): OpenGL ES 添加運動效果


在屏幕上繪制圖形只是OpenGL的相當基礎的特點,你也可以用其他的Android圖形框架類來實現這些,包括Canvas和Drawable對象。OpenGL ES為在三維空間中移動和變換提供了額外的功能,並提供了創建引人注目的用戶體驗的獨特方式。
在本文中,你將進一步使用OpenGL ES學習怎樣為你的圖形添加一個旋轉動作。

一、旋轉一個圖形

用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設置項,這將在下一部分講到。

二、允許連續渲染

如果你勤懇地遵循本系列課程的示例代碼到這個點,請確保你注釋了設置只有當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);
}

除非你的對象改變沒有用戶交互,否則通常打開這個標志是個好主意。准備好取消注釋這行代碼,因為下一節內容將使這個調用再次適用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM