40 #ifndef __VMML__RAY__HPP__ 41 #define __VMML__RAY__HPP__ 43 #include <vmmlib/vector.hpp> 60 Ray(
const vec3& origin,
const vec3& direction)
62 , _direction(
vmml::normalize(direction))
75 T
test(
const vec4& sphere)
const;
79 const vec3 _direction;
85 const vec3 center =
vec3(sphere.x(), sphere.y(), sphere.z());
86 const T radius = sphere.w();
88 const vec3 centerVec = center - _origin;
89 const T vecProjection = centerVec.dot(_direction);
91 const T sqDistance = centerVec.squared_length();
92 const T sqRadius = radius * radius;
95 if (vecProjection < 0 && sqDistance > sqRadius)
99 const T sqCenterToProj = sqDistance - vecProjection * vecProjection;
101 if (sqCenterToProj > sqRadius)
105 const T distSurface = sqrt(sqRadius - sqCenterToProj);
107 if (sqDistance > sqRadius)
108 return vecProjection - distSurface;
110 return vecProjection + distSurface;
T test(const vec4 &sphere) const
Ray-Sphere Intersection.
Ray(const vec3 &origin, const vec3 &direction)
Constructor.