Serializing and deserializing btBvhTriangleMeshShape

Post Reply
User avatar
rewb0rn
Posts: 13
Joined: Tue Feb 02, 2010 11:48 pm
Location: Berlin, Germany

Serializing and deserializing btBvhTriangleMeshShape

Post by rewb0rn »

Hi,

I had a look at the serialization examples and try to serialize and deserialize a btBvhTriangleMeshShape. I load meshes from Ogre, serialize them in my editor and want to use the physics mesh in my server without Ogre support. I think dynamicsWorld->serialize() is no option in this case because I only want to serialize a model to use it later in different cases. I also had a look at the AppConcaveDemo where the optimizedBvh gets serialized. The problem is it seems I still need the mesh data that I have no access to without Ogre. I also tried to serialize the btBvhTriangleMeshShape directly but I dont know how to deserialize the class, seems there is no method for deserialization as in the optimizedBvh case. Is there a short example that shows how to completely serialize and deserialize a btBvhTriangleMeshShape?

Thanks,
rewb0rn
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Serializing and deserializing btBvhTriangleMeshShape

Post by Erwin Coumans »

The .bullet file format and new serialization allows to only serialize a single body or shape or bvh acceleration structure, but I have to provide some sample snippet for that.

What is the problem of serializing the shape including a single object, using the world->serialize method? If there are many objects in the world, you can just create a separate world and insert that single object in there and serialize it, right?
Thanks,
Erwin
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Serializing and deserializing btBvhTriangleMeshShape

Post by Erwin Coumans »

You could use this snippet:

Code: Select all

       int maxSerializeBufferSize = 1024*1024*5;
       btDefaultSerializer*    serializer = new
btDefaultSerializer(maxSerializeBufferSize);
       serializer->startSerialization();
       int len = shape->calculateSerializeBufferSize();
       btChunk* chunk = serializer->allocate(len,1);
       const char* structType = shape->serialize(chunk->m_oldPtr, serializer);
       serializer->finalizeChunk(chunk,structType,BT_SHAPE_CODE,shape);
       serializer->finishSerialization();
       FILE* f2 = fopen("testFile.bullet","wb");
       fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1,f2);
       fclose(f2);

I just updated the Bullet 2.76 to revision 2038, with serialization improvements, so if you already downloaded the release please download it again.

It is now easier to serialize a single collision shape or even btOptimizedBVH, cross-platform.

Code: Select all

        int maxSerializeBufferSize = 1024*1024*5;
        btDefaultSerializer*    serializer = new btDefaultSerializer(maxSerializeBufferSize);
        serializer->startSerialization();
        trimeshShape->serializeSingleShape(serializer);
        serializer->finishSerialization();
        FILE* f2 = fopen("myShape.bullet","wb");
        fwrite(serializer->getBufferPointer(),serializer->getCurrentBufferSize(),1,f2);
        fclose(f2);
This will serialize the trimeshSHape including btOptimizedBVH acceleration structure. You can disable this by setting the flag:

Code: Select all

serializer->setSerializationFlags(BT_SERIALIZE_NO_BVH);
Importing the mesh goes like this:

Code: Select all

        btBulletWorldImporter import(0);//don't store info into the world
        if (import.loadFile("myShape.bullet"))
        {
                int numShape = import.getNumCollisionShapes();
                if (numShape)
                {
                        trimeshShape = (btBvhTriangleMeshShape*)import.getCollisionShapeByIndex(0);
                }
        }

Furthermore you can assign a name to the mesh (or object, constraint) to easier recognize the object. See Bullet/Demos/ConcaveDemo for an example, or use the following snippet (with the final Bullet 2.76 release 2038)

Thanks for the feedback,
Erwin
User avatar
rewb0rn
Posts: 13
Joined: Tue Feb 02, 2010 11:48 pm
Location: Berlin, Germany

Re: Serializing and deserializing btBvhTriangleMeshShape

Post by rewb0rn »

Great news, thank you!
Post Reply