diff --git a/CREDITS b/CREDITS index 44e55a8..0488bad 100644 --- a/CREDITS +++ b/CREDITS @@ -1,7 +1,7 @@ This library [initially] used some [piece of] implementations (may include codes) from these open source projects/resources: -1. Affine Transforms +1. Initial Affine Transforms The original glm repo (g-truc), url: https://github.com/g-truc/glm LICENSE[S]: @@ -11,7 +11,7 @@ LICENSE[S]: FULL LICENSE: https://github.com/g-truc/glm/blob/master/copying.txt -2. Quaternions +2. Initial Quaternions Anton's OpenGL 4 Tutorials book source code: LICENSE: @@ -47,6 +47,8 @@ http://old.cescg.org/CESCG-2002/DSykoraJJelinek/ 7. Quaternions Initial mat4_quat is borrowed from Apple's simd library - 8. Vector Rotation using Quaternion https://gamedev.stackexchange.com/questions/28395/rotating-vector3-by-a-quaternion + +9. Sphere AABB intersect +https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c diff --git a/include/cglm/box.h b/include/cglm/box.h index 72f8b8f..3708107 100644 --- a/include/cglm/box.h +++ b/include/cglm/box.h @@ -229,24 +229,27 @@ glm_aabb_aabb(vec3 box[2], vec3 other[2]) { /*! * @brief check if AABB intersects with sphere * - * @param[in] box bounding box - * @param[in] other other bounding box + * https://github.com/erich666/GraphicsGems/blob/master/gems/BoxSphere.c + * Solid Box - Solid Sphere test. + * + * @param[in] box solid bounding box + * @param[in] s solid sphere */ CGLM_INLINE bool -glm_aabb_sphere(vec3 box[2], vec4 sph) { - vec3 v; - float dist; +glm_aabb_sphere(vec3 box[2], vec4 s) { + float dmin; + int a, b, c; - /* get box closest point to sphere center by clamping */ - v[0] = glm_max(box[0][0], glm_min(sph[0], box[1][0])); - v[1] = glm_max(box[0][1], glm_min(sph[1], box[1][1])); - v[2] = glm_max(box[0][2], glm_min(sph[2], box[1][2])); + a = s[0] >= box[0][0]; + b = s[1] >= box[0][1]; + c = s[2] >= box[0][2]; - /* this is the same as glm_sphere_point */ - dist = glm_vec_distance(v, sph); + dmin = glm_pow2(s[0] - box[a][0]) + + glm_pow2(s[1] - box[b][1]) + + glm_pow2(s[2] - box[c][2]); - return dist < sph[3]; + return dmin <= glm_pow2(s[3]); } /*! diff --git a/include/cglm/call/box.h b/include/cglm/call/box.h index 0d4c8c1..afb7558 100644 --- a/include/cglm/call/box.h +++ b/include/cglm/call/box.h @@ -70,7 +70,7 @@ glmc_aabb_contains(vec3 box[2], vec3 other[2]); CGLM_EXPORT bool -glmc_aabb_sphere(vec3 box[2], vec4 sph); +glmc_aabb_sphere(vec3 box[2], vec4 s); #ifdef __cplusplus } diff --git a/include/cglm/util.h b/include/cglm/util.h index b272d44..af7514c 100644 --- a/include/cglm/util.h +++ b/include/cglm/util.h @@ -98,7 +98,6 @@ glm_make_deg(float *rad) { CGLM_INLINE float glm_pow2(float x) { - return x * x; } diff --git a/src/box.c b/src/box.c index 1352f97..fd639ea 100644 --- a/src/box.c +++ b/src/box.c @@ -91,6 +91,6 @@ glmc_aabb_contains(vec3 box[2], vec3 other[2]) { CGLM_EXPORT bool -glmc_aabb_sphere(vec3 box[2], vec4 sph) { - return glm_aabb_sphere(box, sph); +glmc_aabb_sphere(vec3 box[2], vec4 s) { + return glm_aabb_sphere(box, s); }