summaryrefslogtreecommitdiff
path: root/libs/cglm/test/src/test_project.h
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2022-06-14 00:06:42 -0500
committersanine <sanine.not@pm.me>2022-06-14 00:06:42 -0500
commit2f518e5e28d35ae24a5ac0e31000835e43b01972 (patch)
tree47fdeb9fa5b04e267702acb06424d3f87b37dd84 /libs/cglm/test/src/test_project.h
parent034d5c965ff34cfdf4b153af9f32360a02e35684 (diff)
add cglm as 3rd-party library
Diffstat (limited to 'libs/cglm/test/src/test_project.h')
-rw-r--r--libs/cglm/test/src/test_project.h92
1 files changed, 92 insertions, 0 deletions
diff --git a/libs/cglm/test/src/test_project.h b/libs/cglm/test/src/test_project.h
new file mode 100644
index 0000000..41bdecd
--- /dev/null
+++ b/libs/cglm/test/src/test_project.h
@@ -0,0 +1,92 @@
+/*
+ * 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, unprojecti) {
+ mat4 model, view, proj, mvp;
+ vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
+ vec3 pos = {13.0f, 45.0f, 0.74f};
+ vec3 projected, unprojected;
+
+ glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
+ glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);
+
+ glm_perspective_default(0.5f, proj);
+ glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);
+
+ GLM(project)(pos, mvp, viewport, projected);
+
+ glm_mat4_inv(mvp, mvp);
+ GLM(unprojecti)(projected, mvp, viewport, unprojected);
+
+ /* unprojected of projected vector must be same as original one */
+ /* we used 0.01 because of projection floating point errors */
+ ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
+ ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
+ ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)
+
+ TEST_SUCCESS
+}
+
+TEST_IMPL(GLM_PREFIX, unproject) {
+ mat4 model, view, proj, mvp;
+ vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
+ vec3 pos = {13.0f, 45.0f, 0.74f};
+ vec3 projected, unprojected;
+
+ glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
+ glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);
+
+ glm_perspective_default(0.5f, proj);
+ glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);
+
+ GLM(project)(pos, mvp, viewport, projected);
+ GLM(unproject)(projected, mvp, viewport, unprojected);
+
+ /* unprojected of projected vector must be same as original one */
+ /* we used 0.01 because of projection floating point errors */
+ ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
+ ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
+ ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)
+
+ TEST_SUCCESS
+}
+
+TEST_IMPL(GLM_PREFIX, project) {
+ mat4 model, view, proj, mvp;
+ vec4 viewport = {0.0f, 0.0f, 800.0f, 600.0f};
+ vec3 pos = {13.0f, 45.0f, 0.74f};
+ vec3 projected, unprojected;
+
+ glm_translate_make(model, (vec3){0.0f, 0.0f, -10.0f});
+ glm_lookat((vec3){0.0f, 0.0f, 0.0f}, pos, GLM_YUP, view);
+
+ glm_perspective_default(0.5f, proj);
+ glm_mat4_mulN((mat4 *[]){&proj, &view, &model}, 3, mvp);
+
+ GLM(project)(pos, mvp, viewport, projected);
+ GLM(unproject)(projected, mvp, viewport, unprojected);
+
+ /* unprojected of projected vector must be same as original one */
+ /* we used 0.01 because of projection floating point errors */
+ ASSERT(fabsf(pos[0] - unprojected[0]) < 0.01)
+ ASSERT(fabsf(pos[1] - unprojected[1]) < 0.01)
+ ASSERT(fabsf(pos[2] - unprojected[2]) < 0.01)
+
+ /* test with no projection */
+ glm_mat4_identity(mvp);
+
+ GLM(project)(pos, mvp, viewport, projected);
+ GLM(unproject)(projected, mvp, viewport, unprojected);
+
+ ASSERT(test_eq(pos[0], unprojected[0]))
+ ASSERT(test_eq(pos[1], unprojected[1]))
+ ASSERT(test_eq(pos[2], unprojected[2]))
+
+ TEST_SUCCESS
+}