| 282 | } |
| 283 | |
| 284 | void MeshData::BuildIndexBuffer() |
| 285 | { |
| 286 | PROFILE_CPU(); |
| 287 | Stopwatch stopwatch; |
| 288 | |
| 289 | const int32 vertexCount = Positions.Count(); |
| 290 | MeshData newMesh; |
| 291 | newMesh.Indices.EnsureCapacity(vertexCount); |
| 292 | Array<int32> mapping; |
| 293 | mapping.Resize(vertexCount); |
| 294 | int32 newVertexCounter = 0; |
| 295 | |
| 296 | #if USE_SPATIAL_SORT |
| 297 | // Set up a SpatialSort to quickly find all vertices close to a given position |
| 298 | Assimp::SpatialSort vertexFinder; |
| 299 | vertexFinder.Fill((const aiVector3D*)Positions.Get(), vertexCount, sizeof(Float3)); |
| 300 | std::vector<unsigned int> spatialSortCache; |
| 301 | #endif |
| 302 | |
| 303 | // Build index buffer |
| 304 | for (int32 vertexIndex = 0; vertexIndex < vertexCount; vertexIndex++) |
| 305 | { |
| 306 | // Find duplicated vertex before the current one |
| 307 | const int32 reuseVertexIndex = FindVertex(*this, vertexIndex, 0, vertexIndex, mapping |
| 308 | #if USE_SPATIAL_SORT |
| 309 | , vertexFinder, spatialSortCache |
| 310 | #endif |
| 311 | ); |
| 312 | if (reuseVertexIndex == INVALID_INDEX) |
| 313 | { |
| 314 | newMesh.Indices.Add(newVertexCounter); |
| 315 | mapping[vertexIndex] = newVertexCounter; |
| 316 | newVertexCounter++; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | // Remove vertex |
| 321 | const int32 mapped = mapping[reuseVertexIndex]; |
| 322 | ASSERT(mapped != INVALID_INDEX && mapped < vertexIndex); |
| 323 | newMesh.Indices.Add(mapped); |
| 324 | mapping[vertexIndex] = INVALID_INDEX; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // Skip if no change |
| 329 | if (vertexCount == newVertexCounter) |
| 330 | return; |
| 331 | |
| 332 | // Remove unused vertices |
| 333 | newMesh.SwapBuffers(*this); |
| 334 | #define REMAP_BUFFER(name) RemapBuffer(newMesh.name, name, mapping, newVertexCounter) |
| 335 | REMAP_BUFFER(Positions); |
| 336 | REMAP_BUFFER(Normals); |
| 337 | REMAP_BUFFER(Tangents); |
| 338 | REMAP_BUFFER(BitangentSigns); |
| 339 | REMAP_BUFFER(Colors); |
| 340 | REMAP_BUFFER(BlendIndices); |
| 341 | REMAP_BUFFER(BlendWeights); |
no test coverage detected