TGX 1.0.0
A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.
Loading...
Searching...
No Matches
tgx::Mat4< T > Struct Template Reference

Generic 4x4 matrix [specializations fMat4, dMat4]. More...

#include <Mat4.h>

Public Member Functions

 Mat4 ()
 Default constructor. More...
 
constexpr Mat4 (T x1, T y1, T z1, T t1, T x2, T y2, T z2, T t2, T x3, T y3, T z3, T t3, T x4, T y4, T z4, T t4)
 Constructor from explicit values.
 
 Mat4 (const T *mat)
 Constructor from an array (with column major ordering same as M).
 
 Mat4 (const Mat4 &mat)=default
 Copy constructor.
 
Mat4operator= (const Mat4 &mat)=default
 Assignement operator.
 
 operator T* ()
 Implicit conversion to an array T[16].
 
void setZero ()
 Set as the null matrix (all coefficients equal 0).
 
void setIdentity ()
 Set as the identity matrix.
 
void setOrtho (T left, T right, T bottom, T top, T zNear, T zFar)
 Set as an orthographic projection matrix. More...
 
void setFrustum (T left, T right, T bottom, T top, T zNear, T zFar)
 Set as a perspective projection matrix. More...
 
void setPerspective (T fovy, T aspect, T zNear, T zFar)
 Set as a perspective projection matrix. More...
 
void setRotate (T angle, T x, T y, T z)
 Set as a rotation matrix. More...
 
void setRotate (T angle, const Vec3< T > v)
 Set as a rotation matrix. More...
 
void multRotate (T angle, T x, T y, T z)
 Pre-multiply this matrix by a rotation matrix. More...
 
void multRotate (T angle, const Vec3< T > v)
 Pre-multiply this matrix by a rotation matrix. More...
 
void setTranslate (T x, T y, T z)
 Set as a translation matrix. More...
 
void setTranslate (const Vec3< T > v)
 Set as a translation matrix. More...
 
void multTranslate (T x, T y, T z)
 Pre-multiply this matrix by a translation matrix. More...
 
void multTranslate (const Vec3< T > v)
 Pre-multiply this matrix by a translation matrix. More...
 
void setScale (T x, T y, T z)
 Set as a dilatation matrix. More...
 
void setScale (const Vec3< T > v)
 Set as a dilatation matrix. More...
 
void multScale (T x, T y, T z)
 Pre-multiply this matrix by a dilatation matrix. More...
 
void multScale (const Vec3< T > v)
 Pre-multiply this matrix by a dilatation matrix. More...
 
void invertYaxis ()
 invert the y axis, same as multScale({1,-1,1})
 
void setLookAt (T eyeX, T eyeY, T eyeZ, T centerX, T centerY, T centerZ, T upX, T upY, T upZ)
 Set the matrix for a camera looking at a given direction. More...
 
void setLookAt (const Vec3< T > eye, const Vec3< T > center, const Vec3< T > up)
 Set the matrix for a camera looking at a given direction. More...
 
TGX_INLINE Vec4< T > mult (const Vec4< T > V) const
 Matrix-vector multiplication.
 
TGX_INLINE Vec4< T > mult (const Vec3< T > &V, float w) const
 Matrix-vector multiplication.
 
TGX_INLINE Vec4< T > mult0 (const Vec3< T > &V) const
 Matrix-vector multiplication (last component of vector set to w = 0).
 
TGX_INLINE Vec4< T > mult1 (const Vec3< T > &V) const
 Matrix-vector multiplication (last component of vector set to w = 1)
 
void operator*= (T a)
 Scalar multiplication.
 
void print (Stream &outputStream=Serial) const
 Output a representation of the matrix using a given Stream. More...
 

Public Attributes

M [16]
 The matrix array in column major ordering: More...
 

Detailed Description

template<typename T>
struct tgx::Mat4< T >

Generic 4x4 matrix [specializations fMat4, dMat4].

The class encapsulate a 4x4 matrix with element of type T which must be a floating point type (either float or double). Such a matrix is used in 3D grpahics to represent a transformation (translation, rotation, dilatation...).

The matrix is internally represented by an public array M[16] in column major ordering:

+-----------------------------+
| M[0] | M[4] | M[8] | M[12] |
|------+------+-------+-------|
| M[1] | M[5] | M[9] | M[13] |
|------+------+-------+-------|
| M[2] | M[6] | M[10] | M[14] |
|------+------+-------+-------|
| M[3] | M[7] | M[11] | M[15] |
+-----------------------------+
T M[16]
The matrix array in column major ordering:
Definition: Mat4.h:114
See also
Vec2, Vec3, Vec4

Constructor & Destructor Documentation

◆ Mat4()

template<typename T >
tgx::Mat4< T >::Mat4 ( )
inline

Default constructor.

the matrix content is undefined.

Member Function Documentation

◆ setOrtho()

template<typename T >
void tgx::Mat4< T >::setOrtho ( left,
right,
bottom,
top,
zNear,
zFar 
)
inline

Set as an orthographic projection matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml

Parameters
left,rightcoordinates for the left and right vertical clipping planes
bottom,topcoordinates for the bottom and top horizontal clipping planes.
zNear,zFardistances to the nearer and farther depth clipping planes.

◆ setFrustum()

template<typename T >
void tgx::Mat4< T >::setFrustum ( left,
right,
bottom,
top,
zNear,
zFar 
)
inline

Set as a perspective projection matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glFrustum.xml

