diff --git a/src/bvh/triangle.rs b/src/bvh/triangle.rs index bd2fff99..d29c943d 100644 --- a/src/bvh/triangle.rs +++ b/src/bvh/triangle.rs @@ -35,40 +35,40 @@ impl BvhTriangle { /// Returns the interpolation of the vertices colors pub fn interpolate_colors(&self, hit_uv: &Vec2) -> Color { - self.vertices[2].color * (1.0 - hit_uv.x - hit_uv.y) - + self.vertices[0].color * hit_uv.x - + self.vertices[1].color * hit_uv.y + self.vertices[2].ext.color * (1.0 - hit_uv.x - hit_uv.y) + + self.vertices[0].ext.color * hit_uv.x + + self.vertices[1].ext.color * hit_uv.y } /// Returns the interpolation of the vertices uvs pub fn interpolate_uvs(&self, hit_uv: &Vec2) -> Vec2 { - self.vertices[2].uv * (1.0 - hit_uv.x - hit_uv.y) - + self.vertices[0].uv * hit_uv.x - + self.vertices[1].uv * hit_uv.y + self.vertices[2].ext.uv * (1.0 - hit_uv.x - hit_uv.y) + + self.vertices[0].ext.uv * hit_uv.x + + self.vertices[1].ext.uv * hit_uv.y } /// Returns the interpolation of the vertices normals pub fn interpolate_normals(&self, hit_uv: &Vec2) -> Vec3 { - let n = self.vertices[2].normal * (1.0 - hit_uv.x - hit_uv.y) - + self.vertices[0].normal * hit_uv.x - + self.vertices[1].normal * hit_uv.y; + let n = self.vertices[2].ext.normal * (1.0 - hit_uv.x - hit_uv.y) + + self.vertices[0].ext.normal * hit_uv.x + + self.vertices[1].ext.normal * hit_uv.y; n.get_normalized() } /// Returns the interpolation of the vertices tangents pub fn interpolate_tangents(&self, hit_uv: &Vec2) -> Vec3 { - let mut t = self.vertices[2].tangent * (1.0 - hit_uv.x - hit_uv.y) - + self.vertices[0].tangent * hit_uv.x - + self.vertices[1].tangent * hit_uv.y; + let mut t = self.vertices[2].ext.tangent * (1.0 - hit_uv.x - hit_uv.y) + + self.vertices[0].ext.tangent * hit_uv.x + + self.vertices[1].ext.tangent * hit_uv.y; t.normalize(); t } /// Returns the interpolation of the vertices bitangents pub fn interpolate_bitangents(&self, hit_uv: &Vec2) -> Vec3 { - let mut b = self.vertices[2].bitangent * (1.0 - hit_uv.x - hit_uv.y) - + self.vertices[0].bitangent * hit_uv.x - + self.vertices[1].bitangent * hit_uv.y; + let mut b = self.vertices[2].ext.bitangent * (1.0 - hit_uv.x - hit_uv.y) + + self.vertices[0].ext.bitangent * hit_uv.x + + self.vertices[1].ext.bitangent * hit_uv.y; b.normalize(); b } diff --git a/src/geometry/mod.rs b/src/geometry/mod.rs index dd0063a5..b56cf848 100644 --- a/src/geometry/mod.rs +++ b/src/geometry/mod.rs @@ -1,12 +1,14 @@ -// Copyright © 2022 +// Copyright © 2022-2024 // Author: Antonio Caggiano // SPDX-License-Identifier: MIT pub mod sphere; pub mod triangles; +pub mod vertex; pub use sphere::*; pub use triangles::*; +pub use vertex::*; #[derive(Debug, Clone)] pub enum Geometry { diff --git a/src/geometry/triangles.rs b/src/geometry/triangles.rs index e94804e6..a254d81c 100644 --- a/src/geometry/triangles.rs +++ b/src/geometry/triangles.rs @@ -40,30 +40,30 @@ impl Triangles { ) -> Vec { let mut ret = vec![]; - let trs = model.nodes.get(node).unwrap().trs.clone(); - let tangent_matrix = Mat3::from(&trs); + let trs = model.solved_trs.get(&node).unwrap(); + let tangent_matrix = Mat3::from(&trs.trs); - let inverse_trs = Inversed::from(&trs); + let inverse_trs = Inversed::from(&trs.trs); let normal_matrix = Mat3::from(&inverse_trs).get_transpose(); for i in 0..(indices.len() / 3) { let mut a = self.vertices[indices[i * 3].to_usize().unwrap()]; - a.pos = &trs * a.pos; - a.normal = &normal_matrix * a.normal; - a.tangent = &tangent_matrix * a.tangent; - a.bitangent = &tangent_matrix * a.bitangent; + a.pos = &trs.trs * a.pos; + a.ext.normal = &normal_matrix * a.ext.normal; + a.ext.tangent = &tangent_matrix * a.ext.tangent; + a.ext.bitangent = &tangent_matrix * a.ext.bitangent; let mut b = self.vertices[indices[i * 3 + 1].to_usize().unwrap()]; - b.pos = &trs * b.pos; - b.normal = &normal_matrix * b.normal; - b.tangent = &tangent_matrix * b.tangent; - b.bitangent = &tangent_matrix * b.bitangent; + b.pos = &trs.trs * b.pos; + b.ext.normal = &normal_matrix * b.ext.normal; + b.ext.tangent = &tangent_matrix * b.ext.tangent; + b.ext.bitangent = &tangent_matrix * b.ext.bitangent; let mut c = self.vertices[indices[i * 3 + 2].to_usize().unwrap()]; - c.pos = &trs * c.pos; - c.normal = &normal_matrix * c.normal; - c.tangent = &tangent_matrix * c.tangent; - c.bitangent = &tangent_matrix * c.bitangent; + c.pos = &trs.trs * c.pos; + c.ext.normal = &normal_matrix * c.ext.normal; + c.ext.tangent = &tangent_matrix * c.ext.tangent; + c.ext.bitangent = &tangent_matrix * c.ext.bitangent; let triangle = Box::new(BvhTriangle::new(a, b, c)); let geometry = BvhGeometry::Triangle(triangle); diff --git a/src/vertex.rs b/src/geometry/vertex.rs similarity index 74% rename from src/vertex.rs rename to src/geometry/vertex.rs index ae6157a3..28718c51 100644 --- a/src/vertex.rs +++ b/src/geometry/vertex.rs @@ -1,14 +1,28 @@ -// Copyright © 2022 +// Copyright © 2022-2024 // Author: Antonio Caggiano // SPDX-License-Identifier: MIT -use super::*; +use crate::*; #[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq)] +#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct Vertex { pub pos: Point3, + pub ext: VertexExt, +} + +impl Vertex { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self { + pos: Point3::new(x, y, z), + ext: Default::default(), + } + } +} +#[repr(C)] +#[derive(Debug, Copy, Clone, PartialEq)] +pub struct VertexExt { pub uv: Vec2, pub color: Color, @@ -17,10 +31,9 @@ pub struct Vertex { pub bitangent: Vec3, } -impl Vertex { - pub fn new(x: f32, y: f32, z: f32) -> Self { +impl Default for VertexExt { + fn default() -> Self { Self { - pos: Point3::new(x, y, z), uv: Vec2::default(), color: Color::from(0xFFFFFFFF), normal: Vec3::new(0.0, 0.0, 1.0), @@ -29,9 +42,3 @@ impl Vertex { } } } - -impl Default for Vertex { - fn default() -> Self { - Self::new(0.0, 0.0, 0.0) - } -} diff --git a/src/lib.rs b/src/lib.rs index d6b0cc58..5d096d9a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -// Copyright © 2022 +// Copyright © 2022-2024 // Author: Antonio Caggiano // SPDX-License-Identifier: MIT @@ -22,7 +22,6 @@ pub mod sampler; pub mod scene; pub mod texture; pub mod util; -pub mod vertex; #[cfg(target_arch = "wasm32")] pub mod www; @@ -43,6 +42,5 @@ pub use sampler::*; pub use scene::*; pub use texture::*; pub use util::*; -pub use vertex::*; #[cfg(target_arch = "wasm32")] pub use www::*; diff --git a/src/model.rs b/src/model.rs index 69de56f0..5a77cc39 100644 --- a/src/model.rs +++ b/src/model.rs @@ -432,8 +432,8 @@ impl ModelBuilder { let uvs = self.get_slices(accessor); vertices.resize(uvs.len(), Vertex::default()); for (i, uv) in uvs.into_iter().enumerate() { - vertices[i].uv.x = uv[0]; - vertices[i].uv.y = uv[1]; + vertices[i].ext.uv.x = uv[0]; + vertices[i].ext.uv.y = uv[1]; } Ok(()) } @@ -446,7 +446,7 @@ impl ModelBuilder { let normals = self.get_slices(accessor); vertices.resize(normals.len(), Vertex::default()); for (i, normal) in normals.into_iter().enumerate() { - vertices[i].normal = Vec3::new(normal[0], normal[1], normal[2]); + vertices[i].ext.normal = Vec3::new(normal[0], normal[1], normal[2]); } Ok(()) } @@ -459,10 +459,10 @@ impl ModelBuilder { let colors = self.get_slices(accessor); vertices.resize(colors.len(), Vertex::default()); for (i, color) in colors.into_iter().enumerate() { - vertices[i].color.r = color[0]; - vertices[i].color.g = color[1]; - vertices[i].color.b = color[2]; - vertices[i].color.a = if color.len() == 4 { color[3] } else { 1.0 }; + vertices[i].ext.color.r = color[0]; + vertices[i].ext.color.g = color[1]; + vertices[i].ext.color.b = color[2]; + vertices[i].ext.color.a = if color.len() == 4 { color[3] } else { 1.0 }; } Ok(()) } @@ -492,10 +492,10 @@ impl ModelBuilder { let d = &data[offset]; let tangent = unsafe { std::slice::from_raw_parts::(d as *const u8 as _, 4) }; - vertex.tangent = Vec3::new(tangent[0], tangent[1], tangent[2]); + vertex.ext.tangent = Vec3::new(tangent[0], tangent[1], tangent[2]); // Compute bitangent as for glTF 2.0 spec - vertex.bitangent = vertex.normal.cross(&vertex.tangent) * tangent[3]; + vertex.ext.bitangent = vertex.ext.normal.cross(&vertex.ext.tangent) * tangent[3]; }); Ok(()) diff --git a/tests/test.rs b/tests/test.rs index 177106b2..87675a29 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -36,9 +36,9 @@ fn triangle() { let mut model = Model::new(); let mut prim = Primitive::unit_triangle(); if let Geometry::Triangles(triangle) = &mut prim.geometry { - triangle.vertices[0].color = Color::from(0xFF0000FF); - triangle.vertices[1].color = Color::from(0x00FF00FF); - triangle.vertices[2].color = Color::from(0x0000FFFF); + triangle.vertices[0].ext.color = Color::from(0xFF0000FF); + triangle.vertices[1].ext.color = Color::from(0x00FF00FF); + triangle.vertices[2].ext.color = Color::from(0x0000FFFF); } let prim_handle = model.primitives.push(prim); let mesh = Mesh::new(vec![prim_handle]);