From d10f549c83c65fa59266a0b0f82770da65c833d5 Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Wed, 1 Nov 2023 23:20:06 +0200 Subject: [PATCH] fix(obj): negative indexing for vertex normals and texcoords as well --- src/importer/obj.rs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/importer/obj.rs b/src/importer/obj.rs index bde87bd..83e95a6 100644 --- a/src/importer/obj.rs +++ b/src/importer/obj.rs @@ -290,7 +290,12 @@ pub fn load_obj(obj_path: &PathBuf, file: std::fs::File) -> Result().unwrap() - 1; + let mut normal = it.next().unwrap().parse::().unwrap(); + if normal < 0 { + normal = normals.len() as i32 + normal; + } else { + normal -= 1; + } vertices.push(Vertex{ position: *temp_vertices.get(vert as usize).unwrap(), normal: *normals.get(normal as usize).unwrap(), @@ -304,8 +309,18 @@ pub fn load_obj(obj_path: &PathBuf, file: std::fs::File) -> Result().unwrap() - 1; - let normal = it.next().unwrap().parse::().unwrap() - 1; + let mut t_coords = it.next().unwrap().parse::().unwrap(); + if t_coords < 0 { + t_coords = tex_coords.len() as i32 + t_coords; + } else { + t_coords -= 1; + } + let mut normal = it.next().unwrap().parse::().unwrap(); + if normal < 0 { + normal = normals.len() as i32 + normal; + } else { + normal -= 1; + } vertices.push(Vertex{ position: *temp_vertices.get(vertex as usize).unwrap(), normal: *normals.get(normal as usize).unwrap(), @@ -319,7 +334,12 @@ pub fn load_obj(obj_path: &PathBuf, file: std::fs::File) -> Result().unwrap() - 1; + let mut t_coords = it.next().unwrap().parse::().unwrap(); + if t_coords < 0 { + t_coords = tex_coords.len() as i32 + t_coords; + } else { + t_coords -= 1; + } vertices.push(Vertex{ position: *temp_vertices.get(vertex as usize).unwrap(), normal: calculated_normal, @@ -339,7 +359,7 @@ pub fn load_obj(obj_path: &PathBuf, file: std::fs::File) -> Result Result { // we don't handle lines or points } + Some(ObjToken::SmoothShading) => { + // idc about this + } _ => { warn!("Unhandled obj token: {}", token) }, } }