Skip to content

Commit

Permalink
vertex: Extract to VertexExt
Browse files Browse the repository at this point in the history
Move all members but position to another struct named VertexExt.
  • Loading branch information
Fahien committed Nov 19, 2024
1 parent 8e5b04b commit d75fe44
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 58 deletions.
30 changes: 15 additions & 15 deletions src/bvh/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion src/geometry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright © 2022
// Copyright © 2022-2024
// Author: Antonio Caggiano <info@antoniocaggiano.eu>
// 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 {
Expand Down
30 changes: 15 additions & 15 deletions src/geometry/triangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,30 @@ impl Triangles {
) -> Vec<BvhPrimitive> {
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);
Expand Down
31 changes: 19 additions & 12 deletions src/vertex.rs → src/geometry/vertex.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
// Copyright © 2022
// Copyright © 2022-2024
// Author: Antonio Caggiano <info@antoniocaggiano.eu>
// 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,

Expand All @@ -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),
Expand All @@ -29,9 +42,3 @@ impl Vertex {
}
}
}

impl Default for Vertex {
fn default() -> Self {
Self::new(0.0, 0.0, 0.0)
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022
// Copyright © 2022-2024
// Author: Antonio Caggiano <info@antoniocaggiano.eu>
// SPDX-License-Identifier: MIT

Expand All @@ -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;

Expand All @@ -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::*;
18 changes: 9 additions & 9 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand All @@ -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(())
}
Expand All @@ -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(())
}
Expand Down Expand Up @@ -492,10 +492,10 @@ impl ModelBuilder {
let d = &data[offset];
let tangent = unsafe { std::slice::from_raw_parts::<f32>(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(())
Expand Down
6 changes: 3 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit d75fe44

Please sign in to comment.