diff --git a/LICENSE b/LICENSE index f22658b..2367e77 100644 --- a/LICENSE +++ b/LICENSE @@ -52,3 +52,9 @@ LICENSE: Computing Euler angles from a rotation matrix (euler.pdf) Gregory G. Slabaugh + +4. Extracting Planes +Fast Extraction of Viewing Frustum Planes from the World-View-Projection Matrix +Authors: + Gil Gribb (ggribb@ravensoft.com) + Klaus Hartmann (k_hartmann@osnabrueck.netsurf.de) diff --git a/include/cglm/call/cam.h b/include/cglm/call/cam.h index 03e066a..5b766a3 100644 --- a/include/cglm/call/cam.h +++ b/include/cglm/call/cam.h @@ -48,6 +48,10 @@ glmc_lookat(vec3 eye, vec3 up, mat4 dest); +CGLM_EXPORT +void +glmc_extract_planes(mat4 m, vec4 dest[6]); + #ifdef __cplusplus } #endif diff --git a/include/cglm/call/plane.h b/include/cglm/call/plane.h index 18b090a..f991121 100644 --- a/include/cglm/call/plane.h +++ b/include/cglm/call/plane.h @@ -17,10 +17,6 @@ CGLM_EXPORT void glmc_plane_normalize(vec4 plane); -CGLM_EXPORT -void -glmc_plane_extract(mat4 projView, vec4 dest[6]); - #ifdef __cplusplus } #endif diff --git a/include/cglm/cam.h b/include/cglm/cam.h index a78ba4b..1504637 100644 --- a/include/cglm/cam.h +++ b/include/cglm/cam.h @@ -50,12 +50,14 @@ float * __restrict farVal); CGLM_INLINE void glm_persp_decomp_far(mat4 proj, float * __restrict farVal); CGLM_INLINE void glm_persp_decomp_near(mat4 proj, float *__restrict nearVal); + CGLM_INLINE void glm_extract_planes(mat4 m, vec4 dest[6]); */ #ifndef cglm_vcam_h #define cglm_vcam_h #include "common.h" +#include "plane.h" /*! * @brief set up perspective peprojection matrix @@ -422,4 +424,45 @@ void glm_persp_decomp_near(mat4 proj, float * __restrict nearVal) { *nearVal = proj[3][2] / (proj[2][2] - 1); } + + +/*! + * @brief extracts view frustum planes + * + * planes' space: + * 1- if m = proj: View Space + * 2- if m = projView: World Space + * 3- if m = MVP: Object Space + * + * You probably want to extract planes in world space so use projView as m + * Computing projView: + * glm_mat4_mul(proj, view, projView); + * + * Exracted planes order: [left, right, bottom, top, near, far] + * + * @param[in] m matrix (see brief) + * @param[out] dest exracted view frustum planes (see brief) + */ +CGLM_INLINE +void +glm_extract_planes(mat4 m, vec4 dest[6]) { + mat4 t; + + glm_mat4_transpose_to(m, t); + + glm_vec4_add(t[3], t[0], dest[0]); /* left */ + glm_vec4_sub(t[3], t[0], dest[1]); /* right */ + glm_vec4_add(t[3], t[1], dest[2]); /* bottom */ + glm_vec4_sub(t[3], t[1], dest[3]); /* top */ + glm_vec4_add(t[3], t[2], dest[4]); /* near */ + glm_vec4_sub(t[3], t[2], dest[5]); /* far */ + + 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_vcam_h */ diff --git a/include/cglm/plane.h b/include/cglm/plane.h index d8cd050..9faac9c 100644 --- a/include/cglm/plane.h +++ b/include/cglm/plane.h @@ -11,6 +11,7 @@ #include "common.h" #include "mat4.h" #include "vec4.h" +#include "vec3.h" /* Plane equation: Ax + By + Cz + D = 0; @@ -21,7 +22,6 @@ /* Functions: CGLM_INLINE void glm_plane_normalize(vec4 plane); - CGLM_INLINE void glm_plane_extract(mat4 projView, vec4 dest[6]); */ /*! @@ -35,36 +35,4 @@ 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/cam.c b/src/cam.c index 0dcdc4b..1e36bfd 100644 --- a/src/cam.c +++ b/src/cam.c @@ -66,3 +66,9 @@ glmc_lookat(vec3 eye, mat4 dest) { glm_lookat(eye, center, up, dest); } + +CGLM_EXPORT +void +glmc_extract_planes(mat4 m, vec4 dest[6]) { + glm_extract_planes(m, dest); +} diff --git a/src/plane.c b/src/plane.c index 71e2587..7ee0c0f 100644 --- a/src/plane.c +++ b/src/plane.c @@ -13,9 +13,3 @@ 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); -}