Getting Started
Migrating to UNIGINE 2.0
C++ API Migration
UnigineScript
The Language
Engine Library
Node-Related Classes
GUI-Related Classes
Plugins Library
High-Level Systems
Samples
Usage Examples
C++ API
API Reference
Integration Samples
Usage Examples
C++ Plugins

Math Matrix Functions

See Also

mat4 cubeTransform (int face)

Returns cube viewing matrix for the given cube face.

Arguments

  • int face - Cube face number.

Return value

Cube viewing matrix.

mat4 frustum (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.02.0 * znear / (top - bottom)(top + bottom) / (top - bottom)0.0
0.00.0-(zfar + znear) / (zfar - znear)-2.0 * zfar * znear / (zfar - znear)
0.00.0-1.00.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.

variable inverse4 (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.

Return value

Inversed matrix.

mat4 inverse (mat4 matrix)

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

Arguments

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

Return value

Inversed matrix.

Variable lookAt (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

Variable obliqueProjection (mat4 projection, vec4 plane)

Returns the oblique projection matrix.

Arguments

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

Return value

Oblique projection matrix.

Variable orthoTangent (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));
					

Variable orthoTangent (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.

mat4 ortho (float left, float right, float bottom, float top, float znear, float zfar)

Returns parallel projection matrix:
2.0 / (right - left)0.00.0-(right + left) / (right - left)
0.02.0 / (top - bottom)0.0-(top + bottom) / (top - bottom)
0.00.0-2.0 / (zfar - znear)-(zfar + znear) / (zfar - znear)
0.00.00.01.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.

Variable orthonormalize (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).

mat4 perspective (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.

Variable reflect (variable plane)

Returns the reflection matrix.

Arguments

  • variable plane - Reflection plane.

Return value

Reflection matrix.

mat4 rotateX (float angle)

Returns matrix of rotation at the given angle around X axis:
1.00.00.00.0
0.0cos-sin0.0
0.0sincos0.0
0.00.00.01.0

Arguments

  • float angle - Rotation angle, degrees.

Return value

Rotation matrix.

mat4 rotateY (float angle)

Returns matrix of rotation at the given angle around Y axis:
cos0.0sin0.0
0.01.00.00.0
-sin0.0cos0.0
0.00.00.01.0

Arguments

  • float angle - Rotation angle, degrees.

Return value

Rotation matrix.

mat4 rotateZ (float angle)

Returns matrix of rotation at the given angle around Z axis:
cos-sin0.00.0
sincos0.00.0
0.00.01.00.0
0.00.00.01.0

Arguments

  • float angle - Rotation angle, degrees.

Return value

Rotation matrix.

mat4 rotate (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.

Return value

Rotation matrix.

mat4 rotate (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.

Return value

Rotation matrix.

Variable rotation (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.

Return value

Rotation matrix.

mat4 scale (float x, float y, float z)

Returns scaling matrix:
x0.00.00.0
0.0y0.00.0
0.00.0z0.0
0.00.00.01.0

Arguments

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

Return value

Scaling matrix.

mat4 scale (vec3 vector)

Returns scaling matrix:
scale.x0.00.00.0
0.0scale.y0.00.0
0.00.0scale.z0.0
0.00.00.01.0

Arguments

  • vec3 vector - Scale factor.

Return value

Scaling matrix.

Variable setTo (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

Variable symmetryProjection (mat4 projection)

Returns the symmetric projection matrix.

Arguments

  • mat4 projection - Projection matrix.

Return value

Symmetric projection matrix.

Variable translate (variable v)

Returns translation matrix:
1.00.00.0x
0.01.00.0y
0.00.01.0z
0.00.00.01.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

Variable translate (variable x, variable y, variable z)

Returns translation matrix:
1.00.00.0x
0.01.00.0y
0.00.01.0z
0.00.00.01.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

variable transpose3 (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.

mat4 transpose (mat4 matrix)

Returns transpose matrix of the input matrix:
m00m10m20m30
m01m11m21m31
m02m12m22m32
m03m13m23m33

Arguments

  • mat4 matrix - Argument.

Return value

Transposed matrix.
Last update: 2017-07-03