Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.