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.

539 lines
22 KiB

5 years ago
  1. // Code sample from Filippo Ramaciotti
  2. #define GLM_ENABLE_EXPERIMENTAL
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <glm/gtx/matrix_cross_product.hpp>
  5. #include <glm/gtx/matrix_operation.hpp>
  6. #include <glm/gtc/epsilon.hpp>
  7. #include <glm/gtx/string_cast.hpp>
  8. #include <glm/gtx/euler_angles.hpp>
  9. #include <cstdio>
  10. #include <vector>
  11. #include <utility>
  12. namespace test_eulerAngleX
  13. {
  14. int test()
  15. {
  16. int Error = 0;
  17. float const Angle(glm::pi<float>() * 0.5f);
  18. glm::vec3 const X(1.0f, 0.0f, 0.0f);
  19. glm::vec4 const Y(0.0f, 1.0f, 0.0f, 1.0f);
  20. glm::vec4 const Y1 = glm::rotate(glm::mat4(1.0f), Angle, X) * Y;
  21. glm::vec4 const Y2 = glm::eulerAngleX(Angle) * Y;
  22. glm::vec4 const Y3 = glm::eulerAngleXY(Angle, 0.0f) * Y;
  23. glm::vec4 const Y4 = glm::eulerAngleYX(0.0f, Angle) * Y;
  24. glm::vec4 const Y5 = glm::eulerAngleXZ(Angle, 0.0f) * Y;
  25. glm::vec4 const Y6 = glm::eulerAngleZX(0.0f, Angle) * Y;
  26. glm::vec4 const Y7 = glm::eulerAngleYXZ(0.0f, Angle, 0.0f) * Y;
  27. Error += glm::all(glm::epsilonEqual(Y1, Y2, 0.00001f)) ? 0 : 1;
  28. Error += glm::all(glm::epsilonEqual(Y1, Y3, 0.00001f)) ? 0 : 1;
  29. Error += glm::all(glm::epsilonEqual(Y1, Y4, 0.00001f)) ? 0 : 1;
  30. Error += glm::all(glm::epsilonEqual(Y1, Y5, 0.00001f)) ? 0 : 1;
  31. Error += glm::all(glm::epsilonEqual(Y1, Y6, 0.00001f)) ? 0 : 1;
  32. Error += glm::all(glm::epsilonEqual(Y1, Y7, 0.00001f)) ? 0 : 1;
  33. glm::vec4 const Z(0.0f, 0.0f, 1.0f, 1.0f);
  34. glm::vec4 const Z1 = glm::rotate(glm::mat4(1.0f), Angle, X) * Z;
  35. glm::vec4 const Z2 = glm::eulerAngleX(Angle) * Z;
  36. glm::vec4 const Z3 = glm::eulerAngleXY(Angle, 0.0f) * Z;
  37. glm::vec4 const Z4 = glm::eulerAngleYX(0.0f, Angle) * Z;
  38. glm::vec4 const Z5 = glm::eulerAngleXZ(Angle, 0.0f) * Z;
  39. glm::vec4 const Z6 = glm::eulerAngleZX(0.0f, Angle) * Z;
  40. glm::vec4 const Z7 = glm::eulerAngleYXZ(0.0f, Angle, 0.0f) * Z;
  41. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  42. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  43. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  44. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  45. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  46. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  47. return Error;
  48. }
  49. }//namespace test_eulerAngleX
  50. namespace test_eulerAngleY
  51. {
  52. int test()
  53. {
  54. int Error = 0;
  55. float const Angle(glm::pi<float>() * 0.5f);
  56. glm::vec3 const Y(0.0f, 1.0f, 0.0f);
  57. glm::vec4 const X(1.0f, 0.0f, 0.0f, 1.0f);
  58. glm::vec4 const X1 = glm::rotate(glm::mat4(1.0f), Angle, Y) * X;
  59. glm::vec4 const X2 = glm::eulerAngleY(Angle) * X;
  60. glm::vec4 const X3 = glm::eulerAngleYX(Angle, 0.0f) * X;
  61. glm::vec4 const X4 = glm::eulerAngleXY(0.0f, Angle) * X;
  62. glm::vec4 const X5 = glm::eulerAngleYZ(Angle, 0.0f) * X;
  63. glm::vec4 const X6 = glm::eulerAngleZY(0.0f, Angle) * X;
  64. glm::vec4 const X7 = glm::eulerAngleYXZ(Angle, 0.0f, 0.0f) * X;
  65. Error += glm::all(glm::epsilonEqual(X1, X2, 0.00001f)) ? 0 : 1;
  66. Error += glm::all(glm::epsilonEqual(X1, X3, 0.00001f)) ? 0 : 1;
  67. Error += glm::all(glm::epsilonEqual(X1, X4, 0.00001f)) ? 0 : 1;
  68. Error += glm::all(glm::epsilonEqual(X1, X5, 0.00001f)) ? 0 : 1;
  69. Error += glm::all(glm::epsilonEqual(X1, X6, 0.00001f)) ? 0 : 1;
  70. Error += glm::all(glm::epsilonEqual(X1, X7, 0.00001f)) ? 0 : 1;
  71. glm::vec4 const Z(0.0f, 0.0f, 1.0f, 1.0f);
  72. glm::vec4 const Z1 = glm::eulerAngleY(Angle) * Z;
  73. glm::vec4 const Z2 = glm::rotate(glm::mat4(1.0f), Angle, Y) * Z;
  74. glm::vec4 const Z3 = glm::eulerAngleYX(Angle, 0.0f) * Z;
  75. glm::vec4 const Z4 = glm::eulerAngleXY(0.0f, Angle) * Z;
  76. glm::vec4 const Z5 = glm::eulerAngleYZ(Angle, 0.0f) * Z;
  77. glm::vec4 const Z6 = glm::eulerAngleZY(0.0f, Angle) * Z;
  78. glm::vec4 const Z7 = glm::eulerAngleYXZ(Angle, 0.0f, 0.0f) * Z;
  79. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  80. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  81. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  82. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  83. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  84. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  85. return Error;
  86. }
  87. }//namespace test_eulerAngleY
  88. namespace test_eulerAngleZ
  89. {
  90. int test()
  91. {
  92. int Error = 0;
  93. float const Angle(glm::pi<float>() * 0.5f);
  94. glm::vec3 const Z(0.0f, 0.0f, 1.0f);
  95. glm::vec4 const X(1.0f, 0.0f, 0.0f, 1.0f);
  96. glm::vec4 const X1 = glm::rotate(glm::mat4(1.0f), Angle, Z) * X;
  97. glm::vec4 const X2 = glm::eulerAngleZ(Angle) * X;
  98. glm::vec4 const X3 = glm::eulerAngleZX(Angle, 0.0f) * X;
  99. glm::vec4 const X4 = glm::eulerAngleXZ(0.0f, Angle) * X;
  100. glm::vec4 const X5 = glm::eulerAngleZY(Angle, 0.0f) * X;
  101. glm::vec4 const X6 = glm::eulerAngleYZ(0.0f, Angle) * X;
  102. glm::vec4 const X7 = glm::eulerAngleYXZ(0.0f, 0.0f, Angle) * X;
  103. Error += glm::all(glm::epsilonEqual(X1, X2, 0.00001f)) ? 0 : 1;
  104. Error += glm::all(glm::epsilonEqual(X1, X3, 0.00001f)) ? 0 : 1;
  105. Error += glm::all(glm::epsilonEqual(X1, X4, 0.00001f)) ? 0 : 1;
  106. Error += glm::all(glm::epsilonEqual(X1, X5, 0.00001f)) ? 0 : 1;
  107. Error += glm::all(glm::epsilonEqual(X1, X6, 0.00001f)) ? 0 : 1;
  108. Error += glm::all(glm::epsilonEqual(X1, X7, 0.00001f)) ? 0 : 1;
  109. glm::vec4 const Y(1.0f, 0.0f, 0.0f, 1.0f);
  110. glm::vec4 const Z1 = glm::rotate(glm::mat4(1.0f), Angle, Z) * Y;
  111. glm::vec4 const Z2 = glm::eulerAngleZ(Angle) * Y;
  112. glm::vec4 const Z3 = glm::eulerAngleZX(Angle, 0.0f) * Y;
  113. glm::vec4 const Z4 = glm::eulerAngleXZ(0.0f, Angle) * Y;
  114. glm::vec4 const Z5 = glm::eulerAngleZY(Angle, 0.0f) * Y;
  115. glm::vec4 const Z6 = glm::eulerAngleYZ(0.0f, Angle) * Y;
  116. glm::vec4 const Z7 = glm::eulerAngleYXZ(0.0f, 0.0f, Angle) * Y;
  117. Error += glm::all(glm::epsilonEqual(Z1, Z2, 0.00001f)) ? 0 : 1;
  118. Error += glm::all(glm::epsilonEqual(Z1, Z3, 0.00001f)) ? 0 : 1;
  119. Error += glm::all(glm::epsilonEqual(Z1, Z4, 0.00001f)) ? 0 : 1;
  120. Error += glm::all(glm::epsilonEqual(Z1, Z5, 0.00001f)) ? 0 : 1;
  121. Error += glm::all(glm::epsilonEqual(Z1, Z6, 0.00001f)) ? 0 : 1;
  122. Error += glm::all(glm::epsilonEqual(Z1, Z7, 0.00001f)) ? 0 : 1;
  123. return Error;
  124. }
  125. }//namespace test_eulerAngleZ
  126. namespace test_derivedEulerAngles
  127. {
  128. bool epsilonEqual(glm::mat4 const& mat1, glm::mat4 const& mat2, glm::mat4::value_type const& epsilon)
  129. {
  130. return glm::all(glm::epsilonEqual(mat1[0], mat2[0], epsilon)) ?
  131. (
  132. glm::all(glm::epsilonEqual(mat1[1], mat2[1], epsilon)) ?
  133. (
  134. glm::all(glm::epsilonEqual(mat1[2], mat2[2], epsilon)) ?
  135. (
  136. glm::all(glm::epsilonEqual(mat1[3], mat2[3], epsilon)) ? true : false
  137. ) : false
  138. ) : false
  139. ) : false;
  140. }
  141. template<typename RotationFunc, typename TestDerivedFunc>
  142. int test(RotationFunc rotationFunc, TestDerivedFunc testDerivedFunc, const glm::vec3& basis)
  143. {
  144. int Error = 0;
  145. typedef glm::vec3::value_type value;
  146. value const zeroAngle(0.0f);
  147. value const Angle(glm::pi<float>() * 0.75f);
  148. value const negativeAngle(-Angle);
  149. value const zeroAngleVelocity(0.0f);
  150. value const AngleVelocity(glm::pi<float>() * 0.27f);
  151. value const negativeAngleVelocity(-AngleVelocity);
  152. typedef std::pair<value,value> AngleAndAngleVelocity;
  153. std::vector<AngleAndAngleVelocity> testPairs;
  154. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, zeroAngleVelocity));
  155. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, AngleVelocity));
  156. testPairs.push_back(AngleAndAngleVelocity(zeroAngle, negativeAngleVelocity));
  157. testPairs.push_back(AngleAndAngleVelocity(Angle, zeroAngleVelocity));
  158. testPairs.push_back(AngleAndAngleVelocity(Angle, AngleVelocity));
  159. testPairs.push_back(AngleAndAngleVelocity(Angle, negativeAngleVelocity));
  160. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, zeroAngleVelocity));
  161. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, AngleVelocity));
  162. testPairs.push_back(AngleAndAngleVelocity(negativeAngle, negativeAngleVelocity));
  163. for (size_t i = 0, size = testPairs.size(); i < size; ++i)
  164. {
  165. AngleAndAngleVelocity const& pair = testPairs.at(i);
  166. glm::mat4 const W = glm::matrixCross4(basis * pair.second);
  167. glm::mat4 const rotMt = glm::transpose(rotationFunc(pair.first));
  168. glm::mat4 const derivedRotM = testDerivedFunc(pair.first, pair.second);
  169. Error += epsilonEqual(W, derivedRotM * rotMt, 0.00001f) ? 0 : 1;
  170. }
  171. return Error;
  172. }
  173. }//namespace test_derivedEulerAngles
  174. namespace test_eulerAngleXY
  175. {
  176. int test()
  177. {
  178. int Error = 0;
  179. glm::vec4 const V(1.0f);
  180. float const AngleX(glm::pi<float>() * 0.5f);
  181. float const AngleY(glm::pi<float>() * 0.25f);
  182. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  183. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  184. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleX, axisX) * glm::rotate(glm::mat4(1.0f), AngleY, axisY)) * V;
  185. glm::vec4 const V2 = glm::eulerAngleXY(AngleX, AngleY) * V;
  186. glm::vec4 const V3 = glm::eulerAngleX(AngleX) * glm::eulerAngleY(AngleY) * V;
  187. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  188. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  189. return Error;
  190. }
  191. }//namespace test_eulerAngleXY
  192. namespace test_eulerAngleYX
  193. {
  194. int test()
  195. {
  196. int Error = 0;
  197. glm::vec4 const V(1.0f);
  198. float const AngleX(glm::pi<float>() * 0.5f);
  199. float const AngleY(glm::pi<float>() * 0.25f);
  200. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  201. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  202. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleY, axisY) * glm::rotate(glm::mat4(1.0f), AngleX, axisX)) * V;
  203. glm::vec4 const V2 = glm::eulerAngleYX(AngleY, AngleX) * V;
  204. glm::vec4 const V3 = glm::eulerAngleY(AngleY) * glm::eulerAngleX(AngleX) * V;
  205. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  206. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  207. return Error;
  208. }
  209. }//namespace test_eulerAngleYX
  210. namespace test_eulerAngleXZ
  211. {
  212. int test()
  213. {
  214. int Error = 0;
  215. glm::vec4 const V(1.0f);
  216. float const AngleX(glm::pi<float>() * 0.5f);
  217. float const AngleZ(glm::pi<float>() * 0.25f);
  218. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  219. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  220. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleX, axisX) * glm::rotate(glm::mat4(1.0f), AngleZ, axisZ)) * V;
  221. glm::vec4 const V2 = glm::eulerAngleXZ(AngleX, AngleZ) * V;
  222. glm::vec4 const V3 = glm::eulerAngleX(AngleX) * glm::eulerAngleZ(AngleZ) * V;
  223. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  224. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  225. return Error;
  226. }
  227. }//namespace test_eulerAngleXZ
  228. namespace test_eulerAngleZX
  229. {
  230. int test()
  231. {
  232. int Error = 0;
  233. glm::vec4 const V(1.0f);
  234. float const AngleX(glm::pi<float>() * 0.5f);
  235. float const AngleZ(glm::pi<float>() * 0.25f);
  236. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  237. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  238. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleZ, axisZ) * glm::rotate(glm::mat4(1.0f), AngleX, axisX)) * V;
  239. glm::vec4 const V2 = glm::eulerAngleZX(AngleZ, AngleX) * V;
  240. glm::vec4 const V3 = glm::eulerAngleZ(AngleZ) * glm::eulerAngleX(AngleX) * V;
  241. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  242. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  243. return Error;
  244. }
  245. }//namespace test_eulerAngleZX
  246. namespace test_eulerAngleYZ
  247. {
  248. int test()
  249. {
  250. int Error = 0;
  251. glm::vec4 const V(1.0f);
  252. float const AngleY(glm::pi<float>() * 0.5f);
  253. float const AngleZ(glm::pi<float>() * 0.25f);
  254. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  255. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  256. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  257. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleY, axisY) * glm::rotate(glm::mat4(1.0f), AngleZ, axisZ)) * V;
  258. glm::vec4 const V2 = glm::eulerAngleYZ(AngleY, AngleZ) * V;
  259. glm::vec4 const V3 = glm::eulerAngleY(AngleY) * glm::eulerAngleZ(AngleZ) * V;
  260. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  261. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  262. return Error;
  263. }
  264. }//namespace test_eulerAngleYZ
  265. namespace test_eulerAngleZY
  266. {
  267. int test()
  268. {
  269. int Error = 0;
  270. glm::vec4 const V(1.0f);
  271. float const AngleY(glm::pi<float>() * 0.5f);
  272. float const AngleZ(glm::pi<float>() * 0.25f);
  273. glm::vec3 const axisX(1.0f, 0.0f, 0.0f);
  274. glm::vec3 const axisY(0.0f, 1.0f, 0.0f);
  275. glm::vec3 const axisZ(0.0f, 0.0f, 1.0f);
  276. glm::vec4 const V1 = (glm::rotate(glm::mat4(1.0f), AngleZ, axisZ) * glm::rotate(glm::mat4(1.0f), AngleY, axisY)) * V;
  277. glm::vec4 const V2 = glm::eulerAngleZY(AngleZ, AngleY) * V;
  278. glm::vec4 const V3 = glm::eulerAngleZ(AngleZ) * glm::eulerAngleY(AngleY) * V;
  279. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  280. Error += glm::all(glm::epsilonEqual(V1, V3, 0.00001f)) ? 0 : 1;
  281. return Error;
  282. }
  283. }//namespace test_eulerAngleZY
  284. namespace test_eulerAngleYXZ
  285. {
  286. int test()
  287. {
  288. glm::f32 first = 1.046f;
  289. glm::f32 second = 0.52f;
  290. glm::f32 third = -0.785f;
  291. glm::fmat4 rotationEuler = glm::eulerAngleYXZ(first, second, third);
  292. glm::fmat4 rotationInvertedY = glm::eulerAngleY(-1.f*first) * glm::eulerAngleX(second) * glm::eulerAngleZ(third);
  293. glm::fmat4 rotationDumb = glm::fmat4();
  294. rotationDumb = glm::rotate(rotationDumb, first, glm::fvec3(0,1,0));
  295. rotationDumb = glm::rotate(rotationDumb, second, glm::fvec3(1,0,0));
  296. rotationDumb = glm::rotate(rotationDumb, third, glm::fvec3(0,0,1));
  297. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler)).c_str());
  298. std::printf("%s\n", glm::to_string(glm::fmat3(rotationDumb)).c_str());
  299. std::printf("%s\n", glm::to_string(glm::fmat3(rotationInvertedY)).c_str());
  300. std::printf("\nRESIDUAL\n");
  301. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler-(rotationDumb))).c_str());
  302. std::printf("%s\n", glm::to_string(glm::fmat3(rotationEuler-(rotationInvertedY))).c_str());
  303. return 0;
  304. }
  305. }//namespace eulerAngleYXZ
  306. namespace test_eulerAngles
  307. {
  308. template<typename TestRotationFunc>
  309. int test(TestRotationFunc testRotationFunc, glm::vec3 const& I, glm::vec3 const& J, glm::vec3 const& K)
  310. {
  311. int Error = 0;
  312. typedef glm::mat4::value_type value;
  313. value const minAngle(-glm::pi<value>());
  314. value const maxAngle(glm::pi<value>());
  315. value const maxAngleWithDelta(maxAngle - 0.0000001f);
  316. value const minMidAngle(-glm::pi<value>() * 0.5f);
  317. value const maxMidAngle(glm::pi<value>() * 0.5f);
  318. std::vector<glm::vec3> testEulerAngles;
  319. testEulerAngles.push_back(glm::vec3(1.046f, 0.52f, -0.785f));
  320. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, minAngle));
  321. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngle));
  322. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngleWithDelta));
  323. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, minAngle));
  324. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngle));
  325. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngleWithDelta));
  326. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, minAngle));
  327. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngle));
  328. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngleWithDelta));
  329. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngle));
  330. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngleWithDelta));
  331. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, minAngle));
  332. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, minAngle));
  333. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngle));
  334. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngleWithDelta));
  335. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngle));
  336. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngleWithDelta));
  337. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, minAngle));
  338. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, maxAngle));
  339. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, minAngle));
  340. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, maxAngle));
  341. for (size_t i = 0, size = testEulerAngles.size(); i < size; ++i)
  342. {
  343. glm::vec3 const& angles = testEulerAngles.at(i);
  344. glm::mat4 const rotationEuler = testRotationFunc(angles.x, angles.y, angles.z);
  345. glm::mat4 rotationDumb = glm::diagonal4x4(glm::mat4::col_type(1.0f));
  346. rotationDumb = glm::rotate(rotationDumb, angles.x, I);
  347. rotationDumb = glm::rotate(rotationDumb, angles.y, J);
  348. rotationDumb = glm::rotate(rotationDumb, angles.z, K);
  349. glm::vec4 const V(1.0f,1.0f,1.0f,1.0f);
  350. glm::vec4 const V1 = rotationEuler * V;
  351. glm::vec4 const V2 = rotationDumb * V;
  352. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  353. }
  354. return Error;
  355. }
  356. }//namespace test_extractsEulerAngles
  357. namespace test_extractsEulerAngles
  358. {
  359. template<typename RotationFunc, typename TestExtractionFunc>
  360. int test(RotationFunc rotationFunc, TestExtractionFunc testExtractionFunc)
  361. {
  362. int Error = 0;
  363. typedef glm::mat4::value_type value;
  364. value const minAngle(-glm::pi<value>());
  365. value const maxAngle(glm::pi<value>());
  366. value const maxAngleWithDelta(maxAngle - 0.0000001f);
  367. value const minMidAngle(-glm::pi<value>() * 0.5f);
  368. value const maxMidAngle(glm::pi<value>() * 0.5f);
  369. std::vector<glm::vec3> testEulerAngles;
  370. testEulerAngles.push_back(glm::vec3(1.046f, 0.52f, -0.785f));
  371. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, minAngle));
  372. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngle));
  373. testEulerAngles.push_back(glm::vec3(minAngle, minMidAngle, maxAngleWithDelta));
  374. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, minAngle));
  375. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngle));
  376. testEulerAngles.push_back(glm::vec3(minAngle, maxMidAngle, maxAngleWithDelta));
  377. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, minAngle));
  378. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngle));
  379. testEulerAngles.push_back(glm::vec3(maxAngle, minMidAngle, maxAngleWithDelta));
  380. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngle));
  381. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, minMidAngle, maxAngleWithDelta));
  382. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, minAngle));
  383. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, minAngle));
  384. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngle));
  385. testEulerAngles.push_back(glm::vec3(maxAngle, maxMidAngle, maxAngleWithDelta));
  386. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngle));
  387. testEulerAngles.push_back(glm::vec3(maxAngleWithDelta, maxMidAngle, maxAngleWithDelta));
  388. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, minAngle));
  389. testEulerAngles.push_back(glm::vec3(minAngle, 0.0f, maxAngle));
  390. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, minAngle));
  391. testEulerAngles.push_back(glm::vec3(maxAngle, maxAngle, maxAngle));
  392. for (size_t i = 0, size = testEulerAngles.size(); i < size; ++i)
  393. {
  394. glm::vec3 const& angles = testEulerAngles.at(i);
  395. glm::mat4 const rotation = rotationFunc(angles.x, angles.y, angles.z);
  396. glm::vec3 extractedEulerAngles(0.0f);
  397. testExtractionFunc(rotation, extractedEulerAngles.x, extractedEulerAngles.y, extractedEulerAngles.z);
  398. glm::mat4 const extractedRotation = rotationFunc(extractedEulerAngles.x, extractedEulerAngles.y, extractedEulerAngles.z);
  399. glm::vec4 const V(1.0f,1.0f,1.0f,1.0f);
  400. glm::vec4 const V1 = rotation * V;
  401. glm::vec4 const V2 = extractedRotation * V;
  402. Error += glm::all(glm::epsilonEqual(V1, V2, 0.00001f)) ? 0 : 1;
  403. }
  404. return Error;
  405. }
  406. }//namespace test_extractsEulerAngles
  407. int main()
  408. {
  409. int Error = 0;
  410. typedef glm::mat4::value_type value;
  411. glm::vec3 const X(1.0f, 0.0f, 0.0f);
  412. glm::vec3 const Y(0.0f, 1.0f, 0.0f);
  413. glm::vec3 const Z(0.0f, 0.0f, 1.0f);
  414. Error += test_eulerAngleX::test();
  415. Error += test_eulerAngleY::test();
  416. Error += test_eulerAngleZ::test();
  417. Error += test_derivedEulerAngles::test(glm::eulerAngleX<value>, glm::derivedEulerAngleX<value>, X);
  418. Error += test_derivedEulerAngles::test(glm::eulerAngleY<value>, glm::derivedEulerAngleY<value>, Y);
  419. Error += test_derivedEulerAngles::test(glm::eulerAngleZ<value>, glm::derivedEulerAngleZ<value>, Z);
  420. Error += test_eulerAngleXY::test();
  421. Error += test_eulerAngleYX::test();
  422. Error += test_eulerAngleXZ::test();
  423. Error += test_eulerAngleZX::test();
  424. Error += test_eulerAngleYZ::test();
  425. Error += test_eulerAngleZY::test();
  426. Error += test_eulerAngleYXZ::test();
  427. Error += test_eulerAngles::test(glm::eulerAngleXZX<value>, X, Z, X);
  428. Error += test_eulerAngles::test(glm::eulerAngleXYX<value>, X, Y, X);
  429. Error += test_eulerAngles::test(glm::eulerAngleYXY<value>, Y, X, Y);
  430. Error += test_eulerAngles::test(glm::eulerAngleYZY<value>, Y, Z, Y);
  431. Error += test_eulerAngles::test(glm::eulerAngleZYZ<value>, Z, Y, Z);
  432. Error += test_eulerAngles::test(glm::eulerAngleZXZ<value>, Z, X, Z);
  433. Error += test_eulerAngles::test(glm::eulerAngleXZY<value>, X, Z, Y);
  434. Error += test_eulerAngles::test(glm::eulerAngleYZX<value>, Y, Z, X);
  435. Error += test_eulerAngles::test(glm::eulerAngleZYX<value>, Z, Y, X);
  436. Error += test_eulerAngles::test(glm::eulerAngleZXY<value>, Z, X, Y);
  437. Error += test_extractsEulerAngles::test(glm::eulerAngleYXZ<value>, glm::extractEulerAngleYXZ<value>);
  438. Error += test_extractsEulerAngles::test(glm::eulerAngleXZX<value>, glm::extractEulerAngleXZX<value>);
  439. Error += test_extractsEulerAngles::test(glm::eulerAngleXYX<value>, glm::extractEulerAngleXYX<value>);
  440. Error += test_extractsEulerAngles::test(glm::eulerAngleYXY<value>, glm::extractEulerAngleYXY<value>);
  441. Error += test_extractsEulerAngles::test(glm::eulerAngleYZY<value>, glm::extractEulerAngleYZY<value>);
  442. Error += test_extractsEulerAngles::test(glm::eulerAngleZYZ<value>, glm::extractEulerAngleZYZ<value>);
  443. Error += test_extractsEulerAngles::test(glm::eulerAngleZXZ<value>, glm::extractEulerAngleZXZ<value>);
  444. Error += test_extractsEulerAngles::test(glm::eulerAngleXZY<value>, glm::extractEulerAngleXZY<value>);
  445. Error += test_extractsEulerAngles::test(glm::eulerAngleYZX<value>, glm::extractEulerAngleYZX<value>);
  446. Error += test_extractsEulerAngles::test(glm::eulerAngleZYX<value>, glm::extractEulerAngleZYX<value>);
  447. Error += test_extractsEulerAngles::test(glm::eulerAngleZXY<value>, glm::extractEulerAngleZXY<value>);
  448. return Error;
  449. }