Directx Mesh into PhysX Mesh

Started by
5 comments, last by JamesCobras 14 years, 11 months ago
Yes I know there is a similar topic but this one is different. I have managed to load a mesh into PhysX. I know the mesh is fine, because i render it with directx at the same time, and yes it's triangulated, and dead simple (well my most recent one is). I have tried altering some of the code to change the order the vertices are in putted, i.e. shifting them forward and backward by one in the vector. This for some reason created an identical mesh in PhysX (checked with remote debugger). Here is the code i use.

std::vector<float> GetVertices(LPD3DXMESH g_pMesh)
{
std::vector<float> vertices;
DWORD stride =  D3DXGetFVFVertexSize(g_pMesh->GetFVF());
BYTE* vbptr = NULL;
g_pMesh->LockVertexBuffer(0, (LPVOID*)&vbptr);
int ii = -1;
for(unsigned int i = 0; i < g_pMesh->GetNumVertices(); i++)
{
ii++;
D3DXVECTOR3* pos = (D3DXVECTOR3*)vbptr;
vertices.push_back(pos->x);
vertices.push_back(pos->y);
vertices.push_back(pos->z);
vbptr += stride;
}
g_pMesh->UnlockVertexBuffer();
return vertices;
}


std::vector<short> GetIndices(LPD3DXMESH     g_pMesh)
{
//LPVOID* ppData;
DWORD stride = sizeof(short);
BYTE* ibptr = NULL;
short* indices = new short[(g_pMesh)->GetNumFaces() * 3];
std::vector<short> copy;
(g_pMesh)->LockIndexBuffer(0, (LPVOID*)&indices);
for(unsigned int i = 0; i < (g_pMesh)->GetNumFaces() * 3; i++)
{
copy.push_back(indices);
}
(g_pMesh)->UnlockIndexBuffer();
return copy;
}










NxActor* GenTriangleMesh(NxVec3 pos, std::vector<short> indices, std::vector<float> vertices )
{
int NumVerticies = vertices.size() / 3;
int NumTriangles = indices.size() / 3;
//Create pointer for vertices
NxVec3* verts = new NxVec3[NumVerticies];
int ii = -1;
for(int i = 0; i < NumVerticies; i++)
{

++ii;
verts.x = vertices[ii];
verts.y = vertices[++ii];
verts.z = vertices[++ii];
}
//Create pointer for indices
NxU16 *tris = new NxU16[indices.size()];
for(int i = indices.size() - 1; i >= 0; --i)
tris = i;
// Build physical model
NxTriangleMeshDesc TriMeshDesc;
TriMeshDesc.numVertices = NumVerticies;
TriMeshDesc.numTriangles = NumTriangles;
TriMeshDesc.pointStrideBytes = sizeof(NxVec3);
TriMeshDesc.triangleStrideBytes = 3*sizeof(NxU16);
TriMeshDesc.points = verts;
TriMeshDesc.triangles = tris;
TriMeshDesc.flags = NX_MF_16_BIT_INDICES ;//| NX_MF_FLIPNORMALS  ;
NxTriangleMeshShapeDesc ShapeDesc;
NxInitCooking();
// Cooking from memory
MemoryWriteBuffer buf;
bool status = NxCookTriangleMesh(TriMeshDesc, buf);
ShapeDesc.meshData = gPhysicsSDK->createTriangleMesh(MemoryReadBuffer(buf.data));
NxActorDesc actorDesc;
actorDesc.shapes.pushBack(&ShapeDesc);
actorDesc.globalPose.t = pos;

NxActor* act = gScene->createActor(actorDesc);
delete[] verts;
delete[] tris;
return act;
}



Here are the images of the problem: The direct x ones show what it is like at render time and how the meshes should be, the PhysX remote Debugger shows otherwise. As you can see triangles are missing top and bottom, it seems systematic but i don't know how to fix it. Please provide me with some code to fix this or some examples as i have no idea what to do or where to start. Thanks JamesCobras
Advertisement
Hi, As far i can see, your box are wrong from the source, remember, to draw
the primitives directx uses triangles, not quads or some, so, check on your
3d modeling software that, make the mesh only with triangles, no quads, no
five sides polys, maybe your model have some of those, then when directx
triangulates the mesh screw all. Maybe could be that,
Sorry i obviously wasn't clear.
The direct x model renders perfectly in my app when rendered but the collision mesh is not generated properly from the .x mesh file.

It is in this forum because this is similar to parse an .x file. it seems that it is missing some of the faces, but i'm not sure how to code it to fix it.

The mesh is fine and renders well just it is not parsed correctly into PhysX, that is where i need the help, i'm nearly there but not quite.
Oh, well, my code to obtain the mesh info:

LPD3DXMESH mesh;	BYTE				    *ptr = NULL;	BYTE                                *lpIB = NULL;	LPDIRECT3DINDEXBUFFER9              lpIndexBuffer;	D3DINDEXBUFFER_DESC                 ibdesc;	int				    numIndices;	DWORD				    numVerts;	DWORD				    numFaces;	DWORD				    fvf;	DWORD				    vertSize;	int			            count;	HRESULT				LM;	LM = D3DXLoadMeshFromX(ThisMeshDesc.meshfile,D3DXMESH_MANAGED,pDevice,NULL,NULL,NULL,NULL,&mesh);	if(LM != D3D_OK) 	{  	MessageBox(0,ThisMeshDesc.meshfile,"Can't Find:",0);	}			count = 0;	mesh->LockVertexBuffer(0, (LPVOID*)&ptr);	numvertex = numVerts = mesh->GetNumVertices();	numfaces = numFaces = mesh->GetNumFaces();	fvf = mesh->GetFVF();	vertSize = D3DXGetFVFVertexSize(fvf);	verts = new NxVec3[numVerts];				for(DWORD i=0;i<numVerts;i++)		{			NxVec3 *vPtr=(NxVec3 *) ptr;			verts.x = vPtr->x;			verts.y = vPtr->y;			verts.z = vPtr->z;			ptr+=vertSize;				}		mesh->UnlockVertexBuffer();		mesh->LockIndexBuffer ( D3DLOCK_READONLY, (VOID**)&lpIB); 		mesh->GetIndexBuffer( &lpIndexBuffer );		lpIndexBuffer->GetDesc( &ibdesc );		if(ibdesc.Format == D3DFMT_INDEX32)		{numIndices = ibdesc.Size / sizeof(DWORD); 		tri32 = new NxU32[numFaces*3]; 		b_32 = true; }		else{numIndices = ibdesc.Size / sizeof(WORD);		tri16 = new NxU16[numFaces*3];		b_32 = false;}	for(int i=0; i<numIndices; i += 3 )	{		if (ibdesc.Format == D3DFMT_INDEX32  )	    {		tri32[count] = ((DWORD*)lpIB);<br>				count++;<br>				tri32[count] = ((DWORD*)lpIB)[ i+1 ];<br>				count++;<br>				tri32[count] = ((DWORD*)lpIB)[ i+2 ];<br>				count++;<br>	    }<br>		else<br>		{		tri16[count] = ((WORD*)lpIB);<br>				count++;<br>				tri16[count] = ((WORD*)lpIB)[ i+1 ];<br>				count++;<br>				tri16[count] = ((WORD*)lpIB)[ i+2 ];<br>				count++;<br>		}<br>	}<br><br>	mesh-&gt;UnlockIndexBuffer();<br>	mesh-&gt;Release();<br></pre><br><br>I hope that can help a bit
unfortunately that didn't work, maybe i integrated it wrong do you have the rest of the code?

Just that it looked like nothing was passed to PhysX.

Grrrr, this is starting to irritate me. i thought i know how to do this, but it just is too cryptic and the api really isn't much help...

Maybe i should export to a different format to cook file and just render with .x files but a better solution is preferred.

Help please, i'm really getting disparate, has anyone gone about this a different way?

Jamescobras.
For cook the mesh:

NxBodyDesc bodyDesc;			NxConvexMeshDesc temp_convexDesc;			temp_convexDesc.numVertices					= Mesh->numvertex;			temp_convexDesc.pointStrideBytes			= sizeof(NxVec3);			temp_convexDesc.points						= Mesh->verts;			temp_convexDesc.flags 	= NX_CF_COMPUTE_CONVEX;			bodyDesc.solverIterationCount = 5;			NxConvexShapeDesc convexShapeDesc;			convexShapeDesc.localPose.t		= NxVec3(0,0,0);			convexShapeDesc.mass = m_mass;			convexShapeDesc.density = .001;			NxInitCooking();			MemoryWriteBuffer buf;			bool	status = NxCookConvexMesh(temp_convexDesc,buf);			MemoryReadBuffer readBuffer(buf.data);			convexShapeDesc.meshData = PhysicsSDK->createConvexMesh(readBuffer);			NxActorDesc actorDesc;			 bodyDesc.linearVelocity = m_InitialVelocity;			 actorDesc.shapes.pushBack(&convexShapeDesc);			 actorDesc.body = &bodyDesc			 actorDesc.density = 1.0;			 			 actorDesc.globalPose.t = m_InitialPosition;			 actorDesc.group = m_group;			 actorDesc.userData = obj;			 actorDesc.contactReportFlags = NX_NOTIFY_ALL;					PhysXActor = Scene->createActor(actorDesc);


I hope that can help, if you still have the problem, send me MP.
Thanks Mate i had it close to that, just passed the wrong stuff in the wrong places, i think it was the stride amount that i got wrong, but thanks mate, collision, working perfectly.

JamesCobras

This topic is closed to new replies.

Advertisement