From c013bd462cf4ade3432e7373cb96aeb855412f15 Mon Sep 17 00:00:00 2001 From: michaelg Date: Fri, 30 Apr 2021 21:53:17 +0100 Subject: [PATCH] Add LH & RH_NO perspective functions This commit adds functions `glm_perspective_lh_no` and `glm_perspective_rh_no` to the code. Unit tests are added and this commit follows the new pattern of adding the a new file per coordinate-system and clip-space tuple. . Makefile.am updated . removed test/glm_cmp project stub . unit tests include naive implementations to as well as magic number ref-data generated by the corresponding GLM functions. No tests run yet on Windows or Mac. --- CMakeLists.txt | 2 ++ Makefile.am | 6 ++++ include/cglm/cam_lh_no.h | 55 +++++++++++++++++++++++++++++++++++++ include/cglm/cam_lh_zo.h | 11 ++++---- include/cglm/cam_rh_no.h | 55 +++++++++++++++++++++++++++++++++++++ include/cglm/cam_rh_zo.h | 8 ++---- include/cglm/cglm.h | 2 ++ src/cam_lh_no.c | 23 ++++++++++++++++ src/cam_lh_zo.c | 1 - src/cam_rh_no.c | 23 ++++++++++++++++ src/cam_rh_zo.c | 1 - test/CMakeLists.txt | 2 ++ test/glm_cmp/.gitignore | 1 - test/glm_cmp/CMakeLists.txt | 12 -------- test/glm_cmp/README.md | 8 ------ test/glm_cmp/src/main.cpp | 39 -------------------------- test/src/test_cam_lh_no.c | 36 ++++++++++++++++++++++++ test/src/test_cam_lh_zo.c | 21 +++++++------- test/src/test_cam_rh_no.c | 36 ++++++++++++++++++++++++ test/src/test_cam_rh_zo.c | 9 +++--- test/tests.h | 4 +++ 21 files changed, 266 insertions(+), 89 deletions(-) create mode 100644 include/cglm/cam_lh_no.h create mode 100644 include/cglm/cam_rh_no.h create mode 100644 src/cam_lh_no.c create mode 100644 src/cam_rh_no.c delete mode 100644 test/glm_cmp/.gitignore delete mode 100644 test/glm_cmp/CMakeLists.txt delete mode 100644 test/glm_cmp/README.md delete mode 100644 test/glm_cmp/src/main.cpp create mode 100644 test/src/test_cam_lh_no.c create mode 100644 test/src/test_cam_rh_no.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e2048ae..2359131 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,8 @@ add_library(${PROJECT_NAME} src/cam.c src/cam_lh_zo.c src/cam_rh_zo.c + src/cam_lh_no.c + src/cam_rh_no.c src/vec2.c src/vec3.c src/vec4.c diff --git a/Makefile.am b/Makefile.am index f0e2afb..5aaec3b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -43,6 +43,8 @@ cglm_HEADERS = include/cglm/version.h \ include/cglm/cam.h \ include/cglm/cam_lh_zo.h \ include/cglm/cam_rh_zo.h \ + include/cglm/cam_lh_no.h \ + include/cglm/cam_rh_no.h \ include/cglm/io.h \ include/cglm/mat4.h \ include/cglm/mat3.h \ @@ -148,6 +150,8 @@ libcglm_la_SOURCES=\ src/cam.c \ src/cam_lh_zo.c \ src/cam_rh_zo.c \ + src/cam_lh_no.c \ + src/cam_rh_no.c \ src/vec2.c \ src/vec3.c \ src/vec4.c \ @@ -172,6 +176,8 @@ test_tests_SOURCES=\ test/src/test_cam.c \ test/src/test_cam_lh_zo.c \ test/src/test_cam_rh_zo.c \ + test/src/test_cam_lh_no.c \ + test/src/test_cam_rh_no.c \ test/src/test_clamp.c \ test/src/test_euler.c \ test/src/test_bezier.c \ diff --git a/include/cglm/cam_lh_no.h b/include/cglm/cam_lh_no.h new file mode 100644 index 0000000..2c6a6ab --- /dev/null +++ b/include/cglm/cam_lh_no.h @@ -0,0 +1,55 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE void glm_perspective_lh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) + */ + +#ifndef cglm_cam_lh_no_h +#define cglm_cam_lh_no_h + +#include "common.h" +#include "plane.h" + +/*! + * @brief set up perspective projection matrix with a left-hand coordinate + * system and a clip-space of [-1, 1] + * + * @param[in] fovy field of view angle + * @param[in] aspect aspect ratio ( width / height ) + * @param[in] nearVal near clipping plane + * @param[in] farVal far clipping planes + * @param[out] dest result matrix + */ +CGLM_INLINE +void +glm_perspective_lh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) { + /* Impl follows glm::perspectiveLH_NO in glm/ext/matrix_clip_space.inl */ + float fl, fn; + + glm_mat4_zero(dest); + + fl = 1.0f / tanf(fovy * 0.5f); + fn = 1.0f / (farVal - nearVal); + + dest[0][0] = fl / aspect; + dest[1][1] = fl; + dest[2][2] = (farVal + nearVal) * fn; + dest[2][3] = 1.0f; + dest[3][2] = -2.0f * farVal * nearVal * fn; +} + +#endif /*cglm_cam_lh_no_h*/ diff --git a/include/cglm/cam_lh_zo.h b/include/cglm/cam_lh_zo.h index ca71ac3..e800ff6 100644 --- a/include/cglm/cam_lh_zo.h +++ b/include/cglm/cam_lh_zo.h @@ -20,7 +20,6 @@ #include "common.h" #include "plane.h" - /*! * @brief set up perspective projection matrix with a left-hand coordinate * system (suitable, apparently, for DirectX and Metal) and a clip-space with @@ -47,11 +46,11 @@ glm_perspective_lh_zo(float fovy, fl = 1.0f / tanf(fovy * 0.5f); fn = 1.0f / (farVal - nearVal); - dest[0][0] = fl / aspect; - dest[1][1] = fl; - dest[2][2] = farVal * fn; - dest[2][3] = 1.0f; - dest[3][2] = -(farVal * nearVal * fn); + dest[0][0] = fl / aspect; + dest[1][1] = fl; + dest[2][2] = farVal * fn; + dest[2][3] = 1.0f; + dest[3][2] = -farVal * nearVal * fn; } #endif /*cglm_cam_lh_zo_h*/ diff --git a/include/cglm/cam_rh_no.h b/include/cglm/cam_rh_no.h new file mode 100644 index 0000000..71a99ad --- /dev/null +++ b/include/cglm/cam_rh_no.h @@ -0,0 +1,55 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +/* + Functions: + CGLM_INLINE void glm_perspective_rh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) + */ + +#ifndef cglm_cam_rh_no_h +#define cglm_cam_rh_no_h + +#include "common.h" +#include "plane.h" + +/*! + * @brief set up perspective projection matrix with a right-hand coordinate + * system and a clip space of [-1, 1]. + * + * @param[in] fovy field of view angle + * @param[in] aspect aspect ratio ( width / height ) + * @param[in] nearVal near clipping plane + * @param[in] farVal far clipping planes + * @param[out] dest result matrix + */ +CGLM_INLINE +void +glm_perspective_rh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) { + /* Impl follows glm::perspectiveRH_NO in glm/ext/matrix_clip_space.inl */ + float fl, fn; + + glm_mat4_zero(dest); + + fl = 1.0f / tanf(fovy * 0.5f); + fn = 1.0f / (farVal - nearVal); + + dest[0][0] = fl / aspect; + dest[1][1] = fl; + dest[2][2] = -(farVal + nearVal) * fn; + dest[2][3] = -1.0f; + dest[3][2] = -2.0f * farVal * nearVal * fn; +} + +#endif /*cglm_cam_rh_no_h*/ diff --git a/include/cglm/cam_rh_zo.h b/include/cglm/cam_rh_zo.h index 5f8a81d..bdb08c2 100644 --- a/include/cglm/cam_rh_zo.h +++ b/include/cglm/cam_rh_zo.h @@ -25,8 +25,6 @@ * system (suitable for Vulkan) and a clip-space with depth values from zero * to one. * - * https://github.com/godlikepanos/anki-3d-engine/blob/317cb379ff3a7b09f9034f49c7bdab0f96a1c0b3/AnKi/Math/Mat.h#L1254 - * * @param[in] fovy field of view angle * @param[in] aspect aspect ratio ( width / height ) * @param[in] nearVal near clipping plane @@ -48,11 +46,11 @@ glm_perspective_rh_zo(float fovy, fl = 1.0f / tanf(fovy * 0.5f); fn = 1.0f / (farVal - nearVal); - dest[0][0] = fl / aspect; - dest[1][1] = fl; + dest[0][0] = fl / aspect; + dest[1][1] = fl; dest[2][2] = -farVal * fn; dest[2][3] = -1.0f; dest[3][2] = -farVal * nearVal * fn; } -#endif /*cglm_cam_lh_zo_h*/ +#endif /*cglm_cam_rh_zo_h*/ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index b069d1d..c6c2805 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -19,6 +19,8 @@ #include "cam.h" #include "cam_lh_zo.h" #include "cam_rh_zo.h" +#include "cam_lh_no.h" +#include "cam_rh_no.h" #include "frustum.h" #include "quat.h" #include "euler.h" diff --git a/src/cam_lh_no.c b/src/cam_lh_no.c new file mode 100644 index 0000000..4354373 --- /dev/null +++ b/src/cam_lh_no.c @@ -0,0 +1,23 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" + +CGLM_EXPORT +void +glmc_perspective_lh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) { + glm_perspective_lh_no(fovy, + aspect, + nearVal, + farVal, + dest); +} + diff --git a/src/cam_lh_zo.c b/src/cam_lh_zo.c index 50f0ca7..a64dcd4 100644 --- a/src/cam_lh_zo.c +++ b/src/cam_lh_zo.c @@ -6,7 +6,6 @@ */ #include "../include/cglm/cglm.h" -#include "../include/cglm/cam_lh_zo.h" CGLM_EXPORT void diff --git a/src/cam_rh_no.c b/src/cam_rh_no.c new file mode 100644 index 0000000..3d7d4a8 --- /dev/null +++ b/src/cam_rh_no.c @@ -0,0 +1,23 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#include "../include/cglm/cglm.h" + +CGLM_EXPORT +void +glmc_perspective_rh_no(float fovy, + float aspect, + float nearVal, + float farVal, + mat4 dest) { + glm_perspective_rh_no(fovy, + aspect, + nearVal, + farVal, + dest); +} + diff --git a/src/cam_rh_zo.c b/src/cam_rh_zo.c index 8c5712f..949a506 100644 --- a/src/cam_rh_zo.c +++ b/src/cam_rh_zo.c @@ -6,7 +6,6 @@ */ #include "../include/cglm/cglm.h" -#include "../include/cglm/cam_rh_zo.h" CGLM_EXPORT void diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2bf26a1..26fef9c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,6 +8,8 @@ set(TESTFILES src/test_cam.c src/test_cam_lh_zo.c src/test_cam_rh_zo.c + src/test_cam_lh_no.c + src/test_cam_rh_no.c src/test_struct.c src/test_clamp.c src/test_common.c diff --git a/test/glm_cmp/.gitignore b/test/glm_cmp/.gitignore deleted file mode 100644 index 671b764..0000000 --- a/test/glm_cmp/.gitignore +++ /dev/null @@ -1 +0,0 @@ -glm diff --git a/test/glm_cmp/CMakeLists.txt b/test/glm_cmp/CMakeLists.txt deleted file mode 100644 index d0de862..0000000 --- a/test/glm_cmp/CMakeLists.txt +++ /dev/null @@ -1,12 +0,0 @@ -cmake_minimum_required(VERSION 3.17) -project(glm_cmp LANGUAGES CXX) - -set(CMAKE_CXX_STANDARD 14) -set(CMP_MAIN glmcmp) - -add_subdirectory(glm) - -add_executable(${CMP_MAIN} src/main.cpp) - -target_link_libraries(${CMP_MAIN} PRIVATE glm) - diff --git a/test/glm_cmp/README.md b/test/glm_cmp/README.md deleted file mode 100644 index 1c0c2ac..0000000 --- a/test/glm_cmp/README.md +++ /dev/null @@ -1,8 +0,0 @@ -### Simple GLM app for quick generation of reference values - -#### Usage -1. Clone, link or copy the GLM library's root directory to `test/glm_cmp/glm`. -1. Ensuring your current directory is `test/glm_cmp`: - `mkdir build && cd build && cmake .. && make && ./glmcmp` - -Please do not delete prior reference-data-producing functions as it may be necessary to return to these to examine future bugs or assumptions. By all means remove or comment-out the call site from the `main` function, though, to focus on what you're working on. diff --git a/test/glm_cmp/src/main.cpp b/test/glm_cmp/src/main.cpp deleted file mode 100644 index c594c02..0000000 --- a/test/glm_cmp/src/main.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include - -#include "glm/glm.hpp" -#include "glm/mat4x4.hpp" -#include - -static void outputForPerspectiveLH_ZO() { - const float fovy = glm::radians(45.0f); - const float aspect = 640/480.0f; - const float near = 0.1f; - const float far = 100.0f; - glm::mat4 cmp = glm::perspectiveLH_ZO(fovy, aspect, near, far); - puts("/*reference test data for glm_perspective_lh_zo*/"); - puts("mat4 cmp = {0};"); - printf("cmp[0][0] = %0.7ff;\n", cmp[0][0]); - printf("cmp[1][1] = %0.7ff;\n", cmp[1][1]); - printf("cmp[2][2] = %0.7ff;\n", cmp[2][2]); - printf("cmp[2][3] = %0.7ff;\n", cmp[2][3]); - printf("cmp[3][2] = %0.7ff;\n", cmp[3][2]); -} -static void outputForPerspectiveRH_ZO() { - const float fovy = glm::radians(45.0f); - const float aspect = 640/480.0f; - const float near = 0.1f; - const float far = 100.0f; - glm::mat4 cmp = glm::perspectiveRH_ZO(fovy, aspect, near, far); - puts("/*reference test data for glm_perspective_rh_zo*/"); - puts("mat4 cmp = {0};"); - printf("cmp[0][0] = %0.7ff;\n", cmp[0][0]); - printf("cmp[1][1] = %0.7ff;\n", cmp[1][1]); - printf("cmp[2][2] = %0.7ff;\n", cmp[2][2]); - printf("cmp[2][3] = %0.7ff;\n", cmp[2][3]); - printf("cmp[3][2] = %0.7ff;\n", cmp[3][2]); -} - -int main(int argc, char** argv) { - outputForPerspectiveRH_ZO(); - return 0; -} diff --git a/test/src/test_cam_lh_no.c b/test/src/test_cam_lh_no.c new file mode 100644 index 0000000..33a285a --- /dev/null +++ b/test/src/test_cam_lh_no.c @@ -0,0 +1,36 @@ +/* + * 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(perspective_lh_no) { + mat4 dst; + const float fovy = glm_rad(45.0f); + const float aspect = 640/480.0f; + const float zNearVal = 0.1f; + const float zFarVal = 100.0f; + + glm_perspective_lh_no(fovy, aspect, zNearVal, zFarVal, dst); + + /* Sanity mk. I: longhand version */ + ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect))) + ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2))) + ASSERT(test_eq(dst[2][2], (zFarVal + zNearVal) / (zFarVal - zNearVal))) + ASSERT(test_eq(dst[2][3], 1.0f)) + ASSERT(test_eq(dst[3][2], -2 * zFarVal * zNearVal / (zFarVal - zNearVal))) + + /* Sanity mk. II */ + /*reference test data for glm_perspective_lh_no*/ + mat4 cmp = {0}; + cmp[0][0] = 1.8106601f; + cmp[1][1] = 2.4142134f; + cmp[2][2] = 1.0020020f; + cmp[2][3] = 1.0000000f; + cmp[3][2] = -0.2002002f; + + return test_assert_mat4_eq(dst, cmp); +} diff --git a/test/src/test_cam_lh_zo.c b/test/src/test_cam_lh_zo.c index 418229b..f5f50af 100644 --- a/test/src/test_cam_lh_zo.c +++ b/test/src/test_cam_lh_zo.c @@ -16,22 +16,21 @@ TEST_IMPL(perspective_lh_zo) { glm_perspective_lh_zo(fovy, aspect, zNearVal, zFarVal, dst); - /* Sanity mk. I */ - /* Longhand version of what the above function _should_ be doing */ - ASSERT(test_eq(dst[0][0], 1 / (tanf(fovy / 2) * aspect))) - ASSERT(test_eq(dst[1][1], 1 / tanf(fovy / 2))) - ASSERT(test_eq(dst[2][2], zFarVal / (zFarVal - zNearVal))) - ASSERT(test_eq(dst[2][3], 1.0f)) + /* Sanity mk. I: longhand version */ + ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect))) + ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2))) + ASSERT(test_eq(dst[2][2], zFarVal / (zFarVal - zNearVal))) + ASSERT(test_eq(dst[2][3], 1.0f)) ASSERT(test_eq(dst[3][2], -1 * zFarVal * zNearVal / (zFarVal - zNearVal))) /* Sanity mk. II */ /* "Reference values" generated by GLM's glm::perspectiveLH_ZO */ mat4 cmp = {0}; - cmp[0][0] = 1.8106601f; - cmp[1][1] = 2.4142134f; - cmp[2][2] = 1.0010010f; - cmp[2][3] = 1.0000000f; + cmp[0][0] = 1.8106601f; + cmp[1][1] = 2.4142134f; + cmp[2][2] = 1.0010010f; + cmp[2][3] = 1.0000000f; cmp[3][2] = -0.1001001f; - return (test_assert_mat4_eq(dst, cmp)); + return test_assert_mat4_eq(dst, cmp); } diff --git a/test/src/test_cam_rh_no.c b/test/src/test_cam_rh_no.c new file mode 100644 index 0000000..2bcadcd --- /dev/null +++ b/test/src/test_cam_rh_no.c @@ -0,0 +1,36 @@ +/* + * 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(perspective_rh_no) { + mat4 dst; + const float fovy = glm_rad(45.0f); + const float aspect = 640/480.0f; + const float zNearVal = 0.1f; + const float zFarVal = 100.0f; + + glm_perspective_rh_no(fovy, aspect, zNearVal, zFarVal, dst); + + /* Sanity mk. I: longhand version */ + ASSERT(test_eq(dst[0][0], 1.0f / (tanf(fovy / 2) * aspect))) + ASSERT(test_eq(dst[1][1], 1.0f / tanf(fovy / 2))) + ASSERT(test_eq(dst[2][2], -1.0f * (zFarVal + zNearVal) / (zFarVal - zNearVal))) + ASSERT(test_eq(dst[2][3], -1.0f)) + ASSERT(test_eq(dst[3][2], -2 * zFarVal * zNearVal / (zFarVal - zNearVal))) + + /* Sanity mk. II */ + /*reference test data for glm_perspective_rh_no*/ + mat4 cmp = {0}; + cmp[0][0] = 1.8106601f; + cmp[1][1] = 2.4142134f; + cmp[2][2] = -1.0020020f; + cmp[2][3] = -1.0000000f; + cmp[3][2] = -0.2002002f; + + return test_assert_mat4_eq(dst, cmp); +} diff --git a/test/src/test_cam_rh_zo.c b/test/src/test_cam_rh_zo.c index c2cb5ba..95281a7 100644 --- a/test/src/test_cam_rh_zo.c +++ b/test/src/test_cam_rh_zo.c @@ -17,9 +17,8 @@ TEST_IMPL(perspective_rh_zo) { glm_perspective_rh_zo(fovy, aspect, zNearVal, zFarVal, dst); /* Sanity mk. I: longhand version */ - float focal_len = 1 / tanf(fovy / 2); - ASSERT(test_eq(dst[0][0], focal_len / aspect)) - ASSERT(test_eq(dst[1][1], focal_len)) + ASSERT(test_eq(dst[0][0], 1 / (tanf(fovy / 2) * aspect))) + ASSERT(test_eq(dst[1][1], 1 / tanf(fovy / 2))) ASSERT(test_eq(dst[2][2], zFarVal / (zNearVal - zFarVal))) ASSERT(test_eq(dst[2][3], -1.0f)) ASSERT(test_eq(dst[3][2], -1 * zFarVal * zNearVal / (zFarVal - zNearVal))) @@ -27,8 +26,8 @@ TEST_IMPL(perspective_rh_zo) { /* Sanity mk. II */ /*reference test data for glm_perspective_rh_zo*/ mat4 cmp = {0}; - cmp[0][0] = 1.8106601f; - cmp[1][1] = 2.4142134f; + cmp[0][0] = 1.8106601f; + cmp[1][1] = 2.4142134f; cmp[2][2] = -1.0010010f; cmp[2][3] = -1.0000000f; cmp[3][2] = -0.1001001f; diff --git a/test/tests.h b/test/tests.h index 137d38d..d5688e2 100644 --- a/test/tests.h +++ b/test/tests.h @@ -225,6 +225,8 @@ TEST_DECLARE(glmc_mat2_rmc) /* camera (incl [LR]H cross [NZ]O) */ TEST_DECLARE(perspective_lh_zo) TEST_DECLARE(perspective_rh_zo) +TEST_DECLARE(perspective_lh_no) +TEST_DECLARE(perspective_rh_no) TEST_DECLARE(camera_lookat) TEST_DECLARE(camera_decomp) @@ -951,6 +953,8 @@ TEST_LIST { /* camera (incl [LR]H cross [NZ]O) */ TEST_ENTRY(perspective_lh_zo) TEST_ENTRY(perspective_rh_zo) + TEST_ENTRY(perspective_lh_no) + TEST_ENTRY(perspective_rh_no) TEST_ENTRY(camera_lookat) TEST_ENTRY(camera_decomp)