Added new ray-sphere intersection

This version uses a geometric method (usually faster) and doesn't
calculate the intersection position and normal, only the distance.
This commit is contained in:
boromisp 2012-11-11 23:09:12 +01:00
parent f96b0424ec
commit 3a3c1fd8e8
2 changed files with 30 additions and 0 deletions

View File

@ -68,6 +68,15 @@ namespace glm
genType const & vert0, genType const & vert1, genType const & vert2,
genType & position);
//! Compute the intersection distance of a ray and a sphere.
//! The ray direction vector is unit length.
//! From GLM_GTX_intersect extension.
template <typename genType>
bool intersectRaySphere(
genType const & rayStarting, genType const & rayNormalizedDirection,
genType const & sphereCenter, const typename genType::value_type sphereRadiusSquered,
typename genType::value_type & intersectionDistance);
//! Compute the intersection of a ray and a sphere.
//! From GLM_GTX_intersect extension.
template <typename genType>

View File

@ -127,6 +127,27 @@ namespace glm
return true;
}
template <typename genType>
GLM_FUNC_QUALIFIER bool intersectRaySphere
(
genType const & rayStarting, genType const & rayNormalizedDirection,
genType const & sphereCenter, const typename genType::value_type sphereRadiusSquered,
typename genType::value_type & intersectionDistance
)
{
typename genType::value_type Epsilon = std::numeric_limits<typename genType::value_type>::epsilon();
genType diff = sphereCenter - rayStarting;
typename genType::value_type t0 = dot(diff, rayNormalizedDirection);
typename genType::value_type dSquared = dot(diff, diff) - t0 * t0;
if( dSquared > sphereRadiusSquered )
{
return false;
}
typename genType::value_type t1 = sqrt( sphereRadiusSquered - dSquared );
intersectionDistance = t0 > t1 + Epsilon ? t0 - t1 : t0 + t1;
return intersectionDistance > Epsilon;
}
template <typename genType>
GLM_FUNC_QUALIFIER bool intersectRaySphere
(