also made Moeller-Trumbore a bit more robust (just in case it is ever used again)
parent
e1ff84e0a9
commit
5baddc030a
|
@ -92,41 +92,42 @@ struct MTS_EXPORT_CORE Triangle {
|
||||||
*/
|
*/
|
||||||
FINLINE static bool rayIntersect(const Point &p0, const Point &p1, const Point &p2,
|
FINLINE static bool rayIntersect(const Point &p0, const Point &p1, const Point &p2,
|
||||||
const Ray &ray, Float &u, Float &v, Float &t) {
|
const Ray &ray, Float &u, Float &v, Float &t) {
|
||||||
/* find vectors for two edges sharing v[0] */
|
/* Find vectors for two edges sharing */
|
||||||
Vector edge1 = p1 - p0, edge2 = p2 - p0;
|
Vector edge1 = p1 - p0, edge2 = p2 - p0;
|
||||||
|
|
||||||
/* begin calculating determinant - also used to calculate U parameter */
|
/* Begin calculating determinant - also used to calculate U parameter */
|
||||||
Vector pvec = cross(ray.d, edge2);
|
Vector pvec = cross(ray.d, edge2);
|
||||||
|
|
||||||
/* if determinant is near zero, ray lies in plane of triangle */
|
|
||||||
Float det = dot(edge1, pvec);
|
Float det = dot(edge1, pvec);
|
||||||
|
|
||||||
if (det == 0)
|
if (det == 0)
|
||||||
return false;
|
return false;
|
||||||
Float inv_det = 1.0f / det;
|
Float inv_det = 1.0f / det;
|
||||||
|
|
||||||
/* calculate distance from v[0] to ray origin */
|
/* Calculate distance from v[0] to ray origin */
|
||||||
Vector tvec = ray.o - p0;
|
Vector tvec = ray.o - p0;
|
||||||
|
|
||||||
/* calculate U parameter and test bounds */
|
/* Calculate U parameter and test bounds */
|
||||||
u = dot(tvec, pvec) * inv_det;
|
u = dot(tvec, pvec) * inv_det;
|
||||||
if (u < 0.0 || u > 1.0)
|
if (u < 0.0 || u > 1.0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* prepare to test V parameter */
|
/* Prepare to test V parameter */
|
||||||
Vector qvec = cross(tvec, edge1);
|
Vector qvec = cross(tvec, edge1);
|
||||||
|
|
||||||
/* calculate V parameter and test bounds */
|
/* Calculate V parameter and test bounds */
|
||||||
v = dot(ray.d, qvec) * inv_det;
|
v = dot(ray.d, qvec) * inv_det;
|
||||||
if (v < 0.0 || u + v > 1.0)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
|
/* Inverted comparison (to catch NaNs) */
|
||||||
|
if (v >= 0.0 && u + v <= 1.0) {
|
||||||
/* ray intersects triangle -> compute t */
|
/* ray intersects triangle -> compute t */
|
||||||
t = dot(edge2, qvec) * inv_det;
|
t = dot(edge2, qvec) * inv_det;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** \brief Ray-triangle intersection test
|
/** \brief Ray-triangle intersection test
|
||||||
*
|
*
|
||||||
* Uses the algorithm by Moeller and Trumbore discussed at
|
* Uses the algorithm by Moeller and Trumbore discussed at
|
||||||
|
|
Loading…
Reference in New Issue