diff options
Diffstat (limited to 'engine/src/data_types')
| -rw-r--r-- | engine/src/data_types/matrix.rs | 8 | ||||
| -rw-r--r-- | engine/src/data_types/vector.rs | 380 |
2 files changed, 86 insertions, 302 deletions
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> { |
