# Math Matrix Functions

## mat4cubeTransform (int face)

Returns cube viewing matrix for the given cube face.

### Arguments

• int face - Cube face number.

### Return value

Cube viewing matrix.

## mat4frustum (float left, float right, float bottom, float top, float znear, float zfar)

Returns perspective projection matrix:
 2.0 * znear / (right - left) 0.0 (right + left) / (right - left) 0.0 0.0 2.0 * znear / (top - bottom) (top + bottom) / (top - bottom) 0.0 0.0 0.0 -(zfar + znear) / (zfar - znear) -2.0 * zfar * znear / (zfar - znear) 0.0 0.0 -1.0 0.0

Coordinates of top, left, right, bottom are set relatively to center point of the znear plane. There are two different points (A and B) on the picture above. Since the top, left, right, bottom are coordinates relatively to the center point of the znear plane, coordinates of the A point should be A(left, bottom, znear). Coordinates of the B point are B(k * left, k * bottom, zfar), where k = zfar/znear.

### Arguments

• float left - Left coordinate of the near clipping plane relatively to the center.
• float right - Right coordinate of the near clipping plane relatively to the center.
• float bottom - Bottom coordinate of the near clipping plane relatively to the center.
• float top - Top coordinate of the near clipping plane relatively to the center.
• float znear - Distance to the near depth clipping plane.
• float zfar - Distance to the farther depth clipping plane.

### Return value

Perspective projection matrix.

## variableinverse4 (mat4 matrix)

Inverses a matrix that consists of a 3×4 sub-matrix (upper left) and a translation vector. The last row of the matrix is ignored. Compared to the inverse() function, this one is a bit faster and, which is more important, more stable. A matrix suitable for such inversion looks like this:
 m00 m10 m20 m30 m01 m11 m21 m31 m02 m12 m22 m32 0 0 0 1

### Arguments

• mat4 matrix - Matrix, which will be inversed.

Inversed matrix.

## mat4inverse (mat4 matrix)

Returns inverse of a matrix. Supports mat4 and dmat4 arguments.

### Arguments

• mat4 matrix - Matrix (either mat4 or dmat4).

Inversed matrix.

## VariablelookAt (variable position, variable direction, vec3 up)

Returns the viewing matrix from eye point to the direction point with up vector.

### Arguments

• variable position - Position of the eye point (vec3 or dvec3).
• variable direction - Position of the reference point (vec3 or dvec3).
• vec3 up - Direction of the up vector.

### Return value

Viewing matrix:
• mat4, if the first input argument is vec3
• dmat4 if the first argument is dvec3

## VariableobliqueProjection (mat4 projection, vec4 plane)

Returns the oblique projection matrix.

### Arguments

• mat4 projection - Projection matrix.
• vec4 plane - Clipping plane.

### Return value

Oblique projection matrix.

## VariableorthoTangent (vec3 tangent, vec3 binormal, vec3 normal)

Calculates the tangent space basis basing on the 3 given ortho basis vectors.
Notice
The w component will contain the sign of the binormal.

### Arguments

• vec3 tangent - Tangent vector.
• vec3 binormal - Binormal vector.
• vec3 normal - Normal vector.

### Return value

Tangent space basis.

### Examples

The following example demonstrates how to convert the mesh tangent vector to the normal and binormal vectors. The mesh tangent vector is calculated via the orthoTangent() function.

Source code (UnigineScript)
```vec3 t = vec3(1.0f,0.0f,0.0f); // the tangent basis: tangent vector
vec3 b = vec3(0.0f,1.0f,0.0f); // binormal vector
vec3 n = vec3(0.0f,0.0f,1.0f); // normal vector
vec4 tangent = orthoTangent(t,b,n); // this is the mesh compressed tangent vector

quat compact_tangent_quat = quat(tangent); // convert vec4 to quat

log.message("b: %s\n",typeinfo(compact_tangent_quat.binormal)); // get the col1 (binormal) with w normalization
log.message("n: %s\n\n",typeinfo(compact_tangent_quat.normal)); // get the col2 (normal) with w normalization

// manually normalize the w component of the quaternion
// and convert the quaternion into the matrix
mat4 compact_tangent_mat = mat4(normalize3(compact_tangent_quat)); // when the w component of the quaternion is normalized, the sign of binormal will be lost for the matrix

// get the col1 and col2 of the matrix
log.message("b: %s\n",typeinfo(compact_tangent_mat.binormal * tangent.w)); // you should multiply the binormal by the tangent w component to restore the sign
log.message("n: %s\n",typeinfo(compact_tangent_mat.normal));
```

## VariableorthoTangent (vec4 tangent, vec3 normal)

Calculates the tangent space basis basing on the given ortho basis vectors.
Notice
The w component will contain the sign of the binormal.

### Arguments

• vec4 tangent - Tangent vector: the first 3 components are coordinates of the tangent vector, and the fourth w component is the sign of the binormal.
• vec3 normal - Normal vector.

### Return value

Tangent space basis.

## mat4ortho (float left, float right, float bottom, float top, float znear, float zfar)

Returns parallel projection matrix:
 2.0 / (right - left) 0.0 0.0 -(right + left) / (right - left) 0.0 2.0 / (top - bottom) 0.0 -(top + bottom) / (top - bottom) 0.0 0.0 -2.0 / (zfar - znear) -(zfar + znear) / (zfar - znear) 0.0 0.0 0.0 1.0

