Reputation: 378
I load a mesh and use D3DXIntersect function to find out which triangle of that mesh I'm about to hit, next I need to know the normal vector of that triangle so I can align myself accordingly. D3DXIntersect lets me know the distance, the face index of the triangle and some u and v float coordinates. With this information, is there any way to get or calculate the normal of that triangle?
This is the code I use to find information about that triangle (so far I'm just returning the distance):
void hit_distance(vector3 origen, vector3 direccion)
{
D3DXVECTOR3 origen_b(float(origen.x), float(origen.y), float(origen.z) );
D3DXVECTOR3 direccion_b(float(direccion.x), float(direccion.y), float(direccion.z));
BOOL hit = false;
DWORD faceIndex = -1;
float u = 0.0f;
float v = 0.0f;
float dist = 0.0f;
ID3DXBuffer* allhits = 0;
DWORD numHits = 0;
D3DXIntersect(esfera_de_180, &origen_b, &direccion_b, &hit, &faceIndex, &u, &v, &dist, &allhits, &numHits);
SAFE_RELEASE(allhits);
if (hit)
{
distancia_mesh = dist;
}
}
Upvotes: 0
Views: 314
Reputation: 3180
Yes, that is possible, but it's a little effortful.
With the returning faceIndex you can lookup the three indices of the triangle in the corresponding indexbuffer, which you can resolve to the vertices in the vertexbuffer. These are containing all information you need to compute the normal. Either the face normal by using the positions or the real normal by interpolating the vertex normals with the barycentric coordinates u and v.
To retrieve the index- and vertexbuffer you can use the corresponding methods of your mesh (Docs)
Upvotes: 2