Package limn.math

Class Mat4

java.lang.Object
limn.math.Mat4

public final class Mat4 extends Object
An immutable column-major 4×4 matrix (m[col*4 + row], OpenGL convention), single precision. Composition is a.multiply(b) = apply b first, then a. Right-handed; clip-space z in [-1, 1].
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Mat4
    The identity matrix (immutable, so one shared instance serves everyone).
  • Method Summary

    Modifier and Type
    Method
    Description
    static Mat4
    affine2D(float a, float b, float c, float d, float e, float f)
    The 2D affine map x' = a·x + c·y + e, y' = b·x + d·y + f of the plane, as a matrix whose z row and column are the identity's: the six numbers in the order a 2D canvas's or a vector drawing's matrix lists them.
    float
    get(int row, int col)
    Element at (row, col).
    static Mat4
    The identity transform.
     
    static Mat4
    lookAt(Vec3 eye, Vec3 center, Vec3 up)
    A right-handed world-to-view matrix looking from eye at center.
    this · o.
    static void
    multiplyInto(Mat4 a, Mat4 b, float[] out)
    a · b written column-major into out (length ≥ 16).
    Normal matrix: transpose(inverse(upper-left 3×3)), for transforming normals.
    static void
    normalMatrixInto(Mat4 model, float[] out)
    The normal matrix of model, transpose(inverse(upper-left 3×3)), written column-major into out (length ≥ 9).
    static Mat4
    orthographic(float left, float right, float bottom, float top, float near, float far)
    An orthographic projection mapping the box to clip space with depth in [-1, 1]: the GL convention, not the [0, 1] one.
    static Mat4
    perspective(float fovyRadians, float aspect, float near, float far)
    Perspective projection; fovyRadians is the vertical field of view.
    static Mat4
    perspectiveAt(float distance)
    A perspective seen from an eye distance in front of the plane z = 0, on the z axis, z running toward the viewer: a point is divided by 1 − z / distance, so the plane itself is left where it is, a point nearer the eye is drawn larger and farther from the axis, and one at the eye or past it has no place in front of it (w not above 0).
    static Mat4
    Rotation from a unit quaternion.
    static Mat4
    A pure non-uniform scale about the origin.
    float[]
    Column-major copy (length 16), for GPU upload.
    void
    toArray(float[] out)
    Column-major copy into out (length ≥ 16), the allocation-free upload path.
    Upper-left 3×3.
    Transforms a homogeneous vector.
    Transforms a direction (w=0): no translation, no divide.
    Transforms a position (w=1) and applies the perspective divide.
    static Mat4
    A pure translation.
    The transpose: the inverse rotation, for a matrix that is purely a rotation.
    static Mat4
    trs(Vec3 t, Quat q, Vec3 s)
    Translate · Rotate · Scale, composed directly.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • IDENTITY

      public static final Mat4 IDENTITY
      The identity matrix (immutable, so one shared instance serves everyone).
  • Method Details

    • identity

      public static Mat4 identity()
      The identity transform.
    • translation

      public static Mat4 translation(Vec3 t)
      A pure translation.
    • scale

      public static Mat4 scale(Vec3 s)
      A pure non-uniform scale about the origin.
    • rotation

      public static Mat4 rotation(Quat q)
      Rotation from a unit quaternion.
    • affine2D

      public static Mat4 affine2D(float a, float b, float c, float d, float e, float f)
      The 2D affine map x' = a·x + c·y + e, y' = b·x + d·y + f of the plane, as a matrix whose z row and column are the identity's: the six numbers in the order a 2D canvas's or a vector drawing's matrix lists them. A skew along x by an angle θ is affine2D(1, 0, tan θ, 1, 0, 0).
      Parameters:
      a - how far x moves along x per unit of x
      b - how far y moves per unit of x
      c - how far x moves per unit of y
      d - how far y moves per unit of y
      e - the translation along x
      f - the translation along y
      Returns:
      the matrix
    • perspectiveAt

      public static Mat4 perspectiveAt(float distance)
      A perspective seen from an eye distance in front of the plane z = 0, on the z axis, z running toward the viewer: a point is divided by 1 − z / distance, so the plane itself is left where it is, a point nearer the eye is drawn larger and farther from the axis, and one at the eye or past it has no place in front of it (w not above 0). The vanishing point is the origin; a translation before this matrix moves it. Unlike perspective(float, float, float, float), a camera's projection into clip space, it keeps the units it is given, which is what a picture turned in depth about a point of a window needs. An infinite distance is no perspective, the identity.
      Parameters:
      distance - how far the eye is from the plane, in the plane's own units
      Returns:
      the perspective
      Throws:
      IllegalArgumentException - if distance is not above 0, or is NaN
    • trs

      public static Mat4 trs(Vec3 t, Quat q, Vec3 s)
      Translate · Rotate · Scale, composed directly.
    • perspective

      public static Mat4 perspective(float fovyRadians, float aspect, float near, float far)
      Perspective projection; fovyRadians is the vertical field of view.
    • orthographic

      public static Mat4 orthographic(float left, float right, float bottom, float top, float near, float far)
      An orthographic projection mapping the box to clip space with depth in [-1, 1]: the GL convention, not the [0, 1] one.
    • lookAt

      public static Mat4 lookAt(Vec3 eye, Vec3 center, Vec3 up)
      A right-handed world-to-view matrix looking from eye at center. up must not be parallel to the view direction, which would leave the basis degenerate.
    • get

      public float get(int row, int col)
      Element at (row, col).
    • multiply

      public Mat4 multiply(Mat4 o)
      this · o.
    • transpose

      public Mat4 transpose()
      The transpose: the inverse rotation, for a matrix that is purely a rotation.
    • transform

      public Vec4 transform(Vec4 v)
      Transforms a homogeneous vector. No perspective divide: the caller does that.
    • transformPoint

      public Vec3 transformPoint(Vec3 p)
      Transforms a position (w=1) and applies the perspective divide.
    • transformDirection

      public Vec3 transformDirection(Vec3 d)
      Transforms a direction (w=0): no translation, no divide.
    • toMat3

      public Mat3 toMat3()
      Upper-left 3×3.
    • normalMatrix

      public Mat3 normalMatrix()
      Normal matrix: transpose(inverse(upper-left 3×3)), for transforming normals.
    • invert

      public Mat4 invert()
      Throws:
      ArithmeticException - if the matrix is singular. Gauss–Jordan with partial pivot.
    • toArray

      public float[] toArray()
      Column-major copy (length 16), for GPU upload.
    • toArray

      public void toArray(float[] out)
      Column-major copy into out (length ≥ 16), the allocation-free upload path.
    • multiplyInto

      public static void multiplyInto(Mat4 a, Mat4 b, float[] out)
      a · b written column-major into out (length ≥ 16). Allocation-free.
    • normalMatrixInto

      public static void normalMatrixInto(Mat4 model, float[] out)
      The normal matrix of model, transpose(inverse(upper-left 3×3)), written column-major into out (length ≥ 9). Equals the cofactor matrix divided by the determinant, computed directly. Allocation-free.
      Throws:
      ArithmeticException - if the upper-left 3×3 is singular