Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn intersect_tri(ray: &Ray, v0: Vector4, v1: Vector4, v2: Vector4) -> (f32, f32, f32) {
- const EPSILON : f32 = 0.00000000001;
- let e1 = v1 - v0;
- let e2 = v2 - v0;
- //Begin calculating determinant - also used to calculate u parameter
- let p = ray.dir.cross3(e2);
- let det = e1.dot3(p);
- //if determinant is near zero, ray is parallel to plane of triangle
- if det > -EPSILON && det < EPSILON {
- return (NO_INTERSECTION, 0.0, 0.0);
- }
- let inv_det = 1.0 / det;
- // Calculate distance from V0 to ray origin
- let t = ray.org - v0;
- // Calculate u parameter and test bound
- let u = t.dot3(p)* inv_det;
- // The intersection lies outside of the triangle
- if u < 0.0 || u > 1.0 {
- return (NO_INTERSECTION, 0.0, 0.0);
- }
- // Prepare to test v parameter
- let q = t.cross3(e1);
- // Calculate V parameter and test bound
- let v = ray.dir.dot3(q)*inv_det;
- // The intersection lies outside of the triangle
- if v < 0.0 || u + v > 1.0 {
- return (NO_INTERSECTION, 0.0, 0.0);
- }
- let t = e2.dot3(q)*inv_det;
- if t > EPSILON { //ray intersection
- return (t, u, v);
- }
- return (NO_INTERSECTION, 0.0, 0.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement