diff --git a/src/lib.rs b/src/lib.rs index 2c6cbf8..d6a9f18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -388,6 +388,12 @@ pub struct Mesh { /// Optional material id associated with this mesh. The material id indexes /// into the Vec of Materials loaded from the associated `MTL` file pub material_id: Option, + /// Faces from the obj file are reindexed when reading them + /// For example if the first two faces are [4,2,6] and [4,3,7] + /// they will be stored inside the mesh.indices as [0,1,2] and [0,2,3] + /// To obtain back the original index we use this face_reindex which maps from final_index->original_index + /// Therefore faces_original_index[0]=4 and faces_original_index[1]=2 + pub faces_original_index: Vec, } /// Options for processing the mesh during loading. @@ -955,6 +961,28 @@ fn export_faces( mesh.face_arities = Vec::new(); } + //store inside the mesh the faces_original_index + let max_final_idx = index_map.values().copied().max().unwrap_or(0) as usize; + + if max_final_idx != 0 { + mesh.faces_original_index.resize(max_final_idx + 1, 0); + for (original_indidces, final_index) in index_map { + let final_index = final_index as usize; + + //sanity check that the index we are going to write is the same as the one that was written by a different face + if mesh.faces_original_index[final_index] != 0 { + //we already have a original index associated with this one but we make sure it's the same + let old = mesh.faces_original_index[final_index]; + assert!( + original_indidces.v as u32 == old, + "Something is wrong when creating the face_orignal_index. The old index doesn't correspond to the new one." + ); + } + + mesh.faces_original_index[final_index] = original_indidces.v as u32; + } + } + Ok(mesh) }