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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
//! Utility for transformation matrix creation //! //! This crate allows the user to //! create transformation matrices, mostly for the use in //! OpenGL applications. //! //! # Quick Start //! //! You can start by creating a new //! [`Transform`](struct.Transform.html) object, then adding //! some transformations, and finalizing the transformation //! with the //! [`orthographic()`](struct.Transform.html#method.orthographic) //! projection. //! //! ``` //! use transform_matrix::*; //! //! let t = Transform::new() //! .translate(2.0, 0.0, 0.0) //! .scale(3.0, 0.0, 0.0) //! .orthographic(10.0, 10.0, 1.0); //! //! assert_eq!(t, [ //! [0.6, 0.0, 0.0, 0.0], //! [0.0, 0.0, 0.0, 0.0], //! [0.0, 0.0, 0.0, 0.0], //! [0.20000005, 1.0, -1.0, 1.0] //! ]); //! ``` mod matrix; use matrix::Matrix; pub struct Transform { matrix: Matrix, } impl Transform { /// Begins a new transformation /// /// *Note*: The transformation starts as an identity matrix pub fn new() -> Self { Self { matrix: matrix::identity(), } } /// Adds a translation transformation /// /// # Arguments /// /// * `x` - Amount by which to move on the x-axis /// /// * `y` - Amount by which to move on the y-axis /// /// * `z` - Amount by which to move on the z-axis pub fn translate(mut self, x: f32, y: f32, z: f32) -> Self { self.matrix = matrix::multiply(self.matrix, matrix::translate(x, y, z)); self } /// Adds a scale transformation /// /// # Arguments /// /// * `x` - Amount by which to scale on the x-axis /// /// * `y` - Amount by which to scale on the y-axis /// /// * `z` - Amount by which to scale on the z-axis pub fn scale(mut self, x: f32, y: f32, z: f32) -> Self { self.matrix = matrix::multiply(self.matrix, matrix::scale(x, y, z)); self } /// Adds a rotation transformation /// /// # Arguments /// /// * `x` - Amount by which to rotate on the x-axis /// /// * `y` - Amount by which to rotate on the y-axis /// /// * `z` - Amount by which to rotate on the z-axis pub fn rotate(mut self, x: f32, y: f32, z: f32) -> Self { self.matrix = matrix::multiply(self.matrix, matrix::rotate_x(x)); self.matrix = matrix::multiply(self.matrix, matrix::rotate_y(y)); self.matrix = matrix::multiply(self.matrix, matrix::rotate_z(z)); self } /// Adds an orthographic projection transformation /// /// # Arguments /// /// * `width` - Size of the view on the x-axis /// /// * `height` - Size of the view on the y-axis /// /// * `depth` - Size of the view on the z-axis /// /// *Note*: This is the last step of the transformation /// and returns the matrix pub fn orthographic(self, width: f32, height: f32, depth: f32) -> Matrix { matrix::multiply(self.matrix, matrix::orthographic(width, height, depth)) } } impl Default for Transform { fn default() -> Self { Transform::new() } }