@SuppressWarnings("unchecked")
public Mesh buildMeshPolygons(final Element colladaGeometry, final Element polys) {
if (polys == null || polys.getChild("input") == null) {
return null;
}
final Mesh polyMesh = new Mesh(extractName(colladaGeometry, polys));
polyMesh.getMeshData().setIndexMode(IndexMode.Triangles);
// Build and set RenderStates for our material
_colladaMaterialUtils.applyMaterial(polys.getAttributeValue("material"), polyMesh);
final LinkedList<ColladaInputPipe> pipes = new LinkedList<ColladaInputPipe>();
final int maxOffset = extractPipes(polys, pipes);
final int interval = maxOffset + 1;
// use interval & sum of sizes of p entries to determine buffer sizes.
int numEntries = 0;
int numIndices = 0;
for (final Element vals : polys.getChildren("p")) {
final int length = _colladaDOMUtil.parseIntArray(vals).length;
numEntries += length;
numIndices += (length / interval - 2) * 3;
}
numEntries /= interval;
// Construct nio buffers for specified inputs.
for (final ColladaInputPipe pipe : pipes) {
pipe.setupBuffer(numEntries, polyMesh.getMeshData(), _dataCache);
}
// Add to vert mapping
final int[] indices = new int[numEntries];
final MeshVertPairs mvp = new MeshVertPairs(polyMesh, indices);
_dataCache.getVertMappings().put(colladaGeometry, mvp);
// Prepare indices buffer
final IndexBufferData<?> meshIndices = BufferUtils.createIndexBufferData(numIndices, polyMesh.getMeshData()
.getVertexCount() - 1);
polyMesh.getMeshData().setIndices(meshIndices);
// go through the polygon entries
int firstIndex = 0, vecIndex;
final int[] currentVal = new int[interval];
for (final Element dia : polys.getChildren("p")) {
// for each p, iterate using max offset
final int[] vals = _colladaDOMUtil.parseIntArray(dia);
final int first = firstIndex + 0;
System.arraycopy(vals, 0, currentVal, 0, interval);
vecIndex = processPipes(pipes, currentVal);
if (vecIndex != Integer.MIN_VALUE) {
indices[firstIndex + 0] = vecIndex;
}
int prev = firstIndex + 1;
System.arraycopy(vals, interval, currentVal, 0, interval);
vecIndex = processPipes(pipes, currentVal);
if (vecIndex != Integer.MIN_VALUE) {
indices[firstIndex + 1] = vecIndex;
}
// first add the first two entries to the buffers.
// Now go through remaining entries and create a polygon as a triangle fan.
for (int j = 2, max = vals.length / interval; j < max; j++) {
// add first as index
meshIndices.put(first);
// add prev as index
meshIndices.put(prev);
// set prev to current
prev = firstIndex + j;
// add current to buffers
System.arraycopy(vals, j * interval, currentVal, 0, interval);
vecIndex = processPipes(pipes, currentVal);
if (vecIndex != Integer.MIN_VALUE) {
indices[firstIndex + j] = vecIndex;
}
// add current as index
meshIndices.put(prev);
}
firstIndex += vals.length / interval;
}
if (_optimizeMeshes) {
final VertMap map = GeometryTool.minimizeVerts(polyMesh, _optimizeSettings);
_dataCache.setMeshVertMap(polyMesh, map);
}
// update bounds
polyMesh.updateModelBound();
// return
return polyMesh;
}