2

I'm trying to create a TriangleIndexVertexArray with JBullet, but to do this I need to parse a ByteBuffer of all the vertices in the model.

I have got an ArrayList<Vector3f> of all the vertices in the model.

How can I parse this list of Vector3f's to a ByteBuffer?

Joehot200
  • 1,070
  • 15
  • 44

1 Answers1

2

You can do it as follow

Declaring your vertices

ArrayList<Vector3f> verticesnew = new ArrayList<Vector3f>();

Set your vertices

vertices.add( new Vector3f(someVertice) );

Get Float buffer from vertices:

// There are 3 floats needed for each vertex (x,y,z)
int bufferSize = vertices.size() * 3 * Float.SIZE;
FloatBuffer verticesBuffer = ByteBuffer.allocateDirect( bufferSize ).order( ByteOrder.nativeOrder() ).asFloatBuffer();

// Copy the values from the list to the direct float buffer
for ( Vector3f v : vertices )
    verticesBuffer.put( v.x ).put( v.y ).put( v.z );
MChaker
  • 2,610
  • 2
  • 22
  • 38
  • I specifically need to pass a ByteBuffer, and not a FloatBuffer. Is that an issue? – Joehot200 May 08 '15 at 10:44
  • Check [this](http://stackoverflow.com/questions/27492161/convert-floatbuffer-to-bytebuffer) to convert FloatBuffer to ByteBuffer. – MChaker May 08 '15 at 10:54
  • If i got what you mean in your question, you need a `FloatBuffer`. Because `ByteBuffer` is just an intermidiate to get your result. Check [this](http://stackoverflow.com/questions/15651638/writing-my-own-obj-parser-that-loads-the-data-in-vbos-how-to-re-order-data-to-m) also. – MChaker May 08 '15 at 11:01