Debian Codegraph @

tests/core/math/test_projection.h

raw symbols SCIP
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
31 #pragma once
32
33 #include "core/math/aabb.h"
34 #include "core/math/plane.h"
35 #include "core/math/projection.h"
36 #include "core/math/rect2.h"
37 #include "core/math/transform_3d.h"
38
39 #include "thirdparty/doctest/doctest.h"
40
41 namespace TestProjection {
42
43 TEST_CASE("[Projection] Construction") {
44 Projection default_proj;
45
46 CHECK(default_proj[0].is_equal_approx(Vector4(1, 0, 0, 0)));
47 CHECK(default_proj[1].is_equal_approx(Vector4(0, 1, 0, 0)));
48 CHECK(default_proj[2].is_equal_approx(Vector4(0, 0, 1, 0)));
49 CHECK(default_proj[3].is_equal_approx(Vector4(0, 0, 0, 1)));
50
51 Projection from_vec4(
52 Vector4(1, 2, 3, 4),
53 Vector4(5, 6, 7, 8),
54 Vector4(9, 10, 11, 12),
55 Vector4(13, 14, 15, 16));
56
57 CHECK(from_vec4[0].is_equal_approx(Vector4(1, 2, 3, 4)));
58 CHECK(from_vec4[1].is_equal_approx(Vector4(5, 6, 7, 8)));
59 CHECK(from_vec4[2].is_equal_approx(Vector4(9, 10, 11, 12)));
60 CHECK(from_vec4[3].is_equal_approx(Vector4(13, 14, 15, 16)));
61
62 Transform3D transform(
63 Basis(
64 Vector3(1, 0, 0),
65 Vector3(0, 2, 0),
66 Vector3(0, 0, 3)),
67 Vector3(4, 5, 6));
68
69 Projection from_transform(transform);
70
71 CHECK(from_transform[0].is_equal_approx(Vector4(1, 0, 0, 0)));
72 CHECK(from_transform[1].is_equal_approx(Vector4(0, 2, 0, 0)));
73 CHECK(from_transform[2].is_equal_approx(Vector4(0, 0, 3, 0)));
74 CHECK(from_transform[3].is_equal_approx(Vector4(4, 5, 6, 1)));
75 }
76
77 TEST_CASE("[Projection] set_zero()") {
78 Projection proj;
79 proj.set_zero();
80
81 for (int i = 0; i < 4; i++) {
82 for (int j = 0; j < 4; j++) {
83 CHECK(proj.columns[i][j] == 0);
84 }
85 }
86 }
87
88 TEST_CASE("[Projection] set_identity()") {
89 Projection proj;
90 proj.set_identity();
91
92 for (int i = 0; i < 4; i++) {
93 for (int j = 0; j < 4; j++) {
94 CHECK(proj.columns[i][j] == (i == j ? 1 : 0));
95 }
96 }
97 }
98
99 TEST_CASE("[Projection] determinant()") {
100 Projection proj(
101 Vector4(1, 5, 9, 13),
102 Vector4(2, 6, 11, 15),
103 Vector4(4, 7, 11, 15),
104 Vector4(4, 8, 12, 16));
105
106 CHECK(proj.determinant() == -12);
107 }
108
109 TEST_CASE("[Projection] Inverse and invert") {
110 SUBCASE("[Projection] Arbitrary projection matrix inversion") {
111 Projection proj(
112 Vector4(1, 5, 9, 13),
113 Vector4(2, 6, 11, 15),
114 Vector4(4, 7, 11, 15),
115 Vector4(4, 8, 12, 16));
116
117 Projection inverse_truth(
118 Vector4(-4.0 / 12, 0, 1, -8.0 / 12),
119 Vector4(8.0 / 12, -1, -1, 16.0 / 12),
120 Vector4(-20.0 / 12, 2, -1, 5.0 / 12),
121 Vector4(1, -1, 1, -0.75));
122
123 Projection inverse = proj.inverse();
124 CHECK(inverse[0].is_equal_approx(inverse_truth[0]));
125 CHECK(inverse[1].is_equal_approx(inverse_truth[1]));
126 CHECK(inverse[2].is_equal_approx(inverse_truth[2]));
127 CHECK(inverse[3].is_equal_approx(inverse_truth[3]));
128
129 proj.invert();
130 CHECK(proj[0].is_equal_approx(inverse_truth[0]));
131 CHECK(proj[1].is_equal_approx(inverse_truth[1]));
132 CHECK(proj[2].is_equal_approx(inverse_truth[2]));
133 CHECK(proj[3].is_equal_approx(inverse_truth[3]));
134 }
135
136 SUBCASE("[Projection] Orthogonal projection matrix inversion") {
137 Projection p = Projection::create_orthogonal(-125.0f, 125.0f, -125.0f, 125.0f, 0.01f, 25.0f);
138 p = p.inverse() * p;
139
140 CHECK(p[0].is_equal_approx(Vector4(1, 0, 0, 0)));
141 CHECK(p[1].is_equal_approx(Vector4(0, 1, 0, 0)));
142 CHECK(p[2].is_equal_approx(Vector4(0, 0, 1, 0)));
143 CHECK(p[3].is_equal_approx(Vector4(0, 0, 0, 1)));
144 }
145
146 SUBCASE("[Projection] Perspective projection matrix inversion") {
147 Projection p = Projection::create_perspective(90.0f, 1.77777f, 0.05f, 4000.0f);
148 p = p.inverse() * p;
149
150 CHECK(p[0].is_equal_approx(Vector4(1, 0, 0, 0)));
151 CHECK(p[1].is_equal_approx(Vector4(0, 1, 0, 0)));
152 CHECK(p[2].is_equal_approx(Vector4(0, 0, 1, 0)));
153 CHECK(p[3].is_equal_approx(Vector4(0, 0, 0, 1)));
154 }
155 }
156
157 TEST_CASE("[Projection] Matrix product") {
158 Projection proj1(
159 Vector4(1, 5, 9, 13),
160 Vector4(2, 6, 11, 15),
161 Vector4(4, 7, 11, 15),
162 Vector4(4, 8, 12, 16));
163
164 Projection proj2(
165 Vector4(0, 1, 2, 3),
166 Vector4(10, 11, 12, 13),
167 Vector4(20, 21, 22, 23),
168 Vector4(30, 31, 32, 33));
169
170 Projection prod = proj1 * proj2;
171
172 CHECK(prod[0].is_equal_approx(Vector4(22, 44, 69, 93)));
173 CHECK(prod[1].is_equal_approx(Vector4(132, 304, 499, 683)));
174 CHECK(prod[2].is_equal_approx(Vector4(242, 564, 929, 1273)));
175 CHECK(prod[3].is_equal_approx(Vector4(352, 824, 1359, 1863)));
176 }
177
178 TEST_CASE("[Projection] Vector transformation") {
179 Projection proj(
180 Vector4(1, 5, 9, 13),
181 Vector4(2, 6, 11, 15),
182 Vector4(4, 7, 11, 15),
183 Vector4(4, 8, 12, 16));
184
185 Vector4 vec4(1, 2, 3, 4);
186 CHECK(proj.xform(vec4).is_equal_approx(Vector4(33, 70, 112, 152)));
187 CHECK(proj.xform_inv(vec4).is_equal_approx(Vector4(90, 107, 111, 120)));
188
189 Vector3 vec3(1, 2, 3);
190 CHECK(proj.xform(vec3).is_equal_approx(Vector3(21, 46, 76) / 104));
191 }
192
193 TEST_CASE("[Projection] Plane transformation") {
194 Projection proj(
195 Vector4(1, 5, 9, 13),
196 Vector4(2, 6, 11, 15),
197 Vector4(4, 7, 11, 15),
198 Vector4(4, 8, 12, 16));
199
200 Plane plane(1, 2, 3, 4);
201 CHECK(proj.xform4(plane).is_equal_approx(Plane(33, 70, 112, 152)));
202 }
203
204 TEST_CASE("[Projection] Values access") {
205 Projection proj(
206 Vector4(00, 01, 02, 03),
207 Vector4(10, 11, 12, 13),
208 Vector4(20, 21, 22, 23),
209 Vector4(30, 31, 32, 33));
210
211 CHECK(proj[0] == Vector4(00, 01, 02, 03));
212 CHECK(proj[1] == Vector4(10, 11, 12, 13));
213 CHECK(proj[2] == Vector4(20, 21, 22, 23));
214 CHECK(proj[3] == Vector4(30, 31, 32, 33));
215 }
216
217 TEST_CASE("[Projection] flip_y() and flipped_y()") {
218 Projection proj(
219 Vector4(00, 01, 02, 03),
220 Vector4(10, 11, 12, 13),
221 Vector4(20, 21, 22, 23),
222 Vector4(30, 31, 32, 33));
223
224 Projection flipped = proj.flipped_y();
225
226 CHECK(flipped[0] == proj[0]);
227 CHECK(flipped[1] == -proj[1]);
228 CHECK(flipped[2] == proj[2]);
229 CHECK(flipped[3] == proj[3]);
230
231 proj.flip_y();
232
233 CHECK(proj[0] == flipped[0]);
234 CHECK(proj[1] == flipped[1]);
235 CHECK(proj[2] == flipped[2]);
236 CHECK(proj[3] == flipped[3]);
237 }
238
239 TEST_CASE("[Projection] Jitter offset") {
240 Projection proj(
241 Vector4(00, 01, 02, 03),
242 Vector4(10, 11, 12, 13),
243 Vector4(20, 21, 22, 23),
244 Vector4(30, 31, 32, 33));
245
246 Projection offsetted = proj.jitter_offseted(Vector2(1, 2));
247
248 CHECK(offsetted[0] == proj[0]);
249 CHECK(offsetted[1] == proj[1]);
250 CHECK(offsetted[2] == proj[2]);
251 CHECK(offsetted[3] == proj[3] + Vector4(1, 2, 0, 0));
252
253 proj.add_jitter_offset(Vector2(1, 2));
254
255 CHECK(proj[0] == offsetted[0]);
256 CHECK(proj[1] == offsetted[1]);
257 CHECK(proj[2] == offsetted[2]);
258 CHECK(proj[3] == offsetted[3]);
259 }
260
261 TEST_CASE("[Projection] Adjust znear") {
262 Projection persp = Projection::create_perspective(90, 0.5, 1, 50, false);
263 Projection adjusted = persp.perspective_znear_adjusted(2);
264
265 CHECK(adjusted[0] == persp[0]);
266 CHECK(adjusted[1] == persp[1]);
267 CHECK(adjusted[2].is_equal_approx(Vector4(persp[2][0], persp[2][1], -1.083333, persp[2][3])));
268 CHECK(adjusted[3].is_equal_approx(Vector4(persp[3][0], persp[3][1], -4.166666, persp[3][3])));
269
270 persp.adjust_perspective_znear(2);
271
272 CHECK(persp[0] == adjusted[0]);
273 CHECK(persp[1] == adjusted[1]);
274 CHECK(persp[2] == adjusted[2]);
275 CHECK(persp[3] == adjusted[3]);
276 }
277
278 TEST_CASE("[Projection] Set light bias") {
279 Projection proj;
280 proj.set_light_bias();
281
282 CHECK(proj[0] == Vector4(0.5, 0, 0, 0));
283 CHECK(proj[1] == Vector4(0, 0.5, 0, 0));
284 CHECK(proj[2] == Vector4(0, 0, 0.5, 0));
285 CHECK(proj[3] == Vector4(0.5, 0.5, 0.5, 1));
286 }
287
288 TEST_CASE("[Projection] Depth correction") {
289 Projection corrected = Projection::create_depth_correction(true);
290
291 CHECK(corrected[0] == Vector4(1, 0, 0, 0));
292 CHECK(corrected[1] == Vector4(0, -1, 0, 0));
293 CHECK(corrected[2] == Vector4(0, 0, -0.5, 0));
294 CHECK(corrected[3] == Vector4(0, 0, 0.5, 1));
295
296 Projection proj;
297 proj.set_depth_correction(true, true, true);
298
299 CHECK(proj[0] == corrected[0]);
300 CHECK(proj[1] == corrected[1]);
301 CHECK(proj[2] == corrected[2]);
302 CHECK(proj[3] == corrected[3]);
303
304 proj.set_depth_correction(false, true, true);
305
306 CHECK(proj[0] == Vector4(1, 0, 0, 0));
307 CHECK(proj[1] == Vector4(0, 1, 0, 0));
308 CHECK(proj[2] == Vector4(0, 0, -0.5, 0));
309 CHECK(proj[3] == Vector4(0, 0, 0.5, 1));
310
311 proj.set_depth_correction(false, false, true);
312
313 CHECK(proj[0] == Vector4(1, 0, 0, 0));
314 CHECK(proj[1] == Vector4(0, 1, 0, 0));
315 CHECK(proj[2] == Vector4(0, 0, 0.5, 0));
316 CHECK(proj[3] == Vector4(0, 0, 0.5, 1));
317
318 proj.set_depth_correction(false, false, false);
319
320 CHECK(proj[0] == Vector4(1, 0, 0, 0));
321 CHECK(proj[1] == Vector4(0, 1, 0, 0));
322 CHECK(proj[2] == Vector4(0, 0, 1, 0));
323 CHECK(proj[3] == Vector4(0, 0, 0, 1));
324
325 proj.set_depth_correction(true, true, false);
326
327 CHECK(proj[0] == Vector4(1, 0, 0, 0));
328 CHECK(proj[1] == Vector4(0, -1, 0, 0));
329 CHECK(proj[2] == Vector4(0, 0, -1, 0));
330 CHECK(proj[3] == Vector4(0, 0, 0, 1));
331 }
332
333 TEST_CASE("[Projection] Light atlas rect") {
334 Projection rect = Projection::create_light_atlas_rect(Rect2(1, 2, 30, 40));
335
336 CHECK(rect[0] == Vector4(30, 0, 0, 0));
337 CHECK(rect[1] == Vector4(0, 40, 0, 0));
338 CHECK(rect[2] == Vector4(0, 0, 1, 0));
339 CHECK(rect[3] == Vector4(1, 2, 0, 1));
340
341 Projection proj;
342 proj.set_light_atlas_rect(Rect2(1, 2, 30, 40));
343
344 CHECK(proj[0] == rect[0]);
345 CHECK(proj[1] == rect[1]);
346 CHECK(proj[2] == rect[2]);
347 CHECK(proj[3] == rect[3]);
348 }
349
350 TEST_CASE("[Projection] Make scale") {
351 Projection proj;
352 proj.make_scale(Vector3(2, 3, 4));
353
354 CHECK(proj[0] == Vector4(2, 0, 0, 0));
355 CHECK(proj[1] == Vector4(0, 3, 0, 0));
356 CHECK(proj[2] == Vector4(0, 0, 4, 0));
357 CHECK(proj[3] == Vector4(0, 0, 0, 1));
358 }
359
360 TEST_CASE("[Projection] Scale translate to fit aabb") {
361 Projection fit = Projection::create_fit_aabb(AABB(Vector3(), Vector3(0.1, 0.2, 0.4)));
362
363 CHECK(fit[0] == Vector4(20, 0, 0, 0));
364 CHECK(fit[1] == Vector4(0, 10, 0, 0));
365 CHECK(fit[2] == Vector4(0, 0, 5, 0));
366 CHECK(fit[3] == Vector4(-1, -1, -1, 1));
367
368 Projection proj;
369 proj.scale_translate_to_fit(AABB(Vector3(), Vector3(0.1, 0.2, 0.4)));
370
371 CHECK(proj[0] == fit[0]);
372 CHECK(proj[1] == fit[1]);
373 CHECK(proj[2] == fit[2]);
374 CHECK(proj[3] == fit[3]);
375 }
376
377 TEST_CASE("[Projection] Perspective") {
378 Projection persp = Projection::create_perspective(90, 0.5, 5, 15, false);
379
380 CHECK(persp[0].is_equal_approx(Vector4(2, 0, 0, 0)));
381 CHECK(persp[1].is_equal_approx(Vector4(0, 1, 0, 0)));
382 CHECK(persp[2].is_equal_approx(Vector4(0, 0, -2, -1)));
383 CHECK(persp[3].is_equal_approx(Vector4(0, 0, -15, 0)));
384
385 Projection proj;
386 proj.set_perspective(90, 0.5, 5, 15, false);
387
388 CHECK(proj[0] == persp[0]);
389 CHECK(proj[1] == persp[1]);
390 CHECK(proj[2] == persp[2]);
391 CHECK(proj[3] == persp[3]);
392 }
393
394 TEST_CASE("[Projection] Frustum") {
395 Projection frustum = Projection::create_frustum(15, 20, 10, 12, 5, 15);
396
397 CHECK(frustum[0].is_equal_approx(Vector4(2, 0, 0, 0)));
398 CHECK(frustum[1].is_equal_approx(Vector4(0, 5, 0, 0)));
399 CHECK(frustum[2].is_equal_approx(Vector4(7, 11, -2, -1)));
400 CHECK(frustum[3].is_equal_approx(Vector4(0, 0, -15, 0)));
401
402 Projection proj;
403 proj.set_frustum(15, 20, 10, 12, 5, 15);
404
405 CHECK(proj[0] == frustum[0]);
406 CHECK(proj[1] == frustum[1]);
407 CHECK(proj[2] == frustum[2]);
408 CHECK(proj[3] == frustum[3]);
409 }
410
411 TEST_CASE("[Projection] Ortho") {
412 Projection ortho = Projection::create_orthogonal(15, 20, 10, 12, 5, 15);
413
414 CHECK(ortho[0].is_equal_approx(Vector4(0.4, 0, 0, 0)));
415 CHECK(ortho[1].is_equal_approx(Vector4(0, 1, 0, 0)));
416 CHECK(ortho[2].is_equal_approx(Vector4(0, 0, -0.2, 0)));
417 CHECK(ortho[3].is_equal_approx(Vector4(-7, -11, -2, 1)));
418
419 Projection proj;
420 proj.set_orthogonal(15, 20, 10, 12, 5, 15);
421
422 CHECK(proj[0] == ortho[0]);
423 CHECK(proj[1] == ortho[1]);
424 CHECK(proj[2] == ortho[2]);
425 CHECK(proj[3] == ortho[3]);
426 }
427
428 TEST_CASE("[Projection] get_fovy()") {
429 double fov = Projection::get_fovy(90, 0.5);
430 CHECK(fov == doctest::Approx(53.1301));
431 }
432
433 TEST_CASE("[Projection] Perspective values extraction") {
434 Projection persp = Projection::create_perspective(90, 0.5, 1, 50, true);
435
436 double znear = persp.get_z_near();
437 double zfar = persp.get_z_far();
438 double aspect = persp.get_aspect();
439 double fov = persp.get_fov();
440
441 CHECK(znear == doctest::Approx(1));
442 CHECK(zfar == doctest::Approx(50));
443 CHECK(aspect == doctest::Approx(0.5));
444 CHECK(fov == doctest::Approx(90));
445
446 persp.set_perspective(38, 1.3, 0.2, 8, false);
447
448 znear = persp.get_z_near();
449 zfar = persp.get_z_far();
450 aspect = persp.get_aspect();
451 fov = persp.get_fov();
452
453 CHECK(znear == doctest::Approx(0.2));
454 CHECK(zfar == doctest::Approx(8));
455 CHECK(aspect == doctest::Approx(1.3));
456 CHECK(fov == doctest::Approx(Projection::get_fovy(38, 1.3)));
457
458 persp.set_perspective(47, 2.5, 0.9, 14, true);
459
460 znear = persp.get_z_near();
461 zfar = persp.get_z_far();
462 aspect = persp.get_aspect();
463 fov = persp.get_fov();
464
465 CHECK(znear == doctest::Approx(0.9));
466 CHECK(zfar == doctest::Approx(14));
467 CHECK(aspect == doctest::Approx(2.5));
468 CHECK(fov == doctest::Approx(47));
469 }
470
471 TEST_CASE("[Projection] Frustum values extraction") {
472 Projection frustum = Projection::create_frustum_aspect(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, true);
473
474 double znear = frustum.get_z_near();
475 double zfar = frustum.get_z_far();
476 double aspect = frustum.get_aspect();
477 double fov = frustum.get_fov();
478
479 CHECK(znear == doctest::Approx(0.5));
480 CHECK(zfar == doctest::Approx(50));
481 CHECK(aspect == doctest::Approx(4.0 / 3.0));
482 CHECK(fov == doctest::Approx(Math::rad_to_deg(Math::atan(2.0))));
483
484 frustum.set_frustum(2.0, 1.5, Vector2(-0.5, 2), 2, 12, false);
485
486 znear = frustum.get_z_near();
487 zfar = frustum.get_z_far();
488 aspect = frustum.get_aspect();
489 fov = frustum.get_fov();
490
491 CHECK(znear == doctest::Approx(2));
492 CHECK(zfar == doctest::Approx(12));
493 CHECK(aspect == doctest::Approx(1.5));
494 CHECK(fov == doctest::Approx(Math::rad_to_deg(Math::atan(1.0) + Math::atan(0.5))));
495 }
496
497 TEST_CASE("[Projection] Orthographic values extraction") {
498 Projection ortho = Projection::create_orthogonal(-2, 3, -0.5, 1.5, 1.2, 15);
499
500 double znear = ortho.get_z_near();
501 double zfar = ortho.get_z_far();
502 double aspect = ortho.get_aspect();
503
504 CHECK(znear == doctest::Approx(1.2));
505 CHECK(zfar == doctest::Approx(15));
506 CHECK(aspect == doctest::Approx(2.5));
507
508 ortho.set_orthogonal(-7, 2, 2.5, 5.5, 0.5, 6);
509
510 znear = ortho.get_z_near();
511 zfar = ortho.get_z_far();
512 aspect = ortho.get_aspect();
513
514 CHECK(znear == doctest::Approx(0.5));
515 CHECK(zfar == doctest::Approx(6));
516 CHECK(aspect == doctest::Approx(3));
517 }
518
519 TEST_CASE("[Projection] Orthographic check") {
520 Projection persp = Projection::create_perspective(90, 0.5, 1, 50, false);
521 Projection ortho = Projection::create_orthogonal(15, 20, 10, 12, 5, 15);
522
523 CHECK(!persp.is_orthogonal());
524 CHECK(ortho.is_orthogonal());
525 }
526
527 TEST_CASE("[Projection] Planes extraction") {
528 Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
529 Vector<Plane> planes = persp.get_projection_planes(Transform3D());
530
531 CHECK(planes[Projection::PLANE_NEAR].normalized().is_equal_approx(Plane(0, 0, 1, -1)));
532 CHECK(planes[Projection::PLANE_FAR].normalized().is_equal_approx(Plane(0, 0, -1, 40)));
533 CHECK(planes[Projection::PLANE_LEFT].normalized().is_equal_approx(Plane(-0.707107, 0, 0.707107, 0)));
534 CHECK(planes[Projection::PLANE_TOP].normalized().is_equal_approx(Plane(0, 0.707107, 0.707107, 0)));
535 CHECK(planes[Projection::PLANE_RIGHT].normalized().is_equal_approx(Plane(0.707107, 0, 0.707107, 0)));
536 CHECK(planes[Projection::PLANE_BOTTOM].normalized().is_equal_approx(Plane(0, -0.707107, 0.707107, 0)));
537
538 Plane plane_array[6]{
539 persp.get_projection_plane(Projection::PLANE_NEAR),
540 persp.get_projection_plane(Projection::PLANE_FAR),
541 persp.get_projection_plane(Projection::PLANE_LEFT),
542 persp.get_projection_plane(Projection::PLANE_TOP),
543 persp.get_projection_plane(Projection::PLANE_RIGHT),
544 persp.get_projection_plane(Projection::PLANE_BOTTOM)
545 };
546
547 CHECK(plane_array[Projection::PLANE_NEAR].normalized().is_equal_approx(planes[Projection::PLANE_NEAR].normalized()));
548 CHECK(plane_array[Projection::PLANE_FAR].normalized().is_equal_approx(planes[Projection::PLANE_FAR].normalized()));
549 CHECK(plane_array[Projection::PLANE_LEFT].normalized().is_equal_approx(planes[Projection::PLANE_LEFT].normalized()));
550 CHECK(plane_array[Projection::PLANE_TOP].normalized().is_equal_approx(planes[Projection::PLANE_TOP].normalized()));
551 CHECK(plane_array[Projection::PLANE_RIGHT].normalized().is_equal_approx(planes[Projection::PLANE_RIGHT].normalized()));
552 CHECK(plane_array[Projection::PLANE_BOTTOM].normalized().is_equal_approx(planes[Projection::PLANE_BOTTOM].normalized()));
553 }
554
555 TEST_CASE("[Projection] Perspective Half extents") {
556 constexpr real_t sqrt3 = 1.7320508;
557 Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
558 Vector2 ne = persp.get_viewport_half_extents();
559 Vector2 fe = persp.get_far_plane_half_extents();
560
561 CHECK(ne.is_equal_approx(Vector2(1, 1) * 1));
562 CHECK(fe.is_equal_approx(Vector2(1, 1) * 40));
563
564 persp.set_perspective(120, sqrt3, 0.8, 10, true);
565 ne = persp.get_viewport_half_extents();
566 fe = persp.get_far_plane_half_extents();
567
568 CHECK(ne.is_equal_approx(Vector2(sqrt3, 1.0) * 0.8));
569 CHECK(fe.is_equal_approx(Vector2(sqrt3, 1.0) * 10));
570
571 persp.set_perspective(60, 1.2, 0.5, 15, false);
572 ne = persp.get_viewport_half_extents();
573 fe = persp.get_far_plane_half_extents();
574
575 CHECK(ne.is_equal_approx(Vector2(sqrt3 / 3 * 1.2, sqrt3 / 3) * 0.5));
576 CHECK(fe.is_equal_approx(Vector2(sqrt3 / 3 * 1.2, sqrt3 / 3) * 15));
577 }
578
579 TEST_CASE("[Projection] Orthographic Half extents") {
580 Projection ortho = Projection::create_orthogonal(-3, 3, -1.5, 1.5, 1.2, 15);
581 Vector2 ne = ortho.get_viewport_half_extents();
582 Vector2 fe = ortho.get_far_plane_half_extents();
583
584 CHECK(ne.is_equal_approx(Vector2(3, 1.5)));
585 CHECK(fe.is_equal_approx(Vector2(3, 1.5)));
586
587 ortho.set_orthogonal(-7, 7, -2.5, 2.5, 0.5, 6);
588 ne = ortho.get_viewport_half_extents();
589 fe = ortho.get_far_plane_half_extents();
590
591 CHECK(ne.is_equal_approx(Vector2(7, 2.5)));
592 CHECK(fe.is_equal_approx(Vector2(7, 2.5)));
593 }
594
595 TEST_CASE("[Projection] Endpoints") {
596 constexpr real_t sqrt3 = 1.7320508;
597 Projection persp = Projection::create_perspective(90, 1, 1, 40, false);
598 Vector3 ep[8];
599 persp.get_endpoints(Transform3D(), ep);
600
601 CHECK(ep[0].is_equal_approx(Vector3(-1, 1, -1) * 40));
602 CHECK(ep[1].is_equal_approx(Vector3(-1, -1, -1) * 40));
603 CHECK(ep[2].is_equal_approx(Vector3(1, 1, -1) * 40));
604 CHECK(ep[3].is_equal_approx(Vector3(1, -1, -1) * 40));
605 CHECK(ep[4].is_equal_approx(Vector3(-1, 1, -1) * 1));
606 CHECK(ep[5].is_equal_approx(Vector3(-1, -1, -1) * 1));
607 CHECK(ep[6].is_equal_approx(Vector3(1, 1, -1) * 1));
608 CHECK(ep[7].is_equal_approx(Vector3(1, -1, -1) * 1));
609
610 persp.set_perspective(120, sqrt3, 0.8, 10, true);
611 persp.get_endpoints(Transform3D(), ep);
612
613 CHECK(ep[0].is_equal_approx(Vector3(-sqrt3, 1, -1) * 10));
614 CHECK(ep[1].is_equal_approx(Vector3(-sqrt3, -1, -1) * 10));
615 CHECK(ep[2].is_equal_approx(Vector3(sqrt3, 1, -1) * 10));
616 CHECK(ep[3].is_equal_approx(Vector3(sqrt3, -1, -1) * 10));
617 CHECK(ep[4].is_equal_approx(Vector3(-sqrt3, 1, -1) * 0.8));
618 CHECK(ep[5].is_equal_approx(Vector3(-sqrt3, -1, -1) * 0.8));
619 CHECK(ep[6].is_equal_approx(Vector3(sqrt3, 1, -1) * 0.8));
620 CHECK(ep[7].is_equal_approx(Vector3(sqrt3, -1, -1) * 0.8));
621
622 persp.set_perspective(60, 1.2, 0.5, 15, false);
623 persp.get_endpoints(Transform3D(), ep);
624
625 CHECK(ep[0].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 15));
626 CHECK(ep[1].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 15));
627 CHECK(ep[2].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 15));
628 CHECK(ep[3].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 15));
629 CHECK(ep[4].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 0.5));
630 CHECK(ep[5].is_equal_approx(Vector3(-sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 0.5));
631 CHECK(ep[6].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, sqrt3 / 3, -1) * 0.5));
632 CHECK(ep[7].is_equal_approx(Vector3(sqrt3 / 3 * 1.2, -sqrt3 / 3, -1) * 0.5));
633 }
634
635 TEST_CASE("[Projection] LOD multiplier") {
636 constexpr real_t sqrt3 = 1.7320508;
637 Projection proj;
638 real_t multiplier;
639
640 proj.set_perspective(60, 1, 1, 40, false);
641 multiplier = proj.get_lod_multiplier();
642 CHECK(multiplier == doctest::Approx(2 * sqrt3 / 3));
643
644 proj.set_perspective(120, 1.5, 0.5, 20, false);
645 multiplier = proj.get_lod_multiplier();
646 CHECK(multiplier == doctest::Approx(3 * sqrt3));
647
648 proj.set_orthogonal(15, 20, 10, 12, 5, 15);
649 multiplier = proj.get_lod_multiplier();
650 CHECK(multiplier == doctest::Approx(5));
651
652 proj.set_orthogonal(-5, 15, -8, 10, 1.5, 10);
653 multiplier = proj.get_lod_multiplier();
654 CHECK(multiplier == doctest::Approx(20));
655
656 proj.set_frustum(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, false);
657 multiplier = proj.get_lod_multiplier();
658 CHECK(multiplier == doctest::Approx(8.0 / 3.0));
659
660 proj.set_frustum(2.0, 1.2, Vector2(-0.1, 0.8), 1, 10, true);
661 multiplier = proj.get_lod_multiplier();
662 CHECK(multiplier == doctest::Approx(2));
663 }
664
665 TEST_CASE("[Projection] Pixels per meter") {
666 constexpr real_t sqrt3 = 1.7320508;
667 Projection proj;
668 int ppm;
669
670 proj.set_perspective(60, 1, 1, 40, false);
671 ppm = proj.get_pixels_per_meter(1024);
672 CHECK(ppm == int(1536.0f / sqrt3));
673
674 proj.set_perspective(120, 1.5, 0.5, 20, false);
675 ppm = proj.get_pixels_per_meter(1200);
676 CHECK(ppm == int(800.0f / sqrt3));
677
678 proj.set_orthogonal(15, 20, 10, 12, 5, 15);
679 ppm = proj.get_pixels_per_meter(500);
680 CHECK(ppm == 100);
681
682 proj.set_orthogonal(-5, 15, -8, 10, 1.5, 10);
683 ppm = proj.get_pixels_per_meter(640);
684 CHECK(ppm == 32);
685
686 proj.set_frustum(1.0, 4.0 / 3.0, Vector2(0.5, -0.25), 0.5, 50, false);
687 ppm = proj.get_pixels_per_meter(2048);
688 CHECK(ppm == 1536);
689
690 proj.set_frustum(2.0, 1.2, Vector2(-0.1, 0.8), 1, 10, true);
691 ppm = proj.get_pixels_per_meter(800);
692 CHECK(ppm == 400);
693 }
694 } //namespace TestProjection