summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-06-02 22:00:14 +0200
committerHampusM <hampus@hampusmat.com>2026-06-02 22:00:29 +0200
commit52462205323c20feb219dc8215a9db4ca551bd84 (patch)
treed94b1e0048509e6190780ca82ecb4c02d68d31e3
parent7c7b85983ef72564117d2059487b25aa3e5e5bad (diff)
refactor(engine): implement vector math op traits with macroHEADmaster
-rw-r--r--engine/src/collision.rs4
-rw-r--r--engine/src/data_types/matrix.rs8
-rw-r--r--engine/src/data_types/vector.rs380
-rw-r--r--engine/src/math.rs6
-rw-r--r--engine/src/mesh/cube.rs12
-rw-r--r--engine/src/shader/default.rs7
6 files changed, 100 insertions, 317 deletions
diff --git a/engine/src/collision.rs b/engine/src/collision.rs
index 139b924..c053a7f 100644
--- a/engine/src/collision.rs
+++ b/engine/src/collision.rs
@@ -100,7 +100,7 @@ impl Collider<SphereCollider> for SphereCollider
{
fn intersects(&self, other: &SphereCollider) -> bool
{
- (&self.center - &other.center).length() <= self.radius + other.radius
+ (self.center - other.center).length() <= self.radius + other.radius
}
}
@@ -136,6 +136,6 @@ impl Collider<Vec3<f32>> for SphereCollider
{
fn intersects(&self, other: &Vec3<f32>) -> bool
{
- (&self.center - other).length() <= self.radius
+ (self.center - *other).length() <= self.radius
}
}
diff --git a/engine/src/data_types/matrix.rs b/engine/src/data_types/matrix.rs
index 486ab24..cc43e7b 100644
--- a/engine/src/data_types/matrix.rs
+++ b/engine/src/data_types/matrix.rs
@@ -94,7 +94,7 @@ impl Matrix<f32, 4, 4>
self.set_cell(3, 3, 1.0);
}
- pub fn look_at(&mut self, eye: &Vec3<f32>, target: &Vec3<f32>, up: &Vec3<f32>)
+ pub fn look_at(&mut self, eye: Vec3<f32>, target: Vec3<f32>, up: Vec3<f32>)
{
let rev_target_direction = (eye - target).normalize();
@@ -117,9 +117,9 @@ impl Matrix<f32, 4, 4>
// The vector is negated since we want the world to be translated in the opposite
// direction of where we want the camera to move.
let camera_pos = -Vec3 {
- x: camera_right.dot(eye),
- y: camera_up.dot(eye),
- z: rev_target_direction.dot(eye),
+ x: camera_right.dot(&eye),
+ y: camera_up.dot(&eye),
+ z: rev_target_direction.dot(&eye),
};
self.set_cell(0, 3, camera_pos.x);
diff --git a/engine/src/data_types/vector.rs b/engine/src/data_types/vector.rs
index 77173d6..980ca59 100644
--- a/engine/src/data_types/vector.rs
+++ b/engine/src/data_types/vector.rs
@@ -1,7 +1,69 @@
-use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
+use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::color::Color;
+macro_rules! impl_math_op_traits {
+ (impl $op_trait: ident for $vector: ident, ($($vec_field: ident),+)) => {
+ paste::paste! {
+ impl<Value> $op_trait for $vector<Value>
+ where
+ Value: $op_trait<Output = Value>,
+ {
+ type Output = Self;
+
+ fn [<$op_trait:lower>](self, rhs: Self) -> Self::Output
+ {
+ Self::Output {$(
+ $vec_field: self.$vec_field.[<$op_trait:lower>](rhs.$vec_field)
+ ),+}
+ }
+ }
+
+ impl<Value> [<$op_trait Assign>] for $vector<Value>
+ where
+ Value: [<$op_trait Assign>],
+ {
+ fn [<$op_trait:lower _assign>](&mut self, rhs: Self)
+ {
+ $(
+ self.$vec_field.[<$op_trait:lower _assign>](rhs.$vec_field);
+ )+
+ }
+ }
+ }
+ };
+
+ (impl $op_trait: ident <Value> for $vector: ident, ($($vec_field: ident),+)) => {
+ paste::paste! {
+ impl<Value> $op_trait<Value> for $vector<Value>
+ where
+ Value: $op_trait<Output = Value> + Clone,
+ {
+ type Output = Self;
+
+ fn [<$op_trait:lower>](self, rhs: Value) -> Self::Output
+ {
+ Self {$(
+ $vec_field: self.$vec_field.[<$op_trait:lower>](rhs.clone())
+ ),+}
+ }
+ }
+
+ impl<Value> [<$op_trait Assign>]<Value> for $vector<Value>
+ where
+ Value: [<$op_trait Assign>] + Clone,
+ {
+ fn [<$op_trait:lower _assign>](&mut self, rhs: Value)
+ {
+ $(
+ self.$vec_field.[<$op_trait:lower _assign>](rhs.clone());
+ )+
+ }
+ }
+ }
+ };
+}
+
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Vec2<Value>
{
@@ -22,88 +84,13 @@ impl Vec2<u32>
pub const ZERO: Self = Self { x: 0, y: 0 };
}
-impl<Value> Add for Vec2<Value>
-where
- Value: Add<Value, Output = Value>,
-{
- type Output = Self;
-
- fn add(self, rhs: Self) -> Self::Output
- {
- Self::Output { x: self.x + rhs.x, y: self.y + rhs.y }
- }
-}
-
-impl<Value> AddAssign for Vec2<Value>
-where
- Value: Add<Value, Output = Value> + Clone,
-{
- fn add_assign(&mut self, rhs: Self)
- {
- self.x = self.x.clone() + rhs.x;
- self.y = self.y.clone() + rhs.y;
- }
-}
-
-impl<Value> Add<Value> for Vec2<Value>
-where
- Value: Add<Output = Value> + Clone,
-{
- type Output = Self;
-
- fn add(self, rhs: Value) -> Self::Output
- {
- Self {
- x: self.x + rhs.clone(),
- y: self.y + rhs,
- }
- }
-}
-
-impl<Value> Sub<Value> for Vec2<Value>
-where
- Value: Sub<Output = Value> + Clone,
-{
- type Output = Self;
-
- fn sub(self, rhs: Value) -> Self::Output
- {
- Self {
- x: self.x - rhs.clone(),
- y: self.y - rhs,
- }
- }
-}
+impl_math_op_traits!(impl Add for Vec2, (x, y));
+impl_math_op_traits!(impl Sub for Vec2, (x, y));
-impl<Value> Mul<Value> for Vec2<Value>
-where
- Value: Mul<Output = Value> + Clone,
-{
- type Output = Self;
-
- fn mul(self, rhs: Value) -> Self::Output
- {
- Self {
- x: self.x * rhs.clone(),
- y: self.y * rhs,
- }
- }
-}
-
-impl<Value> Div<Value> for Vec2<Value>
-where
- Value: Div<Output = Value> + Clone,
-{
- type Output = Self;
-
- fn div(self, rhs: Value) -> Self::Output
- {
- Self {
- x: self.x / rhs.clone(),
- y: self.y / rhs,
- }
- }
-}
+impl_math_op_traits!(impl Add<Value> for Vec2, (x, y));
+impl_math_op_traits!(impl Sub<Value> for Vec2, (x, y));
+impl_math_op_traits!(impl Mul<Value> for Vec2, (x, y));
+impl_math_op_traits!(impl Div<Value> for Vec2, (x, y));
impl<Value> From<[Value; 2]> for Vec2<Value>
{
@@ -204,85 +191,13 @@ impl<Value> Vec3<Value>
}
}
-impl<Value> Sub for Vec3<Value>
-where
- Value: Sub<Value, Output = Value>,
-{
- type Output = Self;
-
- fn sub(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- z: self.z - rhs.z,
- }
- }
-}
-
-impl<Value> Sub for &Vec3<Value>
-where
- for<'a, 'b> &'a Value: Sub<&'b Value, Output = Value>,
-{
- type Output = Vec3<Value>;
-
- fn sub(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: &self.x - &rhs.x,
- y: &self.y - &rhs.y,
- z: &self.z - &rhs.z,
- }
- }
-}
-
-impl<Value> Add for Vec3<Value>
-where
- Value: Add<Value, Output = Value>,
-{
- type Output = Self;
-
- fn add(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
- z: self.z + rhs.z,
- }
- }
-}
-
-impl<Value> Add for &Vec3<Value>
-where
- for<'a, 'b> &'a Value: Add<&'b Value, Output = Value>,
-{
- type Output = Vec3<Value>;
-
- fn add(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: &self.x + &rhs.x,
- y: &self.y + &rhs.y,
- z: &self.z + &rhs.z,
- }
- }
-}
-
-impl<Value> Mul for Vec3<Value>
-where
- Value: Mul<Value, Output = Value>,
-{
- type Output = Self;
+impl_math_op_traits!(impl Add for Vec3, (x, y, z));
+impl_math_op_traits!(impl Sub for Vec3, (x, y, z));
- fn mul(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- z: self.z * rhs.z,
- }
- }
-}
+impl_math_op_traits!(impl Add<Value> for Vec3, (x, y, z));
+impl_math_op_traits!(impl Sub<Value> for Vec3, (x, y, z));
+impl_math_op_traits!(impl Mul<Value> for Vec3, (x, y, z));
+impl_math_op_traits!(impl Div<Value> for Vec3, (x, y, z));
impl<Value> Neg for Vec3<Value>
where
@@ -300,78 +215,6 @@ where
}
}
-impl<Value> Add<Value> for Vec3<Value>
-where
- Value: Add<Value, Output = Value> + Clone,
-{
- type Output = Self;
-
- fn add(mut self, rhs: Value) -> Self::Output
- {
- self.x = self.x + rhs.clone();
- self.y = self.y + rhs.clone();
- self.z = self.z + rhs.clone();
-
- self
- }
-}
-
-impl<Value> Sub<Value> for Vec3<Value>
-where
- Value: Sub<Value, Output = Value> + Clone,
-{
- type Output = Self;
-
- fn sub(mut self, rhs: Value) -> Self::Output
- {
- self.x = self.x - rhs.clone();
- self.y = self.y - rhs.clone();
- self.z = self.z - rhs.clone();
-
- self
- }
-}
-
-impl<Value> Mul<Value> for Vec3<Value>
-where
- Value: Mul<Value, Output = Value> + Clone,
-{
- type Output = Self;
-
- fn mul(mut self, rhs: Value) -> Self::Output
- {
- self.x = self.x * rhs.clone();
- self.y = self.y * rhs.clone();
- self.z = self.z * rhs.clone();
-
- self
- }
-}
-
-impl<Value> AddAssign for Vec3<Value>
-where
- Value: AddAssign<Value>,
-{
- fn add_assign(&mut self, rhs: Self)
- {
- self.x += rhs.x;
- self.y += rhs.y;
- self.z += rhs.z;
- }
-}
-
-impl<Value> SubAssign for Vec3<Value>
-where
- Value: SubAssign<Value>,
-{
- fn sub_assign(&mut self, rhs: Self)
- {
- self.x -= rhs.x;
- self.y -= rhs.y;
- self.z -= rhs.z;
- }
-}
-
impl From<f32> for Vec3<f32>
{
fn from(value: f32) -> Self
@@ -417,73 +260,14 @@ pub struct Vec4<Value>
pub w: Value,
}
-impl<Value> Mul for Vec4<Value>
-where
- Value: Mul<Value, Output = Value>,
-{
- type Output = Self;
-
- fn mul(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x * rhs.x,
- y: self.y * rhs.y,
- z: self.z * rhs.z,
- w: self.w * rhs.w,
- }
- }
-}
-
-impl<Value> Add for Vec4<Value>
-where
- Value: Add<Value, Output = Value>,
-{
- type Output = Self;
+impl_math_op_traits!(impl Add for Vec4, (x, y, z, w));
+impl_math_op_traits!(impl Sub for Vec4, (x, y, z, w));
+impl_math_op_traits!(impl Mul for Vec4, (x, y, z, w));
- fn add(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x + rhs.x,
- y: self.y + rhs.y,
- z: self.z + rhs.z,
- w: self.w + rhs.w,
- }
- }
-}
-
-impl<Value> Sub for Vec4<Value>
-where
- Value: Sub<Value, Output = Value>,
-{
- type Output = Self;
-
- fn sub(self, rhs: Self) -> Self::Output
- {
- Self::Output {
- x: self.x - rhs.x,
- y: self.y - rhs.y,
- z: self.z - rhs.z,
- w: self.w - rhs.w,
- }
- }
-}
-
-impl<Value> Mul<Value> for Vec4<Value>
-where
- Value: Mul<Value, Output = Value> + Clone,
-{
- type Output = Self;
-
- fn mul(mut self, rhs: Value) -> Self::Output
- {
- self.x = self.x * rhs.clone();
- self.y = self.y * rhs.clone();
- self.z = self.z * rhs.clone();
- self.w = self.w * rhs.clone();
-
- self
- }
-}
+impl_math_op_traits!(impl Add<Value> for Vec4, (x, y, z, w));
+impl_math_op_traits!(impl Sub<Value> for Vec4, (x, y, z, w));
+impl_math_op_traits!(impl Mul<Value> for Vec4, (x, y, z, w));
+impl_math_op_traits!(impl Div<Value> for Vec4, (x, y, z, w));
impl<Value: Clone> From<Value> for Vec4<Value>
{
diff --git a/engine/src/math.rs b/engine/src/math.rs
index 0340de8..eeed61e 100644
--- a/engine/src/math.rs
+++ b/engine/src/math.rs
@@ -5,9 +5,9 @@ use crate::vector::Vec3;
/// Calculates the surface normal of a triangle.
#[must_use]
pub fn calc_triangle_surface_normal(
- egde_a: &Vec3<f32>,
- edge_b: &Vec3<f32>,
- edge_c: &Vec3<f32>,
+ egde_a: Vec3<f32>,
+ edge_b: Vec3<f32>,
+ edge_c: Vec3<f32>,
) -> Vec3<f32>
{
let v1 = edge_b - egde_a;
diff --git a/engine/src/mesh/cube.rs b/engine/src/mesh/cube.rs
index b30449f..61d1e26 100644
--- a/engine/src/mesh/cube.rs
+++ b/engine/src/mesh/cube.rs
@@ -365,14 +365,14 @@ fn create_side(side_positions: &SidePositions, data: &mut Data)
{
let normal = match side_positions.normal_calc_order {
NormalCalcOrder::Clockwise => calc_triangle_surface_normal(
- &side_positions.up_left,
- &side_positions.up_right,
- &side_positions.down_left,
+ side_positions.up_left,
+ side_positions.up_right,
+ side_positions.down_left,
),
NormalCalcOrder::CounterClockwise => calc_triangle_surface_normal(
- &side_positions.up_left,
- &side_positions.down_left,
- &side_positions.up_right,
+ side_positions.up_left,
+ side_positions.down_left,
+ side_positions.up_right,
),
};
diff --git a/engine/src/shader/default.rs b/engine/src/shader/default.rs
index 1dc85fc..5b360ec 100644
--- a/engine/src/shader/default.rs
+++ b/engine/src/shader/default.rs
@@ -142,7 +142,7 @@ pub fn enqueue_set_shader_bindings(
),
(
model_3d_shader_cursor.field("view"),
- create_view_matrix(&camera, &camera_world_pos.position).into(),
+ create_view_matrix(&camera, camera_world_pos.position).into(),
),
(
lighting_shader_cursor.field("view_pos"),
@@ -325,14 +325,13 @@ fn create_model_matrix(transform: Transform) -> Matrix<f32, 4, 4>
matrix
}
-fn create_view_matrix(camera: &Camera, camera_world_pos: &Vec3<f32>)
- -> Matrix<f32, 4, 4>
+fn create_view_matrix(camera: &Camera, camera_world_pos: Vec3<f32>) -> Matrix<f32, 4, 4>
{
let mut view = Matrix::new();
// tracing::debug!("Camera target: {:?}", camera.target);
- view.look_at(camera_world_pos, &camera.target, &camera.global_up);
+ view.look_at(camera_world_pos, camera.target, camera.global_up);
view
}