diff --git a/include/cglm/cam.h b/include/cglm/cam.h index 5d39ef2..d69de83 100644 --- a/include/cglm/cam.h +++ b/include/cglm/cam.h @@ -426,6 +426,31 @@ glm_persp_decomp_near(mat4 proj, float * __restrict nearVal) { *nearVal = proj[3][2] / (proj[2][2] - 1); } +/*! + * @brief returns field of view angle along the Y-axis (in radians) + * + * if you need to degrees, use glm_deg to convert it or use this: + * fovy_deg = glm_deg(glm_persp_fovy(projMatrix)) + * + * @param[in] proj perspective projection matrix + */ +CGLM_INLINE +float +glm_persp_fovy(mat4 proj) { + return 2.0 * atan(1.0 / proj[1][1]); +} + +/*! + * @brief returns aspect ratio of perspective projection + * + * @param[in] proj perspective projection matrix + */ +CGLM_INLINE +float +glm_persp_aspect(mat4 proj) { + return proj[1][1] / proj[0][0]; +} + /*! * @brief extracts view frustum planes * diff --git a/makefile.am b/makefile.am index f1dadfc..5830700 100644 --- a/makefile.am +++ b/makefile.am @@ -93,7 +93,8 @@ libcglm_la_SOURCES=\ test_tests_SOURCES=\ test/src/test_common.c \ test/src/test_main.c \ - test/src/test_mat4.c + test/src/test_mat4.c \ + test/src/test_cam.c all-local: sh ./post-build.sh diff --git a/test/src/test_cam.c b/test/src/test_cam.c new file mode 100644 index 0000000..cfa7747 --- /dev/null +++ b/test/src/test_cam.c @@ -0,0 +1,25 @@ +/* + * 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" + +void +test_camera_decomp(void **state) { + mat4 proj; + float aspect, fovy, nearVal, farVal; + + aspect = 0.782f; + fovy = glm_rad(49.984f); + nearVal = 0.1f; + farVal = 100.0f; + + glm_perspective(fovy, aspect, nearVal, farVal, proj); + assert_true(fabsf(aspect - glm_persp_aspect(proj)) < FLT_EPSILON); + assert_true(fabsf(fovy - glm_persp_fovy(proj)) < FLT_EPSILON); + assert_true(fabsf(49.984f - glm_deg(glm_persp_fovy(proj))) < FLT_EPSILON); +} + diff --git a/test/src/test_main.c b/test/src/test_main.c index 4578997..25c7d17 100644 --- a/test/src/test_main.c +++ b/test/src/test_main.c @@ -11,6 +11,9 @@ main(int argc, const char * argv[]) { const struct CMUnitTest tests[] = { /* mat4 */ cmocka_unit_test(test_mat4), + + /* camera */ + cmocka_unit_test(test_camera_decomp) }; return cmocka_run_group_tests(tests, diff --git a/test/src/test_tests.h b/test/src/test_tests.h index 7e8fe20..a6a8a58 100644 --- a/test/src/test_tests.h +++ b/test/src/test_tests.h @@ -9,4 +9,8 @@ /* mat4 */ void test_mat4(void **state); +/* camera */ +void +test_camera_decomp(void **state); + #endif /* test_tests_h */