? Vertex Arrays - Index Out of Bounds issue ?

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?

Let me add one more thing…

I use the same ‘createVertexArray()’ to create the vertices FloatBuffer which I then use in rendering in VBO mode (as opposed to Vertex Array mode). And I have NO PROBLEMS rendering the VBOs. So it is weird that the buffers were created the same way and they work for VBOs but only sometimes for the Vertex Array…using the exact same 3D model.

I render it this way:


private void createVBO(GL gl) {
    vbo = new int[objects.size()];

    // Generate the vertex buffer objects for the model
    gl.glGenBuffers(objects.size(), vbo, 0);

    // Bind the vertex buffers
    for (int i = 0; i < objects.size(); i++) {
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[i]);
      Obj tempObj = objects.get(i);
      int totalBuffer = tempObj.numOfFaces * 3 * 3;
      gl.glBufferData(GL.GL_ARRAY_BUFFER, totalBuffer * 4, vertices[i], GL.GL_STATIC_DRAW);
      gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
    }
}

private void renderVBO(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.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo[i]);                // <=== Bind the buffer
    gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);                      // <=== VertexPointer offset is 0 (zero)
    gl.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);                      // <=== Unbind the buffer
    gl.glDrawArrays(GL.GL_TRIANGLES, 0, numFaceIndices);
  }
  gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
}

The VBO rendering method is not much different from the Vertex Array rendering method in the previous post…only the binding is required and the VertexPointer has a zero offset.

One of your direct NIO buffers is empty. What is strange is that it is supported with VBO but not with vertex arrays.

A previous thread suggestions didn’t work either.

Ken Russel suggested in http://www.java-gaming.org/index.php?;topic=12412.0 to perform a buffer rewind(). I already had a vertices[i].rewind() in the array creation method, and I even tried to add it to the render method but it had no benefit.

Further I even did:


gl.glVertexPointer(3, GL.GL_FLOAT, 0, vertices[i].position(0));

without success. I’m doing this on Windows XP…it works fine for most of my 3DS models, but not for one. I can read that “bad” model just fine and can render it both in immediate mode and in VBO mode without any problems…but in this Vertex Array issue it failed.

I see…so I’ll need to put some kind of check to skip empty buffers, I guess. Do you have a suggestion, am I correct? What’s the best way to do it.

edit: I did the following quick check before proceeding with rendering and it might be a good solution.


if (normals[i].hasRemaining()) {
 // render
}

Sorry :frowning: it is a common “bug”, I have found such bugs even on “good” graphics cards. Some strange things happen too when you draw nothing between a glBegin/glEnd pair.