### How do I properly use a TriangleIndexVertexArray?

Posted:

**Sun May 10, 2015 10:42 pm**I'm trying to use JBullet to make a TriangleIndexVertexArray.

However, when I try to add this to a BvhTriangleMeshShape, it throws an IndexOutOfBoundsException, on this line:
I believe that the issue is that I am supplying the wrong values to TriangleIndexVertexArray, but I cannot work out how. What is the value I am passing wrongly here?

inBuf and veBuf are both just ByteBuffers of all the vertex and index data. I don't think there's anything wrong there.
I'm using 4 * 3 because in this line of code I got from an example.
Is this wrong? What should I use instead?

is the amount of faces (this is the same as the amount of indices, right?) there are in the model, and of course

is the amount of vertices in the model.

I cannot find any proper documentation, so I am hoping someone can help me: What am I parsing wrongly to the TriangleIndexVertexArray?

Thanks!

Code: Select all

`TriangleIndexVertexArray trSh = new TriangleIndexVertexArray(mod.faces.size(), inBuf, 4 * 3, mod.vertices.size(), veBuf, 4 * 3);`

Code: Select all

`shape = new BvhTriangleMeshShape(trSh, true);`

inBuf and veBuf are both just ByteBuffers of all the vertex and index data. I don't think there's anything wrong there.

Code: Select all

```
// There are 3 floats needed for each vertex (x,y,z)
int vbufferSize = sh.size() * 3 * Float.SIZE;
FloatBuffer verticesBuffer = ByteBuffer.allocateDirect( vbufferSize ).order( ByteOrder.nativeOrder() ).asFloatBuffer();
// Copy the values from the list to the direct float buffer
for ( Vector3f v : sh )
verticesBuffer.put( v.x ).put( v.y ).put( v.z );
// There are 3 floats needed for each vertex (x,y,z)
int fbufferSize = sh.size() * 3 * Float.SIZE;
FloatBuffer facesBuffer = ByteBuffer.allocateDirect( fbufferSize ).order( ByteOrder.nativeOrder() ).asFloatBuffer();
// Copy the values from the list to the direct float buffer
for ( Face v : mod.getFaces() ){
int[] ind = v.getVertexIndices();
facesBuffer.put( ind[0] ).put( ind[1] ).put( ind[2] );
}
ByteBuffer veBuf = (ByteBuffer) ((sun.nio.ch.DirectBuffer)verticesBuffer).attachment();
ByteBuffer inBuf = (ByteBuffer) ((sun.nio.ch.DirectBuffer)facesBuffer).attachment();
```

Code: Select all

`//TriangleIndexVertexArray indexVertexArrays=new TriangleIndexVertexArray(Bunny.NUM_TRIANGLES,Bunny.getIndexBuffer(),4 * 3,Bunny.NUM_VERTICES,Bunny.getVertexBuffer(),4 * 3);`

Code: Select all

`mod.faces.size()`

Code: Select all

`mod.vertices.size()`

I cannot find any proper documentation, so I am hoping someone can help me: What am I parsing wrongly to the TriangleIndexVertexArray?

Thanks!