Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. fn intersect_tri(ray: &Ray, v0: Vector4, v1: Vector4, v2: Vector4) -> (f32, f32, f32) {
  2. const EPSILON : f32 = 0.00000000001;
  3.  
  4. let e1 = v1 - v0;
  5. let e2 = v2 - v0;
  6.  
  7. //Begin calculating determinant - also used to calculate u parameter
  8. let p = ray.dir.cross3(e2);
  9.  
  10. let det = e1.dot3(p);
  11.  
  12. //if determinant is near zero, ray is parallel to plane of triangle
  13. if det > -EPSILON && det < EPSILON {
  14. return (NO_INTERSECTION, 0.0, 0.0);
  15. }
  16. let inv_det = 1.0 / det;
  17.  
  18. // Calculate distance from V0 to ray origin
  19. let t = ray.org - v0;
  20.  
  21. // Calculate u parameter and test bound
  22. let u = t.dot3(p)* inv_det;
  23.  
  24. // The intersection lies outside of the triangle
  25. if u < 0.0 || u > 1.0 {
  26. return (NO_INTERSECTION, 0.0, 0.0);
  27. }
  28.  
  29. // Prepare to test v parameter
  30. let q = t.cross3(e1);
  31.  
  32. // Calculate V parameter and test bound
  33. let v = ray.dir.dot3(q)*inv_det;
  34.  
  35. // The intersection lies outside of the triangle
  36. if v < 0.0 || u + v > 1.0 {
  37. return (NO_INTERSECTION, 0.0, 0.0);
  38. }
  39.  
  40. let t = e2.dot3(q)*inv_det;
  41.  
  42. if t > EPSILON { //ray intersection
  43. return (t, u, v);
  44. }
  45.  
  46. return (NO_INTERSECTION, 0.0, 0.0);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement