Move slab intersection to Box::IntersectSlab

Makes it easier to debug because I don't have to change things in three places.

This change also includes the fix! The swap was the problem. I didn't have the optimization right. I'll have to go back and figure it out.
This commit is contained in:
Eryn Wells 2014-08-03 12:20:48 -07:00
parent 8275c9baee
commit 8d5dfe5154

View file

@ -64,7 +64,7 @@ Box::DoesIntersect(const Ray& ray,
* axes. This is the Kay-Kajiya box intersection algorithm. * axes. This is the Kay-Kajiya box intersection algorithm.
*/ */
Double t0, t1; //Double t0, t1;
Double tNear = -std::numeric_limits<Double>::infinity(); Double tNear = -std::numeric_limits<Double>::infinity();
Double tFar = std::numeric_limits<Double>::infinity(); Double tFar = std::numeric_limits<Double>::infinity();
@ -82,6 +82,7 @@ Box::DoesIntersect(const Ray& ray,
* planes (X, Y, and Z planes). * planes (X, Y, and Z planes).
*/ */
#if 0
/* /*
* Unrolling the loop means lots of duplicated code. So we start with the X * Unrolling the loop means lots of duplicated code. So we start with the X
* planes... * planes...
@ -175,6 +176,17 @@ Box::DoesIntersect(const Ray& ray,
return false; return false;
} }
} }
#endif
if (!IntersectSlab(mNear.x, mFar.x, ray.origin.x, ray.direction.x, tNear, tFar)) {
return false;
}
if (!IntersectSlab(mNear.y, mFar.y, ray.origin.y, ray.direction.y, tNear, tFar)) {
return false;
}
if (!IntersectSlab(mNear.z, mFar.z, ray.origin.z, ray.direction.z, tNear, tFar)) {
return false;
}
/* We have an intersection! */ /* We have an intersection! */
stats.boxIntersections++; stats.boxIntersections++;
@ -226,5 +238,59 @@ Box::compute_normal(const Vector3& p)
return Vector3(); return Vector3();
} }
inline bool
Box::IntersectSlab(const Double& slabLow,
const Double& slabHigh,
const Double& rayOriginComponent,
const Double& rayDirectionComponent,
Double& tNear,
Double& tFar)
const
{
Double t0, t1;
if (NearZero(rayDirectionComponent)) {
/* The ray is parallel to the X axis. */
if (rayOriginComponent < slabLow || rayOriginComponent > slabHigh) {
/* The ray's origin is not between the slabs, so no intersection. */
return false;
}
} else {
t0 = (slabLow - rayOriginComponent) / rayDirectionComponent;
t1 = (slabHigh - rayOriginComponent) / rayDirectionComponent;
#if 0
if (t0 <= t1) {
tNear = t0;
tFar = t1;
} else {
tNear = t1;
tFar = t0;
}
#endif
if (t0 > t1) {
Double tmp = t0;
t0 = t1;
t1 = tmp;
}
if (t0 > tNear) {
tNear = t0;
}
if (t1 < tFar) {
tFar = t1;
}
if (tNear > tFar) {
/* Box is missed. */
return false;
}
if (tFar < 0.0) {
/* Box is behind the ray. */
return false;
}
}
return true;
}
} /* namespace charles */ } /* namespace charles */