Matrix.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) 2006-2012 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "Matrix.h"
  21. // STD
  22. #include <cstring> // memcpy
  23. #include <cmath>
  24. namespace love
  25. {
  26. // | e0 e4 e8 e12 |
  27. // | e1 e5 e9 e13 |
  28. // | e2 e6 e10 e14 |
  29. // | e3 e7 e11 e15 |
  30. Matrix::Matrix()
  31. {
  32. setIdentity();
  33. }
  34. Matrix::~Matrix()
  35. {
  36. }
  37. // | e0 e4 e8 e12 |
  38. // | e1 e5 e9 e13 |
  39. // | e2 e6 e10 e14 |
  40. // | e3 e7 e11 e15 |
  41. // | e0 e4 e8 e12 |
  42. // | e1 e5 e9 e13 |
  43. // | e2 e6 e10 e14 |
  44. // | e3 e7 e11 e15 |
  45. Matrix Matrix::operator * (const Matrix &m) const
  46. {
  47. Matrix t;
  48. t.e[0] = (e[0]*m.e[0]) + (e[4]*m.e[1]) + (e[8]*m.e[2]) + (e[12]*m.e[3]);
  49. t.e[4] = (e[0]*m.e[4]) + (e[4]*m.e[5]) + (e[8]*m.e[6]) + (e[12]*m.e[7]);
  50. t.e[8] = (e[0]*m.e[8]) + (e[4]*m.e[9]) + (e[8]*m.e[10]) + (e[12]*m.e[11]);
  51. t.e[12] = (e[0]*m.e[12]) + (e[4]*m.e[13]) + (e[8]*m.e[14]) + (e[12]*m.e[15]);
  52. t.e[1] = (e[1]*m.e[0]) + (e[5]*m.e[1]) + (e[9]*m.e[2]) + (e[13]*m.e[3]);
  53. t.e[5] = (e[1]*m.e[4]) + (e[5]*m.e[5]) + (e[9]*m.e[6]) + (e[13]*m.e[7]);
  54. t.e[9] = (e[1]*m.e[8]) + (e[5]*m.e[9]) + (e[9]*m.e[10]) + (e[13]*m.e[11]);
  55. t.e[13] = (e[1]*m.e[12]) + (e[5]*m.e[13]) + (e[9]*m.e[14]) + (e[13]*m.e[15]);
  56. t.e[2] = (e[2]*m.e[0]) + (e[6]*m.e[1]) + (e[10]*m.e[2]) + (e[14]*m.e[3]);
  57. t.e[6] = (e[2]*m.e[4]) + (e[6]*m.e[5]) + (e[10]*m.e[6]) + (e[14]*m.e[7]);
  58. t.e[10] = (e[2]*m.e[8]) + (e[6]*m.e[9]) + (e[10]*m.e[10]) + (e[14]*m.e[11]);
  59. t.e[14] = (e[2]*m.e[12]) + (e[6]*m.e[13]) + (e[10]*m.e[14]) + (e[14]*m.e[15]);
  60. t.e[3] = (e[3]*m.e[0]) + (e[7]*m.e[1]) + (e[11]*m.e[2]) + (e[15]*m.e[3]);
  61. t.e[7] = (e[3]*m.e[4]) + (e[7]*m.e[5]) + (e[11]*m.e[6]) + (e[15]*m.e[7]);
  62. t.e[11] = (e[3]*m.e[8]) + (e[7]*m.e[9]) + (e[11]*m.e[10]) + (e[15]*m.e[11]);
  63. t.e[15] = (e[3]*m.e[12]) + (e[7]*m.e[13]) + (e[11]*m.e[14]) + (e[15]*m.e[15]);
  64. return t;
  65. }
  66. void Matrix::operator *= (const Matrix &m)
  67. {
  68. Matrix t = (*this) * m;
  69. memcpy((void *)this->e, (void *)t.e, sizeof(float)*16);
  70. }
  71. const float *Matrix::getElements() const
  72. {
  73. return e;
  74. }
  75. void Matrix::setIdentity()
  76. {
  77. memset(e, 0, sizeof(float)*16);
  78. e[0] = e[5] = e[10] = e[15] = 1;
  79. }
  80. void Matrix::setTranslation(float x, float y)
  81. {
  82. setIdentity();
  83. e[12] = x;
  84. e[13] = y;
  85. }
  86. void Matrix::setRotation(float rad)
  87. {
  88. setIdentity();
  89. float c = cos(rad), s = sin(rad);
  90. e[0] = c;
  91. e[4] = -s;
  92. e[1] = s;
  93. e[5] = c;
  94. }
  95. void Matrix::setScale(float sx, float sy)
  96. {
  97. setIdentity();
  98. e[0] = sx;
  99. e[5] = sy;
  100. }
  101. void Matrix::setShear(float kx, float ky)
  102. {
  103. setIdentity();
  104. e[1] = ky;
  105. e[4] = kx;
  106. }
  107. void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  108. {
  109. memset(e, 0, sizeof(float)*16); // zero out matrix
  110. float c = cos(angle), s = sin(angle);
  111. // matrix multiplication carried out on paper:
  112. // |1 x| |c -s | |sx | | 1 ky | |1 -ox|
  113. // | 1 y| |s c | | sy | |kx 1 | | 1 -oy|
  114. // | 1 | | 1 | | 1 | | 1 | | 1 |
  115. // | 1| | 1| | 1| | 1| | 1 |
  116. // move rotate scale skew origin
  117. e[10] = e[15] = 1.0f;
  118. e[0] = c * sx - ky * s * sy; // = a
  119. e[1] = s * sx + ky * c * sy; // = b
  120. e[4] = kx * c * sx - s * sy; // = c
  121. e[5] = kx * s * sx + c * sy; // = d
  122. e[12] = x - ox * e[0] - oy * e[4];
  123. e[13] = y - ox * e[1] - oy * e[5];
  124. }
  125. void Matrix::translate(float x, float y)
  126. {
  127. Matrix t;
  128. t.setTranslation(x, y);
  129. this->operator *=(t);
  130. }
  131. void Matrix::rotate(float rad)
  132. {
  133. Matrix t;
  134. t.setRotation(rad);
  135. this->operator *=(t);
  136. }
  137. void Matrix::scale(float sx, float sy)
  138. {
  139. Matrix t;
  140. t.setScale(sx, sy);
  141. this->operator *=(t);
  142. }
  143. void Matrix::shear(float kx, float ky)
  144. {
  145. Matrix t;
  146. t.setShear(kx,ky);
  147. this->operator *=(t);
  148. }
  149. // | x |
  150. // | y |
  151. // | 0 |
  152. // | 1 |
  153. // | e0 e4 e8 e12 |
  154. // | e1 e5 e9 e13 |
  155. // | e2 e6 e10 e14 |
  156. // | e3 e7 e11 e15 |
  157. void Matrix::transform(vertex *dst, const vertex *src, int size) const
  158. {
  159. for (int i = 0; i<size; i++)
  160. {
  161. // Store in temp variables in case src = dst
  162. float x = (e[0]*src[i].x) + (e[4]*src[i].y) + (0) + (e[12]);
  163. float y = (e[1]*src[i].x) + (e[5]*src[i].y) + (0) + (e[13]);
  164. dst[i].x = x;
  165. dst[i].y = y;
  166. }
  167. }
  168. } // love