void RawModel::Condense()

in src/raw/RawModel.cpp [338:408]


void RawModel::Condense() {
  // Only keep surfaces that are referenced by one or more triangles.
  {
    std::vector<RawSurface> oldSurfaces = surfaces;

    surfaces.clear();

    std::set<int> survivingSurfaceIds;
    for (auto& triangle : triangles) {
      const RawSurface& surface = oldSurfaces[triangle.surfaceIndex];
      const int surfaceIndex = AddSurface(surface.name.c_str(), surface.id);
      surfaces[surfaceIndex] = surface;
      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;
      }
    }
  }

  // Only keep materials that are referenced by one or more triangles.
  {
    std::vector<RawMaterial> oldMaterials = materials;

    materials.clear();

    for (auto& triangle : triangles) {
      const RawMaterial& material = oldMaterials[triangle.materialIndex];
      const int materialIndex = AddMaterial(material);
      materials[materialIndex] = material;
      triangle.materialIndex = materialIndex;
    }
  }

  // Only keep textures that are referenced by one or more materials.
  {
    std::vector<RawTexture> oldTextures = textures;

    textures.clear();

    for (auto& material : materials) {
      for (int j = 0; j < RAW_TEXTURE_USAGE_MAX; j++) {
        if (material.textures[j] >= 0) {
          const RawTexture& texture = oldTextures[material.textures[j]];
          const int textureIndex =
              AddTexture(texture.name, texture.fileName, texture.fileLocation, texture.usage);
          textures[textureIndex] = texture;
          material.textures[j] = textureIndex;
        }
      }
    }
  }

  // Only keep vertices that are referenced by one or more triangles.
  {
    std::vector<RawVertex> oldVertices = vertices;

    vertexHash.clear();
    vertices.clear();

    for (auto& triangle : triangles) {
      for (int j = 0; j < 3; j++) {
        triangle.verts[j] = AddVertex(oldVertices[triangle.verts[j]]);
      }
    }
  }
}