android OpenGLES开发第四课 绘制一个六棱柱

来源:互联网 发布:谷歌输入法 mac 编辑:程序博客网 时间:2024/05/16 02:12

前几课我们绘制的是平面的图形,下面我们要绘制一个立体的图形——六棱柱,这里我们使用了顶点索引,这是一个新的概念,它是一个指明了顶点绘制顺序的ByteBuffer,还有就是这里我们使用的是二维的顶点数组,其中第二维中所有的顶点是同一个平面上的点,所以我们是一个面一个面的绘制,Polygon 类改动的代码较多,其他类基本不变,下面一一描述:

public class Polygon {// 保存每一个平面的顶点索引的ByteBuffer数组private ByteBuffer[] indexBuffer;// 保存每一个平面的顶点坐标的FloatBuffer数组private FloatBuffer[] faceVertexBuffer;// 保存每一个平面的顶点坐标的二维数组private float[][] faceVertices = { new float[] { -0.5f, -0.816f, 1.0f,// bottom// left// 00.5f, -0.816f, 1.0f,// bottom right 1-1.0f, 0.0f, 1.0f,// middle left 21.0f, 0.0f, 1.0f,// middle right 3-0.5f, 0.816f, 1.0f, // top left 40.5f, 0.816f, 1.0f // top right 5},// front facenew float[] { -0.5f, -0.816f, -1.0f,// bottom left 60.5f, -0.816f, -1.0f,// bottom right 7-1.0f, 0.0f, -1.0f,// middle left 81.0f, 0.0f, -1.0f,// middle right 9-0.5f, 0.816f, -1.0f, // top left 100.5f, 0.816f, -1.0f // top right 11},// back facenew float[] { -0.5f, -0.816f, -1.0f,// bottom left 60.5f, -0.816f, -1.0f,// bottom right 7-0.5f, -0.816f, 1.0f,// bottom left 00.5f, -0.816f, 1.0f // bottom right 1},// bottom facenew float[] { -1.0f, 0.0f, -1.0f,// middle left 8-0.5f, -0.816f, -1.0f,// bottom left 6-1.0f, 0.0f, 1.0f,// middle left 2-0.5f, -0.816f, 1.0f // bottom left 0},// bottom left facenew float[] { 0.5f, -0.816f, -1.0f,// bottom right 71.0f, 0.0f, -1.0f,// middle right 90.5f, -0.816f, 1.0f,// bottom right 11.0f, 0.0f, 1.0f // middle right 3},// bottom right facenew float[] { 0.5f, 0.816f, -1.0f, // top right 111.0f, 0.0f, -1.0f,// middle right 90.5f, 0.816f, 1.0f,// top right 51.0f, 0.0f, 1.0f // middle right 3},// top right facenew float[] { -0.5f, 0.816f, -1.0f, // top left 100.5f, 0.816f, -1.0f, // top right 11-0.5f, 0.816f, 1.0f,// top left 40.5f, 0.816f, 1.0f // top right 5},// top facenew float[] { -0.5f, 0.816f, -1.0f, // top left 10-1.0f, 0.0f, -1.0f,// middle left 8-0.5f, 0.816f, 1.0f, // top left 4-1.0f, 0.0f, 1.0f // middle left 2} // top left face};// 定义每一个平面颜色的数组private float[] colors = { 1.0f, 0.0f, 0.0f, 1.0f, // 00.0f, 1.0f, 0.0f, 1.0f,// 10.0f, 0.0f, 1.0f, 1.0f, // 21.0f, 1.0f, 1.0f, 1.0f, // 31.0f, 1.0f, 0.0f, 1.0f,// 41.0f, 0.0f, 1.0f, 1.0f,// 50.0f, 1.0f, 1.0f, 1.0f,// 60.5f, 0.5f, 0.5f, 1.0f, // 7};// 保存每一个平面的顶点索引的二维数组private byte[][] indies = {new byte[] { 0, 1, 2, 1, 3, 2, 2, 3, 4, 3, 5, 4 },// front facenew byte[] { 0, 1, 2, 1, 3, 2, 2, 3, 4, 3, 5, 4 },// back facenew byte[] { 0, 1, 2, 1, 3, 2 },// bottom facenew byte[] { 0, 1, 2, 1, 3, 2 },// bottom left facenew byte[] { 0, 1, 2, 1, 3, 2 },// bottom right facenew byte[] { 0, 1, 2, 1, 3, 2 },// top right facenew byte[] { 0, 1, 2, 1, 3, 2 },// top facenew byte[] { 0, 1, 2, 1, 3, 2 } // top left face};public Polygon() {// 利用循环初始化顶点坐标faceVertexBuffer数组和顶点索引indexBuffer数组ByteBuffer bb;faceVertexBuffer = new FloatBuffer[8];for (int i = 0; i < faceVertices.length; i++) {bb = ByteBuffer.allocateDirect(faceVertices[i].length * 4);bb.order(ByteOrder.nativeOrder());faceVertexBuffer[i] = bb.asFloatBuffer();faceVertexBuffer[i].put(faceVertices[i]);faceVertexBuffer[i].position(0);}indexBuffer = new ByteBuffer[8];for (int i = 0; i < indies.length; i++) {indexBuffer[i] = ByteBuffer.allocateDirect(indies[i].length);indexBuffer[i].put(indies[i]);indexBuffer[i].position(0);}}public void draw(GL10 gl) {// 利用循环绘制六棱柱的每一个面,并给不同的面设置不同的颜色gl.glFrontFace(GL10.GL_CW);for (int i = 0; i < 8; i++) {gl.glVertexPointer(3, GL10.GL_FLOAT, 0, faceVertexBuffer[i]);gl.glColor4f(colors[4 * i + 0], colors[4 * i + 1],colors[4 * i + 2], colors[4 * i + 3]);gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);gl.glDrawElements(GL10.GL_TRIANGLES, indies[i].length,GL10.GL_UNSIGNED_BYTE, indexBuffer[i]);// 另一种绘制的方法glDrawElementsgl.glDisableClientState(GL10.GL_VERTEX_ARRAY);}}}