OpenGL ES

(五)移动图形(Adding Motion)

2017-03-21  本文已影响81人  thebestofrocky

原文:https://developer.android.com/training/graphics/opengl/motion.html

Drawing objects on screen is a pretty basic feature of OpenGL, but you can do this with other Android graphics framwork classes, including Canvas 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.
绘制图形只是OpenGL的基本功能,用其他的Android 绘图类(如Canvas和Drawable)也能实现同样的效果。OpenGL ES能提供更好的用户体验。OpenGL ES能够在三维空间或者其他独特的方式移动和变化图形。

In this lesson, you take another step forward into using OpenGL ES by learning how to add motion to a shape with rotation.
这一节,进一步学习如何用OpenGL ES移动图形。

Rotate a Shape

旋转图形

Rotating a drawing object with OpenGL ES 2.0 is relatively simple. In your renderer, create another transformation matrix (a rotation matrix) and then combine it with your projection and camera view transformation matrices:
用OpenGL ES旋转图形是小菜一碟。在render中,创建另一个转换矩阵用来旋转,然后和投影和相机矩阵进行结合。

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

If your triangle does not rotate after making these changes, make sure you have commented out the GLSurfaceView.RENDERMODE_WHEN_DIRTY setting, as described in the next section.
如果做了以上的改变三角形还是没有旋转,检查一下是否注释了下一节会提到的GLSurfaceView.RENDERMODE_WHEN_DIRTY这一句。

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 to requestRender() from the GLSurfaceView container:
如果你按教程走到这里,确定你已经把绘制模式改为连续绘制模式,不然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);
}

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.
如果没有用户输入的情况下,图形仍会改变,那么就关闭这个模式,否则就开启。下一节中,我们会用到这行代码。

上一篇下一篇

猜你喜欢

热点阅读