Skip to content

Instantly share code, notes, and snippets.

@ming4883
Created July 9, 2014 08:45
Show Gist options
  • Save ming4883/8375b4a70522e80ab452 to your computer and use it in GitHub Desktop.
Save ming4883/8375b4a70522e80ab452 to your computer and use it in GitHub Desktop.
Where is the problem?
//
// extract from http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
//
template<typename T>
struct AABB
{
Vec3<T> min;
Vec3<T> max;
bool intersect(const Ray<T> &r)
{
T tmin = (min.x - r.orig.x) / r.dir.x;
T tmax = (max.x - r.orig.x) / r.dir.x;
if (tmin > tmax) swap(tmin, tmax);
T tymin = (min.y - r.orig.y) / r.dir.y;
T tymax = (max.y - r.orig.y) / r.dir.y;
if (tymin > tymax) swap(tymin, tymax);
if ((tmin > tymax) || (tymin > tmax))
return false;
if (tymin > tmin)
tmin = tymin;
if (tymax < tmax)
tmax = tymax;
T tzmin = (min.z - r.orig.z) / r.dir.z;
T tzmax = (max.z - r.orig.z) / r.dir.z;
if (tzmin > tzmax) swap(tzmin, tzmax);
if ((tmin > tzmax) || (tzmin > tmax))
return false;
if (tzmin > tmin)
tmin = tzmin;
if (tzmax < tmax)
tmax = tzmax;
if ((tmin > r.tmax) || (tmax < r.tmin)) return false;
if (r.tmin < tmin) r.tmin = tmin;
if (r.tmax > tmax) r.tmax = tmax;
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment