d(scalar) {}
- Scalar intersectRay(const Ray<3>& ray, Ray<3>::Intersection& intersection)
+ bool intersectRay(const Ray<3>& ray, Ray<3>::Intersection& hit)
{
// solve: [(ray.point + t*ray.direction) dot normal] + d = 0
- Scalar denominator = cml::dot(ray.direction, normal);
+ Scalar denom = cml::dot(ray.direction, normal);
// check for parallel condition
- if (denominator == SCALAR(0.0))
+ if (denom == SCALAR(0.0))
{
if (isEqual(cml::dot(ray.point, normal), -d))
{
// the ray lies on the plane
- intersection.point = ray.point;
- intersection.normal.set(0.0, 0.0, 0.0);
- //intersection.normal = normal;
- return SCALAR(0.0);
+ hit.distance = SCALAR(0.0);
+ hit.normal.set(0.0, 0.0, 0.0);
+ return true;
}
// no solution
- return SCALAR(-1.0);
+ return false;
}
- Scalar distance = cml::dot(ray.point, normal) + d;
- Scalar t = -distance / denominator;
- if (t > SCALAR(0.0))
- {
- ray.solve(intersection.point, t);
-
- if (distance >= 0.0) intersection.normal = normal;
- else intersection.normal = -normal;
- }
+ Scalar numer = cml::dot(ray.point, normal) + d;
+ hit.distance = -numer / denom;
+ if (hit.distance < SCALAR(0.0)) return false;
- return t;
+ if (numer >= 0.0) hit.normal = normal;
+ else hit.normal = -normal;
+ return true;
}