TriangleMesh Problem

Hello,

I have a problem with the validation of TriangleMeshes.
Even with only one triangle the validation is not working.
Please tell me where the problem is in the following code:

static const PxVec3 verts[] = { PxVec3(0,0,0), PxVec3(10,0,0), PxVec3(10,0,10)
};
static const PxU16 indices[] = {1, 2 ,3
};

PxTriangleMeshDesc meshDesc;
meshDesc.points.count = 3;
meshDesc.points.stride = sizeof(PxVec3);
meshDesc.points.data = verts;

meshDesc.triangles.count = 3;
meshDesc.triangles.stride = 3*sizeof(PxU16);
meshDesc.triangles.data = indices;

PxDefaultMemoryOutputStream writeBuffer;
PxTriangleMeshCookingResult::Enum result;
gCooking->cookTriangleMesh(meshDesc, writeBuffer, &result);
PxDefaultMemoryInputData input(writeBuffer.getData(), writeBuffer.getSize());
PxTriangleMesh* triMesh = gPhysics->createTriangleMesh(input);

PxTransform pos = PxTransform(PxVec3(0, 10, 0));
PxTransform relativePose(PxQuat(PxHalfPi, PxVec3(1, 0, 0)));
PxRigidDynamic* triActor = gPhysics->createRigidDynamic(pos);
PxShape* triShape = PxRigidActorExt::createExclusiveShape(*triActor, PxTriangleMeshGeometry(triMesh), *groundMaterial);
triShape->setLocalPose(relativePose);
gScene->addActor(*triActor);

→ : invalid parameter : TriangleMesh::loadFromDesc: desc.isValid() failed!
→ : invalid parameter : Supplied PxGeometry is not valid. Shape creation method returns NULL.

Thanks!

Hi,
sorry for the late answer. The provided mesh descriptor is invalid because of several reasons:

  • if you are using Px16 indices, you need to set mesh desc flag: meshDesc.flags = PxMeshFlag::e16_BIT_INDICES;
  • you have only one triangle so meshDesc.triangles.count = 1;
  • the provided indices must start from index 0 not 1 so static const PxU16 indices = { 0, 1 ,2
    };

The example above would look like this:
static const PxVec3 verts = { PxVec3(0,0,0), PxVec3(10,0,0), PxVec3(10,0,10)
};
static const PxU16 indices = { 0, 1 ,2
};

PxTriangleMeshDesc meshDesc;
meshDesc.points.count = 3;
meshDesc.points.stride = sizeof(PxVec3);
meshDesc.points.data = verts;

meshDesc.triangles.count = 1;
meshDesc.triangles.stride = 3 * sizeof(PxU16);
meshDesc.triangles.data = indices;

meshDesc.flags = PxMeshFlag::e16_BIT_INDICES;

PxDefaultMemoryOutputStream writeBuffer;
PxTriangleMeshCookingResult::Enum result;
getCooking().cookTriangleMesh(meshDesc, writeBuffer, &result);

Regards,
Ales