This code is a little different from the WebGL version in that we no longer have the need to introduce a data type (Float32Array in WebGL) to hold an array of natively typed data. The Java ByteBuffer with native byte ordering fills this role.
183 protected int createBuffer(float[] vertexData) 184 { 185 int[] vertexBuffer = new int[1]; 186 187 // Store our model data in a float buffer. 188 FloatBuffer vertexBufferData; 189 190 // The OpenGL Driver expects floating point data in native byte order. 191 vertexBufferData = ByteBuffer.allocateDirect(vertexData.length * bytesPerFloat) 192 .order(ByteOrder.nativeOrder()).asFloatBuffer(); 193 194 vertexBufferData.put(vertexData).position(0); 195 196 // Generate a single buffer handle into element 0 of the array. 197 GLES20.glGenBuffers(1, vertexBuffer, 0); 198 199 // Binding an object in Open GL creates it, and makes it the target of subsequent manipulations. 200 GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer[0]); 201 202 // loads the current buffer, the vertexBuffer found above, with the vertex data. 203 GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, vertexBufferData.capacity() * bytesPerFloat, 204 vertexBufferData, GLES20.GL_STATIC_DRAW); 205 206 return vertexBuffer[0]; 207 }
The first step is to create the buffer and keep an integer reference to it. With OpenGL ES we invoke glGenBuffers to load one buffer handle into the vertexBuffer array. Though we follow the same structure as in the WebGL example and create each buffer individually, this leaves room for improvement by generating multiple buffers at once.
As always, bindBuffer creates a specific type of buffer, and makes that buffer the currently active buffer.
The last step loads the data into the buffer. The STATIC_DRAW option tells OpenGL that this data is not expected to change - allowing OpenGL to make several optimizations.