Struct gamemath::Mat4

source ·
pub struct Mat4 {
    pub rows: [Vec4<f32>; 4],
}
Expand description

A 4x4-component Euclidean matrix useful for linear algebra computation in game development and 3D rendering.

Fields§

§rows: [Vec4<f32>; 4]

The four rows of the matrix, represented by an array of four Vec4<f32> objects.

Implementations§

source§

impl Mat4

source

pub fn identity() -> Mat4

Constructs a 4x4 identity matrix.

Examples
use gamemath::{Mat4, Vec4};

let m = Mat4::identity();

assert_eq!(m[0], Vec4::new(1.0, 0.0, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.0, 1.0, 0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0, 0.0, 1.0, 0.0));
assert_eq!(m[3], Vec4::new(0.0, 0.0, 0.0, 1.0));
source

pub fn frustum( top: f32, left: f32, right: f32, bottom: f32, near: f32, far: f32 ) -> Mat4

Constructs a 4x4 frustum matrix from a top, left, right, bottom, near and far value.

Examples
use gamemath::{Mat4, Vec4};

let m = Mat4::frustum(-10.0, -10.0, 10.0, 10.0, 0.1, 100.0);

assert_eq!(m[0], Vec4::new(0.01, 0.0, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.0, -0.01, 0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0, 0.0, -1.002002, -1.0));
assert_eq!(m[3], Vec4::new(0.0, 0.0, -0.2002002, 0.0));
source

pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> Mat4

Constructs a 4x4 perspective-projection matrix from a fov, aspect, near and far value.

Examples
use gamemath::{Mat4, Vec4};

let m = Mat4::perspective(55.0, 1920.0 / 1080.0, 0.01, 100.0);

assert_eq!(m[0], Vec4::new(1.0805525, 0.0, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.0, 1.9209821, 0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0, 0.0, -1.0002, -1.0));
assert_eq!(m[3], Vec4::new(0.0, 0.0, -0.020002, 0.0));
source

pub fn look_at(eye: Vec3<f32>, target: Vec3<f32>, up: Vec3<f32>) -> Mat4

Constructs a 4x4 view-matrix from a eye, target and up Vec3<f32>. The resulting view-matrix will be “positioned” at the coordinates of the eye vector, loking in the direction of the coordinates of the target vecctor and with its up direction in the direction of the up vector.

Examples
use gamemath::{Mat4, Vec3, Vec4};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m[0], Vec4::new(-1.0, 0.0, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.0, -1.0, 0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0, 0.0, 1.0, 0.0));
assert_eq!(m[3], Vec4::new(0.0, 0.0, 1.0, 1.0));
source

pub fn orthogonal( top: f32, left: f32, right: f32, bottom: f32, near: f32, far: f32 ) -> Mat4

Constructs a 4x4 perspective-orthogonal matrix from a top, left, right, bottom, near and far value.

Examples
use gamemath::{Mat4, Vec4};

let m = Mat4::orthogonal(-1.0, -1.0, 1.0, 1.0, 0.01, 100.0);

assert_eq!(m[0], Vec4::new(1.0, 0.0, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.0, -1.0, 0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0, 0.0, -0.020002, 0.0));
assert_eq!(m[3], Vec4::new(0.0, 0.0, -1.0002, 1.0));
source

pub fn get_left_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing left, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_left_vector(), Vec3::new(-1.0, 0.0, 0.0));
source

pub fn get_right_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing right, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_right_vector(), Vec3::new(1.0, 0.0, 0.0));
source

pub fn get_up_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing up, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_up_vector(), Vec3::new(0.0, -1.0, 0.0));
source

pub fn get_down_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing down, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_down_vector(), Vec3::new(0.0, 1.0, 0.0));
source

pub fn get_backward_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing backwards, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_backward_vector(), Vec3::new(0.0, 0.0, 1.0));
source

pub fn get_forward_vector(&self) -> Vec3<f32>

Extracts and returns a Vec3<f32> pointing forwards, away from the position of a view-matrix. Pretty much only makes sense for a view-matrix.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.get_forward_vector(), Vec3::new(0.0, 0.0, -1.0));
source

pub fn transposed(&self) -> Mat4

Extracts and returns a transposed representation of the calling Mat4 object.

Examples
use gamemath::Mat4;

let m: Mat4 = (( 0.0,  1.0,  2.0,  3.0),
               ( 4.0,  5.0,  6.0,  7.0),
               ( 8.0,  9.0, 10.0, 11.0),
               (12.0, 13.0, 14.0, 15.0)).into();

assert_eq!(m.transposed(), (( 0.0,  4.0,  8.0, 12.0),
                            ( 1.0,  5.0,  9.0, 13.0),
                            ( 2.0,  6.0, 10.0, 14.0),
                            ( 3.0,  7.0, 11.0, 15.0)).into());
source

pub fn transpose(&mut self)

Performs a transpose operation on the calling Mat4 object.

Examples
use gamemath::Mat4;

let mut m: Mat4 = (( 0.0,  1.0,  2.0,  3.0),
                   ( 4.0,  5.0,  6.0,  7.0),
                   ( 8.0,  9.0, 10.0, 11.0),
                   (12.0, 13.0, 14.0, 15.0)).into();

