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.

205 lines
6.3 KiB

5 years ago
  1. #define GLM_ENABLE_EXPERIMENTAL
  2. #define GLM_FORCE_CTOR_INIT
  3. #include <glm/gtx/dual_quaternion.hpp>
  4. #include <glm/gtc/matrix_transform.hpp>
  5. #include <glm/gtc/epsilon.hpp>
  6. #include <glm/gtx/euler_angles.hpp>
  7. #include <glm/vector_relational.hpp>
  8. #if GLM_HAS_TRIVIAL_QUERIES
  9. # include <type_traits>
  10. #endif
  11. int myrand()
  12. {
  13. static int holdrand = 1;
  14. return (((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
  15. }
  16. float myfrand() // returns values from -1 to 1 inclusive
  17. {
  18. return float(double(myrand()) / double( 0x7ffff )) * 2.0f - 1.0f;
  19. }
  20. int test_dquat_type()
  21. {
  22. glm::dvec3 vA;
  23. glm::dquat dqA, dqB;
  24. glm::ddualquat C(dqA, dqB);
  25. glm::ddualquat B(dqA);
  26. glm::ddualquat D(dqA, vA);
  27. return 0;
  28. }
  29. int test_scalars()
  30. {
  31. float const Epsilon = 0.0001f;
  32. int Error(0);
  33. glm::quat src_q1 = glm::quat(1.0f,2.0f,3.0f,4.0f);
  34. glm::quat src_q2 = glm::quat(5.0f,6.0f,7.0f,8.0f);
  35. glm::dualquat src1(src_q1,src_q2);
  36. {
  37. glm::dualquat dst1 = src1 * 2.0f;
  38. glm::dualquat dst2 = 2.0f * src1;
  39. glm::dualquat dst3 = src1;
  40. dst3 *= 2.0f;
  41. glm::dualquat dstCmp(src_q1 * 2.0f,src_q2 * 2.0f);
  42. Error += glm::all(glm::epsilonEqual(dst1.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst1.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  43. Error += glm::all(glm::epsilonEqual(dst2.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst2.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  44. Error += glm::all(glm::epsilonEqual(dst3.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst3.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  45. }
  46. {
  47. glm::dualquat dst1 = src1 / 2.0f;
  48. glm::dualquat dst2 = src1;
  49. dst2 /= 2.0f;
  50. glm::dualquat dstCmp(src_q1 / 2.0f,src_q2 / 2.0f);
  51. Error += glm::all(glm::epsilonEqual(dst1.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst1.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  52. Error += glm::all(glm::epsilonEqual(dst2.real,dstCmp.real, Epsilon)) && glm::all(glm::epsilonEqual(dst2.dual,dstCmp.dual, Epsilon)) ? 0 : 1;
  53. }
  54. return Error;
  55. }
  56. int test_inverse()
  57. {
  58. int Error(0);
  59. float const Epsilon = 0.0001f;
  60. glm::dualquat dqid = glm::dual_quat_identity<float, glm::defaultp>();
  61. glm::mat4x4 mid(1.0f);
  62. for (int j = 0; j < 100; ++j)
  63. {
  64. glm::mat4x4 rot = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  65. glm::vec3 vt = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  66. glm::mat4x4 m = glm::translate(mid, vt) * rot;
  67. glm::quat qr = glm::quat_cast(m);
  68. glm::dualquat dq(qr);
  69. glm::dualquat invdq = glm::inverse(dq);
  70. glm::dualquat r1 = invdq * dq;
  71. glm::dualquat r2 = dq * invdq;
  72. Error += glm::all(glm::epsilonEqual(r1.real, dqid.real, Epsilon)) && glm::all(glm::epsilonEqual(r1.dual, dqid.dual, Epsilon)) ? 0 : 1;
  73. Error += glm::all(glm::epsilonEqual(r2.real, dqid.real, Epsilon)) && glm::all(glm::epsilonEqual(r2.dual, dqid.dual, Epsilon)) ? 0 : 1;
  74. // testing commutative property
  75. glm::dualquat r ( glm::quat( myfrand() * glm::pi<float>() * 2.0f, myfrand(), myfrand(), myfrand() ),
  76. glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f) );
  77. glm::dualquat riq = (r * invdq) * dq;
  78. glm::dualquat rqi = (r * dq) * invdq;
  79. Error += glm::all(glm::epsilonEqual(riq.real, rqi.real, Epsilon)) && glm::all(glm::epsilonEqual(riq.dual, rqi.dual, Epsilon)) ? 0 : 1;
  80. }
  81. return Error;
  82. }
  83. int test_mul()
  84. {
  85. int Error(0);
  86. float const Epsilon = 0.0001f;
  87. glm::mat4x4 mid(1.0f);
  88. for (int j = 0; j < 100; ++j)
  89. {
  90. // generate random rotations and translations and compare transformed by matrix and dualquats random points
  91. glm::vec3 vt1 = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  92. glm::vec3 vt2 = glm::vec3(myfrand() * 10.0f, myfrand() * 10.0f, myfrand() * 10.0f);
  93. glm::mat4x4 rot1 = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  94. glm::mat4x4 rot2 = glm::yawPitchRoll(myfrand() * 360.0f, myfrand() * 360.0f, myfrand() * 360.0f);
  95. glm::mat4x4 m1 = glm::translate(mid, vt1) * rot1;
  96. glm::mat4x4 m2 = glm::translate(mid, vt2) * rot2;
  97. glm::mat4x4 m3 = m2 * m1;
  98. glm::mat4x4 m4 = m1 * m2;
  99. glm::quat qrot1 = glm::quat_cast(rot1);
  100. glm::quat qrot2 = glm::quat_cast(rot2);
  101. glm::dualquat dq1 = glm::dualquat(qrot1,vt1);
  102. glm::dualquat dq2 = glm::dualquat(qrot2,vt2);
  103. glm::dualquat dq3 = dq2 * dq1;
  104. glm::dualquat dq4 = dq1 * dq2;
  105. for (int i = 0; i < 100; ++i)
  106. {
  107. glm::vec4 src_pt = glm::vec4(myfrand() * 4.0f, myfrand() * 5.0f, myfrand() * 3.0f,1.0f);
  108. // test both multiplication orders
  109. glm::vec4 dst_pt_m3 = m3 * src_pt;
  110. glm::vec4 dst_pt_dq3 = dq3 * src_pt;
  111. glm::vec4 dst_pt_m3_i = glm::inverse(m3) * src_pt;
  112. glm::vec4 dst_pt_dq3_i = src_pt * dq3;
  113. glm::vec4 dst_pt_m4 = m4 * src_pt;
  114. glm::vec4 dst_pt_dq4 = dq4 * src_pt;
  115. glm::vec4 dst_pt_m4_i = glm::inverse(m4) * src_pt;
  116. glm::vec4 dst_pt_dq4_i = src_pt * dq4;
  117. Error += glm::all(glm::epsilonEqual(dst_pt_m3, dst_pt_dq3, Epsilon)) ? 0 : 1;
  118. Error += glm::all(glm::epsilonEqual(dst_pt_m4, dst_pt_dq4, Epsilon)) ? 0 : 1;
  119. Error += glm::all(glm::epsilonEqual(dst_pt_m3_i, dst_pt_dq3_i, Epsilon)) ? 0 : 1;
  120. Error += glm::all(glm::epsilonEqual(dst_pt_m4_i, dst_pt_dq4_i, Epsilon)) ? 0 : 1;
  121. }
  122. }
  123. return Error;
  124. }
  125. int test_dual_quat_ctr()
  126. {
  127. int Error(0);
  128. # if GLM_HAS_TRIVIAL_QUERIES
  129. // Error += std::is_trivially_default_constructible<glm::dualquat>::value ? 0 : 1;
  130. // Error += std::is_trivially_default_constructible<glm::ddualquat>::value ? 0 : 1;
  131. // Error += std::is_trivially_copy_assignable<glm::dualquat>::value ? 0 : 1;
  132. // Error += std::is_trivially_copy_assignable<glm::ddualquat>::value ? 0 : 1;
  133. Error += std::is_trivially_copyable<glm::dualquat>::value ? 0 : 1;
  134. Error += std::is_trivially_copyable<glm::ddualquat>::value ? 0 : 1;
  135. Error += std::is_copy_constructible<glm::dualquat>::value ? 0 : 1;
  136. Error += std::is_copy_constructible<glm::ddualquat>::value ? 0 : 1;
  137. # endif
  138. return Error;
  139. }
  140. int test_size()
  141. {
  142. int Error = 0;
  143. Error += 32 == sizeof(glm::dualquat) ? 0 : 1;
  144. Error += 64 == sizeof(glm::ddualquat) ? 0 : 1;
  145. Error += glm::dualquat().length() == 2 ? 0 : 1;
  146. Error += glm::ddualquat().length() == 2 ? 0 : 1;
  147. Error += glm::dualquat::length() == 2 ? 0 : 1;
  148. Error += glm::ddualquat::length() == 2 ? 0 : 1;
  149. return Error;
  150. }
  151. int main()
  152. {
  153. int Error = 0;
  154. Error += test_dual_quat_ctr();
  155. Error += test_dquat_type();
  156. Error += test_scalars();
  157. Error += test_inverse();
  158. Error += test_mul();
  159. Error += test_size();
  160. return Error;
  161. }