Parameters
left,rightcoordinates for the left and right vertical clipping planes
bottom,topcoordinates for the bottom and top horizontal clipping planes.
zNear,zFardistances to the nearer and farther depth clipping planes.

◆ setPerspective()

template<typename T >
void tgx::Mat4< T >::setPerspective ( fovy,
aspect,
zNear,
zFar 
)
inline

Set as a perspective projection matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml

Parameters
fovyfield of view angle, in degrees, in the y direction.
aspectaspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
zNeardistance from the viewer to the near clipping plane.
zFardistance from the viewer to the far clipping plane.

◆ setRotate() [1/2]

template<typename T >
void tgx::Mat4< T >::setRotate ( angle,
x,
y,
z 
)
inline

Set as a rotation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml

Parameters
anglerotation angle in degrees.
x,y,zcoordinates of the direction vector for the rotation.

◆ setRotate() [2/2]

template<typename T >
void tgx::Mat4< T >::setRotate ( angle,
const Vec3< T >  v 
)
inline

Set as a rotation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml

Parameters
anglerotation angle in degrees.
vdirection vector for the rotation.

◆ multRotate() [1/2]

template<typename T >
void tgx::Mat4< T >::multRotate ( angle,
x,
y,
z 
)
inline

Pre-multiply this matrix by a rotation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml

Parameters
anglerotation angle in degrees.
x,y,zcoordinates of the direction vector for the rotation.

◆ multRotate() [2/2]

template<typename T >
void tgx::Mat4< T >::multRotate ( angle,
const Vec3< T >  v 
)
inline

Pre-multiply this matrix by a rotation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml

Parameters
anglerotation angle in degrees.
vdirection vector for the rotation.

◆ setTranslate() [1/2]

template<typename T >
void tgx::Mat4< T >::setTranslate ( x,
y,
z 
)
inline

Set as a translation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml

Parameters
x,y,zcoordinates of the translation vector.

◆ setTranslate() [2/2]

template<typename T >
void tgx::Mat4< T >::setTranslate ( const Vec3< T >  v)
inline

Set as a translation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml

Parameters
vthe translation vector.

◆ multTranslate() [1/2]

template<typename T >
void tgx::Mat4< T >::multTranslate ( x,
y,
z 
)
inline

Pre-multiply this matrix by a translation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml

Parameters
x,y,zcoordinates of the translation vector.

◆ multTranslate() [2/2]

template<typename T >
void tgx::Mat4< T >::multTranslate ( const Vec3< T >  v)
inline

Pre-multiply this matrix by a translation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml

Parameters
vthe translation vector.

◆ setScale() [1/2]

template<typename T >
void tgx::Mat4< T >::setScale ( x,
y,
z 
)
inline

Set as a dilatation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glScale.xml

Parameters
x,y,zscale factors along the x, y, and z axes respectively.

◆ setScale() [2/2]

template<typename T >
void tgx::Mat4< T >::setScale ( const Vec3< T >  v)
inline

Set as a dilatation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glScale.xml

Parameters
vvector representing the scale factors along each axis.

◆ multScale() [1/2]

template<typename T >
void tgx::Mat4< T >::multScale ( x,
y,
z 
)
inline

Pre-multiply this matrix by a dilatation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glScale.xml

Parameters
x,y,zscale factors along the x, y, and z axes respectively.

◆ multScale() [2/2]

template<typename T >
void tgx::Mat4< T >::multScale ( const Vec3< T >  v)
inline

Pre-multiply this matrix by a dilatation matrix.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glScale.xml

Parameters
vvector representing the scale factors along each axis.

◆ setLookAt() [1/2]

template<typename T >
void tgx::Mat4< T >::setLookAt ( eyeX,
eyeY,
eyeZ,
centerX,
centerY,
centerZ,
upX,
upY,
upZ 
)
inline

Set the matrix for a camera looking at a given direction.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml

The formula given by khronos is wrong ! The cross-product of two unit vectors is not normalized.

See https://stackoverflow.com/questions/30409318/lookat-matrix-distorts-when-looking-up-or-down

Parameters
eyeX,eyeY,eyeZposition of the eye point.
centerX,centerY,centerZposition of the reference point (the camera points toward this point).
upX,upY,upZdirection of the up vector.

◆ setLookAt() [2/2]

template<typename T >
void tgx::Mat4< T >::setLookAt ( const Vec3< T >  eye,
const Vec3< T >  center,
const Vec3< T >  up 
)
inline

Set the matrix for a camera looking at a given direction.

https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluLookAt.xml

The formula given by khronos is wrong ! The cross-product of two unit vectors is not normalized.

See https://stackoverflow.com/questions/30409318/lookat-matrix-distorts-when-looking-up-or-down

Parameters
eyeeye position.
centerreference point (the camera points toward this point).
upup vector.

◆ print()

template<typename T >
void tgx::Mat4< T >::print ( Stream &  outputStream = Serial) const
inline

Output a representation of the matrix using a given Stream.

Attention
Defined only in the Arduino environment.

Member Data Documentation

◆ M

template<typename T >
T tgx::Mat4< T >::M[16]

The matrix array in column major ordering:

+-----------------------------+
| M[0] | M[4] | M[8] | M[12] |
|------+------+-------+-------|
| M[1] | M[5] | M[9] | M[13] |
|------+------+-------+-------|
| M[2] | M[6] | M[10] | M[14] |
|------+------+-------+-------|
| M[3] | M[7] | M[11] | M[15] |
+-----------------------------+

The documentation for this struct was generated from the following file: