• Advertisement
Sign in to follow this  

Directx Mesh into PhysX Mesh

This topic is 3175 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

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++)
D3DXVECTOR3* pos = (D3DXVECTOR3*)vbptr;
vbptr += stride;
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++)
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++)

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;
// Cooking from memory
MemoryWriteBuffer buf;
bool status = NxCookTriangleMesh(TriMeshDesc, buf);
ShapeDesc.meshData = gPhysicsSDK->createTriangleMesh(MemoryReadBuffer(buf.data));
NxActorDesc actorDesc;
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

Share this post

Link to post
Share on other sites
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,

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
Oh, well, my code to obtain the mesh info:

BYTE *ptr = NULL;
int numIndices;
DWORD numVerts;
DWORD numFaces;
DWORD fvf;
DWORD vertSize;
int count;

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;

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)[ i ];
tri32[count] = ((DWORD*)lpIB)[ i+1 ];
tri32[count] = ((DWORD*)lpIB)[ i+2 ];
{ tri16[count] = ((WORD*)lpIB)[ i ];
tri16[count] = ((WORD*)lpIB)[ i+1 ];
tri16[count] = ((WORD*)lpIB)[ i+2 ];


I hope that can help a bit

Share this post

Link to post
Share on other sites
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?


Share this post

Link to post
Share on other sites
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;

MemoryWriteBuffer buf;
bool status = NxCookConvexMesh(temp_convexDesc,buf);

MemoryReadBuffer readBuffer(buf.data);
convexShapeDesc.meshData = PhysicsSDK->createConvexMesh(readBuffer);

NxActorDesc actorDesc;

bodyDesc.linearVelocity = m_InitialVelocity;
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.

Share this post

Link to post
Share on other sites
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.


Share this post

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

  • Advertisement