Matrix.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #ifndef MATRIX_H_
  2. #define MATRIX_H_
  3. #include "Vector4.h"
  4. #include "Vector3.h"
  5. #include "Vector2.h"
  6. #include "Quaternion.h"
  7. #include "FileIO.h"
  8. #define MATRIX4F_SIZE (sizeof(float) * 16)
  9. #define PI 3.14159265f
  10. #define TORADIANS(degrees) (degrees * (PI / 180.0f))
  11. #define TODEGREES(radians) (radians * (180.0f / PI))
  12. namespace gameplay
  13. {
  14. /**
  15. * The identify matrix.
  16. */
  17. static const float MATRIX4F_IDENTITY[16] =
  18. {
  19. 1.0f, 0.0f, 0.0f, 0.0f,
  20. 0.0f, 1.0f, 0.0f, 0.0f,
  21. 0.0f, 0.0f, 1.0f, 0.0f,
  22. 0.0f, 0.0f, 0.0f, 1.0f
  23. };
  24. class Matrix
  25. {
  26. public:
  27. /**
  28. * Matrix colums.
  29. */
  30. float m[16];
  31. /**
  32. * Constructor.
  33. */
  34. Matrix(void);
  35. /**
  36. * Constructor.
  37. */
  38. Matrix(float m0, float m1, float m2, float m3,
  39. float m4, float m5, float m6, float m7,
  40. float m8, float m9, float m10, float m11,
  41. float m12, float m13, float m14, float m15);
  42. /**
  43. * Destructor.
  44. */
  45. ~Matrix(void);
  46. /**
  47. * Computes the determinant of this matrix.
  48. *
  49. * @return The determinant.
  50. */
  51. float determinant() const;
  52. /**
  53. * Decomposes the scale, rotation and translation components of this matrix.
  54. *
  55. * @param scale The scale.
  56. * @param rotation The rotation.
  57. * @param translation The translation.
  58. */
  59. bool decompose(Vector3* scale, Quaternion* rotation, Vector3* translation) const;
  60. /**
  61. * Sets the given matrix to be the identity matrix.
  62. */
  63. static void setIdentity(float* matrix);
  64. /**
  65. * Multiplies two matrices and stores the results in dst.
  66. * m1 and m2 may be the same as dst.
  67. */
  68. static void multiply(const float* m1, const float* m2, float* dst);
  69. /**
  70. * Creates a scale matrix for the givent x,y,z scale factors.
  71. */
  72. static void createScale(float x, float y, float z, float* dst);
  73. /**
  74. * Creates a rotation matrix from the given quaternion.
  75. */
  76. static void createRotation(const Quaternion& q, float* dst);
  77. /**
  78. * Creates a rotation matrix from the given axis and angle in degrees.
  79. */
  80. static void createRotation(float x, float y, float z, float angle, float* dst);
  81. /**
  82. * Creates a rotation matrix for the given angle in the x axis and angle in degrees
  83. */
  84. void createRotationX(float angle, float* dst);
  85. /**
  86. * Creates a rotation matrix for the given angle in the y axis and angle in degrees
  87. */
  88. void createRotationY(float angle, float* dst);
  89. /**
  90. * Creates a rotation matrix for the given angle in the z axis and angle in degrees
  91. */
  92. void createRotationZ(float angle, float* dst);
  93. /**
  94. * Creates a translation matrix for the given x, y ,x values.
  95. */
  96. static void createTranslation(float x, float y, float z, float* dst);
  97. /**
  98. * Returns the pointer to the float array.
  99. */
  100. float* getArray();
  101. /**
  102. * Translates the matrix by the x, y, z values.
  103. */
  104. void translate(float x, float y, float z);
  105. /**
  106. * Scales the matrix by the x, y, z factors.
  107. */
  108. void scale(float x, float y, float z);
  109. /**
  110. * Rotates the matrix by the given Quaternion.
  111. */
  112. void rotate(const Quaternion& q);
  113. /**
  114. * Rotates the matrix by the axies specified and angle.
  115. */
  116. void rotate(float x, float y, float z, float angle);
  117. /**
  118. * Rotates the matrix on the y-axis by the specified angle in degrees.
  119. */
  120. void rotateX(float angle);
  121. /**
  122. * Rotates the matrix on the y-axis by the specified angle in degrees.
  123. */
  124. void rotateY(float angle);
  125. /**
  126. * Rotates the matrix on the z-axis by the specified angle in degrees.
  127. */
  128. void rotateZ(float angle);
  129. /**
  130. * Transforms the specified point by this matrix.
  131. *
  132. * Note that the input vector is treated as a point and NOT a vector.
  133. */
  134. void transformPoint(const Vector3& p, Vector3* dst) const;
  135. };
  136. }
  137. #endif