原文: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)也能實(shí)現(xiàn)同樣的效果朵逝。OpenGL ES能提供更好的用戶體驗(yàn)。OpenGL ES能夠在三維空間或者其他獨(dú)特的方式移動(dòng)和變化圖形恶守。
In this lesson, you take another step forward into using OpenGL ES by learning how to add motion to a shape with rotation.
這一節(jié),進(jìn)一步學(xué)習(xí)如何用OpenGL ES移動(dòng)圖形眠砾。
Rotate a Shape
旋轉(zhuǎn)圖形
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旋轉(zhuǎn)圖形是小菜一碟校焦。在render中,創(chuàng)建另一個(gè)轉(zhuǎn)換矩陣用來旋轉(zhuǎn)全谤,然后和投影和相機(jī)矩陣進(jìn)行結(jié)合。
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.
如果做了以上的改變?nèi)切芜€是沒有旋轉(zhuǎn)爷贫,檢查一下是否注釋了下一節(jié)會(huì)提到的GLSurfaceView.RENDERMODE_WHEN_DIRTY這一句认然。
Enable Continuous Rendering
開啟連續(xù)繪制
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:
如果你按教程走到這里,確定你已經(jīng)把繪制模式改為連續(xù)繪制模式漫萄,不然OpenGL只會(huì)把圖形旋轉(zhuǎn)一個(gè)角度之后就不再旋轉(zhuǎn)了卷员,只有等待調(diào)用GLSurfaceView的requestRender() 方法時(shí)才會(huì)繼續(xù)旋轉(zhuǎn)。
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.
如果沒有用戶輸入的情況下腾务,圖形仍會(huì)改變毕骡,那么就關(guān)閉這個(gè)模式,否則就開啟岩瘦。下一節(jié)中未巫,我們會(huì)用到這行代碼。