Matrix.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "math/Matrix.h"
  2. #include "math/Math.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] +
  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. Matrix Matrix::operator*(const Matrix& other) const {
  37. Matrix m = *this;
  38. m *= other;
  39. return m;
  40. }
  41. Vector3 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. Matrix& 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. Matrix& Matrix::scale(float s) {
  53. return scale(Vector3(s, s, s));
  54. }
  55. Matrix& Matrix::translate(const Vector3& v) {
  56. return translateX(v[0]).translateY(v[1]).translateZ(v[2]);
  57. }
  58. Matrix& Matrix::translateX(float tx) {
  59. data[0] += data[3] * tx;
  60. return *this;
  61. }
  62. Matrix& Matrix::translateY(float ty) {
  63. data[1] += data[3] * ty;
  64. return *this;
  65. }
  66. Matrix& Matrix::translateZ(float tz) {
  67. data[2] += data[3] * tz;
  68. return *this;
  69. }
  70. Matrix& 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. Matrix& Matrix::rotate(float degrees, int a, int b) {
  78. degrees *= (M_PI / 180.0f);
  79. float sin = 0.0f;
  80. float cos = 0.0f;
  81. sincosf(degrees, &sin, &cos);
  82. Vector4 v = data[a];
  83. data[a] = cos * data[a] - sin * data[b];
  84. data[b] = sin * v + cos * data[b];
  85. return *this;
  86. }
  87. Matrix& Matrix::rotateX(float degrees) {
  88. return rotate(degrees, 1, 2);
  89. }
  90. Matrix& Matrix::rotateY(float degrees) {
  91. return rotate(-degrees, 0, 2);
  92. }
  93. Matrix& Matrix::rotateZ(float degrees) {
  94. return rotate(degrees, 0, 1);
  95. }
  96. Matrix& Matrix::rotate(const Quaternion& q) {
  97. Vector3 a = q * Vector3(data[0][0], data[1][0], data[2][0]);
  98. Vector3 b = q * Vector3(data[0][1], data[1][1], data[2][1]);
  99. Vector3 c = q * Vector3(data[0][2], data[1][2], data[2][2]);
  100. Vector3 d = q * Vector3(data[0][3], data[1][3], data[2][3]);
  101. set(0, Vector4(a[0], b[0], c[0], d[0]));
  102. set(1, Vector4(a[1], b[1], c[1], d[1]));
  103. set(2, Vector4(a[2], b[2], c[2], d[2]));
  104. return *this;
  105. }