Matrix4x4.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Types.h"
  25. namespace crown
  26. {
  27. struct Matrix3x3;
  28. struct Quaternion;
  29. struct Vector3;
  30. struct Vector4;
  31. /// Column-major 4x4 matrix.
  32. ///
  33. /// The engine uses column vectors for coordinate space transformations
  34. /// so you'll have to specify transformations in reverse order.
  35. /// e.g. (rotation * translation * vector) will produce the result of first translating
  36. /// and then rotating the vector.
  37. /// Also note that a column major matrix needs to be placed to the left of a
  38. /// vector by matrix multiplication, so, to multiply a vector by a matrix you'll have
  39. /// to write something like: matrix * vector. Since we are also using column vectors, inverting
  40. /// the operands would result in an impossible operation.
  41. ///
  42. /// @a verbatim:
  43. /// X base vector
  44. /// | Y base vector
  45. /// | | Z base vector
  46. /// | | | Translation vector
  47. /// | | | |
  48. /// 1 [ Xx Yx Zx Tx ]
  49. /// 2 | Xy Yy Zy Ty |
  50. /// 3 | Xz Yz Zz Tz |
  51. /// 4 [ 0 0 0 1 ]
  52. /// 1 2 3 4
  53. struct Matrix4x4
  54. {
  55. public:
  56. float m[16];
  57. /// Does nothing for efficiency.
  58. Matrix4x4();
  59. /// Constructs from a set of float
  60. Matrix4x4(float r1c1, float r2c1, float r3c1, float r4c1,
  61. float r1c2, float r2c2, float r3c2, float r4c2,
  62. float r1c3, float r2c3, float r3c3, float r4c3,
  63. float r1c4, float r2c4, float r3c4, float r4c4);
  64. /// Constructs from rotation @a r and position @a p.
  65. Matrix4x4(const Quaternion& r, const Vector3& p);
  66. /// Contructs from the @a v array
  67. Matrix4x4(const float v[16]);
  68. Matrix4x4(const Matrix4x4& a);
  69. /// Assignment operator (copies the data)
  70. Matrix4x4& operator=(const Matrix4x4& a);
  71. /// Random access by index
  72. float operator[](uint32_t i) const;
  73. /// Random access by index
  74. float& operator[](uint32_t i);
  75. float operator()(uint32_t row, uint32_t column) const; //!< Random access by row/column pair
  76. Matrix4x4 operator+(const Matrix4x4& a) const;
  77. Matrix4x4& operator+=(const Matrix4x4& a);
  78. Matrix4x4 operator-(const Matrix4x4& a) const;
  79. Matrix4x4& operator-=(const Matrix4x4& a);
  80. Matrix4x4 operator*(float k) const;
  81. Matrix4x4& operator*=(float k);
  82. Matrix4x4 operator/(float k) const;
  83. Matrix4x4& operator/=(float k);
  84. Vector3 operator*(const Vector3& v) const;
  85. Vector4 operator*(const Vector4& v) const;
  86. Matrix4x4 operator*(const Matrix4x4& a) const;
  87. Matrix4x4& operator*=(const Matrix4x4& a);
  88. /// For simmetry
  89. friend Matrix4x4 operator*(float k, const Matrix4x4& a);
  90. /// Builds a rotation matrix about the X axis of @a radians radians
  91. void build_rotation_x(float radians);
  92. /// Builds a rotation matrix about the Y axis of "radians" radians
  93. void build_rotation_y(float radians);
  94. /// Builds a rotation matrix about the Z axis of @a radians radians
  95. void build_rotation_z(float radians);
  96. /// Builds a rotation matrix about an arbitrary axis of "radians" radians
  97. void build_rotation(const Vector3& n, float radians);
  98. /// Sets the rotation portion to the rotation described by @a rot.
  99. void set_rotation(const Quaternion& rot);
  100. /// Sets the rotation portion to the rotation described by @a rot.
  101. void set_rotation(const Matrix3x3& rot);
  102. /// Builds a perspetive projection matrix suited to Right-Handed coordinate systems
  103. void build_projection_perspective_rh(float fovy, float aspect, float near, float far);
  104. /// Builds an orthographic projection matrix suited to Right-Handed coordinate systems
  105. void build_projection_ortho_rh(float left, float right, float bottom, float top, float near, float far);
  106. /// Builds a "Righ-Handed look-at" matrix from a position, a target, and an up vector
  107. void build_look_at_rh(const Vector3& pos, const Vector3& target, const Vector3& up);
  108. /// Builds a "Viewpoint-Oriented billboard" matrix which can be used to make an object face a specific point in space
  109. void build_viewpoint_billboard(const Vector3& pos, const Vector3& target, const Vector3& up);
  110. /// Builds a "Arbitrary-Axis billboard" matrix which can be used to make an object face a specific point in space
  111. void build_axis_billboard(const Vector3& pos, const Vector3& target, const Vector3& axis);
  112. Matrix4x4& transpose();
  113. Matrix4x4 get_transposed() const;
  114. float get_determinant() const;
  115. Matrix4x4& invert();
  116. Matrix4x4 get_inverted() const;
  117. /// Builds the identity matrix
  118. void load_identity();
  119. /// Returns a Vector3 containing the matrix's x base vector.
  120. Vector3 x() const;
  121. /// Returns a Vector3 containing the matrix's y base vector.
  122. Vector3 y() const;
  123. /// Returns a Vector3 containing the matrix's z base vector.
  124. Vector3 z() const;
  125. /// Sets the matrix's x base vector.
  126. void set_x(const Vector3& x);
  127. /// Sets the matrix's y base vector.
  128. void set_y(const Vector3& y);
  129. /// Sets the matrix's z base vector.
  130. void set_z(const Vector3& z);
  131. /// Returns a Vector3 containing the matrix's translation portion
  132. Vector3 translation() const;
  133. /// Fills the matrix's translation portion values contained in @a trans
  134. void set_translation(const Vector3& trans);
  135. /// Returns a Vector3 containing the matrix's scale portion
  136. Vector3 get_scale() const;
  137. /// Fills the matrix's scale portion with the values contained in @a scale
  138. void set_scale(const Vector3& scale);
  139. /// Returns the pointer to the matrix's data
  140. float* to_float_ptr();
  141. /// Returns the pointer to the matrix's data
  142. const float* to_float_ptr() const;
  143. /// Returns a 3x3 matrix according to the matrix's rotation portion
  144. Matrix3x3 to_matrix3x3() const;
  145. /// Returns a quaternion according to the matrix's rotation portion
  146. Quaternion to_quaternion() const;
  147. static const Matrix4x4 IDENTITY;
  148. };
  149. } // namespace crown