Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool rayHitsAABB(vec3f a, vec3f b, vec3f pos, vec3f dir) {
- for int i <- 0..3
- if (dir[i] < 0) { float _o = a[i]; a[i] = -b[i]; b[i] = -_o; pos[i] = -pos[i]; dir[i] = -dir[i]; }
- // pretend ray starts at origin
- a -= pos;
- b -= pos;
- if (b.x < 0 || b.y < 0 || b.z < 0) return false; // ray is pointed away from aabb.
- // multiply every component with dir.(x*y*z)
- // vec3f dista = a / dir, distb = b / dir;
- vec3f mulfac = vec3f(dir.(y * z, x * z, x * y));
- vec3f dista = a * mulfac, distb = b * mulfac;
- float entry = max(dista.x, max(dista.y, dista.z));
- float exit = min(distb.x, min(distb.y, distb.z));
- if (entry <= exit) return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement