openGL ES主要是用于嵌入式设备的3D图形的绘制
GLSurfaceView GLSurfaceView.Renderer
效果如下:
直接代码:
1、GLRender.java
package wu.demo.www; import java.nio.IntBuffer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import android.opengl.GLSurfaceView.Renderer; public class GLRender implements Renderer { int one = 0x10000; //三角形的三个顶点 private int[] triggerBuffer = { 0, one, 0, //上顶点 -one, -one, 0, //下左 one, -one, 0, //下右 }; private int[] quaterBuffer = { -one, one, 0, one, one, 0, -one, -one, 0, one, -one, 0 }; private IntBuffer getVertexBuffer(int capacity, int sum[]) { IntBuffer vertexBuffer = BufferFactory.createIntBuffer(capacity).put(sum); vertexBuffer.position(0); return vertexBuffer; } public void onDrawFrame(GL10 gl) { //清除屏幕和深度缓存 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity(); //移动顶点,左移1.5,向里6个单位 gl.glTranslatef(-1.5f, 0.0f, -6.0f); //移动好了后,设置顶点 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //绘制三角形 gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(9, triggerBuffer)); gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); //重置观察矩阵 gl.glLoadIdentity(); //移动顶点,移动到右边 gl.glTranslatef(1.5f, 0f, -6f); //绘制四边形 gl.glVertexPointer(3, GL10.GL_FIXED, 0, getVertexBuffer(12, quaterBuffer)); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); //取消顶点设置 gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); } 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, ratio, -1, 1, 1, 10); gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); } public void onSurfaceCreated(GL10 gl, EGLConfig config) { //告诉 系统对透视进行修正 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); //白色背景 gl.glClearColor(0, 0, 0, 0); //启用阴影平滑 gl.glShadeModel(GL10.GL_SMOOTH); //设置深度缓存 gl.glClearDepthf(1.0f); //启用深度测试 gl.glEnable(GL10.GL_DEPTH_TEST); //所做深度测试 的类型 gl.glDepthFunc(GL10.GL_LEQUAL); } }
package wu.demo.www; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.nio.IntBuffer; import java.nio.ShortBuffer; /** * A utility class to create buffers. * * All public methods are static. The Singleton pattern was avoided to avoid concerns about * threading and the Android life cycle. If needed, It can be implemented later given some research. */ public class BufferFactory { // This class cannot and should not be instantiated private BufferFactory() {} // We use Buffer.allocateDirect() to get memory outside of // the normal, garbage collected heap. I think this is done // because the buffer is subject to native I/O. // See http://download.oracle.com/javase/1.4.2/docs/api/java/nio/ByteBuffer.html#direct /** * Creates a buffer of floats using memory outside the normal, garbage collected heap * * @param capacity The number of primitives to create in the buffer. */ public static FloatBuffer createFloatBuffer(int capacity) { // 4 is the number of bytes in a float ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4); vbb.order(ByteOrder.nativeOrder()); return vbb.asFloatBuffer(); } /** * Creates a buffer of shorts using memory outside the normal, garbage collected heap * * @param capacity The number of primitives to create in the buffer. */ public static ShortBuffer createShortBuffer(int capacity) { // 2 is the number of bytes in a short ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 2); vbb.order(ByteOrder.nativeOrder()); return vbb.asShortBuffer(); } public static IntBuffer createIntBuffer(int capacity) { ByteBuffer vbb = ByteBuffer.allocateDirect(capacity * 4); vbb.order(ByteOrder.nativeOrder()); return vbb.asIntBuffer(); } }
package wu.demo.www; import android.app.Activity; import android.opengl.GLSurfaceView; import android.opengl.GLSurfaceView.Renderer; import android.os.Bundle; public class OpenGLTest1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Renderer render = new GLRender(); GLSurfaceView glView = new GLSurfaceView(this); glView.setRenderer(render); setContentView(glView); } }