MathUtil.inl 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /**
  2. Copyright 2013 BlackBerry Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. Original file from GamePlay3D: http://gameplay3d.org
  13. This file was modified to fit the cocos2d-x project
  14. */
  15. NS_CC_MATH_BEGIN
  16. class MathUtilC
  17. {
  18. public:
  19. inline static void addMatrix(const float* m, float scalar, float* dst);
  20. inline static void addMatrix(const float* m1, const float* m2, float* dst);
  21. inline static void subtractMatrix(const float* m1, const float* m2, float* dst);
  22. inline static void multiplyMatrix(const float* m, float scalar, float* dst);
  23. inline static void multiplyMatrix(const float* m1, const float* m2, float* dst);
  24. inline static void negateMatrix(const float* m, float* dst);
  25. inline static void transposeMatrix(const float* m, float* dst);
  26. inline static void transformVec4(const float* m, float x, float y, float z, float w, float* dst);
  27. inline static void transformVec4(const float* m, const float* v, float* dst);
  28. inline static void crossVec3(const float* v1, const float* v2, float* dst);
  29. };
  30. inline void MathUtilC::addMatrix(const float* m, float scalar, float* dst)
  31. {
  32. dst[0] = m[0] + scalar;
  33. dst[1] = m[1] + scalar;
  34. dst[2] = m[2] + scalar;
  35. dst[3] = m[3] + scalar;
  36. dst[4] = m[4] + scalar;
  37. dst[5] = m[5] + scalar;
  38. dst[6] = m[6] + scalar;
  39. dst[7] = m[7] + scalar;
  40. dst[8] = m[8] + scalar;
  41. dst[9] = m[9] + scalar;
  42. dst[10] = m[10] + scalar;
  43. dst[11] = m[11] + scalar;
  44. dst[12] = m[12] + scalar;
  45. dst[13] = m[13] + scalar;
  46. dst[14] = m[14] + scalar;
  47. dst[15] = m[15] + scalar;
  48. }
  49. inline void MathUtilC::addMatrix(const float* m1, const float* m2, float* dst)
  50. {
  51. dst[0] = m1[0] + m2[0];
  52. dst[1] = m1[1] + m2[1];
  53. dst[2] = m1[2] + m2[2];
  54. dst[3] = m1[3] + m2[3];
  55. dst[4] = m1[4] + m2[4];
  56. dst[5] = m1[5] + m2[5];
  57. dst[6] = m1[6] + m2[6];
  58. dst[7] = m1[7] + m2[7];
  59. dst[8] = m1[8] + m2[8];
  60. dst[9] = m1[9] + m2[9];
  61. dst[10] = m1[10] + m2[10];
  62. dst[11] = m1[11] + m2[11];
  63. dst[12] = m1[12] + m2[12];
  64. dst[13] = m1[13] + m2[13];
  65. dst[14] = m1[14] + m2[14];
  66. dst[15] = m1[15] + m2[15];
  67. }
  68. inline void MathUtilC::subtractMatrix(const float* m1, const float* m2, float* dst)
  69. {
  70. dst[0] = m1[0] - m2[0];
  71. dst[1] = m1[1] - m2[1];
  72. dst[2] = m1[2] - m2[2];
  73. dst[3] = m1[3] - m2[3];
  74. dst[4] = m1[4] - m2[4];
  75. dst[5] = m1[5] - m2[5];
  76. dst[6] = m1[6] - m2[6];
  77. dst[7] = m1[7] - m2[7];
  78. dst[8] = m1[8] - m2[8];
  79. dst[9] = m1[9] - m2[9];
  80. dst[10] = m1[10] - m2[10];
  81. dst[11] = m1[11] - m2[11];
  82. dst[12] = m1[12] - m2[12];
  83. dst[13] = m1[13] - m2[13];
  84. dst[14] = m1[14] - m2[14];
  85. dst[15] = m1[15] - m2[15];
  86. }
  87. inline void MathUtilC::multiplyMatrix(const float* m, float scalar, float* dst)
  88. {
  89. dst[0] = m[0] * scalar;
  90. dst[1] = m[1] * scalar;
  91. dst[2] = m[2] * scalar;
  92. dst[3] = m[3] * scalar;
  93. dst[4] = m[4] * scalar;
  94. dst[5] = m[5] * scalar;
  95. dst[6] = m[6] * scalar;
  96. dst[7] = m[7] * scalar;
  97. dst[8] = m[8] * scalar;
  98. dst[9] = m[9] * scalar;
  99. dst[10] = m[10] * scalar;
  100. dst[11] = m[11] * scalar;
  101. dst[12] = m[12] * scalar;
  102. dst[13] = m[13] * scalar;
  103. dst[14] = m[14] * scalar;
  104. dst[15] = m[15] * scalar;
  105. }
  106. inline void MathUtilC::multiplyMatrix(const float* m1, const float* m2, float* dst)
  107. {
  108. // Support the case where m1 or m2 is the same array as dst.
  109. float product[16];
  110. product[0] = m1[0] * m2[0] + m1[4] * m2[1] + m1[8] * m2[2] + m1[12] * m2[3];
  111. product[1] = m1[1] * m2[0] + m1[5] * m2[1] + m1[9] * m2[2] + m1[13] * m2[3];
  112. product[2] = m1[2] * m2[0] + m1[6] * m2[1] + m1[10] * m2[2] + m1[14] * m2[3];
  113. product[3] = m1[3] * m2[0] + m1[7] * m2[1] + m1[11] * m2[2] + m1[15] * m2[3];
  114. product[4] = m1[0] * m2[4] + m1[4] * m2[5] + m1[8] * m2[6] + m1[12] * m2[7];
  115. product[5] = m1[1] * m2[4] + m1[5] * m2[5] + m1[9] * m2[6] + m1[13] * m2[7];
  116. product[6] = m1[2] * m2[4] + m1[6] * m2[5] + m1[10] * m2[6] + m1[14] * m2[7];
  117. product[7] = m1[3] * m2[4] + m1[7] * m2[5] + m1[11] * m2[6] + m1[15] * m2[7];
  118. product[8] = m1[0] * m2[8] + m1[4] * m2[9] + m1[8] * m2[10] + m1[12] * m2[11];
  119. product[9] = m1[1] * m2[8] + m1[5] * m2[9] + m1[9] * m2[10] + m1[13] * m2[11];
  120. product[10] = m1[2] * m2[8] + m1[6] * m2[9] + m1[10] * m2[10] + m1[14] * m2[11];
  121. product[11] = m1[3] * m2[8] + m1[7] * m2[9] + m1[11] * m2[10] + m1[15] * m2[11];
  122. product[12] = m1[0] * m2[12] + m1[4] * m2[13] + m1[8] * m2[14] + m1[12] * m2[15];
  123. product[13] = m1[1] * m2[12] + m1[5] * m2[13] + m1[9] * m2[14] + m1[13] * m2[15];
  124. product[14] = m1[2] * m2[12] + m1[6] * m2[13] + m1[10] * m2[14] + m1[14] * m2[15];
  125. product[15] = m1[3] * m2[12] + m1[7] * m2[13] + m1[11] * m2[14] + m1[15] * m2[15];
  126. memcpy(dst, product, MATRIX_SIZE);
  127. }
  128. inline void MathUtilC::negateMatrix(const float* m, float* dst)
  129. {
  130. dst[0] = -m[0];
  131. dst[1] = -m[1];
  132. dst[2] = -m[2];
  133. dst[3] = -m[3];
  134. dst[4] = -m[4];
  135. dst[5] = -m[5];
  136. dst[6] = -m[6];
  137. dst[7] = -m[7];
  138. dst[8] = -m[8];
  139. dst[9] = -m[9];
  140. dst[10] = -m[10];
  141. dst[11] = -m[11];
  142. dst[12] = -m[12];
  143. dst[13] = -m[13];
  144. dst[14] = -m[14];
  145. dst[15] = -m[15];
  146. }
  147. inline void MathUtilC::transposeMatrix(const float* m, float* dst)
  148. {
  149. float t[16] = {
  150. m[0], m[4], m[8], m[12],
  151. m[1], m[5], m[9], m[13],
  152. m[2], m[6], m[10], m[14],
  153. m[3], m[7], m[11], m[15]
  154. };
  155. memcpy(dst, t, MATRIX_SIZE);
  156. }
  157. inline void MathUtilC::transformVec4(const float* m, float x, float y, float z, float w, float* dst)
  158. {
  159. dst[0] = x * m[0] + y * m[4] + z * m[8] + w * m[12];
  160. dst[1] = x * m[1] + y * m[5] + z * m[9] + w * m[13];
  161. dst[2] = x * m[2] + y * m[6] + z * m[10] + w * m[14];
  162. }
  163. inline void MathUtilC::transformVec4(const float* m, const float* v, float* dst)
  164. {
  165. // Handle case where v == dst.
  166. float x = v[0] * m[0] + v[1] * m[4] + v[2] * m[8] + v[3] * m[12];
  167. float y = v[0] * m[1] + v[1] * m[5] + v[2] * m[9] + v[3] * m[13];
  168. float z = v[0] * m[2] + v[1] * m[6] + v[2] * m[10] + v[3] * m[14];
  169. float w = v[0] * m[3] + v[1] * m[7] + v[2] * m[11] + v[3] * m[15];
  170. dst[0] = x;
  171. dst[1] = y;
  172. dst[2] = z;
  173. dst[3] = w;
  174. }
  175. inline void MathUtilC::crossVec3(const float* v1, const float* v2, float* dst)
  176. {
  177. float x = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  178. float y = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  179. float z = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  180. dst[0] = x;
  181. dst[1] = y;
  182. dst[2] = z;
  183. }
  184. NS_CC_MATH_END