Matrix.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. #include "Matrix.h"
  2. namespace gameplay
  3. {
  4. Matrix::Matrix(void)
  5. {
  6. setIdentity(m);
  7. }
  8. Matrix::Matrix(float m0, float m1, float m2, float m3,
  9. float m4, float m5, float m6, float m7,
  10. float m8, float m9, float m10, float m11,
  11. float m12, float m13, float m14, float m15)
  12. {
  13. m[0] = m0;
  14. m[1] = m1;
  15. m[2] = m2;
  16. m[3] = m3;
  17. m[4] = m4;
  18. m[5] = m5;
  19. m[6] = m6;
  20. m[7] = m7;
  21. m[8] = m8;
  22. m[9] = m9;
  23. m[10] = m10;
  24. m[11] = m11;
  25. m[12] = m12;
  26. m[13] = m13;
  27. m[14] = m14;
  28. m[15] = m15;
  29. }
  30. Matrix::~Matrix(void)
  31. {
  32. }
  33. void Matrix::setIdentity(float* matrix)
  34. {
  35. memcpy(matrix, MATRIX4F_IDENTITY, MATRIX4F_SIZE);
  36. }
  37. void Matrix::createRotation(float x, float y, float z, float angle, float* dst)
  38. {
  39. // Make sure the input axis is normalized
  40. float n = x*x + y*y + z*z;
  41. if (n != 1.0f)
  42. {
  43. // Not normalized
  44. n = sqrtf(n);
  45. if (n > 0.000001f) // prevent divide too close to zero
  46. {
  47. n = 1.0f / n;
  48. x *= n;
  49. y *= n;
  50. z *= n;
  51. }
  52. }
  53. float c = cos(angle);
  54. float s = sin(angle);
  55. float t = 1.0f - c;
  56. float tx = t * x;
  57. float ty = t * y;
  58. float tz = t * z;
  59. float txy = tx * y;
  60. float txz = tx * z;
  61. float tyz = ty * z;
  62. float sx = s * x;
  63. float sy = s * y;
  64. float sz = s * z;
  65. dst[0] = c + tx*x;
  66. dst[1] = txy + sz;
  67. dst[2] = txz - sy;
  68. dst[3] = 0.0f;
  69. dst[4] = txy - sz;
  70. dst[5] = c + ty*y;
  71. dst[6] = tyz + sx;
  72. dst[7] = 0.0f;
  73. dst[8] = txz + sy;
  74. dst[9] = tyz - sx;
  75. dst[10] = c + tz*z;
  76. dst[11] = 0.0f;
  77. dst[12] = 0.0f;
  78. dst[13] = 0.0f;
  79. dst[14] = 0.0f;
  80. dst[15] = 1.0f;
  81. }
  82. void Matrix::createRotationX(float angle, float* dst)
  83. {
  84. setIdentity(dst);
  85. float c = cos(angle);
  86. float s = sin(angle);
  87. dst[5] = c;
  88. dst[6] = s;
  89. dst[9] = -s;
  90. dst[10] = c;
  91. }
  92. void Matrix::createRotationY(float angle, float* dst)
  93. {
  94. setIdentity(dst);
  95. float c = cos(angle);
  96. float s = sin(angle);
  97. dst[0] = c;
  98. dst[2] = -s;
  99. dst[8] = s;
  100. dst[10] = c;
  101. }
  102. void Matrix::createRotationZ(float angle, float* dst)
  103. {
  104. setIdentity(dst);
  105. float c = cos(angle);
  106. float s = sin(angle);
  107. dst[0] = c;
  108. dst[1] = s;
  109. dst[4] = -s;
  110. dst[5] = c;
  111. }
  112. void Matrix::createTranslation(float x, float y, float z, float* dst)
  113. {
  114. setIdentity(dst);
  115. dst[12] = x;
  116. dst[13] = y;
  117. dst[14] = z;
  118. }
  119. void Matrix::createScale(float x, float y, float z, float* dst)
  120. {
  121. setIdentity(dst);
  122. dst[0] = x;
  123. dst[5] = y;
  124. dst[10] = z;
  125. }
  126. float* Matrix::getArray()
  127. {
  128. return m;
  129. }
  130. void Matrix::translate(float x, float y, float z)
  131. {
  132. float t[16];
  133. createTranslation(x, y, z, t);
  134. multiply(m, t, m);
  135. }
  136. void Matrix::scale(float x, float y, float z)
  137. {
  138. float s[16];
  139. createScale(x, y, z, s);
  140. multiply(m, s, m);
  141. }
  142. void Matrix::rotate(float x, float y, float z, float angle)
  143. {
  144. float r[16];
  145. createRotation(x, y, z, angle, r);
  146. multiply(m, r, m);
  147. }
  148. void Matrix::rotateX(float angle)
  149. {
  150. float r[16];
  151. createRotationX(angle, r);
  152. multiply(m, r, m);
  153. }
  154. void Matrix::rotateY(float angle)
  155. {
  156. float r[16];
  157. createRotationY(angle, r);
  158. multiply(m, r, m);
  159. }
  160. void Matrix::rotateZ(float angle)
  161. {
  162. float r[16];
  163. createRotationZ(angle, r);
  164. multiply(m, r, m);
  165. }
  166. void Matrix::multiply(const float* m1, const float* m2, float* dst)
  167. {
  168. // Support the case where m1 or m2 is the same array as dst.
  169. float product[16];
  170. product[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  171. product[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  172. product[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  173. product[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  174. product[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  175. product[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  176. product[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  177. product[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  178. product[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  179. product[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  180. product[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  181. product[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  182. product[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  183. product[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  184. product[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  185. product[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  186. memcpy(dst, product, MATRIX4F_SIZE);
  187. }
  188. bool Matrix::decompose(Vector3* scale, Quaternion* rotation, Vector3* translation) const
  189. {
  190. if (translation)
  191. {
  192. // Extract the translation
  193. translation->x = m[12];
  194. translation->y = m[13];
  195. translation->z = m[14];
  196. }
  197. // nothing left to do
  198. if (scale == NULL && rotation == NULL)
  199. {
  200. return true;
  201. }
  202. // Extract the scale.
  203. // This is simply the length of each axis (row/column) in the matrix.
  204. Vector3 xaxis(m[0], m[1], m[2]);
  205. float scaleX = xaxis.length();
  206. Vector3 yaxis(m[4], m[5], m[6]);
  207. float scaleY = yaxis.length();
  208. Vector3 zaxis(m[8], m[9], m[10]);
  209. float scaleZ = zaxis.length();
  210. // Determine if we have a negative scale (true if determinant is less than zero).
  211. // In this case, we simply negate a single axis of the scale.
  212. float det = determinant();
  213. if (det < 0)
  214. {
  215. scaleZ = -scaleZ;
  216. }
  217. if (scale)
  218. {
  219. scale->x = scaleX;
  220. scale->y = scaleY;
  221. scale->z = scaleZ;
  222. }
  223. // nothing left to do
  224. if (rotation == NULL)
  225. return true;
  226. // scale too close to zero, can't decompose rotation
  227. if (scaleX < MATH_TOLERANCE || scaleY < MATH_TOLERANCE || fabs(scaleZ) < MATH_TOLERANCE)
  228. return false;
  229. float rn;
  230. // Factor the scale out of the matrix axes
  231. rn = 1.0f / scaleX;
  232. xaxis.x *= rn;
  233. xaxis.y *= rn;
  234. xaxis.z *= rn;
  235. rn = 1.0f / scaleY;
  236. yaxis.x *= rn;
  237. yaxis.y *= rn;
  238. yaxis.z *= rn;
  239. rn = 1.0f / scaleZ;
  240. zaxis.x *= rn;
  241. zaxis.y *= rn;
  242. zaxis.z *= rn;
  243. // Now calculate the rotation from the resulting matrix (axes)
  244. float trace = xaxis.x + yaxis.y + zaxis.z + 1.0f;
  245. if (trace > MATH_TOLERANCE)
  246. {
  247. float s = 0.5f / sqrt(trace);
  248. rotation->w = 0.25f / s;
  249. rotation->x = ( yaxis.z - zaxis.y ) * s;
  250. rotation->y = ( zaxis.x - xaxis.z ) * s;
  251. rotation->z = ( xaxis.y - yaxis.x ) * s;
  252. }
  253. else
  254. {
  255. if (xaxis.x > yaxis.y && xaxis.x > zaxis.z)
  256. {
  257. float s = 2.0f * sqrt(1.0f + xaxis.x - yaxis.y - zaxis.z);
  258. rotation->w = (yaxis.z - zaxis.y ) / s;
  259. rotation->x = 0.25f * s;
  260. rotation->y = (yaxis.x + xaxis.y ) / s;
  261. rotation->z = (zaxis.x + xaxis.z ) / s;
  262. }
  263. else if (yaxis.y > zaxis.z)
  264. {
  265. float s = 2.0f * sqrt(1.0f + yaxis.y - xaxis.x - zaxis.z);
  266. rotation->w = (zaxis.x - xaxis.z ) / s;
  267. rotation->x = (yaxis.x + xaxis.y ) / s;
  268. rotation->y = 0.25f * s;
  269. rotation->z = (zaxis.y + yaxis.z ) / s;
  270. }
  271. else
  272. {
  273. float s = 2.0f * sqrt(1.0f + zaxis.z - xaxis.x - yaxis.y );
  274. rotation->w = (xaxis.y - yaxis.x ) / s;
  275. rotation->x = (zaxis.x + xaxis.z ) / s;
  276. rotation->y = (zaxis.y + yaxis.z ) / s;
  277. rotation->z = 0.25f * s;
  278. }
  279. }
  280. return true;
  281. }
  282. float Matrix::determinant() const
  283. {
  284. float a0 = m[0] * m[5] - m[1] * m[4];
  285. float a1 = m[0] * m[6] - m[2] * m[4];
  286. float a2 = m[0] * m[7] - m[3] * m[4];
  287. float a3 = m[1] * m[6] - m[2] * m[5];
  288. float a4 = m[1] * m[7] - m[3] * m[5];
  289. float a5 = m[2] * m[7] - m[3] * m[6];
  290. float b0 = m[8] * m[13] - m[9] * m[12];
  291. float b1 = m[8] * m[14] - m[10] * m[12];
  292. float b2 = m[8] * m[15] - m[11] * m[12];
  293. float b3 = m[9] * m[14] - m[10] * m[13];
  294. float b4 = m[9] * m[15] - m[11] * m[13];
  295. float b5 = m[10] * m[15] - m[11] * m[14];
  296. // Calculate the determinant
  297. return (a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0);
  298. }
  299. }