SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
SLMat4< T > Class Template Reference

4x4 matrix template class More...

#include <SLMat4.h>

Public Member Functions

 SLMat4 ()
 Sets identity matrix. More...
 
 SLMat4 (const SLMat4 &A)
 Sets mat by other SLMat4. More...
 
 SLMat4 (const SLMat3f &A)
 Sets mat by other SLMat3. More...
 
 SLMat4 (const T *M)
 Sets mat by array. More...
 
 SLMat4 (T M0, T M4, T M8, T M12, T M1, T M5, T M9, T M13, T M2, T M6, T M10, T M14, T M3, T M7, T M11, T M15)
 
 SLMat4 (T tx, T ty, T tz)
 Sets translate matrix. More...
 
 SLMat4 (T degAng, T axis_x, T axis_y, T axis_z)
 Sets rotation matrix. More...
 
 SLMat4 (T scale_xyz)
 Sets scaling matrix. More...
 
 SLMat4 (const SLVec3< T > &translationVec)
 Sets translate matrix. More...
 
 SLMat4 (const SLVec3< T > &fromUnitVec, const SLVec3< T > &toUnitVec)
 Sets rotation matrix. More...
 
 SLMat4 (const SLVec3< T > &translation, const SLMat3< T > &rotation, const SLVec3< T > &scale)
 Set matrix by translation, rotation & scale. More...
 
void setMatrix (const SLMat4 &A)
 Set matrix by other 4x4 matrix. More...
 
void setMatrix (const SLMat3f &A)
 Set matrix by other 3x3 matrix. More...
 
void setMatrix (const SLMat4 *A)
 Set matrix by other matrix pointer. More...
 
void setMatrix (const T *M)
 Set matrix by float[16] array. More...
 
void setMatrix (T M0, T M4, T M8, T M12, T M1, T M5, T M9, T M13, T M2, T M6, T M10, T M14, T M3, T M7, T M11, T M15)
 Set matrix by components. More...
 
void setMatrix (const SLVec3< T > &translation, const SLMat3< T > &rotation, const SLVec3< T > &scale)
 Set matrix by translation, rotation & scale. More...
 
void setMatrix (const int16_t i, const SLfloat value)
 
void setRotation (const SLMat3< T > &rotation)
 Set 3x3 submatrix describing the rotational part. More...
 
void setTranslation (const SLVec3< T > &translation)
 Set vector as submatrix describing the translational part. More...
 
void m (int i, T val)
 
const T * m () const
 
m (int i) const
 
SLMat3< T > mat3 () const
 
SLMat4< T > & operator= (const SLMat4 &A)
 assignment operator More...
 
SLMat4< T > operator* (const SLMat4 &A) const
 matrix-matrix multiplication More...
 
SLMat4< T > & operator*= (const SLMat4 &A)
 matrix-matrix multiplication More...
 
SLMat4< T > operator+ (const SLMat4 &A) const
 matrix-matrix addition More...
 
SLVec3< T > operator* (const SLVec3< T > &v) const
 SLVec3 mult w. persp div. More...
 
SLVec4< T > operator* (const SLVec4< T > &v) const
 SLVec4 mult. More...
 
SLMat4< T > operator* (T a) const
 scalar mult More...
 
SLMat4< T > & operator*= (T a)
 scalar mult More...
 
SLMat4< T > operator/ (T a) const
 scalar division More...
 
SLMat4< T > & operator/= (T a)
 scalar division More...
 
T & operator() (int row, int col)
 
const T & operator() (int row, int col) const
 
SLbool isEqual (const SLMat4 &A, SLfloat epsilon=0.01f)
 Returns true if one element of the matrix differs more than epsilon. More...
 
void multiply (const SLMat4 &A)
 
SLVec3< T > multVec (SLVec3< T > v) const
 
SLVec4< T > multVec (SLVec4< T > v) const
 
void add (const SLMat4 &A)
 
void translate (T tx, T ty, T tz=0)
 
void translate (const SLVec2< T > &t)
 
void translate (const SLVec3< T > &t)
 
void rotate (T degAng, T axisx, T axisy, T axisz)
 
void rotate (T degAng, const SLVec3< T > &axis)
 
void rotate (const SLVec3< T > &fromUnitVec, const SLVec3< T > &toUnitVec)
 
void scale (T sxyz)
 
void scale (T sx, T sy, T sz)
 
void scale (const SLVec3< T > &sxyz)
 
void frustum (T l, T r, T b, T t, T n, T f)
 Defines a view frustum projection matrix equivalent to glFrustum. More...
 
void perspective (T fov, T aspect, T n, T f)
 Defines a perspective projection matrix with a field of view angle. More...
 
void perspectiveCenteredPP (T w, T h, T fx, T fy, T cx, T cy, T n, T f)
 Defines a projection matrix for a calibrated camera, the principle point has to be centered (from intrinsics matrix) More...
 
void ortho (T l, T r, T b, T t, T n, T f)
 Defines a orthographic projection matrix with a field of view angle. More...
 
void viewport (T x, T y, T ww, T wh, T n=0.0f, T f=1.0f)
 Defines the viewport matrix. More...
 
void lookAt (T EyeX, T EyeY, T EyeZ, T AtX=0, T AtY=0, T AtZ=0, T UpX=0, T UpY=0, T UpZ=0)
 Defines the a view matrix as the corresponding gluLookAt function. More...
 
void lookAt (const SLVec3< T > &Eye, const SLVec3< T > &At=SLVec3< T >::ZERO, const SLVec3< T > &Up=SLVec3< T >::ZERO)
 Defines the a view matrix as the corresponding gluLookAt function. More...
 
void lookAt (SLVec3< T > *eye, SLVec3< T > *at, SLVec3< T > *up, SLVec3< T > *right) const
 Reads out of the matrix the look at parameters. More...
 
void lightAt (T PosX, T PosY, T PosZ, T AtX=0, T AtY=0, T AtZ=0, T UpX=0, T UpY=0, T UpZ=0)
 Defines the a model matrix for light positioning. More...
 
void lightAt (const SLVec3< T > &pos, const SLVec3< T > &At=SLVec3< T >::ZERO, const SLVec3< T > &Up=SLVec3< T >::ZERO)
 Defines the a model matrix for light positioning. More...
 
void posAtUp (T PosX, T PosY, T PosZ, T dirAtX=0, T dirAtY=0, T dirAtZ=0, T dirUpX=0, T dirUpY=0, T dirUpZ=0)
 Same as lightAt. More...
 
void posAtUp (const SLVec3< T > &pos, const SLVec3< T > &dirAt=SLVec3< T >::ZERO, const SLVec3< T > &dirUp=SLVec3< T >::ZERO)
 Same as lightAt. More...
 
SLVec3< T > translation () const
 
SLVec3< T > axisX () const
 
SLVec3< T > axisY () const
 
SLVec3< T > axisZ () const
 
void translation (T tx, T ty, T tz, SLbool keepLinear=true)
 Sets the translation with or without overwriting the linear submatrix. More...
 
void translation (const SLVec3< T > &t, SLbool keepLinear=true)
 
void rotation (T degAng, const SLVec3< T > &axis, SLbool keepTranslation=true)
 Sets the rotation with or without overwriting the translation. More...
 
void rotation (T degAng, T axisx, T axisy, T axisz, SLbool keepTranslation=true)
 
void rotation (const SLVec3< T > &fromUnitVec, const SLVec3< T > &toUnitVec)
 
void scaling (T sxyz, SLbool keepTrans=true)
 Sets the scaling with or without overwriting the translation. More...
 
void scaling (const SLVec3< T > &sxyz, SLbool keepTrans=true)
 
void scaling (T sx, T sy, T sz, SLbool keepTranslation=true)
 
void fromEulerAnglesZXZ (double angle1RAD, double angle2RAD, double angle3RAD, bool keepTranslation=true)
 
void fromEulerAnglesXYZ (double angle1RAD, double angle2RAD, double angle3RAD, bool keepTranslation=true)
 
void toEulerAnglesZYX (T &zRotRAD, T &yRotRAD, T &xRotRAD)
 
void toEulerAnglesXYZ (T &xRotRAD, T &yRotRAD, T &zRotRAD)
 
SLVec3< T > toEulerAngles (int order)
 
void identity ()
 Sets the identity matrix. More...
 
void transpose ()
 Sets the transposed matrix by swaping around the main diagonal. More...
 
SLMat4< T > transposed ()
 Returns the transposed of the matrix and leaves the itself unchanged. More...
 
void invert ()
 Inverts the matrix. More...
 
SLMat4< T > inverted () const
 Computes the inverse of a 4x4 non-singular matrix. More...
 
SLMat3< T > inverseTransposed ()
 
trace () const
 
void compose (SLVec3f trans, SLVec3f rotEulerRAD, SLVec3f scale)
 
void decompose (SLVec3f &trans, SLVec4f &rotQuat, SLVec3f &scale)
 
void decompose (SLVec3f &trans, SLMat3f &rotMat, SLVec3f &scale)
 
void decompose (SLVec3f &trans, SLVec3f &rotEulerRAD, SLVec3f &scale)
 
void print (const SLchar *str=nullptr) const
 
SLstring toString () const
 

Static Public Member Functions

static void swap (T &a, T &b)
 

Private Attributes

_m [16]
 The 16 elements of the matrix. More...
 

Detailed Description

template<class T>
class SLMat4< T >

4x4 matrix template class

Implements a 4 by 4 matrix template class. An array of 16 floats or double is used instead of a 2D array [4][4] to be compliant with OpenGL. The index layout is as follows:

     | 0  4  8 12 |
     | 1  5  9 13 |
 M = | 2  6 10 14 |
     | 3  7 11 15 |

Vectors are interpreted as column vectors when applying matrix multiplications. This means a vector is as a single column, 4-row matrix. The result is that the transformations implemented by the matrices happens right-to-left e.g. if vector V is to be transformed by M1 then M2 then M3, the calculation would be M3 * M2 * M1 * V. The order that matrices are concatenated is vital since matrix multiplication is not commutative, i.e. you can get a different result if you concatenate in the wrong order. The use of column vectors and right-to-left ordering is the standard in most mathematical texts, and is the same as used in OpenGL. It is, however, the opposite of Direct3D, which has inexplicably chosen to differ from the accepted standard and uses row vectors and left-to-right matrix multiplication.

