Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. bool Triangle::intersect(Ray ray){
  2. Vec3 direction = ray.direction;
  3. Vec3 origin = ray.origin;
  4. Vec3 v0v1 = v1 - v0;
  5. Vec3 v0v2 = v2 - v0;
  6. Vec3 pvec = Vec3::cross(direction, v0v2);
  7. float det = Vec3::dot(v0v1, pvec);
  8.  
  9. // if det is negative then triangle is backfacing
  10. if(det<0.0001) return false;
  11.  
  12. if(fabs(det)<0.0001) return false;
  13.  
  14. float inv_det = 1.0/((float)det);
  15.  
  16. Vec3 tvec = origin - v0;
  17. u = Vec3::dot(tvec, pvec) * inv_det;
  18. if (u < 0 || u > 1) return false;
  19.  
  20. Vec3 qvec = Vec3::cross(tvec, v0v1);
  21. v = Vec3::dot(direction, qvec) * inv_det;
  22. if (v < 0 || u + v > 1) return false;
  23.  
  24. t = Vec3::dot(v0v2, qvec);
  25.  
  26. return true;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement