[DirectX9 C++] Get vertices from mesh

Started by
4 comments, last by r_bewick 13 years, 4 months ago
Hi, I am trying to get all the vertices of my meshes in order to use them for collision detection.
For some reason it crashes on some meshes but not others, could my _VERTEX struct possibly be wrong? All I am looking for is the position but maybe the other things could come in handy later.

By crashing I mean that I loop through all the vertices and when I get up to about 90% of numVerts, pVerts suddenly is NULL.
I can load all the meshes in DirectX viewer, and they draw fine, so that is not the problem, but just when I try to get the vertices it seems like there is less vertices than what I get from m_pMesh->GetNumVertices(), therefore making pVerts equal zero at a certain point when I run out of vertices.
Can someone help me fix this?
Thanks,
r_bewick

#define FVF_VERTEX    D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1struct _VERTEX{	D3DXVECTOR3 pos;     // vertex position	D3DXVECTOR3 norm;    // vertex normal	float tu;            // texture coordinates	float tv;};vector<D3DXVECTOR3> CMesh::GetVertices(){	vector<D3DXVECTOR3> vecVertices;		_VERTEX* pVerts;	m_pMesh->LockVertexBuffer(0,(void**)&pVerts);	// get vertex count	int numVerts=m_pMesh->GetNumVertices();	for (int i=0;i<numVerts;i++) 	{		D3DXVECTOR3 vec3Vertice;		vec3Vertice.x = pVerts->pos.x;		vec3Vertice.y = pVerts->pos.y;		vec3Vertice.z = pVerts->pos.z;		vecVertices.push_back(vec3Vertice);		// go to next vertex		pVerts++;	}	// unlock the vertex buffer	m_pMesh->UnlockVertexBuffer();	return vecVertices;}
Advertisement
How sure are you that any mesh you load has a _VERTEX format? It's likely that the "failing" meshes don't have that structure.

If you're looking just for the position from the vertex, use (lock) a BYTE* instead of _VERTEX* and increment it by mesh->GetNumBytesPerVertex() to point to the next vertex.

Another option, mesh->GetDeclaration() and analyse it.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks Buckeye, that sounds exactly right. :)
From this and another post you wrote here http://www.gamedev.net/community/forums/topic.asp?topic_id=564315 Buckeye I was able to figure most of it out, my collisions are now working.

Could you please explain to me what the offset is? for now I have put in 0, but I want to make sure my code works with all directX meshes.
Thanks,
r_bewick

heres the working code.

vector<D3DXVECTOR3> CMesh::GetVertices(){	vector<D3DXVECTOR3> vecVertices;		int numVerts=m_pMesh->GetNumVertices();	BYTE* pVerts;	DWORD numBytesPerVertex = m_pMesh->GetNumBytesPerVertex();	unsigned int uiSize = D3DXGetFVFVertexSize(MeshFVF);	m_pMesh->LockVertexBuffer(0,(void**)&pVerts);	int iOffset = 0;	for (WORD i=0;i<numVerts;i++) 	{		D3DXVECTOR3 vec3Vertice = *((D3DXVECTOR3*)(pVerts + i*uiSize + iOffset));		vecVertices.push_back(vec3Vertice);	}	// unlock the vertex buffer	m_pMesh->UnlockVertexBuffer();	return vecVertices;}
For the vertex position you don't need the offset. The offset is the number of bytes from byte 0 of the vertex structure to the element you're interested in.

E.g., if the vertex structure is:

D3DXVECTOR3 position; // offset 0 (start of structure), 12 bytes long
D3DXVECTOR3 normal; // offset 12 (after 12 bytes of position), 12 bytes long
D3DXVECTOR2 texCoords; // offset 24 (after 12 bytes position + 12 bytes normal), 8 bytes long

the offset to retrieve the texcoords would be 24.

For an unknown mesh, you can get the vertex declaration, and determine the offset to any element from that. Note: if the vertex has a position, that's always offset 0.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Thanks again :)
r_bewick, thanks for the code but I get a mesh's vertices and multiply with a matrix, then how to update back to the mesh's vertex buffer? I tried many ways but it has not been working :(

This topic is closed to new replies.

Advertisement