Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
8.8 KiB

5 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtx/gtx_simd_mat4.cpp
  28. /// @date 2010-09-16 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/glm.hpp>
  32. #include <glm/gtc/matrix_transform.hpp>
  33. #include <glm/gtc/quaternion.hpp>
  34. #include <glm/gtc/random.hpp>
  35. #include <glm/gtx/simd_vec4.hpp>
  36. #include <glm/gtx/simd_mat4.hpp>
  37. #include <cstdio>
  38. #include <ctime>
  39. #include <vector>
  40. #if(GLM_ARCH != GLM_ARCH_PURE)
  41. std::vector<float> test_detA(std::vector<glm::mat4> const & Data)
  42. {
  43. std::vector<float> Test(Data.size());
  44. std::clock_t TimeStart = clock();
  45. for(std::size_t i = 0; i < Test.size() - 1; ++i)
  46. Test[i] = glm::determinant(Data[i]);
  47. std::clock_t TimeEnd = clock();
  48. printf("Det A: %ld\n", TimeEnd - TimeStart);
  49. return Test;
  50. }
  51. std::vector<float> test_detB(std::vector<glm::mat4> const & Data)
  52. {
  53. std::vector<float> Test(Data.size());
  54. std::clock_t TimeStart = clock();
  55. for(std::size_t i = 0; i < Test.size() - 1; ++i)
  56. {
  57. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  58. glm::simdMat4 m(Data[i]);
  59. glm::simdVec4 d(glm::detail::sse_slow_det_ps((__m128 const * const)&m));
  60. glm::vec4 v;//(d);
  61. Test[i] = v.x;
  62. }
  63. std::clock_t TimeEnd = clock();
  64. printf("Det B: %ld\n", TimeEnd - TimeStart);
  65. return Test;
  66. }
  67. std::vector<float> test_detC(std::vector<glm::mat4> const & Data)
  68. {
  69. std::vector<float> Test(Data.size());
  70. std::clock_t TimeStart = clock();
  71. for(std::size_t i = 0; i < Test.size() - 1; ++i)
  72. {
  73. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  74. glm::simdMat4 m(Data[i]);
  75. glm::simdVec4 d(glm::detail::sse_det_ps((__m128 const * const)&m));
  76. glm::vec4 v;//(d);
  77. Test[i] = v.x;
  78. }
  79. std::clock_t TimeEnd = clock();
  80. printf("Det C: %ld\n", TimeEnd - TimeStart);
  81. return Test;
  82. }
  83. std::vector<float> test_detD(std::vector<glm::mat4> const & Data)
  84. {
  85. std::vector<float> Test(Data.size());
  86. std::clock_t TimeStart = clock();
  87. for(std::size_t i = 0; i < Test.size() - 1; ++i)
  88. {
  89. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  90. glm::simdMat4 m(Data[i]);
  91. glm::simdVec4 d(glm::detail::sse_detd_ps((__m128 const * const)&m));
  92. glm::vec4 v;//(d);
  93. Test[i] = v.x;
  94. }
  95. std::clock_t TimeEnd = clock();
  96. printf("Det D: %ld\n", TimeEnd - TimeStart);
  97. return Test;
  98. }
  99. void test_invA(std::vector<glm::mat4> const & Data, std::vector<glm::mat4> & Out)
  100. {
  101. //std::vector<float> Test(Data.size());
  102. Out.resize(Data.size());
  103. std::clock_t TimeStart = clock();
  104. for(std::size_t i = 0; i < Out.size() - 1; ++i)
  105. {
  106. Out[i] = glm::inverse(Data[i]);
  107. }
  108. std::clock_t TimeEnd = clock();
  109. printf("Inv A: %ld\n", TimeEnd - TimeStart);
  110. }
  111. void test_invC(std::vector<glm::mat4> const & Data, std::vector<glm::mat4> & Out)
  112. {
  113. //std::vector<float> Test(Data.size());
  114. Out.resize(Data.size());
  115. std::clock_t TimeStart = clock();
  116. for(std::size_t i = 0; i < Out.size() - 1; ++i)
  117. {
  118. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  119. glm::simdMat4 m(Data[i]);
  120. glm::simdMat4 o;
  121. glm::detail::sse_inverse_fast_ps((__m128 const * const)&m, (__m128 *)&o);
  122. Out[i] = *(glm::mat4*)&o;
  123. }
  124. std::clock_t TimeEnd = clock();
  125. printf("Inv C: %ld\n", TimeEnd - TimeStart);
  126. }
  127. void test_invD(std::vector<glm::mat4> const & Data, std::vector<glm::mat4> & Out)
  128. {
  129. //std::vector<float> Test(Data.size());
  130. Out.resize(Data.size());
  131. std::clock_t TimeStart = clock();
  132. for(std::size_t i = 0; i < Out.size() - 1; ++i)
  133. {
  134. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  135. glm::simdMat4 m(Data[i]);
  136. glm::simdMat4 o;
  137. glm::detail::sse_inverse_ps((__m128 const * const)&m, (__m128 *)&o);
  138. Out[i] = *(glm::mat4*)&o;
  139. }
  140. std::clock_t TimeEnd = clock();
  141. printf("Inv D: %ld\n", TimeEnd - TimeStart);
  142. }
  143. void test_mulA(std::vector<glm::mat4> const & Data, std::vector<glm::mat4> & Out)
  144. {
  145. //std::vector<float> Test(Data.size());
  146. Out.resize(Data.size());
  147. std::clock_t TimeStart = clock();
  148. for(std::size_t i = 0; i < Out.size() - 1; ++i)
  149. {
  150. Out[i] = Data[i] * Data[i];
  151. }
  152. std::clock_t TimeEnd = clock();
  153. printf("Mul A: %ld\n", TimeEnd - TimeStart);
  154. }
  155. void test_mulD(std::vector<glm::mat4> const & Data, std::vector<glm::mat4> & Out)
  156. {
  157. //std::vector<float> Test(Data.size());
  158. Out.resize(Data.size());
  159. std::clock_t TimeStart = clock();
  160. for(std::size_t i = 0; i < Out.size() - 1; ++i)
  161. {
  162. _mm_prefetch((char*)&Data[i + 1], _MM_HINT_T0);
  163. glm::simdMat4 m(Data[i]);
  164. glm::simdMat4 o;
  165. glm::detail::sse_mul_ps((__m128 const * const)&m, (__m128 const * const)&m, (__m128*)&o);
  166. Out[i] = *(glm::mat4*)&o;
  167. }
  168. std::clock_t TimeEnd = clock();
  169. printf("Mul D: %ld\n", TimeEnd - TimeStart);
  170. }
  171. int test_compute_glm()
  172. {
  173. return 0;
  174. }
  175. int test_compute_gtx()
  176. {
  177. std::vector<glm::vec4> Output(1000000);
  178. std::clock_t TimeStart = clock();
  179. for(std::size_t k = 0; k < Output.size(); ++k)
  180. {
  181. float i = float(k) / 1000.f + 0.001f;
  182. glm::vec3 A = glm::normalize(glm::vec3(i));
  183. glm::vec3 B = glm::cross(A, glm::normalize(glm::vec3(1, 1, 2)));
  184. glm::mat4 C = glm::rotate(glm::mat4(1.0f), i, B);
  185. glm::mat4 D = glm::scale(C, glm::vec3(0.8f, 1.0f, 1.2f));
  186. glm::mat4 E = glm::translate(D, glm::vec3(1.4f, 1.2f, 1.1f));
  187. glm::mat4 F = glm::perspective(i, 1.5f, 0.1f, 1000.f);
  188. glm::mat4 G = glm::inverse(F * E);
  189. glm::vec3 H = glm::unProject(glm::vec3(i), G, F, E[3]);
  190. glm::vec3 I = glm::any(glm::isnan(glm::project(H, G, F, E[3]))) ? glm::vec3(2) : glm::vec3(1);
  191. glm::mat4 J = glm::lookAt(glm::normalize(glm::max(B, glm::vec3(0.001f))), H, I);
  192. glm::mat4 K = glm::transpose(J);
  193. glm::quat L = glm::normalize(glm::quat_cast(K));
  194. glm::vec4 M = L * glm::smoothstep(K[3], J[3], glm::vec4(i));
  195. glm::mat4 N = glm::mat4(glm::normalize(glm::max(M, glm::vec4(0.001f))), K[3], J[3], glm::vec4(i));
  196. glm::mat4 O = N * glm::inverse(N);
  197. glm::vec4 P = O * glm::reflect(N[3], glm::vec4(A, 1.0f));
  198. glm::vec4 Q = glm::vec4(glm::dot(M, P));
  199. glm::vec4 R = glm::quat(Q.w, glm::vec3(Q)) * P;
  200. Output[k] = R;
  201. }
  202. std::clock_t TimeEnd = clock();
  203. printf("test_compute_gtx: %ld\n", TimeEnd - TimeStart);
  204. return 0;
  205. }
  206. int main()
  207. {
  208. int Error = 0;
  209. std::vector<glm::mat4> Data(64 * 64 * 1);
  210. for(std::size_t i = 0; i < Data.size(); ++i)
  211. Data[i] = glm::mat4(
  212. glm::vec4(glm::linearRand(glm::vec4(-2.0f), glm::vec4(2.0f))),
  213. glm::vec4(glm::linearRand(glm::vec4(-2.0f), glm::vec4(2.0f))),
  214. glm::vec4(glm::linearRand(glm::vec4(-2.0f), glm::vec4(2.0f))),
  215. glm::vec4(glm::linearRand(glm::vec4(-2.0f), glm::vec4(2.0f))));
  216. {
  217. std::vector<glm::mat4> TestInvA;
  218. test_invA(Data, TestInvA);
  219. }
  220. {
  221. std::vector<glm::mat4> TestInvC;
  222. test_invC(Data, TestInvC);
  223. }
  224. {
  225. std::vector<glm::mat4> TestInvD;
  226. test_invD(Data, TestInvD);
  227. }
  228. {
  229. std::vector<glm::mat4> TestA;
  230. test_mulA(Data, TestA);
  231. }
  232. {
  233. std::vector<glm::mat4> TestD;
  234. test_mulD(Data, TestD);
  235. }
  236. {
  237. std::vector<float> TestDetA = test_detA(Data);
  238. std::vector<float> TestDetB = test_detB(Data);
  239. std::vector<float> TestDetD = test_detD(Data);
  240. std::vector<float> TestDetC = test_detC(Data);
  241. for(std::size_t i = 0; i < TestDetA.size(); ++i)
  242. if(TestDetA[i] != TestDetB[i] && TestDetC[i] != TestDetB[i] && TestDetC[i] != TestDetD[i])
  243. return 1;
  244. }
  245. // shuffle test
  246. glm::simdVec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  247. glm::simdVec4 B(5.0f, 6.0f, 7.0f, 8.0f);
  248. //__m128 C = _mm_shuffle_ps(A.Data, B.Data, _MM_SHUFFLE(1, 0, 1, 0));
  249. Error += test_compute_glm();
  250. Error += test_compute_gtx();
  251. float Det = glm::determinant(glm::simdMat4(1.0));
  252. Error += Det == 1.0f ? 0 : 1;
  253. glm::simdMat4 D = glm::matrixCompMult(glm::simdMat4(1.0), glm::simdMat4(1.0));
  254. return Error;
  255. }
  256. #else
  257. int main()
  258. {
  259. int Error = 0;
  260. return Error;
  261. }
  262. #endif//(GLM_ARCH != GLM_ARCH_PURE)