From c0a4c245f0b992de6874f7143ea2e0020f682fa1 Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Thu, 28 May 2020 14:19:35 +0300 Subject: [PATCH] tests: add test for frustum --- test/src/test_camera.h | 68 ++++++++++++++++++++++++++++++++++++++++++ test/src/tests.c | 2 ++ test/tests.h | 8 +++++ 3 files changed, 78 insertions(+) create mode 100644 test/src/test_camera.h diff --git a/test/src/test_camera.h b/test/src/test_camera.h new file mode 100644 index 0000000..fb1075a --- /dev/null +++ b/test/src/test_camera.h @@ -0,0 +1,68 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "test_common.h" + +TEST_IMPL(GLM_PREFIX, frustum) { + mat4 proj; + vec4 vp = {0.0f, 0.0f, 800.0f, 600.0f}; + float left, right, top, bottom, znear, zfar; + + znear = 0.1f; + zfar = 100.0f; + left = -100.0f; + right = 100.0f; + bottom = -100.0f; + top = 100.0f; + + GLM(frustum)(left, right, bottom, top, znear, zfar, proj); + + ASSERT(test_eq(proj[0][1], 0.0f)) + ASSERT(test_eq(proj[0][2], 0.0f)) + ASSERT(test_eq(proj[0][3], 0.0f)) + + ASSERT(test_eq(proj[1][0], 0.0f)) + ASSERT(test_eq(proj[1][2], 0.0f)) + ASSERT(test_eq(proj[1][3], 0.0f)) + + ASSERT(test_eq(proj[2][3], -1.0f)) + + ASSERT(test_eq(proj[3][0], 0.0f)) + ASSERT(test_eq(proj[3][1], 0.0f)) + ASSERT(test_eq(proj[3][3], 0.0f)) + + vec4 v1 = {1.0f, 20.0f, znear}; + vec4 v2 = {1.0f, 20.0f, zfar}; + vec4 v3, v4; + + /* perspective test */ + GLM(mat4_mulv)(proj, v1, v3); + GLM(project)(v3, proj, vp, v3); + + ASSERT(v3[0] > v1[0]) + ASSERT(v3[1] > v1[1]) + + GLM(mat4_mulv)(proj, v2, v4); + GLM(project)(v4, proj, vp, v4); + + ASSERT(v4[0] < v3[0]) + ASSERT(v4[1] < v3[1]) + + /* not infinity */ + ASSERT(!GLM(vec4_isinf)(proj[0])) + ASSERT(!GLM(vec4_isinf)(proj[1])) + ASSERT(!GLM(vec4_isinf)(proj[2])) + ASSERT(!GLM(vec4_isinf)(proj[3])) + + /* not NaN */ + ASSERT(!GLM(vec4_isnan)(proj[0])) + ASSERT(!GLM(vec4_isnan)(proj[1])) + ASSERT(!GLM(vec4_isnan)(proj[2])) + ASSERT(!GLM(vec4_isnan)(proj[3])) + + TEST_SUCCESS +} diff --git a/test/src/tests.c b/test/src/tests.c index ea9f902..2ea66c3 100644 --- a/test/src/tests.c +++ b/test/src/tests.c @@ -24,6 +24,7 @@ #include "test_affine.h" #include "test_affine_mat.h" #include "test_ray.h" +#include "test_camera.h" #undef GLM #undef GLM_PREFIX @@ -46,6 +47,7 @@ #include "test_affine.h" #include "test_affine_mat.h" #include "test_ray.h" +#include "test_camera.h" #undef GLM #undef GLM_PREFIX diff --git a/test/tests.h b/test/tests.h index 7ce7eae..6ccfc91 100644 --- a/test/tests.h +++ b/test/tests.h @@ -199,6 +199,10 @@ TEST_DECLARE(glmc_mat2_rmc) TEST_DECLARE(camera_lookat) TEST_DECLARE(camera_decomp) +TEST_DECLARE(glm_frustum) + +TEST_DECLARE(glmc_frustum) + /* project */ TEST_DECLARE(glm_unprojecti) TEST_DECLARE(glm_unproject) @@ -890,6 +894,10 @@ TEST_LIST { TEST_ENTRY(camera_lookat) TEST_ENTRY(camera_decomp) + TEST_ENTRY(glm_frustum) + + TEST_ENTRY(glmc_frustum) + /* project */ TEST_ENTRY(glm_unprojecti) TEST_ENTRY(glm_unproject)