OpenGL ES|Add animation

Add animation

Drawing objects on the screen is the most basic feature of OpenGL, but you can also do this by using Android graphics frameworks, including Canvas and Drawable. OpenGL ES provides more capabilities for moving and transforming objects in three-dimensional space or Another unique way to provide unimaginable user experience.

In this lesson, you've taken another step forward by adding motion to rotate objects using OpenGL ES!

Rotate graphics

Using OpenGL ES2.0 to rotate a drawing is relatively simple. In the renderer, create another transformation matrix (rotation matrix) and combine it with the projection and camera view matrices:

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);
}

If the triangle is not rotated after using these transforms, check to see if it is used GLSurfaceView.RENDERMODE_WHEN_DIRTY 设置,下面会讲到.

Turn on continuous rendering

So far, if your code is the same as the example code in the tutorial, make sure you have commented out the "Render only when invalid" setting, otherwise OpenGL will only render the graphics once and wait for the GLSurfaceView's requestRender() method to be called:

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);
}
 
 

Unless the transformation of your drawable object does not depend on user interaction, you will usually want to turn this setting on. Be prepared to uncomment this line of code
, this method will be called again in the next lesson

Next:  OpenGL ES|User Interaction

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811872&siteId=291194637