struct Eagle::Mat4

Overview

A 4x4 matrix in column-major order, matching OpenGL. Index it as m[col, row].

Eagle builds matrices for you: Node3D#transform, Camera3D#view and Camera3D#projection. You need Mat4 directly for custom shaders, or when you place things by hand in Scene3D space.

model = Mat4.trs(v3(0, 1, 0), Quat.from_axis_angle(Vec3::UP, 0.5), Vec3.new(2))
world = model.transform_point(v3(1, 0, 0)) # local point to world
back = model.inverse.transform_point(world)

view = Mat4.look_at(v3(0, 3, 8), Vec3::ZERO)
proj = Mat4.perspective(Mathf.deg2rad(60), 16 / 9, 0.1, 100)
mvp = proj * view * model # what a vertex shader needs

Defined in:

eagle/math/mat4.cr

Constant Summary

IDENTITY = identity

The identity matrix, as a constant.

Constructors

Instance Method Summary

Constructor Detail

def self.identity : Mat4 #

The identity matrix.


def self.look_at(eye : Vec3, target : Vec3, up : Vec3 = Vec3::UP) : Mat4 #

A view matrix for a camera at eye looking at target.


def self.new(m : StaticArray(Float32, 16)) #

Wraps 16 floats in column-major order.


def self.new #

Creates the zero matrix. Use Mat4.identity for "no transform".


def self.orthographic(left : Number, right : Number, bottom : Number, top : Number, near : Number = -1, far : Number = 1) : Mat4 #

An orthographic projection: no perspective, so parallel lines stay parallel. Used for 2D and for directional-light shadow maps.


def self.perspective(fov_y_rad : Number, aspect : Number, near : Number, far : Number) : Mat4 #

A perspective projection with a vertical field of view in radians.


def self.rotation(axis : Vec3, rad : Number) : Mat4 #

Rotation of rad radians around an arbitrary axis.


def self.rotation_x(rad : Number) : Mat4 #

Rotation around the X axis (pitch).


def self.rotation_y(rad : Number) : Mat4 #

Rotation around the Y axis (yaw).


def self.rotation_z(rad : Number) : Mat4 #

Rotation around the Z axis (roll).


def self.scale(v : Vec3) : Mat4 #

A scale matrix.


def self.scale(s : Number) : Mat4 #

A uniform scale matrix.


def self.translation(x : Number, y : Number, z : Number = 0) : Mat4 #

A translation matrix from components.


def self.translation(v : Vec3) : Mat4 #

A translation matrix.


def self.trs(t : Vec3, r : Quat, s : Vec3) : Mat4 #

Translation * rotation * scale, the usual model matrix.


Instance Method Detail

def *(o : Mat4) : Mat4 #

Composes matrices: (a * b) applies b first, then a.


def *(v : Vec4) : Vec4 #

Transforms a Vec4.


def ==(o : Mat4) : Bool #

Exact equality. Use #approx? for computed values.


def [](col : Int, row : Int) : Float32 #

Reads the element at col, row.


def []=(col : Int, row : Int, v : Number) #

Writes the element at col, row.


def approx?(o : Mat4, eps = 1e-4) : Bool #

True when all elements are within eps.


def data : StaticArray(Float32, 16) #

The raw column-major storage.


def determinant : Float32 #

The determinant. Zero means the matrix can't be inverted.


def inverse : Mat4 #

The inverse matrix, for going from world space back to local space.


def to_a : Array(Float32) #

The 16 floats as an array.


def to_mat3 : Mat3 #

The upper-left 3x3, used to build normal matrices.


def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


def to_unsafe : Pointer(Float32) #

Pointer to the 16 floats, for passing to OpenGL.


def transform_dir(v : Vec3) : Vec3 #

Transforms a direction (w = 0), which ignores translation.


def transform_point(v : Vec3) : Vec3 #

Transforms a point (w = 1) and performs the perspective divide.


def translation : Vec3 #

The translation part.


def transposed : Mat4 #

Rows and columns swapped.