m.transpose();

assert_eq!(m, (( 0.0,  4.0,  8.0, 12.0),
               ( 1.0,  5.0,  9.0, 13.0),
               ( 2.0,  6.0, 10.0, 14.0),
               ( 3.0,  7.0, 11.0, 15.0)).into());
source

pub fn determinant(&self) -> f32

calculates and returns the determinant value of the calling Mat4 object.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.determinant(), 1.0);
source

pub fn adjointed(&self) -> Mat4

calculates and returns the adjoint matrix of the calling Mat4 object.

Examples
use gamemath::Mat4;

let m: Mat4 = (( 4.0, 15.0,  2.0, 13.0),
               ( 5.0, 10.0,  7.0, 12.0),
               ( 9.0,  6.0, 11.0,  8.0),
               (16.0,  3.0, 14.0,  1.0)).into();

assert_eq!(m.adjointed(), ((-272.0,  816.0, -816.0,  272.0),
                           ( 272.0, -816.0,  816.0, -272.0),
                           ( 272.0, -816.0,  816.0, -272.0),
                           (-272.0,  816.0, -816.0,  272.0)).into());
source

pub fn inverted(&self) -> Mat4

calculates and returns the inverted matrix of the calling Mat4 object.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                      Vec3::new(0.0, 0.0, 0.0),
                      Vec3::new(0.0, -1.0, 0.0));

assert_eq!(m.inverted(), ((-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, -1.0,  1.0)).into());
source

pub fn invert(&mut self)

Performes the inversion operation on the calling Mat4 object.

Examples
use gamemath::{Mat4, Vec3};

let mut m = Mat4::look_at(Vec3::new(0.0, 0.0, 1.0),
                          Vec3::new(0.0, 0.0, 0.0),
                          Vec3::new(0.0, -1.0, 0.0));

m.invert();

assert_eq!(m, ((-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, -1.0,  1.0)).into());
source

pub fn rotation(radians: f32, axis: Vec3<f32>) -> Mat4

Constructs a 4x4 rotation matrix from a radians value and an axis Vec3<f32>.

Examples
use gamemath::{Mat4, Vec3, Vec4};

let m = Mat4::rotation(1.0, Vec3::new(0.0, 0.0, 1.0));

assert_eq!(m[0], Vec4::new(0.5403023,  -0.84147096, 0.0, 0.0));
assert_eq!(m[1], Vec4::new(0.84147096,  0.5403023,  0.0, 0.0));
assert_eq!(m[2], Vec4::new(0.0,         0.0,        1.0, 0.0));
assert_eq!(m[3], Vec4::new(0.0,         0.0,        0.0, 1.0));
source

pub fn rotated(&self, radians: f32, axis: Vec3<f32>) -> Mat4

Calculates and returns a Mat4 object representing the calling Mat4 object rotated around a Vec3<f32> axis, by a radians value.

Examples
use gamemath::{Mat4, Vec3, Vec4};

let m = Mat4::identity().rotated(1.0, Vec3::new(0.0, 0.0, 1.0));

assert_eq!(m, ((0.5403023,  -0.84147096, 0.0, 0.0),
               (0.84147096,  0.5403023,  0.0, 0.0),
               (0.0,         0.0,        1.0, 0.0),
               (0.0,         0.0,        0.0, 1.0)).into());
source

pub fn rotate(&mut self, radians: f32, axis: Vec3<f32>)

Rotates the calling Mat4 object around a Vec3<f32> axis, by a radians value.

Examples
use gamemath::{Mat4, Vec3, Vec4};

let mut m = Mat4::identity();

m.rotate(1.0, Vec3::new(0.0, 0.0, 1.0));

assert_eq!(m, ((0.5403023,  -0.84147096, 0.0, 0.0),
               (0.84147096,  0.5403023,  0.0, 0.0),
               (0.0,         0.0,        1.0, 0.0),
               (0.0,         0.0,        0.0, 1.0)).into());
source

pub fn scaled(&self, factor: Vec3<f32>) -> Mat4

Calculates and returns a Mat4 object representing the calling Mat4 object scaled by a Vec3<f32>.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::identity();

assert_eq!(m.scaled(Vec3::new(1.0, 2.0, 3.0)), ((1.0, 0.0, 0.0, 0.0),
                                                (0.0, 2.0, 0.0, 0.0),
                                                (0.0, 0.0, 3.0, 0.0),
                                                (0.0, 0.0, 0.0, 1.0)).into());
source

pub fn scale(&mut self, factor: Vec3<f32>)

Performs the scale operation on the calling Mat4 object, scaling it by a Vec3<f32>.

Examples
use gamemath::{Mat4, Vec3};

let mut m = Mat4::identity();

m.scale(Vec3::new(1.0, 2.0, 3.0));

assert_eq!(m, ((1.0, 0.0, 0.0, 0.0),
               (0.0, 2.0, 0.0, 0.0),
               (0.0, 0.0, 3.0, 0.0),
               (0.0, 0.0, 0.0, 1.0)).into());
source

pub fn translated(&self, translation: Vec3<f32>) -> Mat4

Calculates and returns a Mat4 object representing the calling Mat4 object translated by a Vec3<f32>.

Examples
use gamemath::{Mat4, Vec3};

let m = Mat4::identity();

assert_eq!(m.translated(Vec3::new(1.0, 2.0, 3.0)), ((1.0, 0.0, 0.0, 0.0),
                                                    (0.0, 1.0, 0.0, 0.0),
                                                    (0.0, 0.0, 1.0, 0.0),
                                                    (1.0, 2.0, 3.0, 1.0)).into());
source

pub fn translate(&mut self, translation: Vec3<f32>)

Performs the translate operation on the calling Mat4 object, translating it by a Vec3<f32>.

Examples
use gamemath::{Mat4, Vec3};

let mut m = Mat4::identity();

m.translate(Vec3::new(1.0, 2.0, 3.0));

assert_eq!(m, ((1.0, 0.0, 0.0, 0.0),
               (0.0, 1.0, 0.0, 0.0),
               (0.0, 0.0, 1.0, 0.0),
               (1.0, 2.0, 3.0, 1.0)).into());

Trait Implementations§

source§

impl Add for Mat4

§

type Output = Mat4

The resulting type after applying the + operator.
source§

fn add(self, right: Mat4) -> Mat4

Performs the + operation. Read more
source§

impl AddAssign for Mat4

source§

fn add_assign(&mut self, right: Mat4)

Performs the += operation. Read more
source§

impl Clone for Mat4

source§

fn clone(&self) -> Mat4

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Mat4

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Mat4

source§

fn default() -> Mat4

Returns the “default value” for a type. Read more
source§

impl From<[[f32; 4]; 4]> for Mat4

source§

fn from(slice: [[f32; 4]; 4]) -> Mat4

Converts to this type from the input type.
source§

impl From<[Vec4<f32>; 4]> for Mat4

source§

fn from(slice: [Vec4<f32>; 4]) -> Mat4

Converts to this type from the input type.
source§

impl From<[f32; 16]> for Mat4

source§

fn from(slice: [f32; 16]) -> Mat4

Converts to this type from the input type.
source§

impl From<((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32))> for Mat4

source§

fn from( tuple: ((f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32), (f32, f32, f32, f32)) ) -> Mat4

Converts to this type from the input type.
source§

impl From<(Vec4<f32>, Vec4<f32>, Vec4<f32>, Vec4<f32>)> for Mat4

source§

fn from(tuple: (Vec4<f32>, Vec4<f32>, Vec4<f32>, Vec4<f32>)) -> Mat4

Converts to this type from the input type.
source§

impl From<(f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32)> for Mat4

source§

fn from( tuple: (f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32, f32) ) -> Mat4

Converts to this type from the input type.
source§

impl From<Quat> for Mat4

source§

fn from(quat: Quat) -> Mat4

Converts to this type from the input type.
source§

impl From<f32> for Mat4

source§

fn from(value: f32) -> Mat4

Converts to this type from the input type.
source§

impl Index<(usize, usize)> for Mat4

§

type Output = f32

The returned type after indexing.
source§

fn index(&self, index: (usize, usize)) -> &f32

Performs the indexing (container[index]) operation. Read more
source§

impl Index<usize> for Mat4

§

type Output = Vec4<f32>

The returned type after indexing.
source§

fn index(&self, index: usize) -> &Vec4<f32>

Performs the indexing (container[index]) operation. Read more
source§

impl IndexMut<(usize, usize)> for Mat4

source§

fn index_mut(&mut self, index: (usize, usize)) -> &mut f32

Performs the mutable indexing (container[index]) operation. Read more
source§

impl IndexMut<usize> for Mat4

source§

fn index_mut(&mut self, index: usize) -> &mut Vec4<f32>

Performs the mutable indexing (container[index]) operation. Read more
source§

impl Mul<Vec4<f32>> for Mat4

§

type Output = Vec4<f32>

The resulting type after applying the * operator.
source§

fn mul(self, vec: Vec4<f32>) -> Vec4<f32>

Performs the * operation. Read more
source§

impl Mul for Mat4

§

type Output = Mat4

The resulting type after applying the * operator.
source§

fn mul(self, right: Mat4) -> Mat4

Performs the * operation. Read more
source§

impl MulAssign for Mat4

source§

fn mul_assign(&mut self, right: Mat4)

Performs the *= operation. Read more
source§

impl PartialEq for Mat4

source§

fn eq(&self, other: &Mat4) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub for Mat4

§

type Output = Mat4

The resulting type after applying the - operator.
source§

fn sub(self, right: Mat4) -> Mat4

Performs the - operation. Read more
source§

impl SubAssign for Mat4

source§

fn sub_assign(&mut self, right: Mat4)

Performs the -= operation. Read more
source§

impl Copy for Mat4

source§

impl StructuralPartialEq for Mat4

Auto Trait Implementations§

§

impl RefUnwindSafe for Mat4

§

impl Send for Mat4

§

impl Sync for Mat4

§

impl Unpin for Mat4

§

impl UnwindSafe for Mat4

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.