Serialization

Physics APIs, Physics file formats, Maya, Max, XSI, Cinema 4D, Lightwave, Blender, thinkingParticles™ and other simulation tools, exporters and importers
Post Reply
Wümpftlbrümpftl
Posts: 3
Joined: Sun Dec 05, 2010 9:28 pm
Location: Germany
Contact:

Serialization

Post by Wümpftlbrümpftl »

Hi guys,

I'm currently trying to save and read collisionshape data in/from a file. To do this I tried to serialize the data like Erwin describes here.
The saving process seems to work well, but when I want to recreate the saved collisionshape with btBulletWorldImporter, it tells me that there aren't any collisionshapes. I stepped into the loading methods with the debugger - it seems that there is no parsing. Then I tried first to create a btBulletFile with my data from the file but this time I got an assertion.

My saving code:

Code: Select all

// ...
int maxSerializeBufferSize = 1024*1024*5;
btDefaultSerializer Serializer(maxSerializeBufferSize);
Serializer.startSerialization();
if(m_pCollisionShape)
     m_pCollisionShape->serializeSingleShape(&Serializer);
else
{
     btBoxShape BoxShape(btVector3(2,2,2));
     BoxShape.serializeSingleShape(&Serializer);
}
Serializer.finishSerialization();

FileOut.CollisionShapeDataSize = Serializer.getCurrentBufferSize();

std::ofstream Stream(GetFilename());
Stream.write((char*)&FileOut, sizeof(File));
Stream.write((char*)Serializer.getBufferPointer(), FileOut.CollisionShapeDataSize);
The code for the loading

Code: Select all

// ...
char* pCollisionShapeData = new char[FileIn.CollisionShapeDataSize];
Stream.read(pCollisionShapeData, FileIn.CollisionShapeDataSize);

btBulletWorldImporter Importer(0);
if(Importer.loadFileFromMemory(pCollisionShapeData, FileIn.CollisionShapeDataSize) && Importer.getNumCollisionShapes())
{
    btCollisionShape* pShape = Importer.getCollisionShapeByIndex(0);
    if(pShape->getShapeType() == BOX_SHAPE_PROXYTYPE)
        m_pCollisionShape = new btBoxShape(*static_cast<btBoxShape*>(pShape));
    else if(pShape->getShapeType() == TRIANGLE_MESH_SHAPE_PROXYTYPE)
        m_pCollisionShape = new btBvhTriangleMeshShape(*static_cast<btBvhTriangleMeshShape*>(pShape));
}
else
{
    // ...
}
delete[] pCollisionShapeData;
Has anyone an idea what I am doing wrong or could try next?
Thanks for your answers :)
PeterMacGonagan
Posts: 7
Joined: Sun Dec 05, 2010 4:59 pm

Re: Serialization

Post by PeterMacGonagan »

May be this tutorial will help you: http://bulletphysics.org/mediawiki-1.5. ... ialization

Post the answer if you find it, please.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Serialization

Post by Erwin Coumans »

The btBulletWorldImporter can only be used in combination with world::serialize.

If you use manual single object/shape serialization, you will need to write your own custom importer.

We cannot support all custom work, so please use the world serialize method if you get stuck.
Thanks,
Erwin
Wümpftlbrümpftl
Posts: 3
Joined: Sun Dec 05, 2010 9:28 pm
Location: Germany
Contact:

Re: Serialization

Post by Wümpftlbrümpftl »

Too bad... my world is splitted into several pages, so this could become quite tricky
But thank you for the quick answer :)
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Serialization

Post by Erwin Coumans »

Wümpftlbrümpftl wrote:Too bad... my world is splitted into several pages, so this could become quite tricky
But thank you for the quick answer :)
It shouldn't be hard to import just some collision shapes. The code is already in the btBulletWorldImporter.
On Windows, you can inspect the contents of a .bullet file using the File Inspector. Use File/inspect file.

Can you attach a (zipped) .bullet file, so we can see if there are actual collision shapes in there?
Thanks,
Erwin
Wümpftlbrümpftl
Posts: 3
Joined: Sun Dec 05, 2010 9:28 pm
Location: Germany
Contact:

Re: Serialization

Post by Wümpftlbrümpftl »

I split the collisionshape data from the rest of my file (by droping the last but one line of my saving code) and tried to load this as bullet file with the FileInspector which returned a "Failed to allocate memory!" error.
Here is the file as you suggested :)
Attachments
Palm.zip
btBvhTriangleMeshShape of a simple mesh - saved with the serializeSingleShape method
(15.65 KiB) Downloaded 521 times
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA
Contact:

Re: Serialization

Post by Erwin Coumans »

There is an error in the serialization DNA.

All 0a seems to be preceded by 0d, did you apply some dos/unix line-feed conversion to that binary file btSerializer.cpp or the .bullet file?
Please revert to an unmodified btSerializer.cpp
Thanks,
Erwin
Ripgiblet
Posts: 4
Joined: Thu Dec 30, 2010 1:03 pm

Re: Serialization

Post by Ripgiblet »

Some questions:
1)Does serializeSingleShape work for btHeightfieldTerrainShape?
2)How to convert from btHeightfieldTerrainShape to btBvhTriangleMeshShape?

NOTES:
I am having trouble getting btHeightfieldTerrainShape to serialize, I have successfully serialized many other shapes including btBvhTriangleMeshShape. So if I could convert from btHeightfieldTerrainShape to btBvhTriangleMeshShape, or some other object that I can convert to , serialize out, load, then convert back.
:mrgreen:
Post Reply