DirectX explanation triangular detecting code and

bool IntersectTriangle(const D3DXVECTOR3& orig, const D3DXVECTOR3& dir,
    D3DXVECTOR3& v0, D3DXVECTOR3& v1, D3DXVECTOR3& v2,
    FLOAT* t, FLOAT* u, FLOAT* v)
{
    // Find vectors for two edges sharing vert0
    D3DXVECTOR3 edge1 = v1 - v0; D3DXVECTOR3 edge2 = v2 - v0;
 // Begin calculating determinant - also used to calculate U parameter D3DXVECTOR3 pvec; D3DXVec3Cross(&pvec, &dir, &edge2); // If determinant is near zero, ray lies in plane of triangle FLOAT det = D3DXVec3Dot(&edge1, &pvec); D3DXVECTOR3 tvec; if (det > 0) { tvec = orig - v0; } else { tvec = v0 - orig;it = -
det; } if (det < 0.0001f) return FALSE; // Calculate U parameter and test bounds *u = D3DXVec3Dot(&tvec, &pvec); if (*u < 0.0f || *u > det) return FALSE; // Prepare to test V parameter  D3DXVECTOR3 qvec; D3DXVec3Cross(&qvec, &tvec, &edge1); // Calculate V parameter and test bounds *v = D3DXVec3Dot(&dir, &qvec); if (*v < 0.0f || *u + *v > det) return FALSE; // Calculate t, scale parameters, ray intersects triangle *t = D3DXVec3Dot(&edge2, &qvec); FLOAT fInvDet = 1.0f / det; *t *= fInvDet; *u *= fInvDet; *v *= fInvDet; return TRUE; }

First of all: We explain how theoretically calculated:

The above theoretical part explains the lot of heavyweights:

In fact, most of the code is also well understood, only

IF (DET> 0) 
    { 
        tvec orig = - V0; 
    } 
    the else 
    { 
        tvec V0 = - orig; 
        DET =-DET; 
    } 
This paragraph may have friends there like me confused as to why such a deal?
We calculated t is actually a qualification, if it is not t o less than intersection.
Code is not discussed here is t, that is, for the adjustment here is to ensure that the parameter T t is positive.
We can see DxE2 * E1 can be written so E2xE1 * D D det is greater than 0 then the pointing direction E2xE1 same side, and t is determined by the sign [] = [E1E2T TE1E2] That T must keep E1xE2 on the same side, we can imagine in my mind, det is greater than 0 then it is not directly V0 - "Orig, det is not less than 0 is Orig -" V0?
Obviously that is the case. In short, demo tweak the code T in the phrase of the role is in this. However, this method can not detect seemingly case-ray with his back to the plane. Do not believe you can try to use the most simple data, or returns true.

Guess you like

Origin www.cnblogs.com/Tonarinototoro/p/11404047.html