Sign in to follow this  

how to read triangles from a vertex buffer

Recommended Posts

Hello, i've got to find the center of all the faces of a mesh. I can find its vertex buffer and lock/unlock it but i'm not able to read it correctly because i don't know how are stored the data. I'm using a D3DXVECTOR3 pointer to read data. Now, how can i read points from a same triangle. It seems that i read points from differents triangles, so i got bad centers. I'm using index 1.2.3 for first triangle, 4.5.6 for second, etc... but it doesn't work sorry for this poor english and thank you

Share this post

Link to post
Share on other sites
Option one: Get the FVF code or the declaration for the vertex buffer and calculate the vertex component offsets manually, after which you should add the said offsets to the vertex pointer to access individual vertex elements


Option two: If you have an ID3DXMesh, clone the mesh to a format that you do know to handle, and extract the info in a straight-forward manner from it. During format conversion, the mesh interface does option one, described above, automatically.

See the SDK for correct FVF component orders, if you go the manual way.

Share this post

Link to post
Share on other sites
Original post by Bouteille51
So, if i understand, locking the vertex buffer and transforming the void * pointer into a D3DXVECTO3 pointer only works for a kind of FVF (just with x,y,z) ?

Well, it works in 2 cases:
- Your vertex structure is only x,y,z
- You know the size of your vertex structure

You can easily determine the size of your vertex structure from the FVF. Say vertices points to the locked vertices, you can do:

BYTE* vertices;
while(vertices < vertices + size_of_vertex * numVertices)
D3DXVECTOR* vec = static_cast<D3DXVECTOR*>(vertices);
vertices += size_of_vertex

Share this post

Link to post
Share on other sites
Alternatively, it is possible to calculate the vertex size from the fvf code or from a vertex format declaration; this can be done in the same function as the component offset calculation I mentioned earlier.

You can get a vertex declarator from a fvf code - it is far easier to determine the offsets and the vertex size from that than the raw fvf flags, and D3DX even offers to calculate the size for you via the D3DXGetDeclVertexSize() function. The declarator holds the data offsets implicitly, so that doesn't need any further manual calculations either.

When you do have the offsets, it is easy to address a vertex's component with them:

BYTE *pVertices;
//Lock the vertex buffer as usual, save the pointer to pVertices.
//Code for that omitted as irrelevant for this example.
//DWORD numVertices; has been initialized to some known value beforehand.
//Also, the offsets and the vertex size are already determined (using the declarator, preferably)!

//pointers to individual vertex components
D3DXVECTOR3 *pos, *nrm;
D3DXVECTOR2 *texcoord;

for (DWORD vi=0;vi<numVertices;vi++)
pos = (D3DXVECTOR3*)pVertices+vi*vertexSize+posOffset;
nrm = (D3DXVECTOR3*)pVertices+vi*vertexSize+nrmOffset;
texcoord = (D3DXVECTOR2*)pVertices+vi*vertexSize+tcoord0Offset;
//Repeat this as necessary, until you get all the component pointers you want.

//Manipulate the vertex's data here as necessary!


//Unlock the vertex buffer - again, code omitted as irrelevant.


EDIT: Just noticed, bad vertex pointer handling on my behalf - fixed!

[Edited by - Nik02 on September 5, 2004 5:33:00 PM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this