From 719c073fc67c87d6b2da1bc01b74751d3f5e59f0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 25 Oct 2019 03:47:34 +0200 Subject: Draft `Scrollable` widget (no clipping yet!) --- wgpu/src/transformation.rs | 67 +++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 19 deletions(-) (limited to 'wgpu/src/transformation.rs') diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index 1101e135..ed80b31a 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -1,30 +1,59 @@ -#[derive(Debug, Clone, Copy)] -pub struct Transformation([f32; 16]); +use nalgebra::Matrix3; +use std::ops::Mul; + +/// A 2D transformation matrix. +/// +/// It can be used to apply a transformation to a [`Target`]. +/// +/// [`Target`]: struct.Target.html +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Transformation(Matrix3); impl Transformation { - #[rustfmt::skip] - pub fn identity() -> Self { - Transformation([ - 1.0, 0.0, 0.0, 0.0, - 0.0, 1.0, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 0.0, 1.0, - ]) + /// Get the identity transformation. + pub fn identity() -> Transformation { + Transformation(Matrix3::identity()) } + /// Creates an orthographic projection. + /// + /// You should rarely need this. On creation, a [`Target`] is automatically + /// set up with the correct orthographic projection. + /// + /// [`Target`]: struct.Target.html #[rustfmt::skip] - pub fn orthographic(width: u16, height: u16) -> Self { - Transformation([ - 2.0 / width as f32, 0.0, 0.0, 0.0, - 0.0, 2.0 / height as f32, 0.0, 0.0, - 0.0, 0.0, 1.0, 0.0, - -1.0, -1.0, 0.0, 1.0, - ]) + 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. + /// + /// You can use this to pan your camera, for example. + 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) } } impl From for [f32; 16] { - fn from(transformation: Transformation) -> [f32; 16] { - transformation.0 + #[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] + ] } } -- cgit From ace4217b22131865623faac99cfdb5692a84d1ae Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 29 Oct 2019 19:45:47 +0100 Subject: Fix `Transformation` docs --- wgpu/src/transformation.rs | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'wgpu/src/transformation.rs') diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index ed80b31a..53583e7e 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -2,10 +2,6 @@ use nalgebra::Matrix3; use std::ops::Mul; /// A 2D transformation matrix. -/// -/// It can be used to apply a transformation to a [`Target`]. -/// -/// [`Target`]: struct.Target.html #[derive(Debug, Clone, Copy, PartialEq)] pub struct Transformation(Matrix3); @@ -16,11 +12,6 @@ impl Transformation { } /// Creates an orthographic projection. - /// - /// You should rarely need this. On creation, a [`Target`] is automatically - /// set up with the correct orthographic projection. - /// - /// [`Target`]: struct.Target.html #[rustfmt::skip] pub fn orthographic(width: u16, height: u16) -> Transformation { Transformation(nalgebra::Matrix3::new( @@ -31,8 +22,6 @@ impl Transformation { } /// Creates a translate transformation. - /// - /// You can use this to pan your camera, for example. pub fn translate(x: f32, y: f32) -> Transformation { Transformation(Matrix3::new_translation(&nalgebra::Vector2::new(x, y))) } -- cgit From 298c42ac5f208745cd3b23b3cc8f10f7c8769797 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 31 Oct 2019 01:41:04 +0100 Subject: Replace `nalgebra` with `glam` `glam` compiles much faster and leverages SIMD nicely. --- wgpu/src/transformation.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'wgpu/src/transformation.rs') diff --git a/wgpu/src/transformation.rs b/wgpu/src/transformation.rs index 53583e7e..b0d14cc8 100644 --- a/wgpu/src/transformation.rs +++ b/wgpu/src/transformation.rs @@ -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); +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 AsRef<[f32; 16]> for Transformation { + fn as_ref(&self) -> &[f32; 16] { + self.0.as_ref() + } +} + impl From 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] - ] + t.as_ref().clone() } } -- cgit