I'm relatively new to jMonkey, but I think I've come across an often repeated mistake in the code. Several places use capacity() instead of limit() when trying to determine how many items are in a buffer.
Why is this wrong? Two reasons. First of all it assume the buffer is full, when you really don't want to count the empty values. Secondly, the buffer may be just a slice of the allocated buffer with other stuff following it, and you don't want to count the stuff following it as something it is not. Consider the following…
ByteBuffer buffer = ByteBuffer.allocateDirect( number_of_bytes );
buffer.order( ByteOrder.nativeOrder() );
FloatBuffer vertices = buffer.slice().asFloatBuffer();
vertices.limit( VERTEX_COUNT*3 );
// .. load vertices..
FloatBuffer normals = buffer.slice().asFloatBuffer();
normals.limit( VERTEX_COUNT*3 );
// .. load normals..
// possibly more data..
If I pass this into a TriMesh, the current code base will also try to count the normals, and anything else following, as vertices.
Here are some of the locations I've found...
- BufferUtils: clone( * ), create*(buffer,size), get*Array(buffer)
- GeomBatch.setVertexBuffer(..), line 185 and lots of other places
- TriangleBatch.recalcTriangleQuantity(), lines 113 and 117
- TriangleBatch.getMaxIndex(), line 419
- TriangleBatch.writeObject(..), lines 142 and 144 (and several other writeObject(..) methods)
- Geometry.reconstruct(..), line 224
- TriMesh(..), line 117
- TriMesh.reconstruct(..), line 163
- Line.generateIndices(..), line 161
- Point.generateIndices(..), line 142
- LWJGLRender.draw(Curve), line 682
- BoundingSphere.calcWelzl(..), line 220
- OrientedBoundingBox.containAABB(..), line 215
- SpringSystem.createRectField(..), line 197
And noting the habit, I'm sure there is more.