Displaying Graphics with OpenGL ES (五)——Add Motion

在屏幕上绘制对象是OpenGL最基本的功能,但是你还可以用其他Android图形框架类做到这一点,包括Canvas和Drawable。OpenGL ES 提供了在三维里移动和转换绘制对象的额外功能,或其他独一无二的令人惊叹的用户体验。

这一节,你将再向前迈进一步用OpenGL ES 去给图形添加motion。

1.Rotate a Shape

用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设置,如下一节中所述。

二.Enable Continuous Rendering

如果你确保照着示例代码那样,确保您注释掉设置渲染模式那一行只是在dirty时才画,否则OpenGL旋转的这个形状只会有一次,然后等待到requestRender()从GLSurfaceView容器调用:

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

除非无任何用户交互的变化,开启这个标志是明智的。准备去掉这一代码注释,因为下一课应用只调用一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值