Problem with setting stride properly when creating triangle mesh

Hello. I’m new to using PhysX but I have a little bit of experience using other physics engines. I want to test out the waters because I’ve been having big issues with Bullet. So my problem seems to be that the stride for my vertices in the triangle mesh descriptor seems to be wrong for some reason but I can’t figure out why and I’m at my wit’s end.

The error that I’m getting is this:

…..\PhysXCooking\src\Cooking.cpp (127) : invalid parameter : Cooking::cook/createTriangleMesh: user-provided triangle mesh descriptor is invalid!

…..\GeomUtils\src\mesh\GuTriangleMesh.cpp (81) : invalid operation : Gu::TriangleMesh::release: double deletion detected!

And here’s my code:

if(Mesh->getMeshBuffer(0)->getIndexCount())
{

	void *Vertices = Mesh->getMeshBuffer(0)->getVertices();
	u16 *Indices = Mesh->getMeshBuffer(0)->getIndices();

	Desc.triangles.count = Mesh->getMeshBuffer(0)->getIndexCount() / 3;
	Desc.triangles.stride = 3 * sizeof(u16);
	Desc.triangles.data = Indices;

	Desc.points.count = Mesh->getMeshBuffer(0)->getVertexCount();
	Desc.points.stride = sizeof(vector3df) + sizeof(SColor) + sizeof(vector2df) + sizeof(vector2df);
	Desc.points.data = Vertices;

}

[...]

	PxDefaultMemoryOutputStream writeBuffer;
	XPhysics->Cooking->cookTriangleMesh(XChunk->Desc, writeBuffer);

	PxDefaultMemoryInputData readBuffer(writeBuffer.getData(), writeBuffer.getSize());
	XChunk->PxMesh = XPhysics->Physics->createTriangleMesh(readBuffer);

The indices are 16 bit and the vertices are Irrlicht’s S3DVertex2TCoords which look like this: S3DVertex2TCoords(vector3df vertex, vector3df normal, SColor color, vector2df tcoords, vector2df tcoords2). The vertices are at the beginning of the data so I set the stride to the size of everything but the first vector3df. I even made sure that the vertices actually are at the beginning of the data by arithmetically stepping through the void pointer like so:

f32 *ptr = static_cast<f32*>(Vertices);
	for(int i = 0; i < 17; ++i)
	{
		if(i == 6)
			printf("i: %d = %d\n", i, (u32)*ptr);
		else
			printf("i: %d = %f\n", i, *ptr);
		ptr++;
	}

Here’s the console output which confirms that the vertex position is at the begining of the data: http://i.imgur.com/vGILeBw.png

I feel like I’m doing everything right but obviously something is wrong. I would appreciate any help greatly. Thank you for your time.

Edit: I changed the indices code and I’m getting a different message now which seems to imply I’m closer:

Desc.flags |= PxMeshFlag::e16_BIT_INDICES;
	Desc.triangles.count = Mesh->getMeshBuffer(0)->getIndexCount() / 3;
	Desc.triangles.stride = 3 * sizeof(u16);
	Desc.triangles.data = Indices;

Now the error output is as seen below. Apparently you have to manually specify that you want to use 16 bit vertices. However now it says that the vertex data is corrupted. I wonder what’s wrong now.

…..\PhysXCooking\src\mesh\TriangleMeshBuilder.cpp (204) : internal error : input mesh contains corrupted vertex data

…..\GeomUtils\src\mesh\GuTriangleMesh.cpp (81) : invalid operation : Gu::TriangleMesh::release: double deletion detected!

“…so I set the stride to the size of everything but the first vector3df”

I think that is the problem. The stride is the byte separation between the start of one position and the start of the next.

In your case it is

stride = sizeof(vector3df)*2 + sizeof(SColor) + sizeof(vector2df) + sizeof(vector2df);

Note the multiple of 2 above.

Cheers,

Gordon