| 41 | Ray::~Ray() {} |
| 42 | |
| 43 | bool Ray::intersects(const AABB& box, float* distance) const |
| 44 | { |
| 45 | float lowt = 0.0f; |
| 46 | float t; |
| 47 | bool hit = false; |
| 48 | Vec3 hitpoint; |
| 49 | const Vec3& min = box._min; |
| 50 | const Vec3& max = box._max; |
| 51 | const Vec3& rayorig = _origin; |
| 52 | const Vec3& raydir = _direction; |
| 53 | |
| 54 | // Check origin inside first |
| 55 | if (rayorig > min && rayorig < max) |
| 56 | return true; |
| 57 | |
| 58 | // Check each face in turn, only check closest 3 |
| 59 | // Min x |
| 60 | if (rayorig.x <= min.x && raydir.x > 0) |
| 61 | { |
| 62 | t = (min.x - rayorig.x) / raydir.x; |
| 63 | if (t >= 0) |
| 64 | { |
| 65 | // Substitute t back into ray and check bounds and dist |
| 66 | hitpoint = rayorig + raydir * t; |
| 67 | if (hitpoint.y >= min.y && hitpoint.y <= max.y && hitpoint.z >= min.z && hitpoint.z <= max.z && |
| 68 | (!hit || t < lowt)) |
| 69 | { |
| 70 | hit = true; |
| 71 | lowt = t; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | // Max x |
| 76 | if (rayorig.x >= max.x && raydir.x < 0) |
| 77 | { |
| 78 | t = (max.x - rayorig.x) / raydir.x; |
| 79 | if (t >= 0) |
| 80 | { |
| 81 | // Substitute t back into ray and check bounds and dist |
| 82 | hitpoint = rayorig + raydir * t; |
| 83 | if (hitpoint.y >= min.y && hitpoint.y <= max.y && hitpoint.z >= min.z && hitpoint.z <= max.z && |
| 84 | (!hit || t < lowt)) |
| 85 | { |
| 86 | hit = true; |
| 87 | lowt = t; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | // Min y |
| 92 | if (rayorig.y <= min.y && raydir.y > 0) |
| 93 | { |
| 94 | t = (min.y - rayorig.y) / raydir.y; |
| 95 | if (t >= 0) |
| 96 | { |
| 97 | // Substitute t back into ray and check bounds and dist |
| 98 | hitpoint = rayorig + raydir * t; |
| 99 | if (hitpoint.x >= min.x && hitpoint.x <= max.x && hitpoint.z >= min.z && hitpoint.z <= max.z && |
| 100 | (!hit || t < lowt)) |
nothing calls this directly
no test coverage detected