I made a mesh with blender (actually I’ve imported an svg from inkscape), and I know that jme converts the mesh into triangles. I’d like to have a list of these triangles vertex, but I don’t know how to do it…
Thanks!
(The intended purpose is to feed these triangles to dyn4j)
Well I just did this code to iterate over each vertex.
public static void iterateOverMesh(Mesh m) {
VertexBuffer vB = m.getBuffer(VertexBuffer.Type.Position);
if (vB.getNumComponents() != 3) {
throw new RuntimeException("Position Buffer doesn't have 3-component Vectors but " + vB.getNumComponents());
}
FloatBuffer buf = (FloatBuffer)vB.getDataReadOnly();
Vector3f vTemp = new Vector3f();
for (int i = 0; i < vB.getNumElements(); i++) {
vTemp.set(buf.get(i * 3), buf.get(i * 3 + 1), buf.get(i * 3 + 2));
// do stuff
}
That way you could easily build an ArrayList<Vector3f> vertices (Note that you should use the capacity constructor and pass getNumElements for performance reasons).
Now you need to do the same with a shortBuffer for the IndexBuffer. There you could also grab three indices and store them as a Vector3f (for convenience).
Then something like:
Triangle = { vertices.get(v.x), vertices.get(v.y), vertices.get(v.z)};
Vector3f v1=new Vector3f();
Vector3f v2=new Vector3f();
Vector3f v3=new Vector3f();
for (int i = 0; i < m.getTriangleCount(); i++) {
m.getTriangle(i, v1,v2,v3);
System.out.println(i+" ");
System.out.println(v1);
System.out.println(v2);
System.out.println(v3);
}
Gives me a
java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:540)
at java.nio.DirectShortBufferU.get(DirectShortBufferU.java:253)
at com.jme3.scene.mesh.IndexShortBuffer.get(IndexShortBuffer.java:53)
at com.jme3.scene.mesh.WrappedIndexBuffer.get(WrappedIndexBuffer.java:80)
at com.jme3.scene.Mesh.getTriangle(Mesh.java:893)