您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

195 行
7.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <lol/main.h>
  15. #include "lol/unit.h"
  16. namespace lol
  17. {
  18. LOLUNIT_FIXTURE(CameraTest)
  19. {
  20. Camera tc;
  21. vec3 eye;
  22. vec3 target;
  23. vec3 up;
  24. mat4 m_lookat;
  25. quat q_lookat;
  26. vec3 v_lookat;
  27. float fov;
  28. float screen_size;
  29. float screen_ratio;
  30. float near;
  31. float far;
  32. bool is_shifted;
  33. void SetUp()
  34. {
  35. eye = vec3(0.f, 0.f, 50.f);
  36. target = vec3::zero;
  37. up = vec3::axis_y;
  38. m_lookat = mat4::lookat(eye, target, up);
  39. q_lookat = quat(m_lookat);
  40. v_lookat = vec3::toeuler_zyx(q_lookat);
  41. fov = 90.f;
  42. screen_size = 800.f;
  43. screen_ratio = 1.0f;
  44. near = 1.f;
  45. far = 1000.f;
  46. is_shifted = false;
  47. }
  48. void TearDown() {}
  49. #define TEST_VECTOR(v0, v1) \
  50. LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.x, v1.x, 1.e-5f); \
  51. LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.y, v1.y, 1.e-5f); \
  52. LOLUNIT_ASSERT_DOUBLES_EQUAL(v0.z, v1.z, 1.e-5f);
  53. LOLUNIT_TEST(SetViewTest)
  54. {
  55. tc.SetView(eye, target, up);
  56. TEST_VECTOR(eye, tc.GetPosition());
  57. TEST_VECTOR(target, tc.GetTarget());
  58. TEST_VECTOR(up, tc.GetUp());
  59. tc.SetView(eye, q_lookat);
  60. TEST_VECTOR(eye, tc.GetPosition());
  61. TEST_VECTOR(target, tc.GetTarget());
  62. TEST_VECTOR(up, tc.GetUp());
  63. tc.SetView(eye, v_lookat);
  64. TEST_VECTOR(eye, tc.GetPosition());
  65. TEST_VECTOR(target, tc.GetTarget());
  66. TEST_VECTOR(up, tc.GetUp());
  67. tc.SetView(m_lookat);
  68. TEST_VECTOR(eye, tc.GetPosition());
  69. TEST_VECTOR(target, tc.GetTarget());
  70. TEST_VECTOR(up, tc.GetUp());
  71. tc.UseTarget(false);
  72. TEST_VECTOR(vec3(0.f, 0.f, 49.f), tc.GetTarget());
  73. }
  74. #define TEST_MATRIX(m0, m1) \
  75. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][0], m1[0][0], 1.e-5f); \
  76. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][0], m1[1][0], 1.e-5f); \
  77. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][0], m1[2][0], 1.e-5f); \
  78. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][0], m1[3][0], 1.e-5f); \
  79. \
  80. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][1], m1[0][1], 1.e-5f); \
  81. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][1], m1[1][1], 1.e-5f); \
  82. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][1], m1[2][1], 1.e-5f); \
  83. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][1], m1[3][1], 1.e-5f); \
  84. \
  85. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][2], m1[0][2], 1.e-5f); \
  86. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][2], m1[1][2], 1.e-5f); \
  87. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][2], m1[2][2], 1.e-5f); \
  88. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][2], m1[3][2], 1.e-5f); \
  89. \
  90. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[0][3], m1[0][3], 1.e-5f); \
  91. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[1][3], m1[1][3], 1.e-5f); \
  92. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[2][3], m1[2][3], 1.e-5f); \
  93. LOLUNIT_ASSERT_DOUBLES_EQUAL(m0[3][3], m1[3][3], 1.e-5f);
  94. LOLUNIT_TEST(SetProjectionTest)
  95. {
  96. mat4 refmx = mat4::perspective(fov, screen_size, screen_size * screen_ratio, near, far);
  97. tc.SetProjection(fov, near, far, screen_size, screen_ratio);
  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(fov, near, far);
  106. TEST_MATRIX(refmx, tc.GetProjection());
  107. LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
  108. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
  109. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  110. LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
  111. LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
  112. LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
  113. tc.SetProjection(refmx);
  114. TEST_MATRIX(refmx, tc.GetProjection());
  115. tc.SetFov(fov);
  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);
  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.SetScreenInfos(screen_size, screen_ratio);
  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(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. tc.SetDrawInfos(near, far);
  148. TEST_MATRIX(refmx, tc.GetProjection());
  149. LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
  150. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
  151. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  152. LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
  153. LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
  154. LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
  155. is_shifted = true;
  156. refmx = mat4::shifted_perspective(fov, screen_size, screen_ratio, near, far);
  157. tc.UseShift(is_shifted);
  158. TEST_MATRIX(refmx, tc.GetProjection());
  159. LOLUNIT_ASSERT_DOUBLES_EQUAL(fov, tc.GetFov(), 1.e-5f);
  160. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_size, tc.GetScreenSize(), 1.e-5f);
  161. LOLUNIT_ASSERT_DOUBLES_EQUAL(screen_ratio, tc.GetScreenRatio(), 1.e-5f);
  162. LOLUNIT_ASSERT_DOUBLES_EQUAL(near, tc.GetNear(), 1.e-5f);
  163. LOLUNIT_ASSERT_DOUBLES_EQUAL(far, tc.GetFar(), 1.e-5f);
  164. LOLUNIT_ASSERT(is_shifted == tc.IsShifted());
  165. }
  166. };
  167. } /* namespace lol */