Definition at line 51 of file SLMat4.h.

Constructor & Destructor Documentation

◆ SLMat4() [1/11]

template<class T >
SLMat4< T >::SLMat4

Sets identity matrix.

Definition at line 262 of file SLMat4.h.

263 { identity();
264 }
void identity()
Sets the identity matrix.
Definition: SLMat4.h:1333

◆ SLMat4() [2/11]

template<class T >
SLMat4< T >::SLMat4 ( const SLMat4< T > &  A)

Sets mat by other SLMat4.

Definition at line 267 of file SLMat4.h.

268 { setMatrix(A);
269 }
void setMatrix(const SLMat4 &A)
Set matrix by other 4x4 matrix.
Definition: SLMat4.h:335

◆ SLMat4() [3/11]

template<class T >
SLMat4< T >::SLMat4 ( const SLMat3f A)

Sets mat by other SLMat3.

Definition at line 272 of file SLMat4.h.

273 { setMatrix(A);
274 }

◆ SLMat4() [4/11]

template<class T >
SLMat4< T >::SLMat4 ( const T *  M)

Sets mat by array.

Definition at line 289 of file SLMat4.h.

290 {
291  setMatrix(M);
292 }

◆ SLMat4() [5/11]

template<class T >
SLMat4< T >::SLMat4 ( M0,
M4,
M8,
M12,
M1,
M5,
M9,
M13,
M2,
M6,
M10,
M14,
M3,
M7,
M11,
M15 
)

Definition at line 277 of file SLMat4.h.

281 {
282  setMatrix(M0, M4, M8, M12,
283  M1, M5, M9, M13,
284  M2, M6, M10,M14,
285  M3, M7, M11,M15);
286 }

◆ SLMat4() [6/11]

template<class T >
SLMat4< T >::SLMat4 ( tx,
ty,
tz 
)

Sets translate matrix.

Definition at line 295 of file SLMat4.h.

296 {
297  translation(tx, ty, tz, false);
298 }
SLVec3< T > translation() const
Definition: SLMat4.h:184

◆ SLMat4() [7/11]

template<class T >
SLMat4< T >::SLMat4 ( degAng,
axis_x,
axis_y,
axis_z 
)
explicit

Sets rotation matrix.

Definition at line 307 of file SLMat4.h.

308 {
309  rotation(degAng, ax, ay, az, false);
310 }
void rotation(T degAng, const SLVec3< T > &axis, SLbool keepTranslation=true)
Sets the rotation with or without overwriting the translation.
Definition: SLMat4.h:978

◆ SLMat4() [8/11]

template<class T >
SLMat4< T >::SLMat4 ( scale_xyz)
explicit

Sets scaling matrix.

Definition at line 313 of file SLMat4.h.

314 {
315  scaling(scale_xyz);
316 }
void scaling(T sxyz, SLbool keepTrans=true)
Sets the scaling with or without overwriting the translation.
Definition: SLMat4.h:1146

◆ SLMat4() [9/11]

template<class T >
SLMat4< T >::SLMat4 ( const SLVec3< T > &  translationVec)
explicit

Sets translate matrix.

Definition at line 301 of file SLMat4.h.

302 {
303  translation(translationVec.x, translationVec.y, translationVec.z, false);
304 }
T y
Definition: SLVec3.h:43
T x
Definition: SLVec3.h:43
T z
Definition: SLVec3.h:43

◆ SLMat4() [10/11]

template<class T >
SLMat4< T >::SLMat4 ( const SLVec3< T > &  fromUnitVec,
const SLVec3< T > &  toUnitVec 
)

Sets rotation matrix.

Definition at line 319 of file SLMat4.h.

320 {
321  rotation(fromUnitVec, toUnitVec);
322 }

◆ SLMat4() [11/11]

template<class T >
SLMat4< T >::SLMat4 ( const SLVec3< T > &  translation,
const SLMat3< T > &  rotation,
const SLVec3< T > &  scale 
)

Set matrix by translation, rotation & scale.

Definition at line 325 of file SLMat4.h.

328 {
330 }
void scale(T sxyz)
Definition: SLMat4.h:667

Member Function Documentation

◆ add()

template<class T >
void SLMat4< T >::add ( const SLMat4< T > &  A)

Adds the matrix to an other matrix A.

Definition at line 566 of file SLMat4.h.

567 {
568  for (SLint i = 0; i < 16; ++i)
569  _m[i] = _m[i] + A._m[i];
570 }
int SLint
Definition: SL.h:170
T _m[16]
The 16 elements of the matrix.
Definition: SLMat4.h:255

◆ axisX()

template<class T >
SLVec3<T> SLMat4< T >::axisX ( ) const
inline

Definition at line 185 of file SLMat4.h.

185 {return SLVec3<T>(_m[ 0], _m[ 1], _m[ 2]);}
3D vector template class for standard 3D vector algebra.
Definition: SLVec3.h:40

◆ axisY()

template<class T >
SLVec3<T> SLMat4< T >::axisY ( ) const
inline

Definition at line 186 of file SLMat4.h.

186 {return SLVec3<T>(_m[ 4], _m[ 5], _m[ 6]);}

◆ axisZ()

template<class T >
SLVec3<T> SLMat4< T >::axisZ ( ) const
inline

Definition at line 187 of file SLMat4.h.

187 {return SLVec3<T>(_m[ 8], _m[ 9], _m[10]);}

◆ compose()

template<class T >
void SLMat4< T >::compose ( SLVec3f  trans,
SLVec3f  rotEulerRAD,
SLVec3f  scale 
)

Composes the matrix from a translation vector, a rotation Euler angle vector and the scaling factors.

Definition at line 1468 of file SLMat4.h.

1471 {
1472  fromEulerAnglesXYZ(rotEulerRAD.x, rotEulerRAD.y, rotEulerRAD.z);
1473  scale(scaleFactors);
1474  translate(trans);
1475 }
void fromEulerAnglesXYZ(double angle1RAD, double angle2RAD, double angle3RAD, bool keepTranslation=true)
Definition: SLMat4.h:1214
void translate(T tx, T ty, T tz=0)
Definition: SLMat4.h:601

◆ decompose() [1/3]

template<class T >
void SLMat4< T >::decompose ( SLVec3f trans,
SLMat3f rotMat,
SLVec3f scale 
)

Decomposes the matrix into a translation vector, a 3x3 rotation matrix and the scaling factors using polar decomposition introduced by Ken Shoemake. See the paper in lib-SLExternal/Shoemake/polar-decomp.pdf

Definition at line 1506 of file SLMat4.h.

1507 {
1508  SLVec4f rotQuat;
1509  decompose(trans, rotQuat, scale);
1510 
1511  // Convert quaternion to 3x3 matrix
1512  SLfloat qx = rotQuat.x;
1513  SLfloat qy = rotQuat.y;
1514  SLfloat qz = rotQuat.z;
1515  SLfloat qw = rotQuat.w;
1516 
1517  SLfloat x2 = qx * 2.0f;
1518  SLfloat y2 = qy * 2.0f;
1519  SLfloat z2 = qz * 2.0f;
1520 
1521  SLfloat wx2 = qw * x2; SLfloat wy2 = qw * y2; SLfloat wz2 = qw * z2;
1522  SLfloat xx2 = qx * x2; SLfloat xy2 = qx * y2; SLfloat xz2 = qx * z2;
1523  SLfloat yy2 = qy * y2; SLfloat yz2 = qy * z2; SLfloat zz2 = qz * z2;
1524 
1525  rotMat.setMatrix(1.0f-(yy2 + zz2), xy2 - wz2 , xz2 + wy2,
1526  xy2 + wz2 , 1.0f-(xx2 + zz2), yz2 - wx2,
1527  xz2 - wy2 , yz2 + wx2 , 1.0f-(xx2 + yy2));
1528 }
float SLfloat
Definition: SL.h:173
void setMatrix(const SLMat3 &A)
Definition: SLMat3.h:299
void decompose(SLVec3f &trans, SLVec4f &rotQuat, SLVec3f &scale)
Definition: SLMat4.h:1483
T w
Definition: SLVec4.h:32
T z
Definition: SLVec4.h:32
T y
Definition: SLVec4.h:32
T x
Definition: SLVec4.h:32

◆ decompose() [2/3]

template<class T >
void SLMat4< T >::decompose ( SLVec3f trans,
SLVec3f rotEulerRAD,
SLVec3f scale 
)

Decomposes the matrix into a translation vector, a rotation Euler angle vector and the scaling factors using polar decomposition introduced by Ken Shoemake. See the paper in lib-SLExternal/Shoemake/polar-decomp.pdf

Definition at line 1536 of file SLMat4.h.

1537 {
1538  SLMat3f rotMat;
1539  decompose(trans, rotMat, scale);
1540 
1541  SLfloat rotAngleXRAD, rotAngleYRAD, rotAngleZRAD;
1542  rotMat.toEulerAnglesXYZ(rotAngleXRAD, rotAngleYRAD, rotAngleZRAD);
1543 
1544  rotEulerRAD.set(rotAngleXRAD, rotAngleYRAD, rotAngleZRAD);
1545 }
void toEulerAnglesXYZ(T &xRotRAD, T &yRotRAD, T &zRotRAD)
Definition: SLMat3.h:608
void set(const T X, const T Y, const T Z)
Definition: SLVec3.h:59

◆ decompose() [3/3]

template<class T >
void SLMat4< T >::decompose ( SLVec3f trans,
SLVec4f rotQuat,
SLVec3f scale 
)

Decomposes the matrix into a translation vector, a rotation quaternion and the scaling factors using polar decomposition introduced by Ken Shoemake. See the paper in lib-SLExternal/Shoemake/polar-decomp.pdf

Definition at line 1483 of file SLMat4.h.

