OpenGl学习第三章

来源:互联网 发布:系统仿真软件arena 编辑:程序博客网 时间:2024/06/10 06:15

上个星期学习了下金字塔的绘制,然后这周有看了看正方体的绘制,通过依次绘制6个面来绘制正方体,整体代码跟上次的金三角没有区别,只是坐标点,颜色,已经绘制的代码有点区别,上代码看看

//这是坐标

private static float[][] cubeVertexCoords = new float[][] {
new float[] { // top
1, 1,-1,
-1, 1,-1,
-1, 1, 1,
1, 1, 1
},
new float[] { // bottom
1,-1, 1,
-1,-1, 1,
-1,-1,-1,
1,-1,-1
},
new float[] { // front
1, 1, 1,
-1, 1, 1,
-1,-1, 1,
1,-1, 1
},
new float[] { // back
1,-1,-1,
-1,-1,-1,
-1, 1,-1,
1, 1,-1
},
new float[] { // left
-1, 1, 1,
-1, 1,-1,
-1,-1,-1,
-1,-1, 1
},
new float[] { // right
1, 1,-1,
1, 1, 1,
1,-1, 1,
1,-1,-1
},
};

//这是颜色
private static float[] cubeColors = new float[] {
0,1,0,1,
1,0.5f,0,1,
1,0,0,1,
1,1,0,1,
0,0,1,1,
1,0,1,1
};


然后必须要把呀转化为FloatBuffer  ,这样就可以使用了

private static FloatBuffer[] cubeVertexBfr;

private static FloatBuffer  cubeColorBfr;


转化我写了个转化的工具类

public  class BufferUtil {


private static FloatBuffer floatBuffer;

public static FloatBuffer  floatToBuffer(float[] f){

ByteBuffer  buffer  =ByteBuffer.allocateDirect(f.length*4);

buffer.order(ByteOrder.nativeOrder());
floatBuffer  =buffer.asFloatBuffer();
floatBuffer.put(f);
floatBuffer.position(0);
return floatBuffer;
}



下边是绘制的代码


gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//在上边打开在下边就要关闭,这个原因是然后边的绘制不会复用前边的颜色等的样式

gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
for (int i = 0; i < 6; i++) // draw each face
{
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeVertexBfr[i]);

gl.glColorPointer(4, GL10.GL_FLOAT, 0, cubeColorBfr);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, 4);
}
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

}




0 0
原创粉丝点击