Fix crash from dangling surface reference.

The condense operation recreates the vectors of surfaces, materials,
textures and vertices so as to exclude anything that isn't referenced
explicitly by a triangle. In the process, we must take care that
references from other properties are cleared out.

This fixes the case when a node references a mesh by id, and then the
mesh is deleted because no triangle references. TODO: go through other
properties and make sure the same problem doesn't exist there.

It is also possible that these vectors should be replaced by maps, at
least for the elements that (now) have unique IDs.
This commit is contained in:
Par Winzell 2018-05-03 14:44:43 -07:00
parent f530af8bf4
commit 3c8cad4730
1 changed files with 9 additions and 0 deletions

View File

@ -269,11 +269,20 @@ void RawModel::Condense()
surfaces.clear(); surfaces.clear();
std::set<int> survivingSurfaceIds;
for (auto &triangle : triangles) { for (auto &triangle : triangles) {
int oldSurfaceIndex = triangle.surfaceIndex;
const RawSurface &surface = oldSurfaces[triangle.surfaceIndex]; const RawSurface &surface = oldSurfaces[triangle.surfaceIndex];
const int surfaceIndex = AddSurface(surface.name.c_str(), surface.id); const int surfaceIndex = AddSurface(surface.name.c_str(), surface.id);
surfaces[surfaceIndex] = surface; surfaces[surfaceIndex] = surface;
triangle.surfaceIndex = surfaceIndex; triangle.surfaceIndex = surfaceIndex;
survivingSurfaceIds.emplace(surface.id);
}
// clear out references to meshes that no longer exist
for (auto &node : nodes) {
if (node.surfaceId != 0 && survivingSurfaceIds.find(node.surfaceId) == survivingSurfaceIds.end()) {
node.surfaceId = 0;
}
} }
} }