Matrix.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "math/Matrix.hpp"
  2. #include "math/Math.hpp"
  3. Core::Matrix::Matrix() {
  4. unit();
  5. }
  6. Core::Matrix& Core::Matrix::unit() {
  7. return translateTo(Vector3(0.0f, 0.0f, 0.0f));
  8. }
  9. Core::Matrix& Core::Matrix::set(int index, const Vector4& v) {
  10. data[index] = v;
  11. return *this;
  12. }
  13. Core::Matrix Core::Matrix::transpose() {
  14. Matrix m;
  15. for(int x = 0; x < 4; x++) {
  16. for(int y = 0; y < 4; y++) {
  17. m.data[x][y] = data[y][x];
  18. }
  19. }
  20. return m;
  21. }
  22. const float* Core::Matrix::getValues() const {
  23. return &(data[0][0]);
  24. }
  25. Core::Matrix& Core::Matrix::operator*=(const Matrix& m) {
  26. data[0] = data[0][0] * m.data[0] + data[0][1] * m.data[1] +
  27. data[0][2] * m.data[2] + data[0][3] * m.data[3];
  28. data[1] = data[1][0] * m.data[0] + data[1][1] * m.data[1] +
  29. data[1][2] * m.data[2] + data[1][3] * m.data[3];
  30. data[2] = data[2][0] * m.data[0] + data[2][1] * m.data[1] +
  31. data[2][2] * m.data[2] + data[2][3] * m.data[3];
  32. data[3] = data[3][0] * m.data[0] + data[3][1] * m.data[1] +
  33. data[3][2] * m.data[2] + data[3][3] * m.data[3];
  34. return *this;
  35. }
  36. Core::Matrix Core::Matrix::operator*(const Matrix& other) const {
  37. Matrix m = *this;
  38. m *= other;
  39. return m;
  40. }
  41. Core::Vector3 Core::Matrix::operator*(const Vector3& v) const {
  42. Vector4 v4(v[0], v[1], v[2], 1.0f);
  43. return Vector3(data[0].dot(v4), data[1].dot(v4), data[2].dot(v4)) *
  44. (1.0f / data[3].dot(v4));
  45. }
  46. Core::Matrix& Core::Matrix::scale(const Vector3& v) {
  47. data[0] *= v[0];
  48. data[1] *= v[1];
  49. data[2] *= v[2];
  50. return *this;
  51. }
  52. Core::Matrix& Core::Matrix::scale(float s) {
  53. return scale(Vector3(s, s, s));
  54. }
  55. Core::Matrix& Core::Matrix::translate(const Vector3& v) {
  56. return translateX(v[0]).translateY(v[1]).translateZ(v[2]);
  57. }
  58. Core::Matrix& Core::Matrix::translateX(float tx) {
  59. data[0] += data[3] * tx;
  60. return *this;
  61. }
  62. Core::Matrix& Core::Matrix::translateY(float ty) {
  63. data[1] += data[3] * ty;
  64. return *this;
  65. }
  66. Core::Matrix& Core::Matrix::translateZ(float tz) {
  67. data[2] += data[3] * tz;
  68. return *this;
  69. }
  70. Core::Matrix& Core::Matrix::translateTo(const Vector3& v) {
  71. data[0] = Vector4(1.0f, 0.0f, 0.0f, v[0]);
  72. data[1] = Vector4(0.0f, 1.0f, 0.0f, v[1]);
  73. data[2] = Vector4(0.0f, 0.0f, 1.0f, v[2]);
  74. data[3] = Vector4(0.0f, 0.0f, 0.0f, 1.0f);
  75. return *this;
  76. }
  77. Core::Matrix& Core::Matrix::rotate(float degrees, int a, int b) {
  78. float sin = 0.0f;
  79. float cos = 0.0f;
  80. Math::sinCos(Core::Math::degreeToRadian(degrees), sin, cos);
  81. Vector4 v = data[a];
  82. data[a] = cos * data[a] - sin * data[b];
  83. data[b] = sin * v + cos * data[b];
  84. return *this;
  85. }
  86. Core::Matrix& Core::Matrix::rotateX(float degrees) {
  87. return rotate(degrees, 1, 2);
  88. }
  89. Core::Matrix& Core::Matrix::rotateY(float degrees) {
  90. return rotate(-degrees, 0, 2);
  91. }
  92. Core::Matrix& Core::Matrix::rotateZ(float degrees) {
  93. return rotate(degrees, 0, 1);
  94. }
  95. Core::Matrix& Core::Matrix::rotate(const Quaternion& q) {
  96. Vector3 a = q * Vector3(data[0][0], data[1][0], data[2][0]);
  97. Vector3 b = q * Vector3(data[0][1], data[1][1], data[2][1]);
  98. Vector3 c = q * Vector3(data[0][2], data[1][2], data[2][2]);
  99. Vector3 d = q * Vector3(data[0][3], data[1][3], data[2][3]);
  100. set(0, Vector4(a[0], b[0], c[0], d[0]));
  101. set(1, Vector4(a[1], b[1], c[1], d[1]));
  102. set(2, Vector4(a[2], b[2], c[2], d[2]));
  103. return *this;
  104. }