package com.yarin.android.Examples_12_05; import java.nio.ByteBuffer; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLUtils; import android.opengl.GLSurfaceView.Renderer; public class GLRender implements Renderer { float xrot, yrot, zrot; int texture = -1; int one = 0x10000; IntBuffer vertices = IntBuffer.wrap(new int[]{ -one,-one,one, one,-one,one, one,one,one, -one,one,one, -one,-one,-one, -one,one,-one, one,one,-one, one,-one,-one, -one,one,-one, -one,one,one, one,one,one, one,one,-one, -one,-one,-one, one,-one,-one, one,-one,one, -one,-one,one, one,-one,-one, one,one,-one, one,one,one, one,-one,one, -one,-one,-one, -one,-one,one, -one,one,one, -one,one,-one, }); IntBuffer texCoords = IntBuffer.wrap(new int[]{ one,0,0,0,0,one,one,one, 0,0,0,one,one,one,one,0, one,one,one,0,0,0,0,one, 0,one,one,one,one,0,0,0, 0,0,0,one,one,one,one,0, one,0,0,0,0,one,one,one, }); ByteBuffer indices = ByteBuffer.wrap(new byte[]{ 0,1,3,2, 4,5,7,6, 8,9,11,10, 12,13,15,14, 16,17,19,18, 20,21,23,22, }); @Override public void onDrawFrame(GL10 gl) { // �����Ļ����Ȼ��� gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); // ���õ�ǰ��ģ�͹۲���� gl.glLoadIdentity(); gl.glTranslatef(0.0f, 0.0f, -5.0f); //����3���������ת gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f); // ������ gl.glBindTexture(GL10.GL_TEXTURE_2D, texture); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); //������ı��ζ�Ӧ�Ķ��� gl.glVertexPointer(3, GL10.GL_FIXED, 0, vertices); gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, texCoords); //���� gl.glDrawElements(GL10.GL_TRIANGLE_STRIP, 24, GL10.GL_UNSIGNED_BYTE, indices); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); xrot+=0.5f; yrot+=0.6f; zrot+=0.3f; } @Override public void onSurfaceChanged(GL10 gl, int width, int height) { float ratio = (float) width / height; //����OpenGL�����Ĵ�С gl.glViewport(0, 0, width, height); //����ͶӰ���� gl.glMatrixMode(GL10.GL_PROJECTION); //����ͶӰ���� gl.glLoadIdentity(); // �����ӿڵĴ�С gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); // ѡ��ģ�͹۲���� gl.glMatrixMode(GL10.GL_MODELVIEW); // ����ģ�͹۲���� gl.glLoadIdentity(); } @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) { // ��ɫ���� gl.glClearColor(0, 0, 0, 0); gl.glEnable(GL10.GL_CULL_FACE); // ������Ӱƽ�� gl.glShadeModel(GL10.GL_SMOOTH); // ������Ȳ��� gl.glEnable(GL10.GL_DEPTH_TEST); //��������ӳ�� gl.glClearDepthf(1.0f); //��Ȳ��Ե����� gl.glDepthFunc(GL10.GL_LEQUAL); //��ϸ��͸������ gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); //����2D��ͼ,���� gl.glEnable(GL10.GL_TEXTURE_2D); IntBuffer intBuffer = IntBuffer.allocate(1); // �������� gl.glGenTextures(1, intBuffer); texture = intBuffer.get(); // ����Ҫʹ�õ����� gl.glBindTexture(GL10.GL_TEXTURE_2D, texture); //�������� GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, GLImage.mBitmap, 0); // �����˲� gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); } }