Extracting Triangle from IntersectInformation

Started by
4 comments, last by neneboricua19 19 years, 3 months ago
Hi, guys. I'm stumped about how to find the actual triangle from an IntersectionInformation instance, which only gives you the index of the triangle. I was thinking of extracting the VB from the Mesh object and Locking it. However, how do I find the nth triangle? Thanks in advance. BTW, I'm using C# and MDX.
Advertisement
Triangle n can be found by locking both the index buffer and the vertex buffer. Use the index of the triangle given to you by your intersection method. Use this to find the indices of your trianlge.

v0 = pVertexBuffer[pIindexBuffer[n*3+0]]
v1 = pVertexBuffer[pIindexBuffer[n*3+1]]
v2 = pVertexBuffer[pIindexBuffer[n*3+2]]

So now v0, v1, and v2 contain the vertices of the triangle that was intersected. Of course, this assumes that the variables "pVertexBuffer" and "pIndexBuffer" are arrays refering to your vertex and index data respectively. You can get these by locking the vertex and index buffers of your mesh.

neneboricua
So like this?

CustomVertex.PositionNormalColored[] v = mesh.LockVertexBuffer(typeof(CustomVertex.PositionNormalColored),LockFlags.NoOverwrite,new int[]{1});int[] i = mesh.LockIndicesBuffer(typeof(int),LockFlags.NoOverwrite,new int[]{1});Vector3 v0 = v[i[3*index+0]].GetPosition;Vector3 v1 = v[i[3*index+1]].GetPosition;Vector3 v2 = v[i[3*index+2]].GetPosition;


Also, this returns the coordinates in mesh space, right?
Pretty much. I'm not familiar with Managed DX but that seems correct. Just remember to unlock both your vertex and index buffers when you're done accessing the triangle information.

The coordinates will be in mesh space. So if you want to know where in world space your triangle is, you'll need to transform the vertices of the triangle by the mesh's World matrix.

neneboricua
Thanks. Oh, and one more question, if you don't mind. Do you know if the origin of the Mesh returned by Mesh.Sphere is at the center of the sphere?
Quote:Original post by yaroslavd
Thanks. Oh, and one more question, if you don't mind. Do you know if the origin of the Mesh returned by Mesh.Sphere is at the center of the sphere?

More than likely. In general, the artist working on a model defines what the "origin" of the model is. In the case of a sphere the mesh origin is probably the center of the sphere.

neneboricua

This topic is closed to new replies.

Advertisement