0
美好的一天我的朋友,Android/OpenglES2/examples:爲什麼要使用ByteBuffer進行座標?
我是新來的Android和Java一般,所以對於noob問題感到抱歉。 我遇到了標準的OpenGL ES的例子,並試圖理解這個代碼:
static float wallCoords[] = {
// in counterclockwise order:
-0.5f, 0.6f, 0.0f, // top left
-0.5f, 0.5f, 0.0f, // bottom left
0.5f, 0.5f, 0.0f, // bottom right
0.5f, 0.6f, 0.0f, // top right
};
private final int vertexCount = wallCoords.length/COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 0.0f };
private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
private final short drawOrder[] = { 0, 1, 2, 0, 2, 3 }; // order to draw vertices
...
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
wallCoords.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(wallCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
所以我的問題是,爲什麼不使用例如FloatBuffer.wrap()方法:
vertexBuffer = FloatBuffer.wrap(wallCoords);
謝謝!
是的,正確:)只是發現了這個解釋。然而,這並不能解釋爲什麼沒有舒適的API。無論如何,緩衝區和數組在這種情況下非常緊密(坦率地說,在Android中它總是如此)。 –