Geometry inGeom = inGeoms[inGeomIndex];
Mesh in = inGeom.getMesh();
Mesh out = new Mesh();
int outElementCount = outLength * 3;
ShortBuffer ib = BufferUtils.createShortBuffer(outElementCount);
out.setBuffer(Type.Index, 3, ib);
// generate output buffers based on input buffers
IntMap<VertexBuffer> bufs = in.getBuffers();
for (Entry<VertexBuffer> ent : bufs){
VertexBuffer vb = ent.getValue();
if (vb.getBufferType() == Type.Index)
continue;
// NOTE: we are not actually sure
// how many elements will be in this buffer.
// It will be compacted later.
Buffer b = VertexBuffer.createBuffer(vb.getFormat(),
vb.getNumComponents(),
outElementCount);
VertexBuffer outVb = new VertexBuffer(vb.getBufferType());
outVb.setNormalized(vb.isNormalized());
outVb.setupData(vb.getUsage(), vb.getNumComponents(), vb.getFormat(), b);
out.setBuffer(outVb);
}
int currentVertex = 0;
for (int i = outOffset; i < outOffset + outLength; i++){
OCTTriangle t = tris.get(i);
// find vertex indices for triangle t
in.getTriangle(t.getTriangleIndex(), vertIndicies);
// find indices in new buf
Integer i0 = indexCache.get(vertIndicies[0]);
Integer i1 = indexCache.get(vertIndicies[1]);
Integer i2 = indexCache.get(vertIndicies[2]);
// check which ones were not created
// if not created in new IB, create them
if (i0 == null){
vertexCreated[0] = true;
newIndices[0] = currentVertex++;
indexCache.put(vertIndicies[0], newIndices[0]);
}else{
newIndices[0] = i0.intValue();
vertexCreated[0] = false;
}
if (i1 == null){
vertexCreated[1] = true;
newIndices[1] = currentVertex++;
indexCache.put(vertIndicies[1], newIndices[1]);
}else{
newIndices[1] = i1.intValue();
vertexCreated[1] = false;
}
if (i2 == null){
vertexCreated[2] = true;
newIndices[2] = currentVertex++;
indexCache.put(vertIndicies[2], newIndices[2]);
}else{
newIndices[2] = i2.intValue();
vertexCreated[2] = false;
}
// if any verticies were created for this triangle
// copy them to the output mesh
IntMap<VertexBuffer> inbufs = in.getBuffers();
for (Entry<VertexBuffer> ent : inbufs){
VertexBuffer vb = ent.getValue();
if (vb.getBufferType() == Type.Index)
continue;
VertexBuffer outVb = out.getBuffer(vb.getBufferType());
// copy verticies that were created for this triangle
for (int v = 0; v < 3; v++){
if (!vertexCreated[v])
continue;
// copy triangle's attribute from one
// buffer to another
vb.copyElement(vertIndicies[v], outVb, newIndices[v]);
}
}
// write the indices onto the output index buffer
ib.put((short)newIndices[0])
.put((short)newIndices[1])
.put((short)newIndices[2]);
}
ib.clear();
indexCache.clear();
// since some verticies were cached, it means there's
// extra data in some buffers
IntMap<VertexBuffer> outbufs = out.getBuffers();