//----------------------------------------------------------------------------- // Name: IntersectTriangle() // Desc: Given a ray origin (orig) and direction (dir), and three vertices of // of a triangle, this function returns TRUE and the interpolated texture // coordinates if the ray intersects the triangle //----------------------------------------------------------------------------- public static bool IntersectTriangle(ref Vector3 orig, ref Vector3 dir, ref Vector3 v0, ref Vector3 v1, ref Vector3 v2, ref float t, ref float u, ref float v) { // Find vectors for two edges sharing vert0 Vector3 edge1 = DXUtils.Minus(v1, v0); Vector3 edge2 = DXUtils.Minus(v2, v0); // Begin calculating determinant - also used to calculate U parameter Vector3 pvec = Vector3.Cross(dir, edge2); // If determinant is near zero, ray lies in plane of triangle float det = Vector3.Dot(edge1, pvec); if (det < 0.0001f) { return(false); } // Calculate distance from vert0 to ray origin Vector3 tvec = DXUtils.Minus(orig, v0); // Calculate U parameter and test bounds u = Vector3.Dot(tvec, pvec); if (u < 0.0f || u > det) { return(false); } // Prepare to test V parameter Vector3 qvec = Vector3.Cross(tvec, edge1); // Calculate V parameter and test bounds v = Vector3.Dot(dir, qvec); if (v < 0.0f || u + v > det) { return(false); } // Calculate t, scale parameters, ray intersects triangle t = Vector3.Dot(edge2, qvec); float fInvDet = 1.0f / det; t *= fInvDet; u *= fInvDet; v *= fInvDet; return(true); }
public static float Perimeter(ref Vector3 v1, ref Vector3 v2, ref Vector3 v3) { return(DXUtils.Dist(ref v1, ref v2) + DXUtils.Dist(ref v2, ref v3) + DXUtils.Dist(ref v1, ref v3)); }