OpenGL (ES) -- glBindBuffer throws IllegalArgumentException: remaining < size()
I've made a buffer of vertices that correc开发者_运维问答tly draw when using glDrawArrays, however they fail to load into a VBO. Here's the code:
FloatBuffer circleBuffer = ByteBuffer.allocateDirect(numVertices * 3 *
4).order(ByteOrder.nativeOrder()).asFloatBuffer();
for (int j = 0; j < numVertices; j++) {
circleBuffer.put((float) (Math.cos(theta)));
circleBuffer.put((float) (Math.sin(theta)));
circleBuffer.put(1);
theta += 2 * Math.PI / (numVertices);
}
int[] buffer = new int[1];
int circleIndex=0;
gl11.glGenBuffers(1, buffer,0);
circleIndex = buffer[0];
gl11.glBindBuffer(GL11.GL_ARRAY_BUFFER, circleIndex);
gl11.glBufferData(GL11.GL_ARRAY_BUFFER, circleBuffer.capacity() * 4,
circleBuffer, GL11.GL_STATIC_DRAW);
I outputed the capacity of the buffer and it is 105, and the remaining is 0. I also tried reassigning the FloatBuffer as a Buffer. What's wrong here? Thanks!
ERROR/AndroidRuntime(7127): java.lang.IllegalArgumentException: remaining() < size
ERROR/AndroidRuntime(7127): at com.google.android.gles_jni.GLImpl.glBufferData(Native Method)
EDIT -- Solution
buffer.flip();
A Java exception which should be deliberately thrown by methods that don't like their parameters. It extends RuntimeException, which means it does not need to be caught. The singular name notwithstanding, can represent unsatisfied constraint between more parameters. The more you use and check the parameters, the more you move towards exception in the method invocation proper. In many cases, code that is throwing NullPointerException should be argument-checking and throwing this, with a decent explanitory message.
精彩评论