Mat4.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. class Mat3;
  28. class Quat;
  29. class Vec3;
  30. class Vec4;
  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. class Mat4
  54. {
  55. public:
  56. float m[16];
  57. /// Does nothing for efficiency.
  58. Mat4();
  59. /// Constructs from a set of float
  60. Mat4(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. Mat4(const Quat& r, const Vec3& p);
  66. /// Contructs from the @a v array
  67. Mat4(const float v[16]);
  68. Mat4(const Mat4& a);
  69. /// Assignment operator (copies the data)
  70. Mat4& operator=(const Mat4& 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. Mat4 operator+(const Mat4& a) const;
  77. Mat4& operator+=(const Mat4& a);
  78. Mat4 operator-(const Mat4& a) const;
  79. Mat4& operator-=(const Mat4& a);
  80. Mat4 operator*(float k) const;
  81. Mat4& operator*=(float k);
  82. Mat4 operator/(float k) const;
  83. Mat4& operator/=(float k);
  84. Vec3 operator*(const Vec3& v) const;
  85. Vec4 operator*(const Vec4& v) const;
  86. Mat4 operator*(const Mat4& a) const;
  87. Mat4& operator*=(const Mat4& a);
  88. /// For simmetry
  89. friend Mat4 operator*(float k, const Mat4& 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 Vec3& n, float radians);
  98. /// Builds a perspetive projection matrix suited to Right-Handed coordinate systems
  99. void build_projection_perspective_rh(float fovy, float aspect, float near, float far);
  100. /// Builds a perspective projection matrix suited to Left-Handed coordinate systems
  101. void build_projection_perspective_lh(float fovy, float aspect, float near, float far);
  102. /// Builds an orthographic projection matrix suited to Right-Handed coordinate systems
  103. void build_projection_ortho_rh(float width, float height, float near, float far);
  104. /// Builds an orthographic projection matrix suited to Left-Handed coordinate systems
  105. void build_projection_ortho_lh(float width, float height, float near, float far);
  106. /// Builds a 2d orthographic projection matrix suited to Right-Handed coordinate systems
  107. void build_projection_ortho_2d_rh(float width, float height, float near, float far);
  108. /// Builds a "Righ-Handed look-at" matrix from a position, a target, and an up vector
  109. void build_look_at_rh(const Vec3& pos, const Vec3& target, const Vec3& up);
  110. /// Builds a "Left-Handed look-at" matrix from a position, a target, and an up vector
  111. void build_look_at_lh(const Vec3& pos, const Vec3& target, const Vec3& up);
  112. /// Builds a "Viewpoint-Oriented billboard" matrix which can be used to make an object face a specific point in space
  113. void build_viewpoint_billboard(const Vec3& pos, const Vec3& target, const Vec3& up);
  114. /// Builds a "Arbitrary-Axis billboard" matrix which can be used to make an object face a specific point in space
  115. void build_axis_billboard(const Vec3& pos, const Vec3& target, const Vec3& axis);
  116. Mat4& transpose();
  117. Mat4 get_transposed() const;
  118. float get_determinant() const;
  119. Mat4& invert();
  120. Mat4 get_inverted() const;
  121. /// Builds the identity matrix
  122. void load_identity();
  123. /// Returns a Vec3 containing the matrix's x base vector.
  124. Vec3 x() const;
  125. /// Returns a Vec3 containing the matrix's y base vector.
  126. Vec3 y() const;
  127. /// Returns a Vec3 containing the matrix's z base vector.
  128. Vec3 z() const;
  129. /// Sets the matrix's x base vector.
  130. void set_x(const Vec3& x);
  131. /// Sets the matrix's y base vector.
  132. void set_y(const Vec3& y);
  133. /// Sets the matrix's z base vector.
  134. void set_z(const Vec3& z);
  135. /// Returns a Vec3 containing the matrix's translation portion
  136. Vec3 translation() const;
  137. /// Fills the matrix's translation portion values contained in @a trans
  138. void set_translation(const Vec3& trans);
  139. /// Returns a Vec3 containing the matrix's scale portion
  140. Vec3 get_scale() const;
  141. /// Fills the matrix's scale portion with the values contained in @a scale
  142. void set_scale(const Vec3& scale);
  143. /// Returns the pointer to the matrix's data
  144. float* to_float_ptr();
  145. /// Returns the pointer to the matrix's data
  146. const float* to_float_ptr() const;
  147. /// Returns a 3x3 matrix according to the matrix's rotation portion
  148. Mat3 to_mat3() const;
  149. /// Returns a quaternion according to the matrix's rotation portion
  150. Quat to_quat() const;
  151. static const Mat4 IDENTITY;
  152. };
  153. } // namespace crown