Skip to content

Instantly share code, notes, and snippets.

@svenstaro
Last active October 9, 2020 21:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenstaro/a64045811d5dcf378b6a to your computer and use it in GitHub Desktop.
Save svenstaro/a64045811d5dcf378b6a to your computer and use it in GitHub Desktop.
inline bool intersect_ray_aabb(const glm::vec3 &origin, const glm::vec3 &dir,
const AABB &aabb) {
glm::vec3 dir_inv = 1.f / dir;
float t1 = (aabb.min().x - origin.x) * dir.x;
float t2 = (aabb.max().x - origin.x) * dir.x;
float tmin = glm::min(t1, t2);
float tmax = glm::max(t1, t2);
for (int i = 1; i < 3; ++i) {
t1 = (aabb.min()[i] - origin[i]) * dir_inv[i];
t2 = (aabb.max()[i] - origin[i]) * dir_inv[i];
tmin = glm::max(tmin, glm::min(t1, t2));
tmax = glm::min(tmax, glm::max(t1, t2));
}
return tmax > glm::max(tmin, 0.f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment