I’m a complete beginner with C++ (and the Physx SDK for that matter). I’m trying to modify a snippet of code to return a custom structure of data. Here are the structs:
struct Vector3f {
float x;
float y;
float z;
Vector3f() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
Vector3f(float xNew, float yNew, float zNew) {
x = xNew;
y = yNew;
z = zNew;
}
} Vector3;
struct MeshData {
Vector3f* vertices;
Vector3f* normals;
physx::PxU16* triangles;
};
And here is the main code in question:
PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
PxConvexMesh* mesh = gPhysics->createConvexMesh(id);
PxU32 nbVerts = mesh->getNbVertices();
const PxVec3* convexVerts = mesh->getVertices();
const PxU8* indexBuffer = mesh->getIndexBuffer();
const PxU32 nbPolygons = mesh->getNbPolygons();
const PxU32 numberOfVertices = mesh->getNbVertices();
Vector3f* vertices = new Vector3f[numberOfVertices];
Vector3f* normals = new Vector3f[nbPolygons];
PxU16* triangles = new PxU16[nbPolygons * 6];
PxU32 offset = 0;
for(PxU32 i = 0; i<nbPolygons; i++) {
PxHullPolygon face;
bool status = mesh->getPolygonData(i, face);
PX_ASSERT(status);
const PxU8* faceIndices = indexBuffer + face.mIndexBase;
for(PxU32 j = 0; j<face.mNbVerts; j++) {
vertices[offset + j] = Vector3f(convexVerts[faceIndices[j]].x, convexVerts[faceIndices[j]].y, convexVerts[faceIndices[j]].z);
normals[offset + j] = Vector3f(face.mPlane[0], face.mPlane[1], face.mPlane[2]);
}
for(PxU32 j = 2; j<face.mNbVerts; j++) {
*triangles++ = PxU16(offset);
*triangles++ = PxU16(offset + j);
*triangles++ = PxU16(offset + j - 1);
}
offset += face.mNbVerts;
}
meshData.vertices = vertices;
meshData.normals = normals;
meshData.triangles = triangles;
return meshData;
Through some mind-numbing debugging I’ve found that the vertices and normals arrays are causing memory corruption. I’ve tried quite a few different things, but they won’t even compile.
Thank you for your time!