Matrix.cpp 3.1 KB

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