Adding Motion 添加动作

Drawing objects on screen is a pretty basic feature of OpenGL, but you can do this with other Android graphics framwork classes, includingCanvas and Drawable objects. OpenGL ES provides additional capabilities for moving and transforming drawn objects in three dimensions or in other unique ways to create compelling user experiences.http://blog.csdn.net/sergeycao

In this lesson, you take another step forward into using OpenGL ES by learning how to add motion to a shape with rotation.

Rotate a Shape

Rotating a drawing object with OpenGL ES 2.0 is relatively simple. You create another transformation matrix (a rotation matrix) and then combine it with your projection and camera view tranformation matrices:

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

If your triangle does not rotate after making these changes, make sure you have commented out theGLSurfaceView.RENDERMODE_WHEN_DIRTY setting, as described in the next section.

Enable Continuous Rendering

If you have diligently followed along with the example code in this class to this point, make sure you comment out the line that sets the render mode only draw when dirty, otherwise OpenGL rotates the shape only one increment and then waits for a call torequestRender() from the GLSurfaceView container:

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
}

Unless you have objects changing without any user interaction, it’s usually a good idea have this flag turned on. Be ready to uncomment this code, because the next lesson makes this call applicable once again.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值