From 7a9f0268dd582ae4538f21e1226973f6043994ee Mon Sep 17 00:00:00 2001 From: Radu Alexandru Rosu Date: Thu, 14 Dec 2023 10:38:50 +0000 Subject: [PATCH 1/4] Added an index from final face indices to the original ones in the obj file. Useful for reindexing data that is stored per vertex of the obj but gets scrambled when reading using tobj --- src/lib.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index afc0d96..b75fe9c 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 mut max_final_idx = 0; + for (original_indidces, final_index) in index_map.iter() { + if (final_index > &max_final_idx) { + max_final_idx = *final_index; + } + } + mesh.faces_original_index + .resize(max_final_idx as usize + 1, 0); + for (original_indidces, final_index) in index_map { + if (mesh.faces_original_index[final_index as usize] != 0) { + let old = mesh.faces_original_index[final_index as usize]; + assert!( + original_indidces.v as u32 == old, + "Something is wrong when creating the face_orignal_index" + ); + } + mesh.faces_original_index[final_index as usize] = original_indidces.v as u32; + } + // println!("max final idx is {}", max_final_idx); + // println!("mesh.indices.len() {}", mesh.indices.len()); + Ok(mesh) } From 24182fba07af22d7f3c74cb29ad301ab048b28ec Mon Sep 17 00:00:00 2001 From: Radu Alexandru Rosu Date: Wed, 16 Oct 2024 12:15:57 +0000 Subject: [PATCH 2/4] Cleanup --- src/lib.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8d260a8..f291838 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -962,26 +962,26 @@ fn export_faces( } //store inside the mesh the faces_original_index - let mut max_final_idx = 0; - for (original_indidces, final_index) in index_map.iter() { - if (final_index > &max_final_idx) { - max_final_idx = *final_index; - } - } - mesh.faces_original_index - .resize(max_final_idx as usize + 1, 0); + let max_final_idx = index_map.values().copied().max().unwrap_or(0) as usize; + + mesh.faces_original_index.resize(max_final_idx + 1, 0); for (original_indidces, final_index) in index_map { - if (mesh.faces_original_index[final_index as usize] != 0) { - let old = mesh.faces_original_index[final_index as usize]; + 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" + "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 as usize] = original_indidces.v as u32; + + mesh.faces_original_index[final_index] = original_indidces.v as u32; } - // println!("max final idx is {}", max_final_idx); - // println!("mesh.indices.len() {}", mesh.indices.len()); + println!("max final idx is {}", max_final_idx); + println!("mesh.indices.len() {}", mesh.indices.len()); Ok(mesh) } From 72aecddf44c64e019ff8bff5c3b7019ef08c6877 Mon Sep 17 00:00:00 2001 From: Radu Alexandru Rosu Date: Wed, 16 Oct 2024 12:18:24 +0000 Subject: [PATCH 3/4] Remove println --- src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f291838..9e332a6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -980,8 +980,6 @@ fn export_faces( mesh.faces_original_index[final_index] = original_indidces.v as u32; } - println!("max final idx is {}", max_final_idx); - println!("mesh.indices.len() {}", mesh.indices.len()); Ok(mesh) } From b1c900344e9a89756d528df0bca0872d8f540367 Mon Sep 17 00:00:00 2001 From: Radu Alexandru Rosu Date: Wed, 16 Oct 2024 12:27:11 +0000 Subject: [PATCH 4/4] No need to do anything if there are not face indices --- src/lib.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9e332a6..d6a9f18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -964,21 +964,23 @@ fn export_faces( //store inside the mesh the faces_original_index let max_final_idx = index_map.values().copied().max().unwrap_or(0) as usize; - 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!( + 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; + mesh.faces_original_index[final_index] = original_indidces.v as u32; + } } Ok(mesh)