1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::Rad; #[derive(Debug, Copy, Clone, PartialOrd, PartialEq)] pub struct Transform3D { pub inner: nalgebra::Matrix4<f32>, } impl Default for Transform3D { fn default() -> Self { Self { inner: nalgebra::Matrix4::identity(), } } } impl Transform3D { pub fn translation(x: f32, y: f32, z: f32) -> Self { Self { inner: nalgebra::Matrix4::new_translation(&nalgebra::Vector3::new(x, y, z)), } } pub fn rotation<R: Into<Rad>>(roll: R, pitch: R, yaw: R) -> Self { Self { inner: nalgebra::Matrix4::from_euler_angles( roll.into().0, pitch.into().0, yaw.into().0, ), } } pub fn scale(x: f32, y: f32, z: f32) -> Self { Self { inner: nalgebra::Matrix4::new_nonuniform_scaling(&nalgebra::Vector3::new(x, y, z)), } } pub fn transform_point(&self, x: f32, y: f32, z: f32) -> [f32; 3] { let p = nalgebra::Vector4::new(x, y, z, 1.0); let p = self.inner.clone() * p; [p.x, p.y, p.z] } pub fn look_at(x: f32, y: f32, z: f32) -> Self { let eye = nalgebra::Point3::new(0., 0., 0.); let target = nalgebra::Point3::new(x, y, z); let up = nalgebra::Vector3::y(); Self { inner: nalgebra::Matrix4::look_at_lh(&eye, &target, &up), } } } impl std::ops::Mul for Transform3D { type Output = Transform3D; fn mul(self, rhs: Self) -> Self::Output { Self { inner: self.inner * rhs.inner, } } } impl std::ops::MulAssign for Transform3D { fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } } impl From<crate::Transform2D> for Transform3D { fn from(t: crate::Transform2D) -> Self { let t: mint::ColumnMatrix4<f32> = t.into(); Self { inner: t.into() } } }