Android---OpenGL ES之添加动作

本文译自:http://developer.android.com/training/graphics/opengl/motion.html

在屏幕上绘制对象是OpenGL的最基本功能,你可以使用其他的Android图形框架类,如CanvasDrawable对象来完成这些功能。OpenGLES提供了一些用于在三维空间中移动和变换被绘制的对象的能力,以及其他的创建良好用户体验的独特方式。

在本文中,你需要使用前面几篇博文中介绍的示例,给图形添加旋转动作。

旋转图形

使用OpenGL ES 2.0来旋转一个绘制对象是相对简单的。你要创建另外的变换矩阵(旋转矩阵),然后把它跟投影和摄像机变换矩阵组合到一起:

privatefloat[]mRotationMatrix =newfloat[16];
public
void onDrawFrame(GL10 gl) {
    ...
    // Create a rotationtransformation 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 rotationmatrix with the projection and camera view
    Matrix.multiplyMM(mMVPMatrix, 0, mRotationMatrix, 0, mMVPMatrix, 0);

    // Draw triangle
    mTriangle.draw(mMVPMatrix);
}

做了这些改变之后,如果你的三角形没有旋转,那么就要确认你是否完成了GLSurfaceView.RENDERMODE_WHEN_DIRTY设置。

启用连续的渲染

如果你一直跟随在学习本文的示例代码,那么要确保像下面代码那样,注释掉对渲染模式的设置,否则OpenGL只会旋转图形一次,然后等待来自GLSurfaceView容器的requestRender()方法的调用。

publicMyGLSurfaceView(Context context){
   
...
    // Render the view onlywhen there is a change in the drawing data
    //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);// comment out for auto-rotation
}

除非对象的变化跟任何用户交互无关,否则设置一个开关标记是一个好主意。

原文地址:https://www.cnblogs.com/pangblog/p/3296978.html