When I try to render my model using Vertex Arrays I get the following exception
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Required 1 remaining elements in buffer, only had 0
at com.sun.gluegen.runtime.BufferFactory.rangeCheck(BufferFactory.java:247)
at com.sun.opengl.impl.GLImpl.glNormalPointer(GLImpl.java:14963)
The exception points to this line of code…in my rendering method.
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i]);
The weird things is that I only get this exception for one of my models, while another is perfectly fine. I can’t figure out the problem.
Here is how I create the Vertex Arrays:
private void createVertexArray() {
vertices = new FloatBuffer[objects.size()];
for (int i = 0; i < objects.size(); i++) {
Obj tempObj = objects.get(i);
// Fill a buffer with vertex coordinates corresponding to 3 times the number of faces.
int totalBuffer = tempObj.numOfFaces * 3 * 3;
vertices[i] = ByteBuffer.allocateDirect(totalBuffer * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
for (int j = 0; j < tempObj.numOfFaces; j++) {
for (int whichVertex = 0; whichVertex < 3; whichVertex++) {
int index = tempObj.faces[j].vertIndex[whichVertex];
vertices[i].put(tempObj.verts[index].getX());
vertices[i].put(tempObj.verts[index].getY());
vertices[i].put(tempObj.verts[index].getZ());
}
}
vertices[i].rewind();
}
}
And during rendering I do the following:
private void render(GL gl) {
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
for (int i = 0; i < objects.size(); i++) {
Obj tempObj = objects.get(i);
int numFaceIndices = tempObj.numOfFaces * 3;
gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i]); // <===== IndexOutOfBoundsException here
gl.glDrawArrays(GL.GL_TRIANGLES, 0, numFaceIndices);
}
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}
The only thing I could figure is that somehow my vertices floatbuffers are not being properly created - ie they are missing data? I have no clue why that would happen for one model and not the other. As a I said, one model works perfectly fine but a different one doesn’t. I even stripped the code down to just the snippet above and the one model that works fine draws the faces as expected - I can see them, though they are all one color. On the other hand, the “bad” model gives me the exception: “IndexOutOfBoundsException: Required 1 remaining elements in buffer, only had 0”
What does that exception even mean?
Any thoughts?