Sign in to follow this  
abeltherock007

converting to PhysX meshes

Recommended Posts

I am trying to convert the DX10 mesh into Phyx mesh. I am getting an error
" Unhandled exception at 0x004abb18 in DXPHYSX.exe:0xC0000005:Access Violation reading location 0x0057b270.


void Mesh::CreatePhysxActor()
{
///Copy Mesh data into PhysX Mesh Data
m_PhysXVerts.clear();
m_PhysXIndices.clear();

for ( unsigned int i = 0; i < m_Vertices.size(); i++ )
{
m_PhysXVerts.push_back(NxVec3(m_Vertices[i].vPosition.x, m_Vertices[i].vPosition.y, m_Vertices[i].vPosition.z));
}

for ( unsigned int i = 0; i < m_Indices.size(); i++ )
{
m_PhysXIndices.push_back(m_Indices[i]);
}

NxVec3* _pVerts = new NxVec3[m_PhysXVerts.size()/3];
NxU16* _pTriangles = new NxU16[m_PhysXIndices.size()/3];
//Build Physical Model
NxTriangleMeshDesc _triangleDesc;
_triangleDesc.numVertices = (NxU32)(m_PhysXVerts.size()/3);
_triangleDesc.numTriangles = (NxU32)(m_PhysXIndices.size()/3);
_triangleDesc.pointStrideBytes = sizeof(NxVec3);
_triangleDesc.triangleStrideBytes = sizeof(NxU32) * 3;
_triangleDesc.points = _pVerts;
_triangleDesc.triangles = _pTriangles;
_triangleDesc.flags = NX_MF_16_BIT_INDICES;

NxTriangleMeshShapeDesc _triangleMeshShapeDesc;
NxInitCooking();

//Cooking from Memory
MemoryWriteBuffer _writeBuff;
bool bStatus = NxCookTriangleMesh(_triangleDesc, UserStream("C:\stream.bin", false));
_triangleMeshShapeDesc.meshData = PhysicsManager::GetInstance()->GetPhysicsSDKptr()->createTriangleMesh( UserStream("C:\stream.bin", true));

NxCloseCooking();

NxActorDesc _actorDesc;
_actorDesc.shapes.pushBack(&_triangleMeshShapeDesc);
_actorDesc.globalPose.t = NxVec3(m_sNodeInfo.m_sTransformation.m_vTranslation.x, m_sNodeInfo.m_sTransformation.m_vTranslation.y, m_sNodeInfo.m_sTransformation.m_vTranslation.z);

m_sNodeInfo.m_pActor = PhysicsManager::GetInstance()->GetPhysXScene()->createActor(_actorDesc);
}
[\source]

Share this post


Link to post
Share on other sites
I see a few things that are problems. The first biggie is that when you allocate _pVerts you are allocating a too small array. You must not divide m_PhysXVerts.size() by 3! You need _pVerts to have just as many elements as m_PhysXVerts. Same exact thing with _pTriangles. You must not divide m_PhysXIndices.size() by 3 to get the size of the _pTriangles array. Then, same deal when assigning _triangleDesc.numVertices and .numTriangles. Don't divide by 3. The point and triangle stride stuff, I think that looks okay.

Next super big problem...when you assign _triangleDesc.points to be _pVerts, please note that _pVerts doesn't actually have any data in it! You have allocated arrays but you didn't actually put the vertex data into it. Your vertex data is in m_PhysXVerts, but _pVerts is just a blank array that happens to be sized based on m_PhysXverts. You need to put the data into _pVerts. Same deal with _pTriangles. As you have the code, it does not actually contain the indices, which are in m_PhysXIndices but not in _pTriangles.

My suggestion would be to skip m_PhysXVerts and m_PhysXTriangles. Unless you need them later on, just get rid of them and populate _pVerts and _pTriangles up above instead of populating m_PhysX*. (Well, perhaps you're using them for debug visualization? I don't know. You do not use them for anything here, really.)

Share this post


Link to post
Share on other sites

///Copy Mesh data into PhysX Mesh Data
m_PhysXVerts.clear();
m_PhysXIndices.clear();

NxVec3* _pVerts = new NxVec3[m_Vertices.size()];
NxU16* _pTriangles = new NxU16[m_Indices.size()];

for ( unsigned int i = 0; i < m_Vertices.size(); i++ )
{
m_PhysXVerts.push_back(NxVec3(m_Vertices[i].vPosition.x, m_Vertices[i].vPosition.y, m_Vertices[i].vPosition.z));
_pVerts = &m_PhysXVerts[i];
}

for ( unsigned int i = 0; i < m_Indices.size(); i++ )
{
m_PhysXIndices.push_back(m_Indices[i]);
_pTriangles = &m_PhysXIndices[i];
}

//Build Physical Model
NxTriangleMeshDesc _triangleDesc;
_triangleDesc.numVertices = (NxU32)(m_PhysXVerts.size());
_triangleDesc.numTriangles = (NxU32)(m_PhysXIndices.size());
_triangleDesc.pointStrideBytes = sizeof(NxVec3);
_triangleDesc.triangleStrideBytes = sizeof(NxU32) * 3;
_triangleDesc.points = _pVerts;
_triangleDesc.triangles = _pTriangles;
_triangleDesc.flags = NX_MF_16_BIT_INDICES;

NxTriangleMeshShapeDesc _triangleMeshShapeDesc;
NxInitCooking();

//Cooking from Memory
MemoryWriteBuffer _writeBuff;
bool bStatus = NxCookTriangleMesh(_triangleDesc, _writeBuff);
_triangleMeshShapeDesc.meshData = PhysicsManager::GetInstance()->GetPhysicsSDKptr()->createTriangleMesh( MemoryReadBuffer(_writeBuff.data) );

NxCloseCooking();

NxActorDesc _actorDesc;
_actorDesc.shapes.pushBack(&_triangleMeshShapeDesc);
_actorDesc.globalPose.t = NxVec3(m_sNodeInfo.m_sTransformation.m_vTranslation.x, m_sNodeInfo.m_sTransformation.m_vTranslation.y, m_sNodeInfo.m_sTransformation.m_vTranslation.z);

m_sNodeInfo.m_pActor = PhysicsManager::GetInstance()->GetPhysXScene()->createActor(_actorDesc);




Thanks for the reply. The above is the modified code.. still the error i receive is
Unhandled exception at 0x0026bb18 in MultiThreaded_DX.exe: 0xC0000005: Access violation reading location 0x003ebda0.
in the line

bool bStatus = NxCookTriangleMesh(_triangleDesc, _writeBuff);

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