# D3DXIntersect parameters

This topic is 4523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am using D3DXIntersect for a mouse picking function that tells if a mesh has been selected or not. I want to be also able to identify what area o the mesh has been selected but I cant figure out the last 6 parameters from D3DXIntersect function. MSDN doesnt tell to much. Can someone explain these parameters from D3DXIntersect function?
HRESULT WINAPI D3DXIntersect(          LPD3DXBASEMESH pMesh,
const D3DXVECTOR3 *pRayPos,
const D3DXVECTOR3 *pRayDir,
BOOL *pHit,
DWORD *pFaceIndex,
FLOAT *pU,
FLOAT *pV,
FLOAT *pDist,
LPD3DXBUFFER *ppAllHits,
DWORD *pCountOfHits
);



##### Share on other sites
Hello,

Face index is the index of the face/triangle which the ray has intersected with.
u,v are barycentric coordinates of the intersection point on the above mentioned face. These coordinates are not absolute, but rather relative to the plane of the face (hence in 2 dimensions only).
You can use D3DXVec3BaryCentric and the three vertices making the face to extract the world coordinates of your barycentric coordinates.
The two last parameters are only useful if your the mesh you're testing has more than 1 subset (Guessing from your question, your meshes have this). In that case each of these will be tested separetly by the D3DXIntersect function and the last two parameters return the list of intersections and the number of intersection. Each list element will have (faceindex, u, v, dist).

Hope this helps!

Regards,
/Omid

EDIT: typo

##### Share on other sites
Hi Omid,
Quote:
 Face index is the index of the face/triangle which the ray has intersected with.

I am a little confuzed because I made a simple rectangle in 3DS Max and then exported this primitive in .X format. The .X mesh should have two triangles. However when I click on the different parts of the rectangle I recive many (more that two) different FaceIndex values.
Quote:
 You can use D3DXVec3BaryCentric and the three vertices making the face to extract the world coordinates of your barycentric coordinates.

How do I know the position of those three vertices?

[Edited by - Calin on October 7, 2005 11:20:18 AM]

##### Share on other sites
Quote:
 I am a little confuzed because I made a simple rectangle in 3DS Max and then exported this primitive in .X format. The .X mesh should have two triangles. However when I click on the different parts of the rectangle I recive many (more that two) different FaceIndex values.

Are you sure the rectangle had two triangles before exporting? By default, 3ds max creates a plane standard primitive (rectangle) with 32 triangles. Make a test: Export the x-file again as text type and check the number of vertex/faces.

Quote:
 How do I know the position of those the three vertices?

Locking the index/vertex buffers and reading vertex data... Something like this:
mesh->GetIndexBuffer(&pIB);mesh->GetVertexBuffer(&pVB);DWORD numFaces = mesh->GetNumFaces();vert1 = new D3DXVECTOR3[numFaces];vert2 = new D3DXVECTOR3[numFaces];vert3 = new D3DXVECTOR3[numFaces];struct CUSTOMVERTEX{    D3DXVECTOR3 position;    D3DXVECTOR3 normal;};CUSTOMVERTEX *pVertices;WORD* pIndices;	mesh->LockIndexBuffer(D3DLOCK_READONLY, (VOID**)&pIndices);mesh->LockVertexBuffer(D3DLOCK_READONLY,(VOID**)&pVertices);	for ( DWORD face = 0; face < numFaces ; face++ ){   vert1[face] = pVertices[pIndices[3*face+0]].position;   vert2[face] = pVertices[pIndices[3*face+1]].position;   vert3[face] = pVertices[pIndices[3*face+2]].position;}mesh->UnlockVertexBuffer();mesh->UnlockIndexBuffer();

So, use pFaceIndex parameter from D3DXIntersect to get the face intercepted by the ray, and also to get the positions of its vertices:
vert1[pFaceIndex]
vert2[pFaceIndex]
vert3[pFaceIndex]