OpenGL ES|添加动画

添加动画

在屏幕上绘制物体是OpenGL最基本的特性,但是你也可用通过使用Android graphics 框架,包括Canvas和Drawable来完成这件事.OpenGL ES 为三维空间的物体移动和变换提供了更多的能力或者以另外的独特方式提供超乎想象的用户体验.

在这一课中,通过使用OpenGL ES添加运动让物体旋转,你又向前迈出了一步!

旋转图形

使用OpenGL ES2.0旋转绘制图相对比较简单.在renderer中,创建另外一个变换矩阵(旋转矩阵)并且将他与投影和相机视角矩阵组合:

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只会渲染图形一次,然后等待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);
}
 
 

除非你的绘制对象的变换不依赖用户交互,否则通常情况下你需要打开这个设置.准备好取消这行代码的注释
,下一课中会再次调用这个方法

下一篇: OpenGL ES|用户交互

猜你喜欢

转载自blog.csdn.net/l491337898/article/details/80047474