### Arguments

• float left - Left vertical clipping plane.
• float right - Right vertical clipping plane.
• float bottom - Bottom horizontal clipping plane.
• float top - Top horizontal clipping plane.
• float znear - Nearest depth clipping plane.
• float zfar - Farther depth clipping plane.

### Return value

Parallel projection matrix.

## Variableorthonormalize (variable v)

Orthonormalizes a matrix.

### Arguments

• variable v - Matrix (either mat4 or dmat4) to be orthonormalized.

### Return value

Orthonormal matrix (either mat4 or dmat4 depending on the input argument type).

## mat4perspective (float fov, float aspect, float znear, float zfar)

Returns perspective projection matrix.

### Arguments

• float fov - Field of view angle.
• float aspect - Aspect ratio. The aspect ratio is the ratio of width to height.
• float znear - Nearest depth clipping plane.
• float zfar - Farther depth clipping plane.

### Return value

Perspective projection matrix.

## Variablereflect (variable plane)

Returns the reflection matrix.

### Arguments

• variable plane - Reflection plane.

### Return value

Reflection matrix.

## mat4rotateX (float angle)

Returns matrix of rotation at the given angle around X axis:
 1 0.0 0.0 0 0 cos -sin 0 0 sin cos 0 0 0.0 0.0 1

### Arguments

• float angle - Rotation angle, degrees.

Rotation matrix.

## mat4rotateY (float angle)

Returns matrix of rotation at the given angle around Y axis:
 cos 0 sin 0 0.0 1 0.0 0 -sin 0 cos 0 0.0 0 0.0 1

### Arguments

• float angle - Rotation angle, degrees.

Rotation matrix.

## mat4rotateZ (float angle)

Returns matrix of rotation at the given angle around Z axis:
 cos -sin 0 0 sin cos 0 0 0.0 0.0 1 0 0.0 0.0 0 1

### Arguments

• float angle - Rotation angle, degrees.

Rotation matrix.

## mat4rotate (float x, float y, float z, float angle)

Returns matrix of rotation at the given angle around the axis.

### Arguments

• float x - X component of rotation axis.
• float y - Y component of rotation axis.
• float z - Z component of rotation axis.
• float angle - Rotation angle, degrees.

Rotation matrix.

## mat4rotate (vec3 axis, float angle)

Returns matrix of rotation at the given angle around the axis.

### Arguments

• vec3 axis - Rotation axis.
• float angle - Rotation angle, degrees.

Rotation matrix.

## Variablerotation (variable v)

Returns rotation matrix of the input matrix, supports quat, mat4 and dmat4 arguments.

### Arguments

• variable v - Argument, can be of quat, mat4 or dmat4 type.

Rotation matrix.

## mat4scale (float x, float y, float z)

Returns scaling matrix:
 x 0.0 0.0 0 0.0 y 0.0 0 0.0 0.0 z 0 0.0 0.0 0.0 1

### Arguments

• float x - X component of scaling vector.
• float y - Y component of scaling vector.
• float z - Z component of scaling vector.

Scaling matrix.

## mat4scale (vec3 vector)

Returns scaling matrix:
 scale.x 0.0 0.0 0 0.0 scale.y 0.0 0 0.0 0.0 scale.z 0 0.0 0.0 0.0 1

### Arguments

• vec3 vector - Scale factor.

Scaling matrix.

## VariablesetTo (variable position, variable direction, vec3 up)

Returns transformation matrix required to place an object into the given point.

### Arguments

• variable position - Position of the target point (vec3 or dvec3).
• variable direction - Direction (vec3 or dvec3).
• vec3 up - Direction of the up vector.

### Return value

Transformation matrix:
• mat4, if the first input argument is vec3
• dmat4 if the first argument is dvec3

## VariablesymmetryProjection (mat4 projection)

Returns the symmetric projection matrix.

### Arguments

• mat4 projection - Projection matrix.

### Return value

Symmetric projection matrix.

## Variabletranslate (variable v)

Returns translation matrix:
 1 0 0 x 0 1 0 y 0 0 1 z 0 0 0 1.0

### Arguments

• variable v - Translation vector (either vec3 or dvec3).

### Return value

Translation matrix:
• mat4, if the input argument is vec3
• dmat4 if the argument is dvec3

## Variabletranslate (variable x, variable y, variable z)

Returns translation matrix:
 1 0 0 x 0 1 0 y 0 0 1 z 0 0 0 1.0

### Arguments

• variable x - X component of translation vector (int, float or double).
• variable y - Y component of translation vector (int, float or double).
• variable z - Z component of translation vector (int, float or double).

### Return value

Translation matrix:
• mat4, if the first input argument is int or float
• dmat4, if the first argument is double

## variabletranspose3 (mat4 matrix)

Transposes the upper left 3×3 sub-matrix of a matrix.

### Arguments

• mat4 matrix - Matrix, part of which will be transposed.

### Return value

matrix, in which the upper left 3×3 sub-matrix is transposed.

## mat4transpose (mat4 matrix)

Returns transpose matrix of the input matrix:
 m00 m10 m20 m30 m01 m11 m21 m31 m02 m12 m22 m32 m03 m13 m23 m33

### Arguments

• mat4 matrix - Argument.

### Return value

Transposed matrix.
Last update: 2017-07-03