Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vertex reordering when reading file #67

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>,
/// 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<u32>,
}

/// Options for processing the mesh during loading.
Expand Down Expand Up @@ -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)
}

Expand Down