1484 {
1485  // Input matrix A
1486  HMatrix A;
1487  A[0][0]=_m[0]; A[0][1]=_m[4]; A[0][2]=_m[ 8]; A[0][3]=_m[12];
1488  A[1][0]=_m[1]; A[1][1]=_m[5]; A[1][2]=_m[ 9]; A[1][3]=_m[13];
1489  A[2][0]=_m[2]; A[2][1]=_m[6]; A[2][2]=_m[10]; A[2][3]=_m[14];
1490  A[3][0]=_m[3]; A[3][1]=_m[7]; A[3][2]=_m[11]; A[3][3]=_m[15];
1491 
1492  AffineParts parts;
1493  decomp_affine(A, &parts);
1494 
1495  trans.set(parts.t.x, parts.t.y, parts.t.z);
1496  scale.set(parts.k.x, parts.k.y, parts.k.z);
1497  rotQuat.set(parts.q.x, parts.q.y, parts.q.z, parts.q.w);
1498 }
void set(const T X, const T Y, const T Z, const T W=1)
Definition: SLVec4.h:49

◆ fromEulerAnglesXYZ()

template<class T >
void SLMat4< T >::fromEulerAnglesXYZ ( double  angle1RAD,
double  angle2RAD,
double  angle3RAD,
bool  keepTranslation = true 
)

Sets the linear 3x3 submatrix as a rotation matrix from the 3 euler angles in radians around the z-axis, y-axis & x-axis. By default the translation components are set to 0. See: http://en.wikipedia.org/wiki/Euler_angles

Definition at line 1214 of file SLMat4.h.

1218 {
1219  double s1 = sin(angle1RAD), c1 = cos(angle1RAD);
1220  double s2 = sin(angle2RAD), c2 = cos(angle2RAD);
1221  double s3 = sin(angle3RAD), c3 = cos(angle3RAD);
1222 
1223  _m[0]=(T) (c2*c3); _m[4]=(T)-(c2*s3); _m[8] =(T) s2;
1224  _m[1]=(T) (s1*s2*c3) + (c1*s3); _m[5]=(T)-(s1*s2*s3) + (c1*c3); _m[9] =(T)-(s1*c2);
1225  _m[2]=(T)-(c1*s2*c3) + (s1*s3); _m[6]=(T) (c1*s2*s3) + (s1*c3); _m[10]=(T) (c1*c2);
1226 
1227  _m[3]=_m[7]=_m[11]=0; _m[15]=1;
1228 
1229  if (!keepTrans)
1230  { _m[12] = _m[13] = _m[14] = 0;
1231  }
1232 }

◆ fromEulerAnglesZXZ()

template<class T >
void SLMat4< T >::fromEulerAnglesZXZ ( double  angle1RAD,
double  angle2RAD,
double  angle3RAD,
bool  keepTranslation = true 
)

Sets the linear 3x3 submatrix as a rotation matrix from the 3 euler angles in radians around the z-axis, x-axis & z-axis. By default the translation components are set to 0. See: http://en.wikipedia.org/wiki/Euler_angles

Definition at line 1187 of file SLMat4.h.

1191 {
1192  double s1 = sin(angle1RAD), c1 = cos(angle1RAD);
1193  double s2 = sin(angle2RAD), c2 = cos(angle2RAD);
1194  double s3 = sin(angle3RAD), c3 = cos(angle3RAD);
1195 
1196  _m[0]=(T)( c1*c3 - s1*c2*s3); _m[4]=(T)( s1*c3 + c1*c2*s3); _m[8] =(T)( s2*s3);
1197  _m[1]=(T)(-c1*s3 - s1*c2*c3); _m[5]=(T)( c1*c2*c3 - s1*s3); _m[9] =(T)( s2*c3);
1198  _m[2]=(T)( s1*s2); _m[6]=(T)(-c1*s2); _m[10]=(T)( c2);
1199 
1200  _m[3]=_m[7]=_m[11]=0; _m[15]=1;
1201 
1202  if (!keepTrans)
1203  { _m[12] = _m[13] = _m[14] = 0;
1204  }
1205 }

◆ frustum()

template<class T >
void SLMat4< T >::frustum ( l,
r,
b,
t,
n,
f 
)

Defines a view frustum projection matrix equivalent to glFrustum.

Defines a view frustum projection matrix equivalent to OpenGL's glFrustum.

The view frustum is the truncated view pyramid of a central projection that is defined with the folowing parameters:

Parameters
lDistance from the center of projection (COP) to the left border on the near clipping plane.
rDistance from the COP to the right border on the near clipping plane.
bDistance from the COP to the bottom border on the near clipping plane.
tDistance from the COP to the top border on the near clipping plane.
nDistance from the eye to near clipping plane of the view frustum.
fDistance from the eye to far clipping plane of the view frustum.

Definition at line 855 of file SLMat4.h.

857 {
858  _m[0]=2*n/(r-l); _m[4]=0; _m[8] = (r+l)/(r-l); _m[12]=0;
859  _m[1]=0; _m[5]=2*n/(t-b); _m[9] = (t+b)/(t-b); _m[13]=0;
860  _m[2]=0; _m[6]=0; _m[10]=-(f+n)/(f-n); _m[14]=-2*f*n/(f-n);
861  _m[3]=0; _m[7]=0; _m[11]=-1; _m[15]=0;
862 }

◆ identity()

template<class T >
void SLMat4< T >::identity

Sets the identity matrix.

Definition at line 1333 of file SLMat4.h.

1334 {
1335  _m[0]=_m[5]=_m[10]=_m[15]=1;
1336  _m[1]=_m[2]=_m[3]=_m[4]=_m[6]=_m[7]=_m[8]=_m[9]=_m[11]=_m[12]=_m[13]=_m[14]=0;
1337 }

◆ inverseTransposed()

template<class T >
SLMat3< T > SLMat4< T >::inverseTransposed

Computes the inverse transposed matrix of the upper left 3x3 matrix for the transformation of vertex normals.

Definition at line 1441 of file SLMat4.h.

1442 {
1443  SLMat3<T> i(_m[0], _m[4], _m[8],
1444  _m[1], _m[5], _m[9],
1445  _m[2], _m[6], _m[10]);
1446  i.invert();
1447  i.transpose();
1448  return i;
1449 }

◆ invert()

template<class T >
void SLMat4< T >::invert

Inverts the matrix.

Definition at line 1364 of file SLMat4.h.

1365 {
1366  setMatrix(inverted());
1367 }
SLMat4< T > inverted() const
Computes the inverse of a 4x4 non-singular matrix.
Definition: SLMat4.h:1371

◆ inverted()

template<class T >
SLMat4< T > SLMat4< T >::inverted

Computes the inverse of a 4x4 non-singular matrix.

Definition at line 1371 of file SLMat4.h.

1372 {
1373  SLMat4<T> i;
1374 
1375  // Code from Mesa-2.2\src\glu\project.c
1376  T det, d12, d13, d23, d24, d34, d41;
1377 
1378  // Inverse = adjoint / det. (See linear algebra texts.)
1379  // pre-compute 2x2 dets for last two rows when computing
1380  // cof_actors of first two rows.
1381  d12 = ( _m[2]*_m[ 7] - _m[ 3]*_m[ 6]);
1382  d13 = ( _m[2]*_m[11] - _m[ 3]*_m[10]);
1383  d23 = ( _m[6]*_m[11] - _m[ 7]*_m[10]);
1384  d24 = ( _m[6]*_m[15] - _m[ 7]*_m[14]);
1385  d34 = (_m[10]*_m[15] - _m[11]*_m[14]);
1386  d41 = (_m[14]*_m[ 3] - _m[15]*_m[ 2]);
1387 
1388  i._m[0] = (_m[5]*d34 - _m[9]*d24 + _m[13]*d23);
1389  i._m[1] = -(_m[1]*d34 + _m[9]*d41 + _m[13]*d13);
1390  i._m[2] = (_m[1]*d24 + _m[5]*d41 + _m[13]*d12);
1391  i._m[3] = -(_m[1]*d23 - _m[5]*d13 + _m[ 9]*d12);
1392 
1393  // Compute determinant as early as possible using these cof_actors.
1394  det = _m[0]*i._m[0] + _m[4]*i._m[1] + _m[8]*i._m[2] + _m[12]*i._m[3];
1395 
1396  // Run singularity test.
1397  if (fabs(det) < FLT_EPSILON)
1398  {
1399  SL_LOG("4x4-Matrix is singular. Inversion impossible.");
1400  exit(-1);
1401  }
1402  else
1403  {
1404  T invDet = 1 / det;
1405  // Compute rest of inverse.
1406  i._m[0] *= invDet;
1407  i._m[1] *= invDet;
1408  i._m[2] *= invDet;
1409  i._m[3] *= invDet;
1410 
1411  i._m[4] = -(_m[4]*d34 - _m[8]*d24 + _m[12]*d23)*invDet;
1412  i._m[5] = (_m[0]*d34 + _m[8]*d41 + _m[12]*d13)*invDet;
1413  i._m[6] = -(_m[0]*d24 + _m[4]*d41 + _m[12]*d12)*invDet;
1414  i._m[7] = (_m[0]*d23 - _m[4]*d13 + _m[8]*d12)*invDet;
1415 
1416  // Pre-compute 2x2 dets for first two rows when computing
1417  // cofactors of last two rows.
1418  d12 = _m[ 0]*_m[ 5] - _m[ 1]*_m[ 4];
1419  d13 = _m[ 0]*_m[ 9] - _m[ 1]*_m[ 8];
1420  d23 = _m[ 4]*_m[ 9] - _m[ 5]*_m[ 8];
1421  d24 = _m[ 4]*_m[13] - _m[ 5]*_m[12];
1422  d34 = _m[ 8]*_m[13] - _m[ 9]*_m[12];
1423  d41 = _m[12]*_m[ 1] - _m[13]*_m[ 0];
1424 
1425  i._m[ 8] = (_m[7]*d34 - _m[11]*d24 + _m[15]*d23)*invDet;
1426  i._m[ 9] = -(_m[3]*d34 + _m[11]*d41 + _m[15]*d13)*invDet;
1427  i._m[10] = (_m[3]*d24 + _m[ 7]*d41 + _m[15]*d12)*invDet;
1428  i._m[11] = -(_m[3]*d23 - _m[ 7]*d13 + _m[11]*d12)*invDet;
1429  i._m[12] = -(_m[6]*d34 - _m[10]*d24 + _m[14]*d23)*invDet;
1430  i._m[13] = (_m[2]*d34 + _m[10]*d41 + _m[14]*d13)*invDet;
1431  i._m[14] = -(_m[2]*d24 + _m[ 6]*d41 + _m[14]*d12)*invDet;
1432  i._m[15] = (_m[2]*d23 - _m[ 6]*d13 + _m[10]*d12)*invDet;
1433  }
1434  return i;
1435 }
#define SL_LOG(...)
Definition: SL.h:233
4x4 matrix template class
Definition: SLMat4.h:52

◆ isEqual()

template<class T >
SLbool SLMat4< T >::isEqual ( const SLMat4< T > &  A,
SLfloat  epsilon = 0.01f 
)

Returns true if one element of the matrix differs more than epsilon.

Definition at line 521 of file SLMat4.h.

522 {
523  for (SLuint i = 0; i < 16; ++i)
524  {
525  if (std::abs(_m[i] - A._m[i]) > epsilon)
526  return false;
527  }
528  return true;
529 }
unsigned int SLuint
Definition: SL.h:171
T abs(T a)
Definition: Utils.h:249

◆ lightAt() [1/2]

template<class T >
void SLMat4< T >::lightAt ( const SLVec3< T > &  pos,
const SLVec3< T > &  At = SLVec3<T>::ZERO,
const SLVec3< T > &  Up = SLVec3<T>::ZERO 
)

Defines the a model matrix for light positioning.

Defines the a model matrix for positioning a light source.

Utility method for defining the transformation matrix for a spot light. There is no equivalent function for this purpose in OpenGL. /param pos Vector to the position of the light source. /param At Vector to a target point where a spot light shines to.

Definition at line 795 of file SLMat4.h.

798 {
799  SLVec3<T> VX, VY, VZ;
800  SLMat3<T> xz(0.0, 0.0, 1.0, // matrix that transforms VZ into a
801  0.0, 0.0, 0.0, // vector that is perpendicular to YZ and
802  -1.0, 0.0, 0.0); // lies in the x-z plane
803 
804  VZ = pos-At; VZ.normalize();
805 
806  if (Up==SLVec3<T>::ZERO)
807  { VX = xz*VZ; VX.normalize();
808  VY = VZ^VX; VY.normalize();
809  } else
810  { VX = Up^VZ; VX.normalize();
811  VY = VZ^VX; VY.normalize();
812  }
813 
814  setMatrix(VX.x, VY.x, VZ.x, pos.x,
815  VX.y, VY.y, VZ.y, pos.y,
816  VX.z, VY.z, VZ.z, pos.z,
817  0.0, 0.0, 0.0, 1.0);
818 }
SLVec3 & normalize()
Definition: SLVec3.h:124

◆ lightAt() [2/2]

template<class T >
void SLMat4< T >::lightAt ( PosX,
PosY,
PosZ,
AtX = 0,
AtY = 0,
AtZ = 0,
UpX = 0,
UpY = 0,
UpZ = 0 
)

Defines the a model matrix for light positioning.

Defines the a model matrix for positioning a light at pos and shining at At

Definition at line 778 of file SLMat4.h.

781 {
782  lightAt(SLVec3<T>(PosX,PosY,PosZ),
783  SLVec3<T>( AtX, AtY, AtZ),
784  SLVec3<T>( UpX, UpY, UpZ));
785 }
void lightAt(T PosX, T PosY, T PosZ, T AtX=0, T AtY=0, T AtZ=0, T UpX=0, T UpY=0, T UpZ=0)
Defines the a model matrix for light positioning.
Definition: SLMat4.h:778

◆ lookAt() [1/3]

template<class T >
void SLMat4< T >::lookAt ( const SLVec3< T > &  Eye,
const SLVec3< T > &  At = SLVec3<T>::ZERO,
const SLVec3< T > &  Up = SLVec3<T>::ZERO 
)

Defines the a view matrix as the corresponding gluLookAt function.

Defines the view matrix with an eye position, a look at point and an up vector.

This method is equivalent to the OpenGL function gluLookAt.

Parameters
EyeVector to the position of the eye (view point).
AtVector to the target point.
UpVector that points from the viewpoint upwards. If Up is a zero vector a default up vector is calculated with a default look-right vector (VZ) that lies in the x-z plane.

Definition at line 727 of file SLMat4.h.

730 {
731  SLVec3<T> VX, VY, VZ, VT;
732  SLMat3<T> xz(0.0, 0.0, 1.0, // matrix that transforms YZ into a
733  0.0, 0.0, 0.0, // vector that is perpendicular to YZ and
734  -1.0, 0.0, 0.0); // lies in the x-z plane
735 
736  VZ = Eye-At;
737  VZ.normalize();
738 
739  if (Up==SLVec3<T>::ZERO || Up==VZ)
740  { VX.set(xz*VZ);
741  VX.normalize();
742  } else
743  { VX.cross(Up, VZ);
744  VX.normalize();
745  }
746  VY.cross(VZ, VX); VY.normalize();
747  VT = -Eye;
748 
749  setMatrix(VX.x, VX.y, VX.z, VX*VT,
750  VY.x, VY.y, VY.z, VY*VT,
751  VZ.x, VZ.y, VZ.z, VZ*VT,
752  0.0, 0.0, 0.0, 1.0);
753 }
void cross(const SLVec3 &a, const SLVec3 &b)
Definition: SLVec3.h:118

◆ lookAt() [2/3]

template<class T >
void SLMat4< T >::lookAt ( SLVec3< T > *  eye,
SLVec3< T > *  at,
SLVec3< T > *  up,
SLVec3< T > *  ri 
) const

Reads out of the matrix the look at parameters.

This method retrieves the eye position the look at, up & right vector out of the view matrix. Attention: The look-at is normalized vector, not a point.

Definition at line 760 of file SLMat4.h.

764 {
765  SLMat4<T> invRot(_m); // get the current view matrix
766  invRot.translation(0,0,0); // remove the translation
767  invRot.transpose(); // transpose it to get inverse rot.
768  eye->set(invRot.multVec(-translation())); // setMatrix eye
769  ri->set( _m[0], _m[4], _m[8]); // normalized look right vector
770  up->set( _m[1], _m[5], _m[9]); // normalized look up vector
771  at->set(-_m[2],-_m[6],-_m[10]); // normalized look at vector
772 }

◆ lookAt() [3/3]

template<class T >
void SLMat4< T >::lookAt ( EyeX,
EyeY,
EyeZ,
AtX = 0,
AtY = 0,
AtZ = 0,
UpX = 0,
UpY = 0,
UpZ = 0 
)

Defines the a view matrix as the corresponding gluLookAt function.

Defines the view matrix with an eye position, a look at point and an up vector.

This method is equivalent to the OpenGL function gluLookAt.

Definition at line 708 of file SLMat4.h.

711 {
712  lookAt(SLVec3<T>(EyeX,EyeY,EyeZ),
713  SLVec3<T>( AtX, AtY, AtZ),
714  SLVec3<T>( UpX, UpY, UpZ));
715 }
void lookAt(T EyeX, T EyeY, T EyeZ, T AtX=0, T AtY=0, T AtZ=0, T UpX=0, T UpY=0, T UpZ=0)
Defines the a view matrix as the corresponding gluLookAt function.
Definition: SLMat4.h:708

◆ m() [1/3]

template<class T >
const T* SLMat4< T >::m ( ) const
inline

Definition at line 96 of file SLMat4.h.

96 {return _m;}

◆ m() [2/3]

template<class T >
T SLMat4< T >::m ( int  i) const
inline

Definition at line 97 of file SLMat4.h.

97 {assert(i>=0 && i<16); return _m[i];}

◆ m() [3/3]

template<class T >
void SLMat4< T >::m ( int  i,
val 
)
inline

Definition at line 93 of file SLMat4.h.

93 {assert(i>=0 && i<16); _m[i] = val;}

◆ mat3()

template<class T >
SLMat3<T> SLMat4< T >::mat3 ( ) const
inline

Definition at line 98 of file SLMat4.h.

98  {SLMat3<T> m3;
99  m3.setMatrix(_m[0], _m[4], _m[ 8],
100  _m[1], _m[5], _m[ 9],
101  _m[2], _m[6], _m[10]);
102  return m3;}

◆ multiply()

template<class T >
void SLMat4< T >::multiply ( const SLMat4< T > &  A)

Multiplies the matrix with a another matrix. Corresponds to the OpenGL function glMultMatrix*.

Definition at line 536 of file SLMat4.h.

537 {
538  setMatrix(_m[0]*A._m[ 0] + _m[4]*A._m[ 1] + _m[8] *A._m[ 2] + _m[12]*A._m[ 3], //row 1
539  _m[0]*A._m[ 4] + _m[4]*A._m[ 5] + _m[8] *A._m[ 6] + _m[12]*A._m[ 7],
540  _m[0]*A._m[ 8] + _m[4]*A._m[ 9] + _m[8] *A._m[10] + _m[12]*A._m[11],
541  _m[0]*A._m[12] + _m[4]*A._m[13] + _m[8] *A._m[14] + _m[12]*A._m[15],
542  _m[1]*A._m[ 0] + _m[5]*A._m[ 1] + _m[9] *A._m[ 2] + _m[13]*A._m[ 3], //row 2
543  _m[1]*A._m[ 4] + _m[5]*A._m[ 5] + _m[9] *A._m[ 6] + _m[13]*A._m[ 7],
544  _m[1]*A._m[ 8] + _m[5]*A._m[ 9] + _m[9] *A._m[10] + _m[13]*A._m[11],
545  _m[1]*A._m[12] + _m[5]*A._m[13] + _m[9] *A._m[14] + _m[13]*A._m[15],
546  _m[2]*A._m[ 0] + _m[6]*A._m[ 1] + _m[10]*A._m[ 2] + _m[14]*A._m[ 3], //row 3
547  _m[2]*A._m[ 4] + _m[6]*A._m[ 5] + _m[10]*A._m[ 6] + _m[14]*A._m[ 7],
548  _m[2]*A._m[ 8] + _m[6]*A._m[ 9] + _m[10]*A._m[10] + _m[14]*A._m[11],
549  _m[2]*A._m[12] + _m[6]*A._m[13] + _m[10]*A._m[14] + _m[14]*A._m[15],
550  _m[3]*A._m[ 0] + _m[7]*A._m[ 1] + _m[11]*A._m[ 2] + _m[15]*A._m[ 3], //row 4
551  _m[3]*A._m[ 4] + _m[7]*A._m[ 5] + _m[11]*A._m[ 6] + _m[15]*A._m[ 7],
552  _m[3]*A._m[ 8] + _m[7]*A._m[ 9] + _m[11]*A._m[10] + _m[15]*A._m[11],
553  _m[3]*A._m[12] + _m[7]*A._m[13] + _m[11]*A._m[14] + _m[15]*A._m[15]);
554 
555  // | 0 4 8 12 | | 0 4 8 12 |
556  // | 1 5 9 13 | | 1 5 9 13 |
557  // M = | 2 6 10 14 | x | 2 6 10 14 |
558  // | 3 7 11 15 | | 3 7 11 15 |
559 }

◆ multVec() [1/2]

template<class T >
SLVec3< T > SLMat4< T >::multVec ( SLVec3< T >  v) const

Matrix - 3D vector multiplication with perspective division

Definition at line 576 of file SLMat4.h.

577 {
578  T W = 1 / (_m[3]*v.x + _m[7]*v.y + _m[11]*v.z + _m[15]);
579  return SLVec3<T>((_m[0]*v.x + _m[4]*v.y + _m[ 8]*v.z + _m[12]) * W,
580  (_m[1]*v.x + _m[5]*v.y + _m[ 9]*v.z + _m[13]) * W,
581  (_m[2]*v.x + _m[6]*v.y + _m[10]*v.z + _m[14]) * W);
582 }

◆ multVec() [2/2]

template<class T >
SLVec4< T > SLMat4< T >::multVec ( SLVec4< T >  v) const

Matrix - 4D vector multiplication

Definition at line 588 of file SLMat4.h.

589 {
590  return SLVec4<T>(_m[0]*v.x + _m[4]*v.y + _m[ 8]*v.z + _m[12]*v.w,
591  _m[1]*v.x + _m[5]*v.y + _m[ 9]*v.z + _m[13]*v.w,
592  _m[2]*v.x + _m[6]*v.y + _m[10]*v.z + _m[14]*v.w,
593  _m[3]*v.x + _m[7]*v.y + _m[11]*v.z + _m[15]*v.w);
594 }

◆ operator()() [1/2]

template<class T >
T& SLMat4< T >::operator() ( int  row,
int  col 
)
inline

Definition at line 114 of file SLMat4.h.

114 {return _m[4*col+row];}

◆ operator()() [2/2]

template<class T >
const T& SLMat4< T >::operator() ( int  row,
int  col 
) const
inline

Definition at line 115 of file SLMat4.h.

115 {return _m[4*col+row];}

◆ operator*() [1/4]

template<class T >
SLMat4< T > SLMat4< T >::operator* ( const SLMat4< T > &  A) const

matrix-matrix multiplication

Matrix - matrix multiplication

Definition at line 419 of file SLMat4.h.

420 {
421  SLMat4<T> newM((const T*)this);
422  newM.multiply(A);
423  return newM;
424 }

◆ operator*() [2/4]

template<class T >
SLVec3< T > SLMat4< T >::operator* ( const SLVec3< T > &  v) const

SLVec3 mult w. persp div.

Matrix - 3D vector multiplication with perspective division

Definition at line 452 of file SLMat4.h.

453 {
454  T W = 1 / (_m[3]*v.x + _m[7]*v.y + _m[11]*v.z + _m[15]);
455  return SLVec3<T>((_m[0]*v.x + _m[4]*v.y + _m[ 8]*v.z + _m[12]) * W,
456  (_m[1]*v.x + _m[5]*v.y + _m[ 9]*v.z + _m[13]) * W,
457  (_m[2]*v.x + _m[6]*v.y + _m[10]*v.z + _m[14]) * W);
458 }

◆ operator*() [3/4]

template<class T >
SLVec4< T > SLMat4< T >::operator* ( const SLVec4< T > &  v) const

SLVec4 mult.

Matrix - 4D vector multiplication

Definition at line 464 of file SLMat4.h.

465 {
466  return SLVec4<T>(_m[0]*v.x + _m[4]*v.y + _m[ 8]*v.z + _m[12]*v.w,
467  _m[1]*v.x + _m[5]*v.y + _m[ 9]*v.z + _m[13]*v.w,
468  _m[2]*v.x + _m[6]*v.y + _m[10]*v.z + _m[14]*v.w,
469  _m[3]*v.x + _m[7]*v.y + _m[11]*v.z + _m[15]*v.w);
470 }

◆ operator*() [4/4]

template<class T >
SLMat4< T > SLMat4< T >::operator* ( a) const

scalar mult

Scalar multiplication.

Definition at line 476 of file SLMat4.h.

477 {
478  return SLMat4<T>(_m[ 0]*a, _m[ 1]*a, _m[ 2]*a, _m[ 3]*a,
479  _m[ 4]*a, _m[ 5]*a, _m[ 6]*a, _m[ 7]*a,
480  _m[ 8]*a, _m[ 9]*a, _m[10]*a, _m[11]*a,
481  _m[12]*a, _m[13]*a, _m[14]*a, _m[15]*a);
482 }

◆ operator*=() [1/2]

template<class T >
SLMat4< T > & SLMat4< T >::operator*= ( const SLMat4< T > &  A)

matrix-matrix multiplication

Definition at line 430 of file SLMat4.h.

431 {
432  multiply(A);
433  return *this;
434 }
void multiply(const SLMat4 &A)
Definition: SLMat4.h:536

◆ operator*=() [2/2]

template<class T >
SLMat4< T > & SLMat4< T >::operator*= ( a)

scalar mult

Scalar multiplication.

Definition at line 488 of file SLMat4.h.

489 {
490  for (auto & i : _m) i *= a;
491  return *this;
492 }

◆ operator+()

template<class T >
SLMat4< T > SLMat4< T >::operator+ ( const SLMat4< T > &  A) const

matrix-matrix addition

Definition at line 440 of file SLMat4.h.

441 {
442  SLMat4<T> newM((const T*)this);
443  newM.add(A);
444  return newM;
445 }

◆ operator/()

template<class T >
SLMat4< T > SLMat4< T >::operator/ ( a) const

scalar division

Scalar division.

Definition at line 498 of file SLMat4.h.

499 {
500  T invA = 1 / a;
501  SLMat4<T> newM(_m[ 0]*invA, _m[ 1]*invA, _m[ 2]*invA, _m[ 3]*invA,
502  _m[ 4]*invA, _m[ 5]*invA, _m[ 6]*invA, _m[ 7]*invA,
503  _m[ 8]*invA, _m[ 9]*invA, _m[10]*invA, _m[11]*invA,
504  _m[12]*invA, _m[13]*invA, _m[14]*invA, _m[15]*invA);
505  return newM;
506 }

◆ operator/=()

template<class T >
SLMat4< T > & SLMat4< T >::operator/= ( a)

scalar division

Scalar division.

Definition at line 512 of file SLMat4.h.

513 {
514  T invA = 1 / a;
515  for (auto & i : _m) i *= invA;
516  return *this;
517 }

◆ operator=()

template<class T >
SLMat4< T > & SLMat4< T >::operator= ( const SLMat4< T > &  A)

assignment operator

Matrix assignment with instance

Definition at line 406 of file SLMat4.h.

407 {
408  _m[0]=A._m[0]; _m[4]=A._m[4]; _m[ 8]=A._m[ 8]; _m[12]=A._m[12];
409  _m[1]=A._m[1]; _m[5]=A._m[5]; _m[ 9]=A._m[ 9]; _m[13]=A._m[13];
410  _m[2]=A._m[2]; _m[6]=A._m[6]; _m[10]=A._m[10]; _m[14]=A._m[14];
411  _m[3]=A._m[3]; _m[7]=A._m[7]; _m[11]=A._m[11]; _m[15]=A._m[15];
412  return *this;
413 }

◆ ortho()

template<class T >
void SLMat4< T >::ortho ( l,
r,
b,
t,
n,
f 
)

Defines a orthographic projection matrix with a field of view angle.

Defines a ortographic projection matrix equivalent to OpenGL's glOrtho.

Parameters
lDistance from the center of projection (COP) to the left border on the near clipping plane.
rDistance from the COP to the right border on the near clipping plane.
bDistance from the COP to the bottom border on the near clipping plane.
tDistance from the COP to the top border on the near clipping plane.
nDistance from the eye to near clipping plane of the view frustum.
fDistance from the eye to far clipping plane of the view frustum.

Definition at line 911 of file SLMat4.h.

913 {
914  _m[0]=2/(r-l); _m[4]=0; _m[8]=0; _m[12]=-(r+l)/(r-l);
915  _m[1]=0; _m[5]=2/(t-b); _m[9]=0; _m[13]=-(t+b)/(t-b);
916  _m[2]=0; _m[6]=0; _m[10]=-2/(f-n); _m[14]=-(f+n)/(f-n);
917  _m[3]=0; _m[7]=0; _m[11]=0; _m[15]=1;
918 }

◆ perspective()

template<class T >
void SLMat4< T >::perspective ( fov,
aspect,
n,
f 
)

Defines a perspective projection matrix with a field of view angle.

Defines a view frustum projection matrix for a perspective projection.

This method is equivalent to the OpenGL function gluPerspective except that instead of the window aspect the window width and height have to be passed.

Parameters
fovVertical field of view angle (zoom angle)
aspectaspect ratio of of the viewport = width / height
nDistance from the eye to near clipping plane of the view frustum.
fDistance from the eye to far clipping plane of the view frustum.

Definition at line 874 of file SLMat4.h.

876 {
877  T t = (T)tan(fov * DEG2RAD * 0.5)*n;
878  T b = -t;
879  T r = t*aspect;
880  T l = -r;
881  frustum(l,r,b,t,n,f);
882 }
void frustum(T l, T r, T b, T t, T n, T f)
Defines a view frustum projection matrix equivalent to glFrustum.
Definition: SLMat4.h:855
static const float DEG2RAD
Definition: Utils.h:239

◆ perspectiveCenteredPP()

template<class T >
void SLMat4< T >::perspectiveCenteredPP ( w,
h,
fx,
fy,
cx,
cy,
n,
f 
)

Defines a projection matrix for a calibrated camera, the principle point has to be centered (from intrinsics matrix)

Defines a projection matrix for a calibrated camera (from intrinsics matrix) Attention: The principle point has to be centered

This should give an exact result. http://kgeorge.github.io/2014/03/08/calculating-opengl-perspective-matrix-from-opencv-intrinsic-matrix (see also https://stackoverflow.com/questions/22064084/how-to-create-perspective-projection-matrix-given-focal-points-and-camera-princ but the other solutions did not work as well)

Definition at line 891 of file SLMat4.h.

893 {
894  _m[0]=fx/cx; _m[4]=0; _m[8] = 0; _m[12]=0;
895  _m[1]=0; _m[5]=fy/cy; _m[9] = 0; _m[13]=0;
896  _m[2]=0; _m[6]=0; _m[10]=-(f+n)/(f-n); _m[14]=(-2*f*n)/(f-n);
897  _m[3]=0; _m[7]=0; _m[11]=-1; _m[15]=0;
898 }

◆ posAtUp() [1/2]

template<class T >
void SLMat4< T >::posAtUp ( const SLVec3< T > &  pos,
const SLVec3< T > &  dirAt = SLVec3<T>::ZERO,
const SLVec3< T > &  dirUp = SLVec3<T>::ZERO 
)

Same as lightAt.

Definition at line 834 of file SLMat4.h.

837 {
838  lightAt(pos, dirAt, dirUp);
839 }

◆ posAtUp() [2/2]

template<class T >
void SLMat4< T >::posAtUp ( PosX,
PosY,
PosZ,
dirAtX = 0,
dirAtY = 0,
dirAtZ = 0,
dirUpX = 0,
dirUpY = 0,
dirUpZ = 0 
)

Same as lightAt.

Definition at line 822 of file SLMat4.h.

825 {
826  lightAt(SLVec3<T>( PosX, PosY, PosZ),
827  SLVec3<T>(dirAtX, dirAtY, dirAtZ),
828  SLVec3<T>(dirUpX, dirUpY, dirUpZ));
829 }

◆ print()

template<class T >
void SLMat4< T >::print ( const SLchar str = nullptr) const

Prints out the matrix row by row.

Definition at line 1554 of file SLMat4.h.

1555 {
1556  if (str) SL_LOG("%s",str);
1557  SL_LOG("% 3.2f % 3.2f % 3.2f % 3.2f", _m[0],_m[4],_m[ 8],_m[12]);
1558  SL_LOG("% 3.2f % 3.2f % 3.2f % 3.2f", _m[1],_m[5],_m[ 9],_m[13]);
1559  SL_LOG("% 3.2f % 3.2f % 3.2f % 3.2f", _m[2],_m[6],_m[10],_m[14]);
1560  SL_LOG("% 3.2f % 3.2f % 3.2f % 3.2f", _m[3],_m[7],_m[11],_m[15]);
1561 }

◆ rotate() [1/3]

template<class T >
void SLMat4< T >::rotate ( const SLVec3< T > &  fromUnitVec,
const SLVec3< T > &  toUnitVec 
)

Multiplies the matrix with a rotation matrix created with a rotation between two vectors.

Definition at line 645 of file SLMat4.h.

646 {
647  SLMat4<T> R(fromUnitVec, toUnitVec);
648  multiply(R);
649 }

◆ rotate() [2/3]

template<class T >
void SLMat4< T >::rotate ( degAng,
const SLVec3< T > &  axis 
)

Multiplies the matrix with a rotation matrix. Corresponds to the OpenGL function glRotate*.

Definition at line 634 of file SLMat4.h.

635 {
636  SLMat4<T> R(degAng, axis.x, axis.y, axis.z);
637  multiply(R);
638 }

◆ rotate() [3/3]

template<class T >
void SLMat4< T >::rotate ( degAng,
axisx,
axisy,
axisz 
)

Multiplies the matrix with a rotation matrix. Corresponds to the OpenGL function glRotate*.

Definition at line 656 of file SLMat4.h.

657 {
658  SLMat4<T> R(degAng, axisx, axisy, axisz);
659  multiply(R);
660 }

◆ rotation() [1/3]

template<class T >
void SLMat4< T >::rotation ( const SLVec3< T > &  from,
const SLVec3< T > &  to 
)

Defines a rotation matrix that rotates the vector from to the vector to. Code and explanation comes from the paper "Efficiently build a matrix to ratate one vector to another" from Thomas Mueller and John Hughes in the Journal of Graphic Tools, volume 4.

Definition at line 1035 of file SLMat4.h.

1036 {
1037  // Creates a rotation matrix that will rotate the Vector 'from' into
1038  // the Vector 'to'. The resulting matrix is stored in 'this' Matrix.
1039  // For this method to work correctly, vector 'from' and vector 'to'
1040  // must both be unit length vectors.
1041 
1042  T cosAngle = from.dot(to);
1043 
1044  if (fabs(cosAngle-1.0) <= FLT_EPSILON) // cosAngle = 1.0
1045  {
1046  // Special case where 'from' is equal to 'to'. In other words,
1047  // the angle between Vector 'from' and Vector 'to' is zero
1048  // degrees. In this case the identity matrix is returned in
1049  // Matrix 'result'.
1050 
1051  identity();
1052  }
1053  else if (fabs(cosAngle+1.0) <= FLT_EPSILON) // cosAngle = -1.0
1054  {
1055  // Special case where 'from' is directly opposite to 'to'. In
1056  // other words, the angle between Vector 'from' and Vector 'to'
1057  // is 180 degrees. In this case, the following matrix is used:
1058  //
1059  // Let:
1060  // F = from
1061  // S = vector perpendicular to F
1062  // U = S X F
1063  //
1064  // We want to rotate from (F, U, S) to (-F, U, -S)
1065  //
1066  // | -FxFx+UxUx-SxSx -FxFy+UxUy-SxSy -FxFz+UxUz-SxSz 0 |
1067  // | -FxFy+UxUy-SxSy -FyFy+UyUy-SySy -FyFz+UyUz-SySz 0 |
1068  // | -FxFz+UxUz-SxSz -FyFz+UyUz-SySz -FzFz+UzUz-SzSz 0 |
1069  // | 0 0 0 1 |
1070 
1071  SLVec3<T> side(0.f, from.z, -from.y);
1072 
1073  if (fabs(side.dot(side)) <= FLT_EPSILON)
1074  { side.x = -from.z;
1075  side.y = 0.f;
1076  side.z = from.x;
1077  }
1078 
1079  side.normalize();
1080 
1081  SLVec3<T> up;
1082  up.cross(side, from);
1083  up.normalize();
1084 
1085  _m[ 0] = -(from.x * from.x) + (up.x * up.x) - (side.x * side.x);
1086  _m[ 4] = -(from.x * from.y) + (up.x * up.y) - (side.x * side.y);
1087  _m[ 8] = -(from.x * from.z) + (up.x * up.z) - (side.x * side.z);
1088  _m[12] = 0.f;
1089  _m[ 1] = -(from.x * from.y) + (up.x * up.y) - (side.x * side.y);
1090  _m[ 5] = -(from.y * from.y) + (up.y * up.y) - (side.y * side.y);
1091  _m[ 9] = -(from.y * from.z) + (up.y * up.z) - (side.y * side.z);
1092  _m[13] = 0.f;
1093  _m[ 2] = -(from.x * from.z) + (up.x * up.z) - (side.x * side.z);
1094  _m[ 6] = -(from.y * from.z) + (up.y * up.z) - (side.y * side.z);
1095  _m[10] = -(from.z * from.z) + (up.z * up.z) - (side.z * side.z);
1096  _m[14] = 0.f;
1097  _m[ 3] = 0.f;
1098  _m[ 7] = 0.f;
1099  _m[11] = 0.f;
1100  _m[15] = 1.f;
1101  }
1102  else
1103  {
1104  // This is the most common case. Creates the rotation matrix:
1105  //
1106  // | E + HVx^2 HVxVy - Vz HVxVz + Vy 0 |
1107  // R(from, to) = | HVxVy + Vz E + HVy^2 HVyVz - Vx 0 |
1108  // | HVxVz - Vy HVyVz + Vx E + HVz^2 0 |
1109  // | 0 0 0 1 |
1110  //
1111  // where,
1112  // V = from X to
1113  // E = from.dot(to)
1114  // H = (1 - E) / V.dot(V)
1115 
1116  SLVec3<T> v;
1117  v.cross(from, to);
1118  v.normalize();
1119 
1120  T h = (1.f - cosAngle) / v.dot(v);
1121 
1122  _m[ 0] = cosAngle + h * v.x * v.x;
1123  _m[ 4] = h * v.x * v.y - v.z;
1124  _m[ 8] = h * v.x * v.z + v.y;
1125  _m[12] = 0.f;
1126  _m[ 1] = h * v.x * v.y + v.z;
1127  _m[ 5] = cosAngle + h * v.y * v.y;
1128  _m[ 9] = h * v.y * v.z - v.x;
1129  _m[13] = 0.f;
1130  _m[ 2] = h * v.x * v.z - v.y;
1131  _m[ 6] = h * v.y * v.z + v.x;
1132  _m[10] = cosAngle + h * v.z * v.z;
1133  _m[14] = 0.f;
1134  _m[ 3] = 0.f;
1135  _m[ 7] = 0.f;
1136  _m[11] = 0.f;
1137  _m[15] = 1.f;
1138  }
1139 }
T dot(const SLVec3 &v) const
Definition: SLVec3.h:117

◆ rotation() [2/3]

template<class T >
void SLMat4< T >::rotation ( degAng,
const SLVec3< T > &  axis,
SLbool  keepTranslation = true 
)

Sets the rotation with or without overwriting the translation.

Sets the linear 3x3 submatrix as a rotation matrix with a rotation of degAng degrees around an axis. By default the translation components are set to 0.

Definition at line 978 of file SLMat4.h.

980 {
981  rotation(degAng, axis.x, axis.y, axis.z, keepTrans);
982 }

◆ rotation() [3/3]

template<class T >
void SLMat4< T >::rotation ( degAng,
axisx,
axisy,
axisz,
SLbool  keepTranslation = true 
)

Sets the linear 3x3 submatrix as a rotation matrix with a rotation of degAng degrees around an axis. By default the translation components are set to 0.

Definition at line 989 of file SLMat4.h.

992 {
993  T RadAng = (T)degAng * DEG2RAD;
994  T ca=(T)cos(RadAng), sa=(T)sin(RadAng);
995  if (axisx==1 && axisy==0 && axisz==0) // about x-axis
996  { _m[0]=1; _m[4]=0; _m[8]=0;
997  _m[1]=0; _m[5]=ca; _m[9]=-sa;
998  _m[2]=0; _m[6]=sa; _m[10]=ca;
999  } else
1000  if (axisx==0 && axisy==1 && axisz==0) // about y-axis
1001  { _m[0]=ca; _m[4]=0; _m[8]=sa;
1002  _m[1]=0; _m[5]=1; _m[9]=0;
1003  _m[2]=-sa; _m[6]=0; _m[10]=ca;
1004  } else
1005  if (axisx==0 && axisy==0 && axisz==1) // about z-axis
1006  { _m[0]=ca; _m[4]=-sa; _m[8]=0;
1007  _m[1]=sa; _m[5]=ca; _m[9]=0;
1008  _m[2]=0; _m[6]=0; _m[10]=1;
1009  } else // arbitrary axis
1010  { T l = axisx*axisx + axisy*axisy + axisz*axisz; // length squared
1011  T x, y, z;
1012  x=axisx, y=axisy, z=axisz;
1013  if ((l > T(1.0001) || l < T(0.9999)) && l!=0)
1014  { l=T(1.0)/sqrt(l);
1015  x*=l; y*=l; z*=l;
1016  }
1017  T xy=x*y, yz=y*z, xz=x*z, xx=x*x, yy=y*y, zz=z*z;
1018  _m[0]=xx + ca*(1-xx); _m[4]=xy - xy*ca - z*sa; _m[8] =xz - xz*ca + y*sa;
1019  _m[1]=xy - xy*ca + z*sa; _m[5]=yy + ca*(1-yy); _m[9] =yz - yz*ca - x*sa;
1020  _m[2]=xz - xz*ca - y*sa; _m[6]=yz - yz*ca + x*sa; _m[10]=zz + ca*(1-zz);
1021  }
1022  _m[3]=_m[7]=_m[11]=0; _m[15]=1;
1023 
1024  if (!keepTrans)
1025  _m[12] = _m[13] = _m[14] = 0;
1026 }

◆ scale() [1/3]

template<class T >
void SLMat4< T >::scale ( const SLVec3< T > &  s)

Multiplies the matrix with a scaling matrix. Corresponds to the OpenGL function glScale*.

Definition at line 694 of file SLMat4.h.

695 {
696  SLMat4<T> S(s.x, 0, 0, 0,
697  0,s.y, 0, 0,
698  0, 0,s.z, 0,
699  0, 0, 0, 1);
700  multiply(S);
701 }
The SLScene class represents the top level instance holding the scene structure.
Definition: SLScene.h:47

◆ scale() [2/3]

template<class T >
void SLMat4< T >::scale ( sx,
sy,
sz 
)

Multiplies the matrix with a scaling matrix. Corresponds to the OpenGL function glScale*.

Definition at line 680 of file SLMat4.h.

681 {
682  SLMat4<T> S(sx, 0, 0, 0,
683  0,sy, 0, 0,
684  0, 0,sz, 0,
685  0, 0, 0, 1);
686  multiply(S);
687 }

◆ scale() [3/3]

template<class T >
void SLMat4< T >::scale ( sxyz)

Multiplies the matrix with a scaling matrix. Corresponds to the OpenGL function glScale*.

Definition at line 667 of file SLMat4.h.

668 { SLMat4<T> S(s, 0, 0, 0,
669  0, s, 0, 0,
670  0, 0, s, 0,
671  0, 0, 0, 1);
672  multiply(S);
673 }

◆ scaling() [1/3]

template<class T >
void SLMat4< T >::scaling ( const SLVec3< T > &  scale,
SLbool  keepTrans = true 
)

Sets the linear 3x3 submatrix as a scaling matrix with the scaling vector s. By default the translation components are set to 0.

Definition at line 1157 of file SLMat4.h.

1159 {
1160  scaling(scale.x, scale.y, scale.z);
1161 }

◆ scaling() [2/3]

template<class T >
void SLMat4< T >::scaling ( sx,
sy,
sz,
SLbool  keepTranslation = true 
)

Sets the linear 3x3 submatrix as a scaling matrix with the scaling f_actors sx, sy and sz. By default the translation components are set to 0.

Definition at line 1168 of file SLMat4.h.

1170 {
1171  _m[0]=sx; _m[4]=0; _m[8]=0;
1172  _m[1]=0; _m[5]=sy; _m[9]=0;
1173  _m[2]=0; _m[6]=0; _m[10]=sz;
1174  _m[3]=0; _m[7]=0; _m[11]=0; _m[15]=1;
1175 
1176  if (!keepTrans)
1177  _m[12] = _m[13] = _m[14] = 0;
1178 }

◆ scaling() [3/3]

template<class T >
void SLMat4< T >::scaling ( sxyz,
SLbool  keepTrans = true 
)

Sets the scaling with or without overwriting the translation.

Sets the linear 3x3 submatrix as a scaling matrix with the scaling vector s. By default the translation components are set to 0.

Definition at line 1146 of file SLMat4.h.

1148 {
1149  scaling(sxyz, sxyz, sxyz);
1150 }

◆ setMatrix() [1/7]

template<class T >
void SLMat4< T >::setMatrix ( const int16_t  i,
const SLfloat  value 
)
inline

Definition at line 90 of file SLMat4.h.

90 { assert(i >= 0 && i < 16); _m[i] = value; }

◆ setMatrix() [2/7]

template<class T >
void SLMat4< T >::setMatrix ( const SLMat3f A)

Set matrix by other 3x3 matrix.

Definition at line 341 of file SLMat4.h.

342 {
343  _m[0]=A._m[0]; _m[4]=A._m[3]; _m[ 8]=A._m[6]; _m[12]=0;
344  _m[1]=A._m[1]; _m[5]=A._m[4]; _m[ 9]=A._m[7]; _m[13]=0;
345  _m[2]=A._m[2]; _m[6]=A._m[5]; _m[10]=A._m[8]; _m[14]=0;
346  _m[3]=0; _m[7]=0; _m[11]=0; _m[15]=1;
347 }
T _m[9]
Definition: SLMat3.h:41

◆ setMatrix() [3/7]

template<class T >
void SLMat4< T >::setMatrix ( const SLMat4< T > &  A)

Set matrix by other 4x4 matrix.

Definition at line 335 of file SLMat4.h.

336 {
337  for (int i=0; i<16; ++i) _m[i] = A._m[i];
338 }

◆ setMatrix() [4/7]

template<class T >
void SLMat4< T >::setMatrix ( const SLMat4< T > *  A)

Set matrix by other matrix pointer.

Definition at line 350 of file SLMat4.h.

351 {
352  for (int i=0; i<16; ++i) _m[i] = A->_m[i];
353 }

◆ setMatrix() [5/7]

template<class T >
void SLMat4< T >::setMatrix ( const SLVec3< T > &  translation,
const SLMat3< T > &  rotation,
const SLVec3< T > &  scale 
)

Set matrix by translation, rotation & scale.

Definition at line 374 of file SLMat4.h.

377 {
378  setMatrix(scale.x * rotation[0], scale.y * rotation[3], scale.z * rotation[6], translation.x,
379  scale.x * rotation[1], scale.y * rotation[4], scale.z * rotation[7], translation.y,
380  scale.x * rotation[2], scale.y * rotation[5], scale.z * rotation[8], translation.z,
381  0 , 0 , 0 , 1);
382 }

◆ setMatrix() [6/7]

template<class T >
void SLMat4< T >::setMatrix ( const T *  M)

Set matrix by float[16] array.

Definition at line 356 of file SLMat4.h.

357 {
358  for (int i=0; i<16; ++i) _m[i] = M[i];
359 }

◆ setMatrix() [7/7]

template<class T >
void SLMat4< T >::setMatrix ( M0,
M4,
M8,
M12,
M1,
M5,
M9,
M13,
M2,
M6,
M10,
M14,
M3,
M7,
M11,
M15 
)

Set matrix by components.

Definition at line 362 of file SLMat4.h.

366 {
367  _m[0]=M0; _m[4]=M4; _m[ 8]=M8; _m[12]=M12;
368  _m[1]=M1; _m[5]=M5; _m[ 9]=M9; _m[13]=M13;
369  _m[2]=M2; _m[6]=M6; _m[10]=M10; _m[14]=M14;
370  _m[3]=M3; _m[7]=M7; _m[11]=M11; _m[15]=M15;
371 }

◆ setRotation()

template<class T >
void SLMat4< T >::setRotation ( const SLMat3< T > &  rotation)

Set 3x3 submatrix describing the rotational part.

Parameters
rotationSet 3x3 submatrix describing the rotational part

Definition at line 385 of file SLMat4.h.

386 {
387  _m[0]=rotation[0]; _m[4]=rotation[3]; _m[8]=rotation[6];
388  _m[1]=rotation[1]; _m[5]=rotation[4]; _m[9]=rotation[7];
389  _m[2]=rotation[2]; _m[6]=rotation[5]; _m[10]=rotation[8];
390 }

◆ setTranslation()

template<class T >
void SLMat4< T >::setTranslation ( const SLVec3< T > &  translation)

Set vector as submatrix describing the translational part.

Parameters
translationSet vector as submatrix describing the translational part

Definition at line 393 of file SLMat4.h.

394 {
395  _m[12]=translation.x;
396  _m[13]=translation.y;
397  _m[14]=translation.z;
398 }

◆ swap()

template<class T >
static void SLMat4< T >::swap ( T &  a,
T &  b 
)
inlinestatic

Definition at line 252 of file SLMat4.h.

252 {T t; t=a;a=b;b=t;}

◆ toEulerAngles()

template<class T >
SLVec3< T > SLMat4< T >::toEulerAngles ( int  order)

Definition at line 1310 of file SLMat4.h.

1311 {
1312  HMatrix A;
1313  A[0][0]=_m[0]; A[0][1]=_m[4]; A[0][2]=_m[ 8]; A[0][3]=_m[12];
1314  A[1][0]=_m[1]; A[1][1]=_m[5]; A[1][2]=_m[ 9]; A[1][3]=_m[13];
1315  A[2][0]=_m[2]; A[2][1]=_m[6]; A[2][2]=_m[10]; A[2][3]=_m[14];
1316  A[3][0]=_m[3]; A[3][1]=_m[7]; A[3][2]=_m[11]; A[3][3]=_m[15];
1317 
1318  EulerAngles eulerAngles = Eul_FromHMatrix(A, order);
1319 
1320  SLVec3<T> angles;
1321  angles.x = eulerAngles.x;
1322  angles.y = eulerAngles.y;
1323  angles.z = eulerAngles.z;
1324  return angles;
1325 }

◆ toEulerAnglesXYZ()

template<class T >
void SLMat4< T >::toEulerAnglesXYZ ( T &  xRotRAD,
T &  yRotRAD,
T &  zRotRAD 
)

Gets one set of possible x-y-z euler angles that will generate this matrix Assumes that upper 3x3 is a rotation matrix Source: Essential Mathematics for Games and Interactive Applications A Programmer's Guide 2nd edition by James M. Van Verth and Lars M. Bishop

Definition at line 1278 of file SLMat4.h.

1279 {
1280  T Cx, Sx;
1281  T Cy, Sy;
1282  T Cz, Sz;
1283 
1284  Sy = _m[8];
1285  Cy = (T)sqrt(1.0 - Sy*Sy);
1286 
1287  // normal case
1288  if (Utils::abs(Cy) > FLT_EPSILON)
1289  {
1290  T factor = (T)(1.0 / Cy);
1291  Sx = -_m[ 9]*factor;
1292  Cx = _m[10]*factor;
1293  Sz = -_m[ 4]*factor;
1294  Cz = _m[ 0]*factor;
1295  }
1296  else // x and z axes aligned
1297  {
1298  Sz = 0.0;
1299  Cz = 1.0;
1300  Sx = _m[6];
1301  Cx = _m[5];
1302  }
1303 
1304  zRotRAD = atan2f(Sz, Cz);
1305  yRotRAD = atan2f(Sy, Cy);
1306  xRotRAD = atan2f(Sx, Cx);
1307 }

◆ toEulerAnglesZYX()

template<class T >
void SLMat4< T >::toEulerAnglesZYX ( T &  zRotRAD,
T &  yRotRAD,
T &  xRotRAD 
)

Gets one set of possible z-y-x euler angles that will generate this matrix Assumes that upper 3x3 is a rotation matrix Source: Essential Mathematics for Games and Interactive Applications A Programmer's Guide 2nd edition by James M. Van Verth and Lars M. Bishop

Definition at line 1241 of file SLMat4.h.

1242 {
1243  T Cx, Sx;
1244  T Cy, Sy;
1245  T Cz, Sz;
1246 
1247  Sy = -_m[2];
1248  Cy = (T)sqrt(1.0 - Sy*Sy);
1249 
1250  // normal case
1251  if (Utils::abs(Cy) > FLT_EPSILON)
1252  {
1253  T factor = (T)(1.0 / Cy);
1254  Sx = _m[ 6]*factor;
1255  Cx = _m[10]*factor;
1256  Sz = _m[ 1]*factor;
1257  Cz = _m[ 0]*factor;
1258  }
1259  else // x and z axes aligned
1260  {
1261  Sz = 0.0;
1262  Cz = 1.0;
1263  Sx = _m[4];
1264  Cx = _m[8];
1265  }
1266 
1267  zRotRAD = atan2f(Sz, Cz);
1268  yRotRAD = atan2f(Sy, Cy);
1269  xRotRAD = atan2f(Sx, Cx);
1270 }

◆ toString()

template<class T >
SLstring SLMat4< T >::toString

Prints out the matrix row by row.

Definition at line 1567 of file SLMat4.h.

1568 {
1569  SLchar cstr[500];
1570  snprintf(cstr,
1571  sizeof(cstr),
1572  "% 3.3f % 3.3f % 3.3f % 3.3f\n% 3.3f % 3.3f % 3.3f % 3.3f\n% 3.3f % 3.3f % 3.3f % 3.3f\n% 3.3f % 3.3f % 3.3f % 3.3f",
1573  _m[0],_m[4],_m[ 8],_m[12],
1574  _m[1],_m[5],_m[ 9],_m[13],
1575  _m[2],_m[6],_m[10],_m[14],
1576  _m[3],_m[7],_m[11],_m[15]);
1577  SLstring cppstr = cstr;
1578  return cppstr;
1579 }
char SLchar
Definition: SL.h:162
string SLstring
Definition: SL.h:158

◆ trace()

template<class T >
T SLMat4< T >::trace
inline

Returns the trace of the matrix that is the sum of the diagonal components.

Definition at line 1455 of file SLMat4.h.

1456 {
1457  return _m[0] + _m[5] + _m[10] + _m[15];
1458 }

◆ translate() [1/3]

template<class T >
void SLMat4< T >::translate ( const SLVec2< T > &  t)

Multiplies the matrix with a translation matrix. Corresponds to the OpenGL function glTranslate*.

Definition at line 612 of file SLMat4.h.

613 {
614  SLMat4<T> trans(t.x, t.y, 0);
615  multiply(trans);
616 }
T y
Definition: SLVec2.h:30
T x
Definition: SLVec2.h:30

◆ translate() [2/3]

template<class T >
void SLMat4< T >::translate ( const SLVec3< T > &  t)

Multiplies the matrix with a translation matrix. Corresponds to the OpenGL function glTranslate*.

Definition at line 623 of file SLMat4.h.

624 {
625  SLMat4<T> trans(t.x, t.y, t.z);
626  multiply(trans);
627 }

◆ translate() [3/3]

template<class T >
void SLMat4< T >::translate ( tx,
ty,
tz = 0 
)

Multiplies the matrix with a translation matrix. Corresponds to the OpenGL function glTranslate*.

Definition at line 601 of file SLMat4.h.

602 {
603  SLMat4<T> trans(tx, ty, tz);
604  multiply(trans);
605 }

◆ translation() [1/3]

template<class T >
SLVec3<T> SLMat4< T >::translation ( ) const
inline

Definition at line 184 of file SLMat4.h.

184 {return SLVec3<T>(_m[12], _m[13], _m[14]);}

◆ translation() [2/3]

template<class T >
void SLMat4< T >::translation ( const SLVec3< T > &  t,
SLbool  keepLinear = true 
)

Sets the translation components. By default the linear 3x3 submatrix containing rotations and scaling is reset to identity.

Definition at line 948 of file SLMat4.h.

949 {
950  translation(t.x, t.y, t.z, keepLinear);
951 }

◆ translation() [3/3]

template<class T >
void SLMat4< T >::translation ( tx,
ty,
tz,
SLbool  keepLinear = true 
)

Sets the translation with or without overwriting the linear submatrix.

Sets the translation components. By default the linear 3x3 submatrix containing rotations and scaling is reset to identity.

Definition at line 958 of file SLMat4.h.

960 {
961  _m[12]=tx;
962  _m[13]=ty;
963  _m[14]=tz;
964 
965  if (!keepLinear)
966  { _m[0]=1; _m[4]=0; _m[8]=0;
967  _m[1]=0; _m[5]=1; _m[9]=0;
968  _m[2]=0; _m[6]=0; _m[10]=1;
969  _m[3]=0; _m[7]=0; _m[11]=0; _m[15]=1;
970  }
971 }

◆ transpose()

template<class T >
void SLMat4< T >::transpose

Sets the transposed matrix by swaping around the main diagonal.

Definition at line 1341 of file SLMat4.h.

1342 {
1343  swap(_m[1], _m[ 4]);
1344  swap(_m[2], _m[ 8]);
1345  swap(_m[6], _m[ 9]);
1346  swap(_m[3], _m[12]);
1347  swap(_m[7], _m[13]);
1348  swap(_m[11],_m[14]);
1349 }
static void swap(T &a, T &b)
Definition: SLMat4.h:252

◆ transposed()

template<class T >
SLMat4< T > SLMat4< T >::transposed

Returns the transposed of the matrix and leaves the itself unchanged.

Definition at line 1353 of file SLMat4.h.

1354 {
1355  SLMat4<T> t( _m[0], _m[1], _m[2], _m[3],
1356  _m[4], _m[5], _m[6], _m[7],
1357  _m[8], _m[9],_m[10],_m[11],
1358  _m[12],_m[13],_m[14],_m[15]);
1359  return t;
1360 }

◆ viewport()

template<class T >
void SLMat4< T >::viewport ( x,
y,
ww,
wh,
n = 0.0f,
f = 1.0f 
)

Defines the viewport matrix.

Defines a viewport matrix as it will be produced by glViewport.

Parameters
xleft window coord. in px.
ytop window coord. in px.
wwwindow width in px.
whwindow height in px.
nnear depth range (default 0)
ffar depth range (default 1)

Definition at line 930 of file SLMat4.h.

932 {
933  T ww2 = ww*0.5f;
934  T wh2 = wh*0.5f;
935 
936  // negate the first wh2 because windows has topdown window coords
937  _m[0]=ww2; _m[4]=0; _m[8] =0; _m[12]=x+ww2;
938  _m[1]=0; _m[5]=-wh2; _m[9] =0; _m[13]=y+wh2;
939  _m[2]=0; _m[6]=0; _m[10]=(f-n)*0.5f; _m[14]=(f+n)*0.5f;
940  _m[3]=0; _m[7]=0; _m[11]=0; _m[15]=1;
941 }

Member Data Documentation

◆ _m

template<class T >
T SLMat4< T >::_m[16]
private

The 16 elements of the matrix.

Definition at line 255 of file SLMat4.h.


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