[c++] 3D: Ray-Triangle Intersection
bool IntersectTriangle(const D3DXVECTOR3& RayOrigin, const D3DXVECTOR3& RayDirection,
D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2, float& t, float& u, float& v)
{
D3DXVECTOR3 edge1 = v1 - v0;
D3DXVECTOR3 edge2 = v2 - v0;
D3DXVECTOR3 pvec;
D3DXVec3Cross(&pvec, &RayDirection, &edge2);
FLOAT det = D3DXVec3Dot(&edge1, &pvec);
D3DXVECTOR3 tvec;
if (det > 0) tvec = RayOrigin - v0;
else {
tvec = v0 - RayOrigin;
det = -det;
}
if (det < 0.0001f) return false;
u = D3DXVec3Dot(&tvec, &pvec);
if (u < 0.0f || u > det) return false;
D3DXVECTOR3 qvec;
D3DXVec3Cross(&qvec, &tvec, &edge1);
v = D3DXVec3Dot(&RayDirection, &qvec);
if (v < 0.0f || u + v > det) return false;
t = D3DXVec3Dot(&edge2, &qvec);
FLOAT fInvDet = 1.0f / det;
t *= fInvDet;
u *= fInvDet;
v *= fInvDet;
return true;
}