Matrix.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright (c) 2006-2015 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(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  35. {
  36. setTransformation(x, y, angle, sx, sy, ox, oy, kx, ky);
  37. }
  38. Matrix::~Matrix()
  39. {
  40. }
  41. // | e0 e4 e8 e12 |
  42. // | e1 e5 e9 e13 |
  43. // | e2 e6 e10 e14 |
  44. // | e3 e7 e11 e15 |
  45. // | e0 e4 e8 e12 |
  46. // | e1 e5 e9 e13 |
  47. // | e2 e6 e10 e14 |
  48. // | e3 e7 e11 e15 |
  49. Matrix Matrix::operator * (const Matrix &m) const
  50. {
  51. Matrix t;
  52. t.e[0] = (e[0]*m.e[0]) + (e[4]*m.e[1]) + (e[8]*m.e[2]) + (e[12]*m.e[3]);
  53. t.e[4] = (e[0]*m.e[4]) + (e[4]*m.e[5]) + (e[8]*m.e[6]) + (e[12]*m.e[7]);
  54. t.e[8] = (e[0]*m.e[8]) + (e[4]*m.e[9]) + (e[8]*m.e[10]) + (e[12]*m.e[11]);
  55. t.e[12] = (e[0]*m.e[12]) + (e[4]*m.e[13]) + (e[8]*m.e[14]) + (e[12]*m.e[15]);
  56. t.e[1] = (e[1]*m.e[0]) + (e[5]*m.e[1]) + (e[9]*m.e[2]) + (e[13]*m.e[3]);
  57. t.e[5] = (e[1]*m.e[4]) + (e[5]*m.e[5]) + (e[9]*m.e[6]) + (e[13]*m.e[7]);
  58. t.e[9] = (e[1]*m.e[8]) + (e[5]*m.e[9]) + (e[9]*m.e[10]) + (e[13]*m.e[11]);
  59. t.e[13] = (e[1]*m.e[12]) + (e[5]*m.e[13]) + (e[9]*m.e[14]) + (e[13]*m.e[15]);
  60. t.e[2] = (e[2]*m.e[0]) + (e[6]*m.e[1]) + (e[10]*m.e[2]) + (e[14]*m.e[3]);
  61. t.e[6] = (e[2]*m.e[4]) + (e[6]*m.e[5]) + (e[10]*m.e[6]) + (e[14]*m.e[7]);
  62. t.e[10] = (e[2]*m.e[8]) + (e[6]*m.e[9]) + (e[10]*m.e[10]) + (e[14]*m.e[11]);
  63. t.e[14] = (e[2]*m.e[12]) + (e[6]*m.e[13]) + (e[10]*m.e[14]) + (e[14]*m.e[15]);
  64. t.e[3] = (e[3]*m.e[0]) + (e[7]*m.e[1]) + (e[11]*m.e[2]) + (e[15]*m.e[3]);
  65. t.e[7] = (e[3]*m.e[4]) + (e[7]*m.e[5]) + (e[11]*m.e[6]) + (e[15]*m.e[7]);
  66. t.e[11] = (e[3]*m.e[8]) + (e[7]*m.e[9]) + (e[11]*m.e[10]) + (e[15]*m.e[11]);
  67. t.e[15] = (e[3]*m.e[12]) + (e[7]*m.e[13]) + (e[11]*m.e[14]) + (e[15]*m.e[15]);
  68. return t;
  69. }
  70. void Matrix::operator *= (const Matrix &m)
  71. {
  72. Matrix t = (*this) * m;
  73. memcpy((void *)this->e, (void *)t.e, sizeof(float)*16);
  74. }
  75. const float *Matrix::getElements() const
  76. {
  77. return e;
  78. }
  79. void Matrix::setIdentity()
  80. {
  81. memset(e, 0, sizeof(float)*16);
  82. e[0] = e[5] = e[10] = e[15] = 1;
  83. }
  84. void Matrix::setTranslation(float x, float y)
  85. {
  86. setIdentity();
  87. e[12] = x;
  88. e[13] = y;
  89. }
  90. void Matrix::setRotation(float rad)
  91. {
  92. setIdentity();
  93. float c = cosf(rad), s = sinf(rad);
  94. e[0] = c;
  95. e[4] = -s;
  96. e[1] = s;
  97. e[5] = c;
  98. }
  99. void Matrix::setScale(float sx, float sy)
  100. {
  101. setIdentity();
  102. e[0] = sx;
  103. e[5] = sy;
  104. }
  105. void Matrix::setShear(float kx, float ky)
  106. {
  107. setIdentity();
  108. e[1] = ky;
  109. e[4] = kx;
  110. }
  111. void Matrix::setTransformation(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky)
  112. {
  113. memset(e, 0, sizeof(float)*16); // zero out matrix
  114. float c = cosf(angle), s = sinf(angle);
  115. // matrix multiplication carried out on paper:
  116. // |1 x| |c -s | |sx | | 1 ky | |1 -ox|
  117. // | 1 y| |s c | | sy | |kx 1 | | 1 -oy|
  118. // | 1 | | 1 | | 1 | | 1 | | 1 |
  119. // | 1| | 1| | 1| | 1| | 1 |
  120. // move rotate scale skew origin
  121. e[10] = e[15] = 1.0f;
  122. e[0] = c * sx - ky * s * sy; // = a
  123. e[1] = s * sx + ky * c * sy; // = b
  124. e[4] = kx * c * sx - s * sy; // = c
  125. e[5] = kx * s * sx + c * sy; // = d
  126. e[12] = x - ox * e[0] - oy * e[4];
  127. e[13] = y - ox * e[1] - oy * e[5];
  128. }
  129. void Matrix::translate(float x, float y)
  130. {
  131. Matrix t;
  132. t.setTranslation(x, y);
  133. this->operator *=(t);
  134. }
  135. void Matrix::rotate(float rad)
  136. {
  137. Matrix t;
  138. t.setRotation(rad);
  139. this->operator *=(t);
  140. }
  141. void Matrix::scale(float sx, float sy)
  142. {
  143. Matrix t;
  144. t.setScale(sx, sy);
  145. this->operator *=(t);
  146. }
  147. void Matrix::shear(float kx, float ky)
  148. {
  149. Matrix t;
  150. t.setShear(kx,ky);
  151. this->operator *=(t);
  152. }
  153. // | x |
  154. // | y |
  155. // | 0 |
  156. // | 1 |
  157. // | e0 e4 e8 e12 |
  158. // | e1 e5 e9 e13 |
  159. // | e2 e6 e10 e14 |
  160. // | e3 e7 e11 e15 |
  161. void Matrix::transform(Vertex *dst, const Vertex *src, int size) const
  162. {
  163. for (int i = 0; i<size; i++)
  164. {
  165. // Store in temp variables in case src = dst
  166. float x = (e[0]*src[i].x) + (e[4]*src[i].y) + (0) + (e[12]);
  167. float y = (e[1]*src[i].x) + (e[5]*src[i].y) + (0) + (e[13]);
  168. dst[i].x = x;
  169. dst[i].y = y;
  170. }
  171. }
  172. Matrix Matrix::ortho(float left, float right, float bottom, float top)
  173. {
  174. Matrix m;
  175. m.e[0] = 2.0f / (right - left);
  176. m.e[5] = 2.0f / (top - bottom);
  177. m.e[10] = -1.0;
  178. m.e[12] = -(right + left) / (right - left);
  179. m.e[13] = -(top + bottom) / (top - bottom);
  180. return m;
  181. }
  182. } // love