Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool Triangle::intersect(Ray ray){
- Vec3 direction = ray.direction;
- Vec3 origin = ray.origin;
- Vec3 v0v1 = v1 - v0;
- Vec3 v0v2 = v2 - v0;
- Vec3 pvec = Vec3::cross(direction, v0v2);
- float det = Vec3::dot(v0v1, pvec);
- // if det is negative then triangle is backfacing
- if(det<0.0001) return false;
- if(fabs(det)<0.0001) return false;
- float inv_det = 1.0/((float)det);
- Vec3 tvec = origin - v0;
- u = Vec3::dot(tvec, pvec) * inv_det;
- if (u < 0 || u > 1) return false;
- Vec3 qvec = Vec3::cross(tvec, v0v1);
- v = Vec3::dot(direction, qvec) * inv_det;
- if (v < 0 || u + v > 1) return false;
- t = Vec3::dot(v0v2, qvec);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement