Class Matrix4f


  • public class Matrix4f
    extends Object
    Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).

    Implementation covers FloatUtil matrix functionality, exposed in an object oriented manner.

    Unlike PMVMatrix, this class only represents one single matrix without a complete GLMatrixFunc implementation.

    For array operations the layout is expected in column-major order matching OpenGL's implementation, illustration:

        Row-Major                       Column-Major (OpenGL):
    
            |  0   1   2  tx |
            |                |
            |  4   5   6  ty |
        M = |                |
            |  8   9  10  tz |
            |                |
            | 12  13  14  15 |
    
               R   C                      R   C
             m[0*4+3] = tx;             m[0+4*3] = tx;
             m[1*4+3] = ty;             m[1+4*3] = ty;
             m[2*4+3] = tz;             m[2+4*3] = tz;
    
              RC (std subscript order)   RC (std subscript order)
             m03 = tx;                  m03 = tx;
             m13 = ty;                  m13 = ty;
             m23 = tz;                  m23 = tz;
    
     

    Implementation utilizes unrolling of small vertices and matrices wherever possible while trying to access memory in a linear fashion for performance reasons, see:

    See Also:
    PMVMatrix, FloatUtil
    • Constructor Detail

      • Matrix4f

        public Matrix4f()
        Creates a new identity matrix.
      • Matrix4f

        public Matrix4f​(Matrix4f src)
        Creates a new matrix copying the values of the given src matrix.
      • Matrix4f

        public Matrix4f​(float[] m)
        Creates a new matrix based on given float[4*4] column major order.
        Parameters:
        m - 4x4 matrix in column-major order
      • Matrix4f

        public Matrix4f​(float[] m,
                        int m_off)
        Creates a new matrix based on given float[4*4] column major order.
        Parameters:
        m - 4x4 matrix in column-major order
        m_off - offset for matrix m
      • Matrix4f

        public Matrix4f​(FloatBuffer m)
        Creates a new matrix based on given FloatBuffer 4x4 column major order.
        Parameters:
        m - 4x4 matrix in column-major order
    • Method Detail

      • set

        public void set​(int i,
                        float v)
        Sets the ith component with float v 0 <= i < 16
      • loadIdentity

        public final Matrix4f loadIdentity()
        Set this matrix to identity.
              Translation matrix (Column Order):
              1 0 0 0
              0 1 0 0
              0 0 1 0
              0 0 0 1
         
        Returns:
        this matrix for chaining
      • load

        public Matrix4f load​(Matrix4f src)
        Load the values of the given matrix b to this matrix.
        Parameters:
        src - the source values
        Returns:
        this matrix for chaining
      • load

        public Matrix4f load​(float[] src)
        Load the values of the given matrix src to this matrix.
        Parameters:
        src - 4x4 matrix float[16] in column-major order
        Returns:
        this matrix for chaining
      • load

        public Matrix4f load​(float[] src,
                             int src_off)
        Load the values of the given matrix src to this matrix.
        Parameters:
        src - 4x4 matrix float[16] in column-major order
        src_off - offset for matrix src
        Returns:
        this matrix for chaining
      • get

        public float get​(int i)
        Gets the ith component, 0 <= i < 16
      • getColumn

        public Vec4f getColumn​(int column,
                               Vec4f v_out)
        Get the named column of the given column-major matrix to v_out.
        Parameters:
        column - named column to copy
        v_out - the column-vector storage
        Returns:
        given result vector v_out for chaining
      • getColumn

        public Vec3f getColumn​(int column,
                               Vec3f v_out)
        Get the named column of the given column-major matrix to v_out.
        Parameters:
        column - named column to copy
        v_out - the column-vector storage
        Returns:
        given result vector v_out for chaining
      • getRow

        public Vec4f getRow​(int row,
                            Vec4f v_out)
        Get the named row of the given column-major matrix to v_out.
        Parameters:
        row - named row to copy
        v_out - the row-vector storage
        Returns:
        given result vector v_out for chaining
      • getRow

        public Vec3f getRow​(int row,
                            Vec3f v_out)
        Get the named row of the given column-major matrix to v_out.
        Parameters:
        row - named row to copy
        v_out - the row-vector storage
        Returns:
        given result vector v_out for chaining
      • get

        public float[] get​(float[] dst,
                           int dst_off)
        Get this matrix into the given float[16] array at dst_off in column major order.
        Parameters:
        dst - float[16] array storage in column major order
        dst_off - offset
        Returns:
        dst for chaining
      • get

        public float[] get​(float[] dst)
        Get this matrix into the given float[16] array in column major order.
        Parameters:
        dst - float[16] array storage in column major order
        Returns:
        dst for chaining
      • determinant

        public float determinant()
        Returns the determinant of this matrix
        Returns:
        the matrix determinant
      • transpose

        public final Matrix4f transpose()
        Transpose this matrix.
        Returns:
        this matrix for chaining
      • transpose

        public final Matrix4f transpose​(Matrix4f src)
        Transpose the given src matrix into this matrix.
        Parameters:
        src - source 4x4 matrix
        Returns:
        this matrix (result) for chaining
      • invert

        public boolean invert()
        Invert this matrix.
        Returns:
        false if this matrix is singular and inversion not possible, otherwise true
      • invert

        public boolean invert​(Matrix4f src)
        Invert the src matrix values into this matrix
        Parameters:
        src - the source matrix, which values are to be inverted
        Returns:
        false if src matrix is singular and inversion not possible, otherwise true
      • mul

        public final Matrix4f mul​(Matrix4f a,
                                  Matrix4f b)
        Multiply matrix: [this] = [a] x [b]
        Parameters:
        a - 4x4 matrix, can't be this matrix
        b - 4x4 matrix, can't be this matrix
        Returns:
        this matrix for chaining
        See Also:
        mul(Matrix4f)
      • mulVec4f

        public final Vec4f mulVec4f​(Vec4f v_in,
                                    Vec4f v_out)
        Parameters:
        v_in - 4-component column-vector, can be v_out for in-place transformation
        v_out - this * v_in
      • mulVec4f

        public final Vec4f mulVec4f​(Vec4f v_inout)
        Parameters:
        v_inout - 4-component column-vector input and output, i.e. in-place transformation
      • mulVec3f

        public final Vec3f mulVec3f​(Vec3f v_in,
                                    Vec3f v_out)
        Affine 3f-vector transformation by 4x4 matrix 4x4 matrix multiplication with 3-component vector, using 1 for for v_in.w() and dropping v_out.w(), which shall be 1.
        Parameters:
        v_in - 3-component column-vector Vec3f, can be v_out for in-place transformation
        v_out - m_in * v_in, 3-component column-vector Vec3f
      • mulVec3f

        public final Vec3f mulVec3f​(Vec3f v_inout)
        Affine 3f-vector transformation by 4x4 matrix 4x4 matrix multiplication with 3-component vector, using 1 for for v_inout.w() and dropping v_inout.w(), which shall be 1.
        Parameters:
        v_inout - 3-component column-vector Vec3f input and output, i.e. in-place transformation
      • setToTranslation

        public final Matrix4f setToTranslation​(float x,
                                               float y,
                                               float z)
        Set this matrix to translation.
              Translation matrix (Column Order):
              1 0 0 0
              0 1 0 0
              0 0 1 0
              x y z 1
         
        Parameters:
        x - x-axis translate
        y - y-axis translate
        z - z-axis translate
        Returns:
        this matrix for chaining
      • setToTranslation

        public final Matrix4f setToTranslation​(Vec3f t)
        Set this matrix to translation.
              Translation matrix (Column Order):
              1 0 0 0
              0 1 0 0
              0 0 1 0
              x y z 1
         
        Parameters:
        t - translate Vec3f
        Returns:
        this matrix for chaining
      • setToScale

        public final Matrix4f setToScale​(float x,
                                         float y,
                                         float z)
        Set this matrix to scale.
              Scale matrix (Any Order):
              x 0 0 0
              0 y 0 0
              0 0 z 0
              0 0 0 1
         
        Parameters:
        x - x-axis scale
        y - y-axis scale
        z - z-axis scale
        Returns:
        this matrix for chaining
      • setToScale

        public final Matrix4f setToScale​(Vec3f s)
        Set this matrix to scale.
              Scale matrix (Any Order):
              x 0 0 0
              0 y 0 0
              0 0 z 0
              0 0 0 1
         
        Parameters:
        s - scale Vec3f
        Returns:
        this matrix for chaining
      • setToRotationAxis

        public final Matrix4f setToRotationAxis​(float ang_rad,
                                                float x,
                                                float y,
                                                float z)
        Set this matrix to rotation from the given axis and angle in radians.
                Rotation matrix (Column Order):
                xx(1-c)+c  xy(1-c)+zs xz(1-c)-ys 0
                xy(1-c)-zs yy(1-c)+c  yz(1-c)+xs 0
                xz(1-c)+ys yz(1-c)-xs zz(1-c)+c  0
                0          0          0          1
         
        Parameters:
        ang_rad - angle in radians
        x - x of rotation axis
        y - y of rotation axis
        z - z of rotation axis
        Returns:
        this matrix for chaining
        See Also:
        Matrix-FAQ Q38
      • setToRotationAxis

        public final Matrix4f setToRotationAxis​(float ang_rad,
                                                Vec3f axis)
        Set this matrix to rotation from the given axis and angle in radians.
                Rotation matrix (Column Order):
                xx(1-c)+c  xy(1-c)+zs xz(1-c)-ys 0
                xy(1-c)-zs yy(1-c)+c  yz(1-c)+xs 0
                xz(1-c)+ys yz(1-c)-xs zz(1-c)+c  0
                0          0          0          1
         
        Parameters:
        ang_rad - angle in radians
        axis - rotation axis
        Returns:
        this matrix for chaining
        See Also:
        Matrix-FAQ Q38
      • setToRotationEuler

        public Matrix4f setToRotationEuler​(float bankX,
                                           float headingY,
                                           float attitudeZ)
        Set this matrix to rotation from the given Euler rotation angles in radians.

        The rotations are applied in the given order:

        • y - heading
        • z - attitude
        • x - bank

        Parameters:
        bankX - the Euler pitch angle in radians. (rotation about the X axis)
        headingY - the Euler yaw angle in radians. (rotation about the Y axis)
        attitudeZ - the Euler roll angle in radians. (rotation about the Z axis)
        Returns:
        this matrix for chaining

        Implementation does not use Quaternion and hence is exposed to Gimbal-Lock, consider using setToRotation(Quaternion).

        See Also:
        Matrix-FAQ Q36, euclideanspace.com-eulerToMatrix, setToRotation(Quaternion)
      • setToRotation

        public final Matrix4f setToRotation​(Quaternion q)
        Set this matrix to rotation using the given Quaternion.

        Implementation Details:

        • makes identity matrix if #magnitudeSquared() is is zero using epsilon
        • The fields [m00 .. m22] define the rotation

        Parameters:
        q - the Quaternion representing the rotation
        Returns:
        this matrix for chaining
        See Also:
        Matrix-FAQ Q54, Quaternion.toMatrix(float[]), #getRotation()
      • setToOrtho

        public Matrix4f setToOrtho​(float left,
                                   float right,
                                   float bottom,
                                   float top,
                                   float zNear,
                                   float zFar)
        Set this matrix to orthogonal projection.
              Ortho matrix (Column Order):
              2/dx  0     0    0
              0     2/dy  0    0
              0     0     2/dz 0
              tx    ty    tz   1
         
        Parameters:
        left -
        right -
        bottom -
        top -
        zNear -
        zFar -
        Returns:
        this matrix for chaining
      • setToFrustum

        public Matrix4f setToFrustum​(float left,
                                     float right,
                                     float bottom,
                                     float top,
                                     float zNear,
                                     float zFar)
                              throws IllegalArgumentException
        Set this matrix to frustum.
              Frustum matrix (Column Order):
              2*zNear/dx   0          0   0
              0            2*zNear/dy 0   0
              A            B          C  -1
              0            0          D   0
         
        Parameters:
        left -
        right -
        bottom -
        top -
        zNear -
        zFar -
        Returns:
        this matrix for chaining
        Throws:
        IllegalArgumentException - if zNear <= 0 or zFar <= zNear or left == right, or bottom == top.
      • updateFrustumPlanes

        public void updateFrustumPlanes​(Frustum frustum)
        Calculate the frustum planes in world coordinates using this premultiplied P*MV (column major order) matrix.

        Frustum plane's normals will point to the inside of the viewing frustum, as required by this class.

        Usually called by Frustum.updateFrustumPlanes(Matrix4f).

      • setToLookAt

        public Matrix4f setToLookAt​(Vec3f eye,
                                    Vec3f center,
                                    Vec3f up,
                                    Matrix4f tmp)
        Set this matrix to the look-at matrix based on given parameters.

        Consist out of two matrix multiplications:

           R = L x T,
           with L for look-at matrix and
                T for eye translation.
        
           Result R can be utilized for projection or modelview multiplication, i.e.
                  M = M x R,
                  with M being the projection or modelview matrix.
         

        Parameters:
        eye - 3 component eye vector
        center - 3 component center vector
        up - 3 component up vector
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
      • setToPick

        public Matrix4f setToPick​(float x,
                                  float y,
                                  float deltaX,
                                  float deltaY,
                                  Recti viewport,
                                  Matrix4f mat4Tmp)
        Set this matrix to the pick matrix based on given parameters.

        Traditional gluPickMatrix implementation.

        Consist out of two matrix multiplications:

           R = T x S,
           with T for viewport translation matrix and
                S for viewport scale matrix.
        
           Result R can be utilized for projection multiplication, i.e.
                  P = P x R,
                  with P being the projection matrix.
         

        To effectively use the generated pick matrix for picking, call setToPick(..) and multiply a custom perspective matrix by this pick matrix. Then you may load the result onto the perspective matrix stack.

        Parameters:
        x - the center x-component of a picking region in window coordinates
        y - the center y-component of a picking region in window coordinates
        deltaX - the width of the picking region in window coordinates.
        deltaY - the height of the picking region in window coordinates.
        viewport - Rect4i viewport
        mat4Tmp - temp storage
        Returns:
        this matrix for chaining or null if either delta value is <= zero.
      • rotate

        public final Matrix4f rotate​(float ang_rad,
                                     float x,
                                     float y,
                                     float z,
                                     Matrix4f tmp)
        Rotate this matrix about give axis and angle in radians, i.e. multiply by axis-rotation matrix.
        Parameters:
        angrad - angle in radians
        x - x of rotation axis
        y - y of rotation axis
        z - z of rotation axis
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
        See Also:
        Matrix-FAQ Q38
      • rotate

        public final Matrix4f rotate​(float ang_rad,
                                     Vec3f axis,
                                     Matrix4f tmp)
        Rotate this matrix about give axis and angle in radians, i.e. multiply by axis-rotation matrix.
        Parameters:
        angrad - angle in radians
        axis - rotation axis
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
        See Also:
        Matrix-FAQ Q38
      • translate

        public final Matrix4f translate​(float x,
                                        float y,
                                        float z,
                                        Matrix4f tmp)
        Translate this matrix, i.e. multiply by translation matrix.
        Parameters:
        x - x translation
        y - y translation
        z - z translation
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
      • translate

        public final Matrix4f translate​(Vec3f t,
                                        Matrix4f tmp)
        Translate this matrix, i.e. multiply by translation matrix.
        Parameters:
        t - translation Vec3f
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
      • scale

        public final Matrix4f scale​(float x,
                                    float y,
                                    float z,
                                    Matrix4f tmp)
        Scale this matrix, i.e. multiply by scale matrix.
        Parameters:
        x - x scale
        y - y scale
        z - z scale
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
      • scale

        public final Matrix4f scale​(float s,
                                    Matrix4f tmp)
        Scale this matrix, i.e. multiply by scale matrix.
        Parameters:
        s - scale for x-, y- and z-axis
        tmp - temporary Matrix4f used for multiplication
        Returns:
        this matrix for chaining
      • push

        public final void push()
        Push the matrix to it's stack, while preserving this matrix values.
        See Also:
        pop()
      • pop

        public final void pop()
        Pop the current matrix from it's stack, replacing this matrix values.
        See Also:
        push()
      • mapObjToWin

        public static boolean mapObjToWin​(Vec3f obj,
                                          Matrix4f mMv,
                                          Matrix4f mP,
                                          Recti viewport,
                                          Vec3f winPos)
        Map object coordinates to window coordinates.

        Traditional gluProject implementation.

        Parameters:
        obj - object position, 3 component vector
        mMv - modelview matrix
        mP - projection matrix
        viewport - Rect4i viewport
        winPos - 3 component window coordinate, the result
        Returns:
        true if successful, otherwise false (z is 1)
      • mapObjToWin

        public static boolean mapObjToWin​(Vec3f obj,
                                          Matrix4f mPMv,
                                          Recti viewport,
                                          Vec3f winPos)
        Map object coordinates to window coordinates.

        Traditional gluProject implementation.

        Parameters:
        obj - object position, 3 component vector
        mPMv - [projection] x [modelview] matrix, i.e. P x Mv
        viewport - Rect4i viewport
        winPos - 3 component window coordinate, the result
        Returns:
        true if successful, otherwise false (z is 1)
      • mapWinToObj

        public static boolean mapWinToObj​(float winx,
                                          float winy,
                                          float winz,
                                          Matrix4f mMv,
                                          Matrix4f mP,
                                          Recti viewport,
                                          Vec3f objPos,
                                          Matrix4f mat4Tmp)
        Map window coordinates to object coordinates.

        Traditional gluUnProject implementation.

        Parameters:
        winx -
        winy -
        winz -
        mMv - 4x4 modelview matrix
        mP - 4x4 projection matrix
        viewport - Rect4i viewport
        objPos - 3 component object coordinate, the result
        mat4Tmp - 16 component matrix for temp storage
        Returns:
        true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z)
      • mapWinToObj

        public static boolean mapWinToObj​(float winx,
                                          float winy,
                                          float winz,
                                          Matrix4f invPMv,
                                          Recti viewport,
                                          Vec3f objPos)
        Map window coordinates to object coordinates.

        Traditional gluUnProject implementation.

        Parameters:
        winx -
        winy -
        winz -
        invPMv - inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv), if null method returns false
        viewport - Rect4i viewport
        objPos - 3 component object coordinate, the result
        Returns:
        true if successful, otherwise false (null invert matrix, or becomes infinity due to zero z)
      • mapWinToObj

        public static boolean mapWinToObj​(float winx,
                                          float winy,
                                          float winz1,
                                          float winz2,
                                          Matrix4f invPMv,
                                          Recti viewport,
                                          Vec3f objPos1,
                                          Vec3f objPos2)
        Map two window coordinates to two object coordinates, distinguished by their z component.

        Traditional gluUnProject implementation.

        Parameters:
        winx -
        winy -
        winz1 -
        winz2 -
        invPMv - inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv), if null method returns false
        viewport - Rect4i viewport vector
        objPos1 - 3 component object coordinate, the result
        Returns:
        true if successful, otherwise false (null invert matrix, or becomes infinity due to zero z)
      • mapWinToObj4

        public static boolean mapWinToObj4​(float winx,
                                           float winy,
                                           float winz,
                                           float clipw,
                                           Matrix4f mMv,
                                           Matrix4f mP,
                                           Recti viewport,
                                           float near,
                                           float far,
                                           Vec4f objPos,
                                           Matrix4f mat4Tmp)
        Map window coordinates to object coordinates.

        Traditional gluUnProject4 implementation.

        Parameters:
        winx -
        winy -
        winz -
        clipw -
        mMv - 4x4 modelview matrix
        mP - 4x4 projection matrix
        viewport - Rect4i viewport vector
        near -
        far -
        obj_pos - 4 component object coordinate, the result
        mat4Tmp - 16 component matrix for temp storage
        Returns:
        true if successful, otherwise false (failed to invert matrix, or becomes infinity due to zero z)
      • mapWinToObj4

        public static boolean mapWinToObj4​(float winx,
                                           float winy,
                                           float winz,
                                           float clipw,
                                           Matrix4f invPMv,
                                           Recti viewport,
                                           float near,
                                           float far,
                                           Vec4f objPos)
        Map window coordinates to object coordinates.

        Traditional gluUnProject4 implementation.

        Parameters:
        winx -
        winy -
        winz -
        clipw -
        invPMv - inverse [projection] x [modelview] matrix, i.e. Inv(P x Mv), if null method returns false
        viewport - Rect4i viewport vector
        near -
        far -
        obj_pos - 4 component object coordinate, the result
        Returns:
        true if successful, otherwise false (null invert matrix, or becomes infinity due to zero z)
      • toString

        public StringBuilder toString​(StringBuilder sb,
                                      String rowPrefix,
                                      String f)
        Parameters:
        sb - optional passed StringBuilder instance to be used
        rowPrefix - optional prefix for each row
        f - the format string of one floating point, i.e. "%10.5f", see Formatter
        Returns:
        matrix string representation