Struct Mat4

Source
#[repr(C)]
pub struct Mat4 { pub values: [f32; 16], }
Expand description

A struct representing a 4x4 matrix.

It’s values are stored in column-major order by default, as expected by OpenGL and accepted by all other APIs. To change this, use feature mat-row-major

Fields§

§values: [f32; 16]

Implementations§

Source§

impl Mat4

Source

pub const fn identity() -> Self

Creates the identity matrix.

Source

pub const fn translate(t: Vec3) -> Self

Creates a 3D translation matrix.

Source

pub fn rotate(r: Quaternion) -> Self

Creates a 3D rotation matrix.

Source

pub const fn scale(s: Vec3) -> Self

Creates a 3D scale matrix.

Source

pub fn local_to_world(t: Vec3, r: Quaternion, s: Vec3) -> Self

Creates a 3D local-to-world/object-to-world matrix.

When multiplying this matrix by a vector, it will be

  • scaled by s
  • rotated by r
  • translated by t

in this order.

Source

pub fn world_to_local(t: Vec3, r: Quaternion, s: Vec3) -> Self

Creates a 3D world-to-local/world-to-object matrix.

This matrix does the opposite of local_to_world()

Source

pub fn orthographic_vulkan( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Self

Creates an orthographic projection matrix with z mapped to [0; 1], as expected by Vulkan.

Source

pub fn orthographic_opengl( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Self

Creates an orthographic projection matrix with z mapped to [-1; 1], as expected by OpenGL.

Source

pub fn inverse_orthographic_vulkan( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Self

Creates an inverse orthographics projection matrix with z mapped to [0; 1], as expected by Vulkan.

The world position of a uv/depth pair can be reconstructed with the same code as shown in inverse_perspective_vulkan()

Source

pub fn inverse_orthographic_opengl( left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32, ) -> Self

Creates an inverse orthographics projection matrix with z mapped to [-1; 1], as expected by OpenGL.

The world position of a uv/depth pair can be reconstructed with the same code as shown in inverse_perspective_opengl()

Source

pub fn perspective_vulkan( fov_rad: f32, near: f32, far: f32, aspect: f32, ) -> Self

Creates a perspective projection matrix with z mapped to [0; 1], as expected by Vulkan.

Source

pub fn perspective_opengl( fov_rad: f32, near: f32, far: f32, aspect: f32, ) -> Self

Creates a perspective projection matrix with z mapped to [-1; 1], as expected by OpenGL.

Source

pub fn inverse_perspective_vulkan( fov_rad: f32, near: f32, far: f32, aspect: f32, ) -> Self

Creates an inverse perspective matrix with z mapped to [0; 1], as expected by Vulkan.

This matrix can be used to reconstruct the world position from a uv/depth pair in a shader. This pseudo code can be used:

vec4 clipPos = vec4(uv.xy, depth, 1.0);
vec4 worldPos = invProjection * clipPos;
worldPos /= worldPos.w;
Source

pub fn inverse_perspective_opengl( fov_rad: f32, near: f32, far: f32, aspect: f32, ) -> Self

Creates an inverse perspective matrix with z mapped to [-1; 1], as expected by OpenGL.

This matrix can be used to reconstruct the world position from a uv/depth pair in a shader. This pseudo code can be used:

vec4 clipPos = vec4(uv.xy, depth * 2.0 - 1.0, 1.0);
vec4 worldPos = invProjection * clipPos;
worldPos /= worldPos.w;
Source

pub const fn get(&self, column: usize, row: usize) -> f32

Returns a value indexed by column and row

Source

pub fn set(&mut self, column: usize, row: usize, val: f32)

Sets the value indexed by column and row to val

Source

pub fn transposed(&self) -> Mat4

Returns a transposed copy of self.

Source

pub fn as_slice(&self) -> &[f32]

Returns the underlying values as a slice

Source

pub fn as_mut_slice(&mut self) -> &mut [f32]

Returns the underlying values as a mutable slice

Source

pub fn as_ptr(&self) -> *const f32

Returns the underlying values as a pointer with no size information

Source

pub fn as_mut_ptr(&mut self) -> *mut f32

Returns the underlying values as a mutable pointer with no size information

Trait Implementations§

Source§

impl Clone for Mat4

Source§

fn clone(&self) -> Mat4

Returns a duplicate of the value. Read more
1.0.0 · Source§

const 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() -> Self

Creates the identity matrix.

Source§

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

Source§

fn from(d: [[f32; 4]; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<[f32; 16]> for Mat4

Source§

fn from(d: [f32; 16]) -> Self

Converts to this type from the input type.
Source§

impl Index<(usize, usize)> for Mat4

Source§

type Output = f32

The returned type after indexing.
Source§

fn index(&self, (c, r): (usize, usize)) -> &Self::Output

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

impl IndexMut<(usize, usize)> for Mat4

Source§

fn index_mut(&mut self, (c, r): (usize, usize)) -> &mut Self::Output

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

impl Mul<&Mat4> for &Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Mat4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Mat4> for Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Mat4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Vec3> for &Mat4

Source§

type Output = Vec3

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Vec3) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Vec3> for Mat4

Source§

type Output = Vec3

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Vec3) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Vec4> for &Mat4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Vec4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<&Vec4> for Mat4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Vec4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Mat4> for &Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mat4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec3> for &Mat4

Source§

type Output = Vec3

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec3) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec3> for Mat4

Source§

type Output = Vec3

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec3) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec4> for &Mat4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Vec4> for Mat4

Source§

type Output = Vec4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Vec4) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Mat4

Source§

type Output = Mat4

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Mat4) -> Self::Output

Performs the * operation. Read more
Source§

impl PartialEq for Mat4

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Mat4

Source§

impl StructuralPartialEq for Mat4

Auto Trait Implementations§

§

impl Freeze for Mat4

§

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 T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.