diff options
Diffstat (limited to 'engine/src/mesh.rs')
| -rw-r--r-- | engine/src/mesh.rs | 83 |
1 files changed, 58 insertions, 25 deletions
diff --git a/engine/src/mesh.rs b/engine/src/mesh.rs index fb977af..f26c9c1 100644 --- a/engine/src/mesh.rs +++ b/engine/src/mesh.rs @@ -1,5 +1,8 @@ +use engine_macros::Reflection; +use zerocopy::{Immutable, IntoBytes}; + use crate::builder; -use crate::vector::{Vec2, Vec3}; +use crate::vector::Vec3; pub mod cube; @@ -44,21 +47,25 @@ impl Mesh /// Finds the vertex positions that are furthest in every 3D direction. Keep in mind /// that this can be quite time-expensive if the mesh has many vertices. - pub fn find_furthest_vertex_positions(&self) -> DirectionPositions<'_> + pub fn find_furthest_vertex_positions(&self) -> DirectionPositions { - let mut point_iter = self.vertices().iter().map(|vertex| &vertex.pos).into_iter(); + let mut point_iter = self + .vertices() + .iter() + .map(|vertex| Vec3::from(vertex.pos)) + .into_iter(); let first_point = point_iter.next().unwrap(); point_iter .fold( FurthestPosAcc { - up: FurthestPos::new(&first_point, &Vec3::UP), - down: FurthestPos::new(&first_point, &Vec3::DOWN), - left: FurthestPos::new(&first_point, &Vec3::LEFT), - right: FurthestPos::new(&first_point, &Vec3::RIGHT), - back: FurthestPos::new(&first_point, &Vec3::BACK), - front: FurthestPos::new(&first_point, &Vec3::FRONT), + up: FurthestPos::new(first_point, &Vec3::UP), + down: FurthestPos::new(first_point, &Vec3::DOWN), + left: FurthestPos::new(first_point, &Vec3::LEFT), + right: FurthestPos::new(first_point, &Vec3::RIGHT), + back: FurthestPos::new(first_point, &Vec3::BACK), + front: FurthestPos::new(first_point, &Vec3::FRONT), }, |mut furthest_pos_acc, pos| { furthest_pos_acc.up.update_if_further(pos); @@ -77,13 +84,18 @@ impl Mesh builder! { #[builder(name = VertexBuilder, derives = (Debug, Default, Clone))] -#[derive(Debug, Clone, Default, PartialEq)] +#[derive(Debug, Clone, Default, PartialEq, IntoBytes, Immutable, Reflection)] #[non_exhaustive] pub struct Vertex { - pub pos: Vec3<f32>, - pub texture_coords: Vec2<f32>, - pub normal: Vec3<f32>, + #[builder(skip_generate_fn)] + pub pos: [f32; 3], + + #[builder(skip_generate_fn)] + pub texture_coords: [f32; 2], + + #[builder(skip_generate_fn)] + pub normal: [f32; 3], } } @@ -96,18 +108,39 @@ impl Vertex } } +impl VertexBuilder +{ + pub fn pos(mut self, pos: impl Into<[f32; 3]>) -> Self + { + self.pos = pos.into(); + self + } + + pub fn texture_coords(mut self, texture_coords: impl Into<[f32; 2]>) -> Self + { + self.texture_coords = texture_coords.into(); + self + } + + pub fn normal(mut self, normal: impl Into<[f32; 3]>) -> Self + { + self.normal = normal.into(); + self + } +} + #[derive(Debug, Clone)] -pub struct DirectionPositions<'mesh> +pub struct DirectionPositions { - pub up: &'mesh Vec3<f32>, - pub down: &'mesh Vec3<f32>, - pub left: &'mesh Vec3<f32>, - pub right: &'mesh Vec3<f32>, - pub back: &'mesh Vec3<f32>, - pub front: &'mesh Vec3<f32>, + pub up: Vec3<f32>, + pub down: Vec3<f32>, + pub left: Vec3<f32>, + pub right: Vec3<f32>, + pub back: Vec3<f32>, + pub front: Vec3<f32>, } -impl<'mesh> From<FurthestPosAcc<'mesh>> for DirectionPositions<'mesh> +impl<'mesh> From<FurthestPosAcc<'mesh>> for DirectionPositions { fn from(acc: FurthestPosAcc<'mesh>) -> Self { @@ -136,14 +169,14 @@ struct FurthestPosAcc<'mesh> #[derive(Debug)] struct FurthestPos<'mesh> { - pos: &'mesh Vec3<f32>, + pos: Vec3<f32>, dot_prod: f32, direction: &'mesh Vec3<f32>, } impl<'mesh> FurthestPos<'mesh> { - fn new(pos: &'mesh Vec3<f32>, direction: &'mesh Vec3<f32>) -> Self + fn new(pos: Vec3<f32>, direction: &'mesh Vec3<f32>) -> Self { Self { pos, @@ -152,9 +185,9 @@ impl<'mesh> FurthestPos<'mesh> } } - fn update_if_further(&mut self, point: &'mesh Vec3<f32>) + fn update_if_further(&mut self, point: Vec3<f32>) { - let point_dot_prod = self.direction.dot(point); + let point_dot_prod = self.direction.dot(&point); if point_dot_prod > self.dot_prod { self.pos = point; |
