diff --git a/README.md b/README.md index c90f6ea..3e6964a 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ https://github.com/g-truc/glm - euler angles / yaw-pitch-roll to matrix - extract euler angles - inline or pre-compiled function call +- extract view frustum planes
diff --git a/include/cglm/call.h b/include/cglm/call.h index 2b8025a..c306ab9 100644 --- a/include/cglm/call.h +++ b/include/cglm/call.h @@ -20,6 +20,7 @@ extern "C" { #include "call/cam.h" #include "call/quat.h" #include "call/euler.h" +#include "call/plane.h" #include "call/io.h" #ifdef __cplusplus diff --git a/include/cglm/call/plane.h b/include/cglm/call/plane.h new file mode 100644 index 0000000..18b090a --- /dev/null +++ b/include/cglm/call/plane.h @@ -0,0 +1,27 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglmc_plane_h +#define cglmc_plane_h +#ifdef __cplusplus +extern "C" { +#endif + +#include "../cglm.h" + +CGLM_EXPORT +void +glmc_plane_normalize(vec4 plane); + +CGLM_EXPORT +void +glmc_plane_extract(mat4 projView, vec4 dest[6]); + +#ifdef __cplusplus +} +#endif +#endif /* cglmc_plane_h */ diff --git a/include/cglm/cglm.h b/include/cglm/cglm.h index 1dabb70..24544d1 100644 --- a/include/cglm/cglm.h +++ b/include/cglm/cglm.h @@ -17,6 +17,7 @@ #include "cam.h" #include "quat.h" #include "euler.h" +#include "plane.h" #include "util.h" #include "io.h" diff --git a/include/cglm/plane.h b/include/cglm/plane.h new file mode 100644 index 0000000..d8cd050 --- /dev/null +++ b/include/cglm/plane.h @@ -0,0 +1,70 @@ +/* + * Copyright (c), Recep Aslantas. + * + * MIT License (MIT), http://opensource.org/licenses/MIT + * Full license can be found in the LICENSE file + */ + +#ifndef cglm_plane_h +#define cglm_plane_h + +#include "common.h" +#include "mat4.h" +#include "vec4.h" + +/* + Plane equation: Ax + By + Cz + D = 0; + + It stored in vec4 as [A, B, C, D]. (A, B, C) is normal and D is distance +*/ + +/* + Functions: + CGLM_INLINE void glm_plane_normalize(vec4 plane); + CGLM_INLINE void glm_plane_extract(mat4 projView, vec4 dest[6]); + */ + +/*! + * @brief normalizes a plane + * + * @param[in, out] plane pnale to normalize + */ +CGLM_INLINE +void +glm_plane_normalize(vec4 plane) { + glm_vec4_scale(plane, 1.0f / glm_vec_norm(plane), plane); +} + +/*! + * @brief extracts view frustum planes + * + * computing projView: glm_mat4_mul(proj, view, projView); + * + * exracted planes order: [left, right, bottom, top, near, far] + * + * @param[in] projView source + * @param[out] dest exracted view frustum planes (see brief) + */ +CGLM_INLINE +void +glm_plane_extract(mat4 projView, vec4 dest[6]) { + mat4 m; + + glm_mat4_transpose_to(projView, m); + + glm_vec4_add(m[3], m[0], dest[0]); + glm_vec4_sub(m[3], m[0], dest[1]); + glm_vec4_add(m[3], m[1], dest[2]); + glm_vec4_sub(m[3], m[1], dest[3]); + glm_vec4_add(m[3], m[2], dest[4]); + glm_vec4_sub(m[3], m[2], dest[5]); + + glm_plane_normalize(dest[0]); + glm_plane_normalize(dest[1]); + glm_plane_normalize(dest[2]); + glm_plane_normalize(dest[3]); + glm_plane_normalize(dest[4]); + glm_plane_normalize(dest[5]); +} + +#endif /* cglm_plane_h */ diff --git a/src/plane.c b/src/plane.c new file mode 100644 index 0000000..71e2587 --- /dev/null +++ b/src/plane.c @@ -0,0 +1,21 @@ +/* + * 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" +#include "../include/cglm/call.h" + +CGLM_EXPORT +void +glmc_plane_normalize(vec4 plane) { + glm_plane_normalize(plane); +} + +CGLM_EXPORT +void +glmc_plane_extract(mat4 projView, vec4 dest[6]) { + glm_plane_extract(projView, dest); +}