Code: Select all
void mergeIMeshBuffers(IMesh* imesh){
SMesh * mesh = (SMesh*)imesh;
int bufferCount = mesh->getMeshBufferCount();
S3DVertex* resultVertices = new S3DVertex[1<<16];
u16* resultIndices = new u16[1<<16];
u16 numVertices = 0, numIndices = 0, verticesSoFar = 0, indicesSoFar = 0;
for(int i = 0; i < bufferCount; i++){
IMeshBuffer *meshBuffer = mesh->getMeshBuffer(i);
int verticesCount = meshBuffer->getVertexCount();
int indicesCount = meshBuffer->getIndexCount();
if(verticesSoFar + verticesCount <= verticesSoFar || indicesSoFar + indicesCount <= indicesSoFar)
{
// u16 overflow
cout<<"Vertices/Indices count too high or 0. So far : "+to_string(verticesSoFar)+" vertices and "+to_string(indicesSoFar)+" indices. Skipping buffer : "+to_string(i)<<endl;
continue;
}
S3DVertex* vertices = (S3DVertex*)meshBuffer->getVertices();
u16* indices = meshBuffer->getIndices();
for(int j = 0; j < verticesCount; j++)
resultVertices[numVertices++] = vertices[j];
for(int j=0; j< indicesCount; j++)
resultIndices[numIndices++] = indices[j] + verticesSoFar;
verticesSoFar = numVertices;
indicesSoFar = numIndices;
}
mesh->clear();
IMeshBuffer *resultBuffer = (IMeshBuffer*)new SMeshBuffer();
resultBuffer->append(resultVertices, numVertices, resultIndices, numIndices);
resultBuffer->recalculateBoundingBox();
mesh->addMeshBuffer(resultBuffer);
free(resultVertices);
free(resultIndices);
}