Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Matrix4.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_MATRIX4_HPP
8 #define NAZARA_MATRIX4_HPP
9 
11 
12 #include <Nazara/Core/String.hpp>
13 #include <Nazara/Math/Config.hpp>
14 
15 namespace Nz
16 {
17  struct SerializationContext;
18 
19  template<typename T> class EulerAngles;
20  template<typename T> class Quaternion;
21  template<typename T> class Vector2;
22  template<typename T> class Vector3;
23  template<typename T> class Vector4;
24 
25  template<typename T>
26  class Matrix4
27  {
28  public:
29  Matrix4() = default;
30  Matrix4(T r11, T r12, T r13, T r14,
31  T r21, T r22, T r23, T r24,
32  T r31, T r32, T r33, T r34,
33  T r41, T r42, T r43, T r44);
34  //Matrix4(const Matrix3<T>& matrix);
35  Matrix4(const T matrix[16]);
36  template<typename U> explicit Matrix4(const Matrix4<U>& matrix);
37  Matrix4(const Matrix4& matrix) = default;
38  ~Matrix4() = default;
39 
40  Matrix4& ApplyRotation(const Quaternion<T>& rotation);
41  Matrix4& ApplyScale(const Vector3<T>& scale);
42  Matrix4& ApplyTranslation(const Vector3<T>& translation);
43 
44  Matrix4& Concatenate(const Matrix4& matrix);
45  Matrix4& ConcatenateAffine(const Matrix4& matrix);
46 
47  Vector4<T> GetColumn(unsigned int column) const;
48  T GetDeterminant() const;
49  T GetDeterminantAffine() const;
50  bool GetInverse(Matrix4* dest) const;
51  bool GetInverseAffine(Matrix4* dest) const;
52  Quaternion<T> GetRotation() const;
53  //Matrix3 GetRotationMatrix() const;
54  Vector4<T> GetRow(unsigned int row) const;
55  Vector3<T> GetScale() const;
57  Vector3<T> GetTranslation() const;
58  void GetTransposed(Matrix4* dest) const;
59 
60  bool HasNegativeScale() const;
61  bool HasScale() const;
62 
63  Matrix4& Inverse(bool* succeeded = nullptr);
64  Matrix4& InverseAffine(bool* succeeded = nullptr);
65 
66  bool IsAffine() const;
67  bool IsIdentity() const;
68 
70  Matrix4& MakeLookAt(const Vector3<T>& eye, const Vector3<T>& target, const Vector3<T>& up = Vector3<T>::Up());
71  Matrix4& MakeOrtho(T left, T right, T top, T bottom, T zNear = -1.0, T zFar = 1.0);
72  Matrix4& MakePerspective(T angle, T ratio, T zNear, T zFar);
73  Matrix4& MakeRotation(const Quaternion<T>& rotation);
74  Matrix4& MakeScale(const Vector3<T>& scale);
75  Matrix4& MakeTranslation(const Vector3<T>& translation);
76  Matrix4& MakeTransform(const Vector3<T>& translation, const Quaternion<T>& rotation);
77  Matrix4& MakeTransform(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale);
78  Matrix4& MakeViewMatrix(const Vector3<T>& translation, const Quaternion<T>& rotation);
79  Matrix4& MakeZero();
80 
81  Matrix4& Set(T r11, T r12, T r13, T r14,
82  T r21, T r22, T r23, T r24,
83  T r31, T r32, T r33, T r34,
84  T r41, T r42, T r43, T r44);
85  Matrix4& Set(const T matrix[16]);
86  //Matrix4(const Matrix3<T>& matrix);
87  Matrix4& Set(const Matrix4& matrix);
88  template<typename U> Matrix4& Set(const Matrix4<U>& matrix);
89  Matrix4& SetRotation(const Quaternion<T>& rotation);
90  Matrix4& SetScale(const Vector3<T>& scale);
91  Matrix4& SetTranslation(const Vector3<T>& translation);
92 
93  String ToString() const;
94 
95  Vector2<T> Transform(const Vector2<T>& vector, T z = 0.0, T w = 1.0) const;
96  Vector3<T> Transform(const Vector3<T>& vector, T w = 1.0) const;
97  Vector4<T> Transform(const Vector4<T>& vector) const;
98 
99  Matrix4& Transpose();
100 
101  operator T* ();
102  operator const T* () const;
103 
104  T& operator()(unsigned int x, unsigned int y);
105  T operator()(unsigned int x, unsigned int y) const;
106 
107  Matrix4& operator=(const Matrix4& matrix) = default;
108 
109  Matrix4 operator*(const Matrix4& matrix) const;
110  Vector2<T> operator*(const Vector2<T>& vector) const;
111  Vector3<T> operator*(const Vector3<T>& vector) const;
112  Vector4<T> operator*(const Vector4<T>& vector) const;
113  Matrix4 operator*(T scalar) const;
114 
115  Matrix4& operator*=(const Matrix4& matrix);
116  Matrix4& operator*=(T scalar);
117 
118  bool operator==(const Matrix4& mat) const;
119  bool operator!=(const Matrix4& mat) const;
120 
121  static Matrix4 Concatenate(const Matrix4& left, const Matrix4& right);
122  static Matrix4 ConcatenateAffine(const Matrix4& left, const Matrix4& right);
123  static Matrix4 Identity();
124  static Matrix4 LookAt(const Vector3<T>& eye, const Vector3<T>& target, const Vector3<T>& up = Vector3<T>::Up());
125  static Matrix4 Ortho(T left, T right, T top, T bottom, T zNear = -1.0, T zFar = 1.0);
126  static Matrix4 Perspective(T angle, T ratio, T zNear, T zFar);
127  static Matrix4 Rotate(const Quaternion<T>& rotation);
128  static Matrix4 Scale(const Vector3<T>& scale);
129  static Matrix4 Translate(const Vector3<T>& translation);
130  static Matrix4 Transform(const Vector3<T>& translation, const Quaternion<T>& rotation);
131  static Matrix4 Transform(const Vector3<T>& translation, const Quaternion<T>& rotation, const Vector3<T>& scale);
132  static Matrix4 ViewMatrix(const Vector3<T>& translation, const Quaternion<T>& rotation);
133  static Matrix4 Zero();
134 
135  T m11, m12, m13, m14,
136  m21, m22, m23, m24,
137  m31, m32, m33, m34,
138  m41, m42, m43, m44;
139  };
140 
141  typedef Matrix4<double> Matrix4d;
142  typedef Matrix4<float> Matrix4f;
143 
144  template<typename T> bool Serialize(SerializationContext& context, const Matrix4<T>& matrix);
145  template<typename T> bool Unserialize(SerializationContext& context, Matrix4<T>* matrix);
146 }
147 
148 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Matrix4<T>& matrix);
149 
150 template<typename T> Nz::Matrix4<T> operator*(T scale, const Nz::Matrix4<T>& matrix);
151 
152 
153 #include <Nazara/Math/Matrix4.inl>
154 
155 #endif // NAZARA_MATRIX4_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Matrix4 & ApplyScale(const Vector3< T > &scale)
Apply the scale represented by the vector to this matrix.
Definition: Matrix4.inl:97
static Matrix4 Zero()
Shorthand for the &#39;zero&#39; matrix.
Definition: Matrix4.inl:1758
bool HasNegativeScale() const
Checks whetever matrix has negative scale.
Definition: Matrix4.inl:719
Matrix4 & MakeRotation(const Quaternion< T > &rotation)
Makes the matrix the representation of the quaternion.
Definition: Matrix4.inl:924
Matrix4 & Set(T r11, T r12, T r13, T r14, T r21, T r22, T r23, T r24, T r31, T r32, T r33, T r34, T r41, T r42, T r43, T r44)
Sets the components of the matrix.
Definition: Matrix4.inl:1075
Matrix4 & SetRotation(const Quaternion< T > &rotation)
Sets the components of the matrix from a quaternion.
Definition: Matrix4.inl:1160
static Matrix4 Rotate(const Quaternion< T > &rotation)
Shorthand for the &#39;rotation&#39; matrix.
Definition: Matrix4.inl:1644
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
Matrix4 & MakeTranslation(const Vector3< T > &translation)
Makes the matrix with the translation.
Definition: Matrix4.inl:970
void GetTransposed(Matrix4 *dest) const
Gets the transposed of this matrix.
Definition: Matrix4.inl:695
Matrix4 & SetScale(const Vector3< T > &scale)
Sets the components of the matrix from a scale.
Definition: Matrix4.inl:1200
Core class that represents a string.
Definition: String.hpp:22
String ToString() const
Gives a string representation.
Definition: Matrix4.inl:1234
Matrix4 & MakeTransform(const Vector3< T > &translation, const Quaternion< T > &rotation)
Makes the matrix with the translation and the rotation.
Definition: Matrix4.inl:993
static Matrix4 Translate(const Vector3< T > &translation)
Shorthand for the &#39;translation&#39; matrix.
Definition: Matrix4.inl:1680
Matrix4 & MakePerspective(T angle, T ratio, T zNear, T zFar)
Makes the matrix a &#39;perspective matrix&#39;.
Definition: Matrix4.inl:895
Vector3< T > GetSquaredScale() const
Gets the squared scale from this matrix.
Definition: Matrix4.inl:665
static Matrix4 Scale(const Vector3< T > &scale)
Shorthand for the &#39;scale&#39; matrix.
Definition: Matrix4.inl:1662
bool operator==(const Matrix4 &mat) const
Compares the matrix to other one.
Definition: Matrix4.inl:1494
bool operator!=(const Matrix4 &mat) const
Compares the matrix to other one.
Definition: Matrix4.inl:1511
Matrix4 & MakeZero()
Makes the matrix zero (with 0 everywhere)
Definition: Matrix4.inl:1057
Matrix4 & MakeIdentity()
Makes the matrix identity (with 1 on diagonal and 0 for others)
Definition: Matrix4.inl:820
bool IsIdentity() const
Checks whether the matrix is identity.
Definition: Matrix4.inl:804
static Matrix4 ViewMatrix(const Vector3< T > &translation, const Quaternion< T > &rotation)
Shorthand for the &#39;view&#39; matrix.
Definition: Matrix4.inl:1742
Matrix4 & SetTranslation(const Vector3< T > &translation)
Sets the components of the matrix from a translation.
Definition: Matrix4.inl:1219
Matrix4 & MakeOrtho(T left, T right, T top, T bottom, T zNear=-1.0, T zFar=1.0)
Makes the matrix a &#39;orthographic matrix&#39;.
Definition: Matrix4.inl:871
Matrix4 & Inverse(bool *succeeded=nullptr)
Inverts this matrix.
Definition: Matrix4.inl:759
Matrix4 & Concatenate(const Matrix4 &matrix)
Concatenates this matrix to other one.
Definition: Matrix4.inl:143
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
T & operator()(unsigned int x, unsigned int y)
Gets the component (x, y) of the matrix.
Definition: Matrix4.inl:1346
Matrix4 & ApplyRotation(const Quaternion< T > &rotation)
Apply the rotation represented by the quaternion to this matrix.
Definition: Matrix4.inl:84
Matrix4 & Transpose()
Transposes the matrix.
Definition: Matrix4.inl:1299
Vector3< T > GetScale() const
Gets the scale from this matrix.
Definition: Matrix4.inl:651
static Matrix4 LookAt(const Vector3< T > &eye, const Vector3< T > &target, const Vector3< T > &up=Vector3< T >::Up())
Shorthand for the &#39;look at&#39; matrix.
Definition: Matrix4.inl:1582
Quaternion< T > GetRotation() const
Gets the rotation from this matrix.
Definition: Matrix4.inl:566
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
Matrix4 operator*(const Matrix4 &matrix) const
Multiplies the components of the matrix with other matrix.
Definition: Matrix4.inl:1393
Math class that represents an element of the three dimensional vector space with the notion of projec...
Definition: Matrix4.hpp:23
static Matrix4 Perspective(T angle, T ratio, T zNear, T zFar)
Shorthand for the &#39;perspective&#39; matrix.
Definition: Matrix4.inl:1626
T GetDeterminant() const
Calcultes the determinant of this matrix.
Definition: Matrix4.inl:259
Matrix4 & InverseAffine(bool *succeeded=nullptr)
Inverts this matrix.
Definition: Matrix4.inl:778
Matrix4 & MakeViewMatrix(const Vector3< T > &translation, const Quaternion< T > &rotation)
Makes the matrix a &#39;view matrix&#39;.
Definition: Matrix4.inl:1041
static Matrix4 Identity()
Shorthand for the identity matrix.
Definition: Matrix4.inl:1562
Vector4< T > GetRow(unsigned int row) const
Gets the ith row of the matrix.
Definition: Matrix4.inl:625
bool IsAffine() const
Checks whether the matrix is affine.
Definition: Matrix4.inl:793
Matrix4 & MakeLookAt(const Vector3< T > &eye, const Vector3< T > &target, const Vector3< T > &up=Vector3< T >::Up())
Makes the matrix a &#39;look at matrix&#39;.
Definition: Matrix4.inl:842
Matrix4 & ApplyTranslation(const Vector3< T > &translation)
Apply the translation represented by the vector to this matrix.
Definition: Matrix4.inl:122
Vector3< T > GetTranslation() const
Gets the translation from this matrix.
Definition: Matrix4.inl:678
Matrix4 & operator*=(const Matrix4 &matrix)
Multiplies this matrix with another one.
Definition: Matrix4.inl:1463
Math class that represents a transformation of the four dimensional vector space with the notion of p...
Definition: Matrix4.hpp:26
T GetDeterminantAffine() const
Calcultes the determinant of this matrix.
Definition: Matrix4.inl:284
static Matrix4 Ortho(T left, T right, T top, T bottom, T zNear=-1.0, T zFar=1.0)
Shorthand for the &#39;orthographic&#39; matrix.
Definition: Matrix4.inl:1605
Vector2< T > Transform(const Vector2< T > &vector, T z=0.0, T w=1.0) const
Transforms the Vector2 and two components by the matrix.
Definition: Matrix4.inl:1253
Matrix4 & ConcatenateAffine(const Matrix4 &matrix)
Concatenates this matrix to other one.
Definition: Matrix4.inl:183
Vector4< T > GetColumn(unsigned int column) const
Gets the ith column of the matrix.
Definition: Matrix4.inl:231
Math class that represents an element of the three dimensional vector space.
Definition: Matrix4.hpp:22
bool HasScale() const
Checks whetever matrix has scale.
Definition: Matrix4.inl:732
bool GetInverse(Matrix4 *dest) const
Gets the inverse of this matrix.
Definition: Matrix4.inl:315
bool GetInverseAffine(Matrix4 *dest) const
Gets the inverse of this matrix.
Definition: Matrix4.inl:472
Math class that represents an element of the quaternions.
Definition: Matrix4.hpp:20
Matrix4 & MakeScale(const Vector3< T > &scale)
Makes the matrix with the scale.
Definition: Matrix4.inl:950
Math class that represents an element of the two dimensional vector space.
Definition: Matrix4.hpp:21