Replace nalgebra with glam
`glam` compiles much faster and leverages SIMD nicely.
This commit is contained in:
parent
85916c9e87
commit
298c42ac5f
4 changed files with 22 additions and 27 deletions
|
|
@ -1,29 +1,30 @@
|
|||
use nalgebra::Matrix3;
|
||||
use glam::{Mat4, Vec3, Vec4};
|
||||
use std::ops::Mul;
|
||||
|
||||
/// A 2D transformation matrix.
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub struct Transformation(Matrix3<f32>);
|
||||
pub struct Transformation(Mat4);
|
||||
|
||||
impl Transformation {
|
||||
/// Get the identity transformation.
|
||||
pub fn identity() -> Transformation {
|
||||
Transformation(Matrix3::identity())
|
||||
Transformation(Mat4::identity())
|
||||
}
|
||||
|
||||
/// Creates an orthographic projection.
|
||||
#[rustfmt::skip]
|
||||
pub fn orthographic(width: u16, height: u16) -> Transformation {
|
||||
Transformation(nalgebra::Matrix3::new(
|
||||
2.0 / f32::from(width), 0.0, -1.0,
|
||||
0.0, 2.0 / f32::from(height), -1.0,
|
||||
0.0, 0.0, 1.0
|
||||
Transformation(Mat4::from_cols(
|
||||
Vec4::new(2.0 / f32::from(width), 0.0, 0.0, 0.0),
|
||||
Vec4::new(0.0, 2.0 / f32::from(height), 0.0, 0.0),
|
||||
Vec4::new(0.0, 0.0, -1.0, 0.0),
|
||||
Vec4::new(-1.0, -1.0, 0.0, 1.0)
|
||||
))
|
||||
}
|
||||
|
||||
/// Creates a translate transformation.
|
||||
pub fn translate(x: f32, y: f32) -> Transformation {
|
||||
Transformation(Matrix3::new_translation(&nalgebra::Vector2::new(x, y)))
|
||||
Transformation(Mat4::from_translation(Vec3::new(x, y, 0.0)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,14 +36,14 @@ impl Mul for Transformation {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<Transformation> for [f32; 16] {
|
||||
#[rustfmt::skip]
|
||||
fn from(t: Transformation) -> [f32; 16] {
|
||||
[
|
||||
t.0[0], t.0[1], 0.0, t.0[2],
|
||||
t.0[3], t.0[4], 0.0, t.0[5],
|
||||
0.0, 0.0, -1.0, 0.0,
|
||||
t.0[6], t.0[7], 0.0, t.0[8]
|
||||
]
|
||||
impl AsRef<[f32; 16]> for Transformation {
|
||||
fn as_ref(&self) -> &[f32; 16] {
|
||||
self.0.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Transformation> for [f32; 16] {
|
||||
fn from(t: Transformation) -> [f32; 16] {
|
||||
t.as_ref().clone()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue