Getting Vertex Positions from a Mesh

Started by
2 comments, last by Programmer101 16 years, 4 months ago
While this doesn't have anything to do with the graphics part of directx, I think that this is an appropriate place for the topic because it's probably the directx code that is my problem. What I'm trying to do is build a collision tree in newton with the LPD3DXMESH objects in my level. For each mesh I'm building a tree by taking the vertexes out of the mesh data and putting them into the tree. The problem is that... well as you can probably guess, the vertex data I'm putting into the collision tree doesn't match that of the mesh. I'm fairly new to working with LPD3DXMESH vertex buffers so this is probably my problem. I'll plop the code up too:
int AddStaticMesh(LPD3DXMESH &m, const char *name)
{
	//The newton collision object
        NewtonCollision* collision;
	//The vertex data pointer
        char *pVertices;
        //Vertex size and temporary face
	int vert_size;
	D3DXVECTOR3 face[3];
        //Number of faces
	int faces;
        //Vertex declaration array
	D3DVERTEXELEMENT9 pDecl[MAX_FVF_DECL_SIZE];

	//Get the vertex declaration and number of faces from the mesh
        m->GetDeclaration(pDecl);
	faces = m->GetNumFaces();

	//Parse through the mesh to build collision data
	//Lock vertex buffer
	if(FAILED(m->LockVertexBuffer(D3DLOCK_READONLY, (LPVOID*)&pVertices)))
		return FALSE;

        //Initialize the collision object
	collision = NewtonCreateTreeCollision(nWorld, NULL);
	NewtonTreeCollisionBeginBuild(collision);

        //get the vertex size
	vert_size = D3DXGetDeclVertexSize(pDecl,0);

        //Now loop through the faces
	for(int i=0;i<faces;i++)
	{
		//Get vertex 1 and advance the buffer
		sGenericVertex *tempVert = (sGenericVertex*) pVertices;
		face[0] = tempVert->vecPos;
		pVertices+=vert_size;
		//Get vertex 2 and advance the buffer
		sGenericVertex *tempVert2 = (sGenericVertex*) pVertices;
		face[1] = tempVert2->vecPos;
		pVertices+=vert_size;
		//Get vertex 3 and advance the buffer
		sGenericVertex *tempVert3 = (sGenericVertex*) pVertices;
		face[2] = tempVert3->vecPos;
		pVertices+=vert_size;

		//Add the face to the collision tree
		NewtonTreeCollisionAddFace(collision, 3, (dFloat*)face, sizeof(dFloat)*3, 0);
	}

        //Finished building the tree
	NewtonTreeCollisionEndBuild(collision, 0);

	//Unlock vertex buffer
	m->UnlockVertexBuffer();

	//Push back collision
	collisions.push_back(collision);

	return collisions.size()-1;
}

Thanks for reading! Any help would be appreciated.
Advertisement
Quote:Original post by Programmer101
The problem is that... well as you can probably guess, the vertex data I'm putting into the collision tree doesn't match that of the mesh. I'm fairly new to working with LPD3DXMESH vertex buffers so this is probably my problem.


I should mention I have no knowledge of Newton so I can't help you further on that side.

The data you will get won't be transformed. In other words, Newton gets positions for the faces and uses them directly since it looks like your not transforming them. Then when you render you are probably applying it a starting transformation which is not the same as what you sent to Newton.

Once you are sure the two different models are given the same transformation... You should load the faces using the index buffer depending of the primitive type. Probably most of the meshes exported will have an index buffer (not sure if it's not all meshes) and that index buffer will points on the correct vertices for the face you want and this is also dependent on whether you render using triangles, triangle strips, lines, points, etc.

Then if it's still not working, make sure you are using the POSITION element in the declaration, just in case ;). Haven't spent a lot of time on this but I haven't found any restriction on the position of the POSITION in the declaration so they don't have to be at the beginning.

Suggestion (in case you are not doing that already): You should probably use simpler models that the more complex models that are used for rendering... well, if you intend to use complex models. You can also use simple shapers like spheres, boxes or "pills".

Hope I have put everything in the correct order and makes sense... I would guess that your Newton mesh is not created correctly because you are not loading using the correct indices.

JFF
Seems to me like you're ignoring the indices, and trying to create the triangles from the vertex information only. This will not work. You need to access the vertices using the indices in the index buffer.
Thanks to both of you!
It was indeed the indices and as you both mentioned I was ignoring them. When I added the indices in to help me build the collision mesh, it all worked.

This topic is closed to new replies.

Advertisement