绘制多边形
1.glVertexPointer (int size, int type, int stride, Buffer pointer)
第一个参数表示坐标的维数,可以是2或者3,如果是2,则坐标为(x,y),z轴默认为0;如果是3,则坐标为(x,y,z)
第二个参数可以是GL10.GL_FIXED或者GL10.GL_FLOAT,如果是GL10.GL_FIXED,则第四个参数为IntBuffer类 型,如果为GL10.GL_FLOAT,则第四个参数为FloatBuffer类型
第三个参数表示步长
2.glDrawArrays (int mode, int first, int count)
第一个参数有三种类型GL10.GL_TRIANGLES、GL10.GL_TRIANGLE_FAN、GL10.GL_TRIANGLE_STRIP
GL_TRIANGLES:每三个顶之间绘制三角形,之间不连接
GL_TRIANGLE_FAN:以V0V1V2,V0V2V3,V0V3V4,……的形式绘制三角形
GL_TRIANGLE_STRIP:顺序在每三个顶点之间均绘制三角形。这个方法可以保证从相同的方向上所有以三角形均被绘制。以V0V1V2,V1V2V3,V2V3V4……的形式绘制三角形
3.代码
- package com.lanhuidong.opengl;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- import javax.microedition.khronos.egl.EGLConfig;
- import javax.microedition.khronos.opengles.GL10;
- import android.opengl.GLSurfaceView.Renderer;
- import com.lanhuidong.opengl.util.BufferToNativeOrder;
- public class OpenGLRender implements Renderer {
- private static int x = 0x10000 / 4;
- private static float f = 0.5f;
- private static float[] array = new float[] { -f, 0, 0, 0, (float) (f * Math.sqrt(3) / 2), 0, f, 0, 0 };
- private static int[] array2 = new int[] { x, x, 0, x, -x, 0, -x, -x, 0, -x, x, 0 };
- private static int[] array3 = new int[] { x, x, 0, -x, x, 0, -2 * x, 0, 0, -x, -x, 0, x, -x, 0, 2 * x, 0, 0 };
- private static float[] colorarray = new float[] { 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1 };
- /**
- * 调用此方法绘制当前窗口
- */
- @Override
- public void onDrawFrame(GL10 gl) {
- gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
- gl.glLoadIdentity();
- gl.glTranslatef(-0.5f, 0.5f, -1.0f);
- // 画三角行
- gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
- FloatBuffer triangleBuffer = BufferToNativeOrder.getNativeOrderFloatBuffer(array);
- gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer);//
- gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
- gl.glLoadIdentity();
- gl.glTranslatef(0.5f, 0.5f, -1.0f);
- // 画矩形
- IntBuffer quadrangleBuffer = BufferToNativeOrder.getNativeOrderIntBuffer(array2);
- gl.glVertexPointer(3, GL10.GL_FIXED, 0, quadrangleBuffer);
- gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
- gl.glLoadIdentity();
- gl.glTranslatef(-0.5f, -0.5f, -1.0f);
- // 六边形
- gl.glVertexPointer(3, GL10.GL_FIXED, 0, BufferToNativeOrder.getNativeOrderIntBuffer(array3));
- gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 6);
- gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
- }
- /**
- * 窗口被创建或者窗口大小改变时被调用
- */
- @Override
- public void onSurfaceChanged(GL10 gl, int width, int height) {
- gl.glViewport(0, 0, width, height);
- float ratio = (float) width / height;
- gl.glMatrixMode(GL10.GL_PROJECTION);
- gl.glLoadIdentity();
- gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
- }
- /**
- * 窗口被创建或者被重新创建时调用,做初始化工作
- */
- @Override
- public void onSurfaceCreated(GL10 gl, EGLConfig config) {
- gl.glClearColor(0, 0, 1.0f, 0.5f);
- }
- }
- package com.lanhuidong.opengl.util;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.IntBuffer;
- public class BufferToNativeOrder {
- public static IntBuffer getNativeOrderIntBuffer(int[] array){
- ByteBuffer buffer = ByteBuffer.allocateDirect(array.length * 4);
- buffer.order(ByteOrder.nativeOrder());
- IntBuffer intBuffer = buffer.asIntBuffer();
- intBuffer.put(array);
- intBuffer.position(0);
- return intBuffer;
- }
- public static FloatBuffer getNativeOrderFloatBuffer(float[] array){
- ByteBuffer buffer = ByteBuffer.allocateDirect(array.length * 4);
- buffer.order(ByteOrder.nativeOrder());
- FloatBuffer floatBuffer = buffer.asFloatBuffer();
- floatBuffer.put(array);
- floatBuffer.position(0);
- return floatBuffer;
- }
- }
效果: