OpenGl學習第三章
發表時間:2020-10-19
發布人:葵宇科技
浏覽次數:42
上個(gè)禮拜進修了下(xià)金字塔的繪制,然後這周有看了看正方體的繪制,經由過程依次繪制6個(gè)面來繪制正方體,整體代碼跟前次的金三角沒有差别,隻是坐(zuò)标點,色彩,已經繪制的代碼有點差别,上代碼看看
//這是坐(zuò)标
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 ,如(rú)許就可(kě)以應用了
private static FloatBuffer[] cubeVertexBfr;
private static FloatBuffer cubeColorBfr;
轉化我寫了個(gè)轉化的對象類
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;
}
下(xià)邊是繪制的代碼
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//在上邊打開鄙人邊就要封閉,這個(gè)原因是然河畔的繪制不會複竽暌姑前邊的色彩等的樣式
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);
}