继续正方形,让它旋转起来:
public class GL3 extends Activity { private GLSurfaceView mGLSurfaceView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mGLSurfaceView = new GLSurfaceView(this); mGLSurfaceView.setRenderer(new Three()); setContentView(mGLSurfaceView); } @Override protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); } } class Three implements Renderer { private Square square; private float rquad; public Three(){ square = new Square(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { gl.glShadeModel(GL10.GL_SMOOTH); gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); gl.glClearDepthf(1.0f); gl.glEnable(GL10.GL_DEPTH_TEST); gl.glDepthFunc(GL10.GL_LEQUAL); gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { if(height == 0){ height = 1; } gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); } @Override public void onDrawFrame(GL10 gl) { Log.i("onDrawFrame", "........." + rquad); gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -6.0f); /** * 旋转函数 * 用当前的矩阵乘一个旋转矩阵,将值作为当前矩阵 * * rquad为旋转角度,不断递增 */ // gl.glRotatef(rquad, 1.0f, 0.0f, 0.0f); //围绕X轴旋转 // gl.glRotatef(rquad, 0.0f, 1.0f, 0.0f); //围绕Y轴旋转 gl.glRotatef(rquad, 0.0f, 0.0f, 1.0f); //围绕Z轴旋转 square.draw(gl); gl.glLoadIdentity(); rquad += 0.5f; } } class Square { private FloatBuffer vertexBuffer; private float vertices[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f }; public Square() { ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4); byteBuf.order(ByteOrder.nativeOrder()); vertexBuffer = byteBuf.asFloatBuffer(); vertexBuffer.put(vertices); vertexBuffer.position(0); } public void draw(GL10 gl) { gl.glFrontFace(GL10.GL_CW); gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //这里给正方形设置了颜色RGBA值 gl.glColor4f(0.5f, 0.5f, 1.0f, 1.0f); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } }
画正方形注释,见:http://yuanyao.iteye.com/blog/782474