2019-10-25 03:47:34 +02:00
|
|
|
use nalgebra::Matrix3;
|
|
|
|
|
use std::ops::Mul;
|
|
|
|
|
|
|
|
|
|
/// A 2D transformation matrix.
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
|
pub struct Transformation(Matrix3<f32>);
|
2019-10-07 03:56:16 +02:00
|
|
|
|
|
|
|
|
impl Transformation {
|
2019-10-25 03:47:34 +02:00
|
|
|
/// Get the identity transformation.
|
|
|
|
|
pub fn identity() -> Transformation {
|
|
|
|
|
Transformation(Matrix3::identity())
|
2019-10-07 03:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 03:47:34 +02:00
|
|
|
/// Creates an orthographic projection.
|
2019-10-07 03:56:16 +02:00
|
|
|
#[rustfmt::skip]
|
2019-10-25 03:47:34 +02:00
|
|
|
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
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a translate transformation.
|
|
|
|
|
pub fn translate(x: f32, y: f32) -> Transformation {
|
|
|
|
|
Transformation(Matrix3::new_translation(&nalgebra::Vector2::new(x, y)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Mul for Transformation {
|
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
|
|
fn mul(self, rhs: Self) -> Self {
|
|
|
|
|
Transformation(self.0 * rhs.0)
|
2019-10-07 03:56:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<Transformation> for [f32; 16] {
|
2019-10-25 03:47:34 +02:00
|
|
|
#[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]
|
|
|
|
|
]
|
2019-10-07 03:56:16 +02:00
|
|
|
}
|
|
|
|
|
}
|