Deserializing triangle mesh

Post Reply
samapico
Posts: 2
Joined: Thu Jan 17, 2013 10:12 pm

Deserializing triangle mesh

Post by samapico »

I was trying to save and load a triangle mesh, instead of regenerating it every time to save some loading time. I only serialize one shape individually. When loading it, I do this:

Code: Select all

        btBulletWorldImporter import(0);

        if (import.loadFile(qPrintable(filePath)))
        {
            if (import.getNumCollisionShapes() == 1)
            {
                shape = import.getCollisionShapeByIndex(0);
            }
        }
'shape' ends up being a btBvhTriangleMeshShape object, which is what I need.

'import.getNumBvhs()' also returned 1, so the bvh was properly saved in the file.

However, the loading code took basically as much time as recreating the shape every time, which I found suspicious. It was indeed going through btBvhTriangleMeshShape::buildOptimizedBvh(), which I was not expecting.

Then I found this in btBulletWorldImporter.cpp:

Code: Select all

			btTriangleMeshShapeData* trimesh = (btTriangleMeshShapeData*)shapeData;
			btStridingMeshInterfaceData* interfaceData = createStridingMeshInterfaceData(&trimesh->m_meshInterface);
			btTriangleIndexVertexArray* meshInterface = createMeshInterface(*interfaceData);
			if (!meshInterface->getNumSubParts())
			{
				return 0;
			}

			btVector3 scaling; scaling.deSerializeFloat(trimesh->m_meshInterface.m_scaling);
			meshInterface->setScaling(scaling);


			btOptimizedBvh* bvh = 0;
#if 0
			if (trimesh->m_quantizedFloatBvh)
			{
				btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedFloatBvh);
				if (bvhPtr && *bvhPtr)
				{
					bvh = *bvhPtr;
				} else
				{
					bvh = createOptimizedBvh();
					bvh->deSerializeFloat(*trimesh->m_quantizedFloatBvh);
				}
			}
			if (trimesh->m_quantizedDoubleBvh)
			{
				btOptimizedBvh** bvhPtr = m_bvhMap.find(trimesh->m_quantizedDoubleBvh);
				if (bvhPtr && *bvhPtr)
				{
					bvh = *bvhPtr;
				} else
				{
					bvh = createOptimizedBvh();
					bvh->deSerializeDouble(*trimesh->m_quantizedDoubleBvh);
				}
			}
#endif


			btBvhTriangleMeshShape* trimeshShape = createBvhTriangleMeshShape(meshInterface,bvh);
There's a big chunk of disabled code that seems to be dealing with using the deserialized bvh. I removed the #if 0/#endif, and now my loading works perfectly and is about 3-4x faster than rebuilding the bvh.

My question is: Why was that #if block there in the first place? I can't find the repository history for that file for my version... Will I have any negative side effects from reactivating that code block?

I'm using bullet 2.80 (r2531).


The demo code in ConcavePhysicsDemo.cpp to load a serialized shape doesn't really make sense when bvh's are serialized either:

Code: Select all

	btBulletWorldImporter import(0);//don't store info into the world
	if (import.loadFile("myShape.bullet"))
	{
		int numBvh = import.getNumBvhs();
		if (numBvh)
		{
			btOptimizedBvh* bvh = import.getBvhByIndex(0);
			btVector3 aabbMin(-1000,-1000,-1000),aabbMax(1000,1000,1000);
	
			trimeshShape  = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression,aabbMin,aabbMax,false);
			trimeshShape->setOptimizedBvh(bvh);
			//trimeshShape  = new btBvhTriangleMeshShape(m_indexVertexArrays,useQuantizedAabbCompression,aabbMin,aabbMax);
			//trimeshShape->setOptimizedBvh(bvh);
	
		}
		int numShape = import.getNumCollisionShapes();
		if (numShape)
		{
			trimeshShape = (btBvhTriangleMeshShape*)import.getCollisionShapeByIndex(0);
			
			//if you know the name, you can also try to get the shape by name:
			const char* meshName = import.getNameForPointer(trimeshShape);
			if (meshName)
				trimeshShape = (btBvhTriangleMeshShape*)import.getCollisionShapeByName(meshName);
			
		}
	}
Going through both if's both instantiate a new object, then reassigns that pointer to something else. You also have to create m_indexVertexArrays beforehand without knowing the object's number of triangles... Using only the second 'if' worked fine, except for the bvh being rebuilt as mentioned earlier.
Post Reply