这一期主要来看看基于OpenGL ES的动画原理,我们都知道在View上实现动画,需要在View.onDraw方法中不断刷新状态就可以产生动画效果。而在OpenGL ES中实现动画效果并不需要不断刷新View,通过glTranslatef方法可以实现图形移位的动画效果,而glRotatef方法可以实现图形旋转的动画效果。
下面是给出的主要代码,值得注意的是,旋转四个小矩形的glRotatef方法必须放在glTranslatef方法之前,否则将会绕着自己的中心旋转,而不会绕著Z轴旋转。
package mobile.android.rotate.rectangle;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class RectangleSurfaceView implements Renderer
{
private Rectangle rectangle;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
rectangle = new Rectangle();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height)
{
float ratio = (float) width / height;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glFrustumf(-ratio * 2, ratio * 2, -2, 2, 1, 10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
private int angle1 = 0;
private int angle2 = 0;
@Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -2.0f);
gl.glRotatef(angle1++, 0, 0, 1);
rectangle.drawSelf(gl);
gl.glLoadIdentity();
angle2 -= 2;
gl.glRotatef(angle2, 0, 0, 1);
gl.glTranslatef(-3.0f, 0.0f, -4.0f);
gl.glRotatef(45, 0.0f, 0.0f, 1.0f);
rectangle.drawSelf(gl);
gl.glLoadIdentity();
gl.glRotatef(angle2, 0, 0, 1);
gl.glTranslatef(3.0f, 0.0f, -4.0f);
gl.glRotatef(45, 0.0f, 0.0f, 1.0f);
rectangle.drawSelf(gl);
gl.glLoadIdentity();
gl.glRotatef(angle2, 0, 0, 1);
gl.glTranslatef(0.0f, 3.0f, -4.0f);
gl.glRotatef(45, 0.0f, 0.0f, 1.0f);
rectangle.drawSelf(gl);
gl.glLoadIdentity();
gl.glRotatef(angle2, 0, 0, 1);
gl.glTranslatef(0.0f, -3.0f, -4.0f);
gl.glRotatef(45, 0.0f, 0.0f, 1.0f);
rectangle.drawSelf(gl);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
}