图片和图形之添加Motion(14)

原文

概要


在屏幕上绘制的对象是OpenGL的一个非常基本的功能,但你可以与其他Android图形framwork类,包括做这个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 设置,如下一节所述。

启用连续渲染


如果你坚持跟这个类的示例代码一起到这一点,确保你注释掉设置渲染模式只在脏时绘制的行,否则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);
}

除非在没有任何用户交互的情况下更改对象,否则打开此标志通常是一个好主意。准备取消注释此代码,因为下一课再次使此调用适用。

Lastest Update:2018.04.25

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

图片和图形之添加Motion(14)

猜你喜欢

转载自blog.51cto.com/4789781/2120587
今日推荐