From 642cc8d603f85874e47f9025363af0e4c463b6ea Mon Sep 17 00:00:00 2001 From: Recep Aslantas Date: Thu, 4 Jan 2018 13:54:35 +0300 Subject: [PATCH] perspective sizes --- include/cglm/cam.h | 26 ++++++++++++++++++++++++++ test/src/test_cam.c | 15 ++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/include/cglm/cam.h b/include/cglm/cam.h index 00e1acb..ea97bbb 100644 --- a/include/cglm/cam.h +++ b/include/cglm/cam.h @@ -478,6 +478,32 @@ glm_persp_aspect(mat4 proj) { return proj[1][1] / proj[0][0]; } +/*! + * @brief returns aspect ratio of perspective projection + * + * if you don't have fovy then use glm_persp_fovy(proj) to get it + * or pass directly: glm_persp_sizes(proj, glm_persp_fovy(proj), sizes); + * + * @param[in] proj perspective projection matrix + * @param[in] fovy fovy (see brief) + * @param[out] dest sizes order: [Wnear, Hnear, Wfar, Hfar] + */ +CGLM_INLINE +void +glm_persp_sizes(mat4 proj, float fovy, vec4 dest) { + float t, a, nearVal, farVal; + + t = 2.0f * tanf(fovy * 0.5f); + a = glm_persp_aspect(proj); + + glm_persp_decomp_z(proj, &nearVal, &farVal); + + dest[1] = t * nearVal; + dest[3] = t * farVal; + dest[0] = a * dest[1]; + dest[2] = a * dest[3]; +} + /*! * @brief extracts view frustum planes * diff --git a/test/src/test_cam.c b/test/src/test_cam.c index cfa7747..9df4b53 100644 --- a/test/src/test_cam.c +++ b/test/src/test_cam.c @@ -9,7 +9,8 @@ void test_camera_decomp(void **state) { - mat4 proj; + mat4 proj, proj2; + vec4 sizes; float aspect, fovy, nearVal, farVal; aspect = 0.782f; @@ -21,5 +22,17 @@ test_camera_decomp(void **state) { 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); + + glm_persp_sizes(proj, fovy, sizes); + + glm_frustum(-sizes[0] * 0.5, + sizes[0] * 0.5, + -sizes[1] * 0.5, + sizes[1] * 0.5, + nearVal, + farVal, + proj2); + + test_assert_mat4_eq(proj, proj2); }