extract fovy and aspect for perpective matrix

This commit is contained in:
Recep Aslantas
2018-01-04 11:16:09 +03:00
parent 3e4f52b3af
commit 797c4581ee
5 changed files with 59 additions and 1 deletions

View File

@@ -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
*

View File

@@ -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

25
test/src/test_cam.c Normal file
View File

@@ -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);
}

View File

@@ -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,

View File

@@ -9,4 +9,8 @@
/* mat4 */
void test_mat4(void **state);
/* camera */
void
test_camera_decomp(void **state);
#endif /* test_tests_h */