// a ray inside the sphere will not intersect on its way out
- Scalar intersectRay(const Ray<D>& ray,
- typename Ray<D>::Intersection& intersection)
+ bool intersectRay(const Ray<D>& ray, typename Ray<D>::Intersection& hit)
{
Vector b = point - ray.point;
Scalar z = cml::dot(b, ray.direction);
// check if the ball is behind the ray
- if (z < SCALAR(0.0)) return SCALAR(-1.0);
+ if (z < SCALAR(0.0)) return false;
Scalar d2 = cml::dot(b, b) - z*z;
Scalar r2 = radius * radius;
// check for an intersection
- if (d2 > r2) return SCALAR(-1.0);
+ if (d2 > r2) return false;
- Scalar t = z - std::sqrt(r2 - d2);
- ray.solve(intersection.point, t);
- intersection.normal = intersection.point - point;
+ hit.distance = z - std::sqrt(r2 - d2);
+ if (hit.distance < SCALAR(0.0)) return false;
- return t;
+ Vector surfacePoint;
+ ray.solve(surfacePoint, hit.distance);
+ hit.normal = surfacePoint - point;
+ return true;
}