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.
 
 
 

186 lines
7.7 KiB

  1. //
  2. // Lol Engine — Unit tests for the camera object
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2013 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include <lol/engine-internal.h>
  14. #include <lolunit.h>
  15. namespace lol
  16. {
  17. lolunit_declare_fixture(camera_test)
  18. {
  19. Camera tc;
  20. vec3 eye, target, up;
  21. mat4 m_lookat;
  22. quat q_lookat;
  23. vec3 v_lookat;
  24. float fov, screen_size, screen_ratio, near, far;
  25. bool is_shifted;
  26. void setup()
  27. {
  28. eye = vec3(0.f, 0.f, 50.f);
  29. target = vec3::zero;
  30. up = vec3::axis_y;
  31. m_lookat = mat4::lookat(eye, target, up);
  32. q_lookat = quat(mat3(m_lookat));
  33. v_lookat = vec3::toeuler_zyx(q_lookat);
  34. fov = radians(90.f);
  35. screen_size = 800.f;
  36. screen_ratio = 1.0f;
  37. near = 1.f;
  38. far = 1000.f;
  39. is_shifted = false;
  40. }
  41. #define TEST_VECTOR(v0, v1) \
  42. lolunit_assert_doubles_equal(v0.x, v1.x, 1.e-5f); \
  43. lolunit_assert_doubles_equal(v0.y, v1.y, 1.e-5f); \
  44. lolunit_assert_doubles_equal(v0.z, v1.z, 1.e-5f);
  45. lolunit_declare_test(set_view_test)
  46. {
  47. tc.SetView(eye, target, up);
  48. TEST_VECTOR(eye, tc.GetPosition());
  49. TEST_VECTOR(target, tc.GetTarget());
  50. TEST_VECTOR(up, tc.GetUp());
  51. tc.SetView(eye, q_lookat);
  52. TEST_VECTOR(eye, tc.GetPosition());
  53. TEST_VECTOR(target, tc.GetTarget());
  54. TEST_VECTOR(up, tc.GetUp());
  55. tc.SetView(eye, v_lookat);
  56. TEST_VECTOR(eye, tc.GetPosition());
  57. TEST_VECTOR(target, tc.GetTarget());
  58. TEST_VECTOR(up, tc.GetUp());
  59. tc.SetView(m_lookat);
  60. TEST_VECTOR(eye, tc.GetPosition());
  61. TEST_VECTOR(target, tc.GetTarget());
  62. TEST_VECTOR(up, tc.GetUp());
  63. tc.UseTarget(false);
  64. TEST_VECTOR(vec3(0.f, 0.f, 49.f), tc.GetTarget());
  65. }
  66. #define TEST_MATRIX(m0, m1) \
  67. lolunit_assert_doubles_equal(m0[0][0], m1[0][0], 1.e-5f); \
  68. lolunit_assert_doubles_equal(m0[1][0], m1[1][0], 1.e-5f); \
  69. lolunit_assert_doubles_equal(m0[2][0], m1[2][0], 1.e-5f); \
  70. lolunit_assert_doubles_equal(m0[3][0], m1[3][0], 1.e-5f); \
  71. \
  72. lolunit_assert_doubles_equal(m0[0][1], m1[0][1], 1.e-5f); \
  73. lolunit_assert_doubles_equal(m0[1][1], m1[1][1], 1.e-5f); \
  74. lolunit_assert_doubles_equal(m0[2][1], m1[2][1], 1.e-5f); \
  75. lolunit_assert_doubles_equal(m0[3][1], m1[3][1], 1.e-5f); \
  76. \
  77. lolunit_assert_doubles_equal(m0[0][2], m1[0][2], 1.e-5f); \
  78. lolunit_assert_doubles_equal(m0[1][2], m1[1][2], 1.e-5f); \
  79. lolunit_assert_doubles_equal(m0[2][2], m1[2][2], 1.e-5f); \
  80. lolunit_assert_doubles_equal(m0[3][2], m1[3][2], 1.e-5f); \
  81. \
  82. lolunit_assert_doubles_equal(m0[0][3], m1[0][3], 1.e-5f); \
  83. lolunit_assert_doubles_equal(m0[1][3], m1[1][3], 1.e-5f); \
  84. lolunit_assert_doubles_equal(m0[2][3], m1[2][3], 1.e-5f); \
  85. lolunit_assert_doubles_equal(m0[3][3], m1[3][3], 1.e-5f);
  86. lolunit_declare_test(set_projection_test)
  87. {
  88. mat4 refmx = mat4::perspective(fov, screen_size, screen_size * screen_ratio, near, far);
  89. tc.SetProjection(fov, near, far, screen_size, screen_ratio);
  90. TEST_MATRIX(refmx, tc.GetProjection());
  91. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  92. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  93. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  94. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  95. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  96. lolunit_assert(is_shifted == tc.IsShifted());
  97. tc.SetProjection(fov, near, far);
  98. TEST_MATRIX(refmx, tc.GetProjection());
  99. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  100. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  101. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  102. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  103. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  104. lolunit_assert(is_shifted == tc.IsShifted());
  105. tc.SetProjection(refmx);
  106. TEST_MATRIX(refmx, tc.GetProjection());
  107. tc.SetFov(fov);
  108. TEST_MATRIX(refmx, tc.GetProjection());
  109. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  110. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  111. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  112. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  113. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  114. lolunit_assert(is_shifted == tc.IsShifted());
  115. tc.SetScreenInfos(screen_size);
  116. TEST_MATRIX(refmx, tc.GetProjection());
  117. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  118. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  119. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  120. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  121. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  122. lolunit_assert(is_shifted == tc.IsShifted());
  123. tc.SetScreenInfos(screen_size, screen_ratio);
  124. TEST_MATRIX(refmx, tc.GetProjection());
  125. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  126. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  127. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  128. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  129. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  130. lolunit_assert(is_shifted == tc.IsShifted());
  131. tc.SetDrawInfos(far);
  132. TEST_MATRIX(refmx, tc.GetProjection());
  133. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  134. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  135. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  136. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  137. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  138. lolunit_assert(is_shifted == tc.IsShifted());
  139. tc.SetDrawInfos(near, far);
  140. TEST_MATRIX(refmx, tc.GetProjection());
  141. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  142. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  143. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  144. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  145. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  146. lolunit_assert(is_shifted == tc.IsShifted());
  147. is_shifted = true;
  148. refmx = mat4::shifted_perspective(fov, screen_size, screen_ratio, near, far);
  149. tc.UseShift(is_shifted);
  150. TEST_MATRIX(refmx, tc.GetProjection());
  151. lolunit_assert_doubles_equal(fov, tc.GetFov(), 1.e-5f);
  152. lolunit_assert_doubles_equal(screen_size, tc.GetScreenSize(), 1.e-5f);
  153. lolunit_assert_doubles_equal(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  154. lolunit_assert_doubles_equal(near, tc.GetNear(), 1.e-5f);
  155. lolunit_assert_doubles_equal(far, tc.GetFar(), 1.e-5f);
  156. lolunit_assert(is_shifted == tc.IsShifted());
  157. }
  158. };
  159. } /* namespace lol */