Jump to content
  • Advertisement
Sign in to follow this  
diegor82

D3DXMesh: Retrieve face-vertices data

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everybody, Given a D3DXMesh, I can retrieve the number of faces calling mesh->GetNumFaces(). But how can I retrieve the 3 vertex positions for each face? Thank you in advance, Diego

Share this post


Link to post
Share on other sites
Advertisement
hi,

you lock the vertex buffer and read the positions from there. if you know the FVF, then you know the size of the structures contained in there, and the offset of the position (it should be the first in the structure).

if you want to know which vertices build up a specific triangle, then you lock the index buffer too, which contains the indices of the vertices in the vertex buffer (3 words, or dwords if 32-bit meshes are used).

For more info, open the SDK docs, and look at:
DX Graphics/Programming guide/D3D rendering/Rendering primitives/Rendering from vertex and index buffers

kp

Share this post


Link to post
Share on other sites
Thank you...
But the indices don't tell me which vertex belong to which face (indices can represent strip,fans, single triangles),
and
I would like know for each face, the vertex positions v1,v2,v3.

Without additional information I can't do anything with the index buffer.

Do you agreed?

Share this post


Link to post
Share on other sites
No, I don't agree :)

So. Say you want to know the vertices belonging to triangle number 2 (assuming 0 is the first). Say you have a 16-bit index buffer, so it contains WORDs.

Then the index buffer looks like this
0 index of vertex1 in triangle 0
1 index of vertex2 in triangle 0
2 index of vertex3 in triangle 0
3 index of vertex1 in triangle 1
4 index of vertex2 in triangle 1
5 index of vertex3 in triangle 1
6 index of vertex1 in triangle 2
7 index of vertex2 in triangle 2
8 index of vertex3 in triangle 2

So you read elements 6,7,8. These are the indexes pointing into the vertex buffer. eg. you read 102, 223 and 107

So you lock your vertex buffer, and read vertex number 102, 223 and 107. The size of each vertex can be counted from the FVF. Buf usually you have a structure which you can use to access vertices, and you pass a pointer pointing to a structure like this to Lock(). Than you can just say pointer[x].pos

kp

Share this post


Link to post
Share on other sites
Are you sure that in the index buffer there are always three indices per primitive?
I don't think so.

For example, one can save the indices of a triangle strip.
In this case you have 3 index for the first triangle and then shared indices for the rest.

Furthermore, in a indexbuffer of a D3DXMesh there could be more than one type of primitive list (for example, first a triangle strip , then single triangles and the triangle fans).

Now you agreed for sure that the index buffer is not enough
=)

Share this post


Link to post
Share on other sites
In the D3DXMesh the IB/VB data is setup as a triangle list. So, in the IB there will always be 3 indices per triangle.

Share this post


Link to post
Share on other sites

IDirect3DIndexBuffer9 *ib;
IDirect3DVertexBuffer9 *vb;
ID3DXMesh *mesh;

// [.......................................] //

unsigned short *indices;
unsigned char *vertices;

ib->Lock(0,0,(void **)&indices,0);
vb->Lock(0,0,(void **)&vertices,0);

for (unsigned long i = 0; i < mesh->GetNumVertices(); i++)
{
D3DXVECTOR3 *pVertex = (D3DXVECTOR3 *)&vertices[mesh->GetNumBytesPerVertex()*indices];
// <do what you want with pVertex
}

vb->Unlock();
ib->Unlock();


Share this post


Link to post
Share on other sites
The code above is slightly in error.

You should not index the index buffer from a D3DXMesh using num vertices. Since that will often not give you all of them.

You should use num faces * 3.

Like this:

for (unsigned long i = 0; i < (mesh->GetNumFaces() * 3); i++)
{
D3DXVECTOR3 *pVertex = (D3DXVECTOR3 *)&vertices[mesh->GetNumBytesPerVertex()*indices];
// <do what you want with pVertex
}


Share this post


Link to post
Share on other sites
I didn't know that was wrong, personally I haven't ever used an index buffer considering single vertices. However I tried changing (in my engine) mesh->GetNumVertices() with mesh->GetNumFaces()*3 and I didn't see anything on the screen. Guess rendering calls prefer GetNumVertices().

Share this post


Link to post
Share on other sites
Well, it's very possible for the number of vertices to be less than the number of indices (or faces * 3).

For example, a cube can be made from 8 vertices, but it has 12 triangles and therefore has 36 indices in it's index buffer. Of course, depending on the normals and UVs you could have as many as 24 vertices, but it's very unlikely that you would have more then 24 unique vertices for a cube, but you would always have 36 indices in the IB for a D3DXMesh version of the cube, since they use the triangle list form.

Perhaps, there was something else missing or out of whack? Also, it's possible that you have a mesh that has all unique vertices (that could happen if you never optimize them), but it's pretty uncommon. That would make the vertex count succeed in your example, but it doesn't explain why you saw nothing when you used the face count * 3. How is your D3DXMesh created/filled in?

I use D3DXMesh stuff pretty extensively in my work and I am pretty certain of how they work and store their data.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!