diff options
| author | 2020-05-28 21:52:34 +0200 | |
|---|---|---|
| committer | 2020-05-28 21:52:34 +0200 | |
| commit | d3db055583f4cbef1441fd66d07da70424bd1200 (patch) | |
| tree | 9f695bd26f688a5aaf3b8fa687a0e3ff096ffe11 /graphics/src/transformation.rs | |
| parent | ead4186870d1b46015986f702dd63382498060fc (diff) | |
| parent | 709ed1f3f7ad8cf67a176763e394aaae4e808e93 (diff) | |
| download | iced-d3db055583f4cbef1441fd66d07da70424bd1200.tar.gz iced-d3db055583f4cbef1441fd66d07da70424bd1200.tar.bz2 iced-d3db055583f4cbef1441fd66d07da70424bd1200.zip | |
Merge pull request #354 from hecrj/feature/glow-renderer
OpenGL renderer and backend-agnostic graphics subcrate
Diffstat (limited to 'graphics/src/transformation.rs')
| -rw-r--r-- | graphics/src/transformation.rs | 54 | 
1 files changed, 54 insertions, 0 deletions
| diff --git a/graphics/src/transformation.rs b/graphics/src/transformation.rs new file mode 100644 index 00000000..ff3b1d00 --- /dev/null +++ b/graphics/src/transformation.rs @@ -0,0 +1,54 @@ +use glam::{Mat4, Vec3, Vec4}; +use std::ops::Mul; + +/// A 2D transformation matrix. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Transformation(Mat4); + +impl Transformation { +    /// Get the identity transformation. +    pub fn identity() -> Transformation { +        Transformation(Mat4::identity()) +    } + +    /// Creates an orthographic projection. +    #[rustfmt::skip] +    pub fn orthographic(width: u32, height: u32) -> Transformation { +        Transformation(Mat4::from_cols( +            Vec4::new(2.0 / width as f32, 0.0, 0.0, 0.0), +            Vec4::new(0.0, -2.0 / height as f32, 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(Mat4::from_translation(Vec3::new(x, y, 0.0))) +    } + +    /// Creates a scale transformation. +    pub fn scale(x: f32, y: f32) -> Transformation { +        Transformation(Mat4::from_scale(Vec3::new(x, y, 1.0))) +    } +} + +impl Mul for Transformation { +    type Output = Self; + +    fn mul(self, rhs: Self) -> Self { +        Transformation(self.0 * rhs.0) +    } +} + +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() +    } +} | 
