test_projection.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /**************************************************************************/
  2. /* test_projection.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/math/aabb.h"
  32. #include "core/math/plane.h"
  33. #include "core/math/projection.h"
  34. #include "core/math/rect2.h"
  35. #include "core/math/transform_3d.h"
  36. #include "thirdparty/doctest/doctest.h"
  37. namespace TestProjection {
  38. TEST_CASE("[Projection] Construction") {
  39. Projection default_proj;
  40. CHECK(default_proj[0].is_equal_approx(Vector4(1, 0, 0, 0)));
  41. CHECK(default_proj[1].is_equal_approx(Vector4(0, 1, 0, 0)));
  42. CHECK(default_proj[2].is_equal_approx(Vector4(0, 0, 1, 0)));
  43. CHECK(default_proj[3].is_equal_approx(Vector4(0, 0, 0, 1)));
  44. Projection from_vec4(
  45. Vector4(1, 2, 3, 4),
  46. Vector4(5, 6, 7, 8),
  47. Vector4(9, 10, 11, 12),
  48. Vector4(13, 14, 15, 16));
  49. CHECK(from_vec4[0].is_equal_approx(Vector4(1, 2, 3, 4)));
  50. CHECK(from_vec4[1].is_equal_approx(Vector4(5, 6, 7, 8)));
  51. CHECK(from_vec4[2].is_equal_approx(Vector4(9, 10, 11, 12)));
  52. CHECK(from_vec4[3].is_equal_approx(Vector4(13, 14, 15, 16)));
  53. Transform3D transform(
  54. Basis(
  55. Vector3(1, 0, 0),
  56. Vector3(0, 2, 0),
  57. Vector3(0, 0, 3)),
  58. Vector3(4, 5, 6));
  59. Projection from_transform(transform);
  60. CHECK(from_transform[0].is_equal_approx(Vector4(1, 0, 0, 0)));
  61. CHECK(from_transform[1].is_equal_approx(Vector4(0, 2, 0, 0)));
  62. CHECK(from_transform[2].is_equal_approx(Vector4(0, 0, 3, 0)));
  63. CHECK(from_transform[3].is_equal_approx(Vector4(4, 5, 6, 1)));
  64. }
  65. TEST_CASE("[Projection] set_zero()") {
  66. Projection proj;
  67. proj.set_zero();
  68. for (int i = 0; i < 4; i++) {
  69. for (int j = 0; j < 4; j++) {
  70. CHECK(proj.columns[i][j] == 0);
  71. }
  72. }
  73. }
  74. TEST_CASE("[Projection] set_identity()") {
  75. Projection proj;
  76. proj.set_identity();
  77. for (int i = 0; i < 4; i++) {
  78. for (int j = 0; j < 4; j++) {
  79. CHECK(proj.columns[i][j] == (i == j ? 1 : 0));
  80. }
  81. }
  82. }
  83. TEST_CASE("[Projection] determinant()") {
  84. Projection proj(
  85. Vector4(1, 5, 9, 13),
  86. Vector4(2, 6, 11, 15),
  87. Vector4(4, 7, 11, 15),
  88. Vector4(4, 8, 12, 16));
  89. CHECK(proj.determinant() == -12);
  90. }
  91. TEST_CASE("[Projection] Inverse and invert") {
  92. SUBCASE("[Projection] Arbitrary projection matrix inversion") {
  93. Projection proj(
  94. Vector4(1, 5, 9, 13),
  95. Vector4(2, 6, 11, 15),
  96. Vector4(4, 7, 11, 15),
  97. Vector4(4, 8, 12, 16));
  98. Projection inverse_truth(
  99. Vector4(-4.0 / 12, 0, 1, -8.0 / 12),
  100. Vector4(8.0 / 12, -1, -1, 16.0 / 12),
  101. Vector4(-20.0 / 12, 2, -1, 5.0 / 12),
  102. Vector4(1, -1, 1, -0.75));
  103. Projection inverse = proj.inverse();
  104. CHECK(inverse[0].is_equal_approx(inverse_truth[0]));
  105. CHECK(inverse[1].is_equal_approx(inverse_truth[1]));
  106. CHECK(inverse[2].is_equal_approx(inverse_truth[2]));
  107. CHECK(inverse[3].is_equal_approx(inverse_truth[3]));
  108. proj.invert();
  109. CHECK(proj[0].is_equal_approx(inverse_truth[0]));
  110. CHECK(proj[1].is_equal_approx(inverse_truth[1]));
  111. CHECK(proj[2].is_equal_approx(inverse_truth[2]));
  112. CHECK(proj[3].is_equal_approx(inverse_truth[3]));
  113. }
  114. SUBCASE("[Projection] Orthogonal projection matrix inversion") {
  115. Projection p = Projection::create_orthogonal(-125.0f, 125.0f, -125.0f, 125.0f, 0.01f, 25.0f);
  116. p = p.inverse() * p;
  117. CHECK(p[0].is_equal_approx(Vector4(1, 0, 0, 0)));
  118. CHECK(p[1].is_equal_approx(Vector4(0, 1, 0, 0)));
  119. CHECK(p[2].is_equal_approx(Vector4(0, 0, 1, 0)));
  120. CHECK(p[3].is_equal_approx(Vector4(0, 0, 0, 1)));
  121. }
  122. SUBCASE("[Projection] Perspective projection matrix inversion") {
  123. Projection p = Projection::create_perspective(90.0f, 1.77777f, 0.05f, 4000.0f);
  124. p = p.inverse() * p;
  125. CHECK(p[0].is_equal_approx(Vector4(1, 0, 0, 0)));
  126. CHECK(p[1].is_equal_approx(Vector4(0, 1, 0, 0)));
  127. CHECK(p[2].is_equal_approx(Vector4(0, 0, 1, 0)));
  128. CHECK(p[3].is_equal_approx(Vector4(0, 0, 0, 1)));
  129. }
  130. }
  131. TEST_CASE("[Projection] Matrix product") {
  132. Projection proj1(
  133. Vector4(1, 5, 9, 13),
  134. Vector4(2, 6, 11, 15),
  135. Vector4(4, 7, 11, 15),
  136. Vector4(4, 8, 12, 16));
  137. Projection proj2(
  138. Vector4(0, 1, 2, 3),
  139. Vector4(10, 11, 12, 13),
  140. Vector4(20, 21, 22, 23),
  141. Vector4(30, 31, 32, 33));
  142. Projection prod = proj1 * proj2;
  143. CHECK(prod[0].is_equal_approx(Vector4(22, 44, 69, 93)));
  144. CHECK(prod[1].is_equal_approx(Vector4(132, 304, 499, 683)));
  145. CHECK(prod[2].is_equal_approx(Vector4(242, 564, 929, 1273)));
  146. CHECK(prod[3].is_equal_approx(Vector4(352, 824, 1359, 1863)));
  147. }
  148. TEST_CASE("[Projection] Vector transformation") {
  149. Projection proj(
  150. Vector4(1, 5, 9, 13),
  151. Vector4(2, 6, 11, 15),
  152. Vector4(4, 7, 11, 15),
  153. Vector4(4, 8, 12, 16));
  154. Vector4 vec4(1, 2, 3, 4);
  155. CHECK(proj.xform(vec4).is_equal_approx(Vector4(33, 70, 112, 152)));
  156. CHECK(proj.xform_inv(vec4).is_equal_approx(Vector4(90, 107, 111, 120)));
  157. Vector3 vec3(1, 2, 3);
  158. CHECK(proj.xform(vec3).is_equal_approx(Vector3(21, 46, 76) / 104));
  159. }
  160. TEST_CASE("[Projection] Plane transformation") {
  161. Projection proj(
  162. Vector4(1, 5, 9, 13),
  163. Vector4(2, 6, 11, 15),
  164. Vector4(4, 7, 11, 15),
  165. Vector4(4, 8, 12, 16));
  166. Plane plane(1, 2, 3, 4);
  167. CHECK(proj.xform4(plane).is_equal_approx(Plane(33, 70, 112, 152)));
  168. }
  169. TEST_CASE("[Projection] Values access") {
  170. Projection proj(
  171. Vector4(00, 01, 02, 03),
  172. Vector4(10, 11, 12, 13),
  173. Vector4(20, 21, 22, 23),
  174. Vector4(30, 31, 32, 33));
  175. CHECK(proj[0] == Vector4(00, 01, 02, 03));
  176. CHECK(proj[1] == Vector4(10, 11, 12, 13));
  177. CHECK(proj[2] == Vector4(20, 21, 22, 23));
  178. CHECK(proj[3] == Vector4(30, 31, 32, 33));
  179. }
  180. TEST_CASE("[Projection] flip_y() and flipped_y()") {
  181. Projection proj(
  182. Vector4(00, 01, 02, 03),
  183. Vector4(10, 11, 12, 13),
  184. Vector4(20, 21, 22, 23),
  185. Vector4(30, 31, 32, 33));
  186. Projection flipped = proj.flipped_y();
  187. CHECK(flipped[0] == proj[0]);
  188. CHECK(flipped[1] == -proj[1]);
  189. CHECK(flipped[2] == proj[2]);
  190. CHECK(flipped[3] == proj[3]);
  191. proj.flip_y();
  192. CHECK(proj[0] == flipped[0]);
  193. CHECK(proj[1] == flipped[1]);
  194. CHECK(proj[2] == flipped[2]);
  195. CHECK(proj[3] == flipped[3]);
  196. }
  197. TEST_CASE("[Projection] Jitter offset") {
  198. Projection proj(
  199. Vector4(00, 01, 02, 03),
  200. Vector4(10, 11, 12, 13),
  201. Vector4(20, 21, 22, 23),
  202. Vector4(30, 31, 32, 33));
  203. Projection offsetted = proj.jitter_offseted(Vector2(1, 2));
  204. CHECK(offsetted[0] == proj[0]);
  205. CHECK(offsetted[1] == proj[1]);
  206. CHECK(offsetted[2] == proj[2]);
  207. CHECK(offsetted[3] == proj[3] + Vector4(1, 2, 0, 0));
  208. proj.add_jitter_offset(Vector2(1, 2));
  209. CHECK(proj[0] == offsetted[0]);
  210. CHECK(proj[1] == offsetted[1]);
  211. CHECK(proj[2] == offsetted[2]);
  212. CHECK(proj[3] == offsetted[3]);
  213. }
  214. TEST_CASE("[Projection] Adjust znear") {
  215. Projection persp = Projection::create_perspective(90, 0.5, 1, 50, false);
  216. Projection adjusted = persp.perspective_znear_adjusted(2);
  217. CHECK(adjusted[0] == persp[0]);
  218. CHECK(adjusted[1] == persp[1]);
  219. CHECK(adjusted[2].is_equal_approx(Vector4(persp[2][0], persp[2][1], -1.083333, persp[2][3])));
  220. CHECK(adjusted[3].is_equal_approx(Vector4(persp[3][0], persp[3][1], -4.166666, persp[3][3])));
  221. persp.adjust_perspective_znear(2);
  222. CHECK(persp[0] == adjusted[0]);
  223. CHECK(persp[1] == adjusted[1]);
  224. CHECK(persp[2] == adjusted[2]);
  225. CHECK(persp[3] == adjusted[3]);
  226. }
  227. TEST_CASE("[Projection] Set light bias") {
  228. Projection proj;
  229. proj.set_light_bias();
  230. CHECK(proj[0] == Vector4(0.5, 0, 0, 0));
  231. CHECK(proj[1] == Vector4(0, 0.5, 0, 0));
  232. CHECK(proj[2] == Vector4(0, 0, 0.5, 0));
  233. CHECK(proj[3] == Vector4(0.5, 0.5, 0.5, 1));
  234. }
  235. TEST_CASE("[Projection] Depth correction") {
  236. Projection corrected = Projection::create_depth_correction(true);
  237. CHECK(corrected[0] == Vector4(1, 0, 0, 0));
  238. CHECK(corrected[1] == Vector4(0, -1, 0, 0));
  239. CHECK(corrected[2] == Vector4(0, 0, -0.5, 0));
  240. CHECK(corrected[3] == Vector4(0, 0, 0.5, 1));
  241. Projection proj;
  242. proj.set_depth_correction(true, true, true);
  243. CHECK(proj[0] == corrected[0]);
  244. CHECK(proj[1] == corrected[1]);
  245. CHECK(proj[2] == corrected[2]);
  246. CHECK(proj[3] == corrected[3]);
  247. proj.set_depth_correction(false, true, true);
  248. CHECK(proj[0] == Vector4(1, 0, 0, 0));
  249. CHECK(proj[1] == Vector4(0, 1, 0, 0));
  250. CHECK(proj[2] == Vector4(0, 0, -0.5, 0));
  251. CHECK(proj[3] == Vector4(0, 0, 0.5, 1));
  252. proj.set_depth_correction(false, false, true);
  253. CHECK(proj[0] == Vector4(1, 0, 0, 0));
  254. CHECK(proj[1] == Vector4(0, 1, 0, 0));
  255. CHECK(proj[2] == Vector4(0, 0, 0.5, 0));
  256. CHECK(proj[3] == Vector4(0, 0, 0.5, 1));
  257. proj.set_depth_correction(false, false, false);
  258. CHECK(proj[0] == Vector4(1, 0, 0, 0));
  259. CHECK(proj[1] == Vector4(0, 1, 0, 0));
  260. CHECK(proj[2] == Vector4(0, 0, 1, 0));
  261. CHECK(proj[3] == Vector4(0, 0, 0, 1));
  262. proj.set_depth_correction(true, true, false);
  263. CHECK(proj[0] == Vector4(1, 0, 0, 0));
  264. CHECK(proj[1] == Vector4(0, -1, 0, 0));
  265. CHECK(proj[2] == Vector4(0, 0, -1, 0));
  266. CHECK(proj[3] == Vector4(0, 0, 0, 1));
  267. }
  268. TEST_CASE("[Projection] Light atlas rect") {
  269. Projection rect = Projection::create_light_atlas_rect(Rect2(1, 2, 30, 40));
  270. CHECK(rect[0] == Vector4(30, 0, 0, 0));
  271. CHECK(rect[1] == Vector4(0, 40, 0, 0));
  272. CHECK(rect[2] == Vector4(0, 0, 1, 0));
  273. CHECK(rect[3] == Vector4(1, 2, 0, 1));
  274. Projection proj;
  275. proj.set_light_atlas_rect(Rect2(1, 2, 30, 40));
  276. CHECK(proj[0] == rect[0]);
  277. CHECK(proj[1] == rect[1]);
  278. CHECK(proj[2] == rect[2]);
  279. CHECK(proj[3] == rect[3]);
  280. }
  281. TEST_CASE("[Projection] Make scale") {
  282. Projection proj;
  283. proj.make_scale(Vector3(2, 3, 4));
  284. CHECK(proj[0] == Vector4(2, 0, 0, 0));
  285. CHECK(proj[1] == Vector4(0, 3, 0, 0));
  286. CHECK(proj[2] == Vector4(0, 0, 4, 0));
  287. CHECK(proj[3] == Vector4(0, 0, 0, 1));
  288. }
  289. TEST_CASE("[Projection] Scale translate to fit aabb") {
  290. Projection fit = Projection::create_fit_aabb(AABB(Vector3(), Vector3(0.1, 0.2, 0.4)));
  291. CHECK(fit[0] == Vector4(20, 0, 0, 0));
  292. CHECK(fit[1] == Vector4(0, 10, 0, 0));
  293. CHECK(fit[2] == Vector4(0, 0, 5, 0));
  294. CHECK(fit[3] == Vector4(-1, -1, -1, 1));
  295. Projection proj;
  296. proj.scale_translate_to_fit(AABB(Vector3(), Vector3(0.1, 0.2, 0.4)));
  297. CHECK(proj[0] == fit[0]);
  298. CHECK(proj[1] == fit[1]);
  299. CHECK(proj[2] == fit[2]);
  300. CHECK(proj[3] == fit[3]);
  301. }
  302. TEST_CASE("[Projection] Perspective") {
  303. Projection persp = Projection::create_perspective(90, 0.5, 5, 15, false);
  304. CHECK(persp[0].is_equal_approx(Vector4(2, 0, 0, 0)));
  305. CHECK(persp[1].is_equal_approx(Vector4(0, 1, 0, 0)));
  306. CHECK(persp[2].is_equal_approx(Vector4(0, 0, -2, -1)));
  307. CHECK(persp[3].is_equal_approx(Vector4(0, 0, -15, 0)));
  308. Projection proj;
  309. proj.set_perspective(90, 0.5, 5, 15, false);
  310. CHECK(proj[0] == persp[0]);
  311. CHECK(proj[1] == persp[1]);
  312. CHECK(proj[2] == persp[2]);
  313. CHECK(proj[3] == persp[3]);
  314. }
  315. TEST_CASE("[Projection] Frustum") {
  316. Projection frustum = Projection::create_frustum(15, 20, 10, 12, 5, 15);
  317. CHECK(frustum[0].is_equal_approx(Vector4(2, 0, 0, 0)));
  318. CHECK(frustum[1].is_equal_approx(Vector4(0, 5, 0, 0)));
  319. CHECK(frustum[2].is_equal_approx(Vector4(7, 11, -2, -1)));
  320. CHECK(frustum[3].is_equal_approx(Vector4(0, 0, -15, 0)));
  321. Projection proj;
  322. proj.set_frustum(15, 20, 10, 12, 5, 15);
  323. CHECK(proj[0] == frustum[0]);
  324. CHECK(proj[1] == frustum[1]);
  325. CHECK(proj[2] == frustum[2]);
  326. CHECK(proj[3] == frustum[3]);
  327. }
  328. TEST_CASE("[Projection] Ortho") {
  329. Projection ortho = Projection::create_orthogonal(15, 20, 10, 12, 5, 15);
  330. CHECK(ortho[0].is_equal_approx(Vector4(0.4, 0, 0, 0)));
  331. CHECK(ortho[1].is_equal_approx(Vector4(0, 1, 0, 0)));
  332. CHECK(ortho[2].is_equal_approx(Vector4(0, 0, -0.2, 0)));
  333. CHECK(ortho[3].is_equal_approx(Vector4(-7, -11, -2, 1)));
  334. Projection proj;
  335. proj.set_orthogonal(15, 20, 10, 12, 5, 15);
  336. CHECK(proj[0] == ortho[0]);
  337. CHECK(proj[1] == ortho[1]);
  338. CHECK(proj[2] == ortho[2]);
  339. CHECK(proj[3] == ortho[3]);
  340. }
  341. TEST_CASE("[Projection] get_fovy()") {
  342. double fov = Projection::get_fovy(90, 0.5);
  343. CHECK(fov == doctest::Approx(53.1301));
  344. }
  345. TEST_CASE("[Projection] Perspective values extraction") {
  346. Projection persp = Projection::create_perspective(90, 0.5, 1, 50, true);
  347. double znear = persp.get_z_near();
  348. double zfar = persp.get_z_far();
  349. double aspect = persp.get_aspect();
  350. double fov = persp.get_fov();
  351. CHECK(znear == doctest::Approx(1));
  352. CHECK(zfar == doctest::Approx(50));
  353. CHECK(aspect == doctest::Approx(0.5));
  354. CHECK(fov == doctest::Approx(90));
  355. persp.set_perspective(38, 1.3, 0.2, 8, false);
  356. znear = persp.get_z_near();
  357. zfar = persp.get_z_far();
  358. aspect = persp.get_aspect();
  359. fov = persp.get_fov();
  360. CHECK(znear == doctest::Approx(0.2));
  361. CHECK(zfar == doctest::Approx(8));
  362. CHECK(aspect == doctest::Approx(1.3));
  363. CHECK(fov == doctest::Approx(Projection::get_fovy(38, 1.3)));
  364. persp.set_perspective(47, 2.5, 0.9, 14, true);
  365. znear = persp.get_z_near();
  366. zfar = persp.get_z_far();
  367. aspect = persp.get_aspect();
  368. fov = persp.get_fov();
  369. CHECK(znear == doctest::Approx(0.9));
  370. CHECK(zfar == doctest::Approx(14));
  371. CHECK(aspect == doctest::Approx(2.5));
  372. CHECK(fov == doctest::Approx(47));
  373. }
  374. TEST_CASE("[Projection] Frustum values extraction") {
  375. Projection frustum = Projection::create_frustum_aspect(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, true);
  376. double znear = frustum.get_z_near();
  377. double zfar = frustum.get_z_far();
  378. double aspect = frustum.get_aspect();
  379. double fov = frustum.get_fov();
  380. CHECK(znear == doctest::Approx(0.5));
  381. CHECK(zfar == doctest::Approx(50));
  382. CHECK(aspect == doctest::Approx(4.0 / 3.0));
  383. CHECK(fov == doctest::Approx(Math::rad_to_deg(Math::atan(2.0))));
  384. frustum.set_frustum(2.0, 1.5, Vector2(-0.5, 2), 2, 12, false);
  385. znear = frustum.get_z_near();
  386. zfar = frustum.get_z_far();
  387. aspect = frustum.get_aspect();
  388. fov = frustum.get_fov();
  389. CHECK(znear == doctest::Approx(2));
  390. CHECK(zfar == doctest::Approx(12));
  391. CHECK(aspect == doctest::Approx(1.5));
  392. CHECK(fov == doctest::Approx(Math::rad_to_deg(Math::atan(1.0) + Math::atan(0.5))));
  393. }
  394. TEST_CASE("[Projection] Orthographic values extraction") {
  395. Projection ortho = Projection::create_orthogonal(-2, 3, -0.5, 1.5, 1.2, 15);
  396. double znear = ortho.get_z_near();
  397. double zfar = ortho.get_z_far();
  398. double aspect = ortho.get_aspect();
  399. CHECK(znear == doctest::Approx(1.2));
  400. CHECK(zfar == doctest::Approx(15));
  401. CHECK(aspect == doctest::Approx(2.5));
  402. ortho.set_orthogonal(-7, 2, 2.5, 5.5, 0.5, 6);
  403. znear = ortho.get_z_near();
  404. zfar = ortho.get_z_far();
  405. aspect = ortho.get_aspect();
  406. CHECK(znear == doctest::Approx(0.5));
  407. CHECK(zfar == doctest::Approx(6));
  408. CHECK(aspect == doctest::Approx(3));
  409. }
  410. TEST_CASE("[Projection] Orthographic check") {
  411. Projection persp = Projection::create_perspective(90, 0.5, 1, 50, false);
  412. Projection ortho = Projection::create_orthogonal(15, 20, 10, 12, 5, 15);
  413. CHECK(!persp.is_orthogonal());
  414. CHECK(ortho.is_orthogonal());
  415. }
  416. TEST_CASE("[Projection] Planes extraction") {
  417. Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
  418. Vector<Plane> planes = persp.get_projection_planes(Transform3D());
  419. CHECK(planes[Projection::PLANE_NEAR].normalized().is_equal_approx(Plane(0, 0, 1, -1)));
  420. CHECK(planes[Projection::PLANE_FAR].normalized().is_equal_approx(Plane(0, 0, -1, 40)));
  421. CHECK(planes[Projection::PLANE_LEFT].normalized().is_equal_approx(Plane(-0.707107, 0, 0.707107, 0)));
  422. CHECK(planes[Projection::PLANE_TOP].normalized().is_equal_approx(Plane(0, 0.707107, 0.707107, 0)));
  423. CHECK(planes[Projection::PLANE_RIGHT].normalized().is_equal_approx(Plane(0.707107, 0, 0.707107, 0)));
  424. CHECK(planes[Projection::PLANE_BOTTOM].normalized().is_equal_approx(Plane(0, -0.707107, 0.707107, 0)));
  425. Plane plane_array[6]{
  426. persp.get_projection_plane(Projection::PLANE_NEAR),
  427. persp.get_projection_plane(Projection::PLANE_FAR),
  428. persp.get_projection_plane(Projection::PLANE_LEFT),
  429. persp.get_projection_plane(Projection::PLANE_TOP),
  430. persp.get_projection_plane(Projection::PLANE_RIGHT),
  431. persp.get_projection_plane(Projection::PLANE_BOTTOM)
  432. };
  433. CHECK(plane_array[Projection::PLANE_NEAR].normalized().is_equal_approx(planes[Projection::PLANE_NEAR].normalized()));
  434. CHECK(plane_array[Projection::PLANE_FAR].normalized().is_equal_approx(planes[Projection::PLANE_FAR].normalized()));
  435. CHECK(plane_array[Projection::PLANE_LEFT].normalized().is_equal_approx(planes[Projection::PLANE_LEFT].normalized()));
  436. CHECK(plane_array[Projection::PLANE_TOP].normalized().is_equal_approx(planes[Projection::PLANE_TOP].normalized()));
  437. CHECK(plane_array[Projection::PLANE_RIGHT].normalized().is_equal_approx(planes[Projection::PLANE_RIGHT].normalized()));
  438. CHECK(plane_array[Projection::PLANE_BOTTOM].normalized().is_equal_approx(planes[Projection::PLANE_BOTTOM].normalized()));
  439. }
  440. TEST_CASE("[Projection] Perspective Half extents") {
  441. constexpr real_t sqrt3 = 1.7320508;
  442. Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
  443. Vector2 ne = persp.get_viewport_half_extents();
  444. Vector2 fe = persp.get_far_plane_half_extents();
  445. CHECK(ne.is_equal_approx(Vector2(1, 1) * 1));
  446. CHECK(fe.is_equal_approx(Vector2(1, 1) * 40));
  447. persp.set_perspective(120, sqrt3, 0.8, 10, true);
  448. ne = persp.get_viewport_half_extents();
  449. fe = persp.get_far_plane_half_extents();
  450. CHECK(ne.is_equal_approx(Vector2(sqrt3, 1.0) * 0.8));
  451. CHECK(fe.is_equal_approx(Vector2(sqrt3, 1.0) * 10));
  452. persp.set_perspective(60, 1.2, 0.5, 15, false);
  453. ne = persp.get_viewport_half_extents();
  454. fe = persp.get_far_plane_half_extents();
  455. CHECK(ne.is_equal_approx(Vector2(sqrt3 / 3 * 1.2, sqrt3 / 3) * 0.5));
  456. CHECK(fe.is_equal_approx(Vector2(sqrt3 / 3 * 1.2, sqrt3 / 3) * 15));
  457. }
  458. TEST_CASE("[Projection] Orthographic Half extents") {
  459. Projection ortho = Projection::create_orthogonal(-3, 3, -1.5, 1.5, 1.2, 15);
  460. Vector2 ne = ortho.get_viewport_half_extents();
  461. Vector2 fe = ortho.get_far_plane_half_extents();
  462. CHECK(ne.is_equal_approx(Vector2(3, 1.5)));
  463. CHECK(fe.is_equal_approx(Vector2(3, 1.5)));
  464. ortho.set_orthogonal(-7, 7, -2.5, 2.5, 0.5, 6);
  465. ne = ortho.get_viewport_half_extents();
  466. fe = ortho.get_far_plane_half_extents();
  467. CHECK(ne.is_equal_approx(Vector2(7, 2.5)));
  468. CHECK(fe.is_equal_approx(Vector2(7, 2.5)));
  469. }
  470. TEST_CASE("[Projection] Endpoints") {
  471. constexpr real_t sqrt3 = 1.7320508;
  472. Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
  473. Vector3 ep[8];
  474. persp.get_endpoints(Transform3D(), ep);
  475. CHECK(ep[0].is_equal_approx(Vector3(-1, 1, -1) * 40));
  476. CHECK(ep[1].is_equal_approx(Vector3(-1, -1, -1) * 40));
  477. CHECK(ep[2].is_equal_approx(Vector3(1, 1, -1) * 40));
  478. CHECK(ep[3].is_equal_approx(Vector3(1, -1, -1) * 40));
  479. CHECK(ep[4].is_equal_approx(Vector3(-1, 1, -1) * 1));
  480. CHECK(ep[5].is_equal_approx(Vector3(-1, -1, -1) * 1));
  481. CHECK(ep[6].is_equal_approx(Vector3(1, 1, -1) * 1));
  482. CHECK(ep[7].is_equal_approx(Vector3(1, -1, -1) * 1));
  483. persp.set_perspective(120, sqrt3, 0.8, 10, true);
  484. persp.get_endpoints(Transform3D(), ep);
  485. CHECK(ep[0].is_equal_approx(Vector3(-sqrt3, 1, -1) * 10));
  486. CHECK(ep[1].is_equal_approx(Vector3(-sqrt3, -1, -1) * 10));
  487. CHECK(ep[2].is_equal_approx(Vector3(sqrt3, 1, -1) * 10));
  488. CHECK(ep[3].is_equal_approx(Vector3(sqrt3, -1, -1) * 10));
  489. CHECK(ep[4].is_equal_approx(Vector3(-sqrt3, 1, -1) * 0.8));
  490. CHECK(ep[5].is_equal_approx(Vector3(-sqrt3, -1, -1) * 0.8));
  491. CHECK(ep[6].is_equal_approx(Vector3(sqrt3, 1, -1) * 0.8));
  492. CHECK(ep[7].is_equal_approx(Vector3(sqrt3, -1, -1) * 0.8));
  493. persp.set_perspective(60, 1.2, 0.5, 15, false);
  494. persp.get_endpoints(Transform3D(), ep);
  495. CHECK(ep[0].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 15));
  496. CHECK(ep[1].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 15));
  497. CHECK(ep[2].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 15));
  498. CHECK(ep[3].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 15));
  499. CHECK(ep[4].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 0.5));
  500. CHECK(ep[5].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 0.5));
  501. CHECK(ep[6].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 0.5));
  502. CHECK(ep[7].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 0.5));
  503. }
  504. TEST_CASE("[Projection] LOD multiplier") {
  505. constexpr real_t sqrt3 = 1.7320508;
  506. Projection proj;
  507. real_t multiplier;
  508. proj.set_perspective(60, 1, 1, 40, false);
  509. multiplier = proj.get_lod_multiplier();
  510. CHECK(multiplier == doctest::Approx(2 * sqrt3 / 3));
  511. proj.set_perspective(120, 1.5, 0.5, 20, false);
  512. multiplier = proj.get_lod_multiplier();
  513. CHECK(multiplier == doctest::Approx(3 * sqrt3));
  514. proj.set_orthogonal(15, 20, 10, 12, 5, 15);
  515. multiplier = proj.get_lod_multiplier();
  516. CHECK(multiplier == doctest::Approx(5));
  517. proj.set_orthogonal(-5, 15, -8, 10, 1.5, 10);
  518. multiplier = proj.get_lod_multiplier();
  519. CHECK(multiplier == doctest::Approx(20));
  520. proj.set_frustum(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, false);
  521. multiplier = proj.get_lod_multiplier();
  522. CHECK(multiplier == doctest::Approx(8.0 / 3.0));
  523. proj.set_frustum(2.0, 1.2, Vector2(-0.1, 0.8), 1, 10, true);
  524. multiplier = proj.get_lod_multiplier();
  525. CHECK(multiplier == doctest::Approx(2));
  526. }
  527. TEST_CASE("[Projection] Pixels per meter") {
  528. constexpr real_t sqrt3 = 1.7320508;
  529. Projection proj;
  530. int ppm;
  531. proj.set_perspective(60, 1, 1, 40, false);
  532. ppm = proj.get_pixels_per_meter(1024);
  533. CHECK(ppm == int(1536.0f / sqrt3));
  534. proj.set_perspective(120, 1.5, 0.5, 20, false);
  535. ppm = proj.get_pixels_per_meter(1200);
  536. CHECK(ppm == int(800.0f / sqrt3));
  537. proj.set_orthogonal(15, 20, 10, 12, 5, 15);
  538. ppm = proj.get_pixels_per_meter(500);
  539. CHECK(ppm == 100);
  540. proj.set_orthogonal(-5, 15, -8, 10, 1.5, 10);
  541. ppm = proj.get_pixels_per_meter(640);
  542. CHECK(ppm == 32);
  543. proj.set_frustum(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, false);
  544. ppm = proj.get_pixels_per_meter(2048);
  545. CHECK(ppm == 1536);
  546. proj.set_frustum(2.0, 1.2, Vector2(-0.1, 0.8), 1, 10, true);
  547. ppm = proj.get_pixels_per_meter(800);
  548. CHECK(ppm == 400);
  549. }
  550. } //namespace TestProjection