tu te compliques la vie.
http://www.faqs.org/faqs/graphics/algorithms-faq/
Subject 5.06: How do I determine the intersection between a ray and a triangle?
First find the intersection between the ray and the plane in which
the triangle is situated. Then see if the point of intersection is
inside the triangle.
Details may be found in [O'Rourke (C)] pp.226-238, whose code is at
http://cs.smith.edu/~orourke/ .
Efficient code complete with statistical tests is described in the Mo:ller-
Trumbore paper in J. Graphics Tools (C code downloadable from there):
http://www.acm.org/jgt/papers/MollerTrumbore97/
See also the full paper:
http://www.Graphics.Cornell.EDU/pubs/1997/MT97.html
See also the "3D Object Intersection" page, described in Subject 0.05.
ou dans l'exemple 'pick' de directx :
//-----------------------------------------------------------------------------
// 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
//-----------------------------------------------------------------------------
BOOL CMyD3DApplication::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;
det = -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;
}