Little exemple to show what’s has to be done
Exemple :
mesh : P1\P3----P4
¦¯\_ ¦
¦ ¯\_¦
P2----P6\P5
with P1==P3 and P6==P5
Buffers indexes : | 0| 1| 2| 3| 4| 5|
>- JME PositionBuffer : [P1,P2,P3,P4,P5,P6]
>- JME IndexBuffer : [ 0, 1, 5, 2, 4, 3]
<-> JME -> Bullet map : [ 0, 1, 0, 2, 3, 3]
-> Bullet Positions : [P3,P2,P4,P6] == [P1,P2,P4,P5]
-> Bullet Index : [ 0, 1, 3, 0, 3, 2]
This what i did, i use a HashMap and i hope it is better than using a LinkedList.
And i am not sure about the use of buffer.rewind() .
FloatBuffer jmePositions = triMesh.getFloatBuffer(Type.Position);
IndexBuffer jmeIndex = triMesh.getIndexBuffer();
// Generation of jmeToBulletMap, maping JME index to Bullet index.
int jmePositionSize = jmePositions.capacity();
jmeToBulletMap = BufferUtils.createIntBuffer(jmePositionSize / 3);
HashMap<Vector3f, Integer> uniquePositions = new HashMap<Vector3f, Integer>();
for (int i = 0, indice = 0; i < jmePositionSize; i += 3) {
float x = jmePositions.get(i + 0);
float y = jmePositions.get(i + 1);
float z = jmePositions.get(i + 2);
Vector3f p = new Vector3f(x, y, z);
if (!uniquePositions.containsKey(p)) {
uniquePositions.put(p, indice);
jmeToBulletMap.put(indice);
indice++;
} else {
jmeToBulletMap.put(uniquePositions.get(p));
}
}
jmeToBulletMap.rewind();
jmePositions.rewind();
// Generation of bullet indexes with the help of jmeToBulletMap
int jmeIndexSize = jmeIndex.size();
IntBuffer bulletIndexBuffer = BufferUtils.createIntBuffer(jmeIndexSize);
for (int i = 0; i < jmeIndexSize; i += 3) {
// create the bullet index to recreate each face with bullet position
bulletIndexBuffer.put(jmeToBulletMap.get(jmeIndex.get(i + 0)));
bulletIndexBuffer.put(jmeToBulletMap.get(jmeIndex.get(i + 1)));
bulletIndexBuffer.put(jmeToBulletMap.get(jmeIndex.get(i + 2)));
}
bulletIndexBuffer.rewind();
// Generation of bullet position with the help of jmeToBulletMap
int bulletNbPosition = uniquePositions.size() * 3;
FloatBuffer bulletPositions = BufferUtils.createFloatBuffer(bulletNbPosition);
for (int i = 0; i < jmePositionSize / 3; i++) {
// create the bullet positions, do the conversion from JME -> Bullet (not 1:1, with some overwrite)
int iBullet = jmeToBulletMap.get(i);
bulletPositions.put(iBullet * 3 + 0, jmePositions.get(i * 3 + 0));
bulletPositions.put(iBullet * 3 + 1, jmePositions.get(i * 3 + 1));
bulletPositions.put(iBullet * 3 + 2, jmePositions.get(i * 3 + 2));
}
objectId = createFromTriMesh(bulletIndexBuffer, bulletPositions, jmeIndexSize / 3, false);