add btInMemorySerializer (experiments, allows for in-memory deep copy of worlds, bypassing the BulletFileLoader mechanism)
add btCollisionWorldImporter that can load a .bullet file, or use the btInMemorySerializer for deep-copy
This commit is contained in:
@@ -18,6 +18,7 @@ SET(BulletCollision_SRCS
|
||||
CollisionDispatch/btCollisionDispatcher.cpp
|
||||
CollisionDispatch/btCollisionObject.cpp
|
||||
CollisionDispatch/btCollisionWorld.cpp
|
||||
CollisionDispatch/btCollisionWorldImporter.cpp
|
||||
CollisionDispatch/btCompoundCollisionAlgorithm.cpp
|
||||
CollisionDispatch/btCompoundCompoundCollisionAlgorithm.cpp
|
||||
CollisionDispatch/btConvexConcaveCollisionAlgorithm.cpp
|
||||
@@ -126,6 +127,7 @@ SET(CollisionDispatch_HDRS
|
||||
CollisionDispatch/btCollisionObject.h
|
||||
CollisionDispatch/btCollisionObjectWrapper.h
|
||||
CollisionDispatch/btCollisionWorld.h
|
||||
CollisionDispatch/btCollisionWorldImporter.h
|
||||
CollisionDispatch/btCompoundCollisionAlgorithm.h
|
||||
CollisionDispatch/btCompoundCompoundCollisionAlgorithm.h
|
||||
CollisionDispatch/btConvexConcaveCollisionAlgorithm.h
|
||||
|
||||
1147
src/BulletCollision/CollisionDispatch/btCollisionWorldImporter.cpp
Normal file
1147
src/BulletCollision/CollisionDispatch/btCollisionWorldImporter.cpp
Normal file
File diff suppressed because it is too large
Load Diff
190
src/BulletCollision/CollisionDispatch/btCollisionWorldImporter.h
Normal file
190
src/BulletCollision/CollisionDispatch/btCollisionWorldImporter.h
Normal file
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2014 Erwin Coumans http://bulletphysics.org
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef BT_COLLISION_WORLD_IMPORTER_H
|
||||
#define BT_COLLISION_WORLD_IMPORTER_H
|
||||
|
||||
#include "LinearMath/btTransform.h"
|
||||
#include "LinearMath/btVector3.h"
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
#include "LinearMath/btHashMap.h"
|
||||
|
||||
class btCollisionShape;
|
||||
class btCollisionObject;
|
||||
class btBulletSerializedArrays;
|
||||
|
||||
|
||||
struct ConstraintInput;
|
||||
class btCollisionWorld;
|
||||
struct btCollisionShapeData;
|
||||
class btTriangleIndexVertexArray;
|
||||
class btStridingMeshInterface;
|
||||
struct btStridingMeshInterfaceData;
|
||||
class btGImpactMeshShape;
|
||||
class btOptimizedBvh;
|
||||
struct btTriangleInfoMap;
|
||||
class btBvhTriangleMeshShape;
|
||||
class btPoint2PointConstraint;
|
||||
class btHingeConstraint;
|
||||
class btConeTwistConstraint;
|
||||
class btGeneric6DofConstraint;
|
||||
class btGeneric6DofSpringConstraint;
|
||||
class btSliderConstraint;
|
||||
class btGearConstraint;
|
||||
struct btContactSolverInfo;
|
||||
|
||||
|
||||
|
||||
|
||||
class btCollisionWorldImporter
|
||||
{
|
||||
protected:
|
||||
btCollisionWorld* m_collisionWorld;
|
||||
|
||||
int m_verboseMode;
|
||||
|
||||
btAlignedObjectArray<btCollisionShape*> m_allocatedCollisionShapes;
|
||||
btAlignedObjectArray<btCollisionObject*> m_allocatedRigidBodies;
|
||||
|
||||
btAlignedObjectArray<btOptimizedBvh*> m_allocatedBvhs;
|
||||
btAlignedObjectArray<btTriangleInfoMap*> m_allocatedTriangleInfoMaps;
|
||||
btAlignedObjectArray<btTriangleIndexVertexArray*> m_allocatedTriangleIndexArrays;
|
||||
btAlignedObjectArray<btStridingMeshInterfaceData*> m_allocatedbtStridingMeshInterfaceDatas;
|
||||
btAlignedObjectArray<btCollisionObject*> m_allocatedCollisionObjects;
|
||||
|
||||
|
||||
btAlignedObjectArray<char*> m_allocatedNames;
|
||||
|
||||
btAlignedObjectArray<int*> m_indexArrays;
|
||||
btAlignedObjectArray<short int*> m_shortIndexArrays;
|
||||
btAlignedObjectArray<unsigned char*> m_charIndexArrays;
|
||||
|
||||
btAlignedObjectArray<btVector3FloatData*> m_floatVertexArrays;
|
||||
btAlignedObjectArray<btVector3DoubleData*> m_doubleVertexArrays;
|
||||
|
||||
|
||||
btHashMap<btHashPtr,btOptimizedBvh*> m_bvhMap;
|
||||
btHashMap<btHashPtr,btTriangleInfoMap*> m_timMap;
|
||||
|
||||
btHashMap<btHashString,btCollisionShape*> m_nameShapeMap;
|
||||
btHashMap<btHashString,btCollisionObject*> m_nameColObjMap;
|
||||
|
||||
btHashMap<btHashPtr,const char*> m_objectNameMap;
|
||||
|
||||
btHashMap<btHashPtr,btCollisionShape*> m_shapeMap;
|
||||
btHashMap<btHashPtr,btCollisionObject*> m_bodyMap;
|
||||
|
||||
|
||||
//methods
|
||||
|
||||
|
||||
|
||||
char* duplicateName(const char* name);
|
||||
|
||||
btCollisionShape* convertCollisionShape( btCollisionShapeData* shapeData );
|
||||
|
||||
|
||||
public:
|
||||
|
||||
btCollisionWorldImporter(btCollisionWorld* world);
|
||||
|
||||
virtual ~btCollisionWorldImporter();
|
||||
|
||||
bool convertAllObjects( btBulletSerializedArrays* arrays);
|
||||
|
||||
///delete all memory collision shapes, rigid bodies, constraints etc. allocated during the load.
|
||||
///make sure you don't use the dynamics world containing objects after you call this method
|
||||
virtual void deleteAllData();
|
||||
|
||||
void setVerboseMode(int verboseMode)
|
||||
{
|
||||
m_verboseMode = verboseMode;
|
||||
}
|
||||
|
||||
int getVerboseMode() const
|
||||
{
|
||||
return m_verboseMode;
|
||||
}
|
||||
|
||||
// query for data
|
||||
int getNumCollisionShapes() const;
|
||||
btCollisionShape* getCollisionShapeByIndex(int index);
|
||||
int getNumRigidBodies() const;
|
||||
btCollisionObject* getRigidBodyByIndex(int index) const;
|
||||
int getNumConstraints() const;
|
||||
|
||||
int getNumBvhs() const;
|
||||
btOptimizedBvh* getBvhByIndex(int index) const;
|
||||
int getNumTriangleInfoMaps() const;
|
||||
btTriangleInfoMap* getTriangleInfoMapByIndex(int index) const;
|
||||
|
||||
// queris involving named objects
|
||||
btCollisionShape* getCollisionShapeByName(const char* name);
|
||||
btCollisionObject* getCollisionObjectByName(const char* name);
|
||||
|
||||
|
||||
const char* getNameForPointer(const void* ptr) const;
|
||||
|
||||
///those virtuals are called by load and can be overridden by the user
|
||||
|
||||
|
||||
|
||||
//bodies
|
||||
|
||||
virtual btCollisionObject* createCollisionObject( const btTransform& startTransform, btCollisionShape* shape,const char* bodyName);
|
||||
|
||||
///shapes
|
||||
|
||||
virtual btCollisionShape* createPlaneShape(const btVector3& planeNormal,btScalar planeConstant);
|
||||
virtual btCollisionShape* createBoxShape(const btVector3& halfExtents);
|
||||
virtual btCollisionShape* createSphereShape(btScalar radius);
|
||||
virtual btCollisionShape* createCapsuleShapeX(btScalar radius, btScalar height);
|
||||
virtual btCollisionShape* createCapsuleShapeY(btScalar radius, btScalar height);
|
||||
virtual btCollisionShape* createCapsuleShapeZ(btScalar radius, btScalar height);
|
||||
|
||||
virtual btCollisionShape* createCylinderShapeX(btScalar radius,btScalar height);
|
||||
virtual btCollisionShape* createCylinderShapeY(btScalar radius,btScalar height);
|
||||
virtual btCollisionShape* createCylinderShapeZ(btScalar radius,btScalar height);
|
||||
virtual btCollisionShape* createConeShapeX(btScalar radius,btScalar height);
|
||||
virtual btCollisionShape* createConeShapeY(btScalar radius,btScalar height);
|
||||
virtual btCollisionShape* createConeShapeZ(btScalar radius,btScalar height);
|
||||
virtual class btTriangleIndexVertexArray* createTriangleMeshContainer();
|
||||
virtual btBvhTriangleMeshShape* createBvhTriangleMeshShape(btStridingMeshInterface* trimesh, btOptimizedBvh* bvh);
|
||||
virtual btCollisionShape* createConvexTriangleMeshShape(btStridingMeshInterface* trimesh);
|
||||
#ifdef SUPPORT_GIMPACT_SHAPE_IMPORT
|
||||
virtual btGImpactMeshShape* createGimpactShape(btStridingMeshInterface* trimesh);
|
||||
#endif //SUPPORT_GIMPACT_SHAPE_IMPORT
|
||||
virtual btStridingMeshInterfaceData* createStridingMeshInterfaceData(btStridingMeshInterfaceData* interfaceData);
|
||||
|
||||
virtual class btConvexHullShape* createConvexHullShape();
|
||||
virtual class btCompoundShape* createCompoundShape();
|
||||
virtual class btScaledBvhTriangleMeshShape* createScaledTrangleMeshShape(btBvhTriangleMeshShape* meshShape,const btVector3& localScalingbtBvhTriangleMeshShape);
|
||||
|
||||
virtual class btMultiSphereShape* createMultiSphereShape(const btVector3* positions,const btScalar* radi,int numSpheres);
|
||||
|
||||
virtual btTriangleIndexVertexArray* createMeshInterface(btStridingMeshInterfaceData& meshData);
|
||||
|
||||
///acceleration and connectivity structures
|
||||
virtual btOptimizedBvh* createOptimizedBvh();
|
||||
virtual btTriangleInfoMap* createTriangleInfoMap();
|
||||
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //BT_WORLD_IMPORTER_H
|
||||
@@ -149,12 +149,13 @@ struct btBulletSerializedArrays
|
||||
btAlignedObjectArray<struct btDynamicsWorldFloatData*> m_dynamicWorldInfoDataFloat;
|
||||
btAlignedObjectArray<struct btRigidBodyDoubleData*> m_rigidBodyDataDouble;
|
||||
btAlignedObjectArray<struct btRigidBodyFloatData*> m_rigidBodyDataFloat;
|
||||
btAlignedObjectArray<struct btCollisionObjectDoubleData*> m_collisionDataDouble;
|
||||
btAlignedObjectArray<struct btCollisionObjectFloatData*> m_collisionDataFloat;
|
||||
btAlignedObjectArray<struct btCollisionObjectDoubleData*> m_collisionObjectDataDouble;
|
||||
btAlignedObjectArray<struct btCollisionObjectFloatData*> m_collisionObjectDataFloat;
|
||||
btAlignedObjectArray<struct btTypedConstraintFloatData*> m_constraintDataFloat;
|
||||
btAlignedObjectArray<struct btTypedConstraintDoubleData*> m_constraintDataDouble;
|
||||
btAlignedObjectArray<struct btTypedConstraintData*> m_constraintData;//for backwards compatibility
|
||||
|
||||
btAlignedObjectArray<struct btSoftBodyFloatData*> m_softBodyFloatData;
|
||||
btAlignedObjectArray<struct btSoftBodyDoubleData*> m_softBodyDoubleData;
|
||||
|
||||
};
|
||||
|
||||
@@ -668,6 +669,180 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
///In general it is best to use btDefaultSerializer,
|
||||
///in particular when writing the data to disk or sending it over the network.
|
||||
///The btInMemorySerializer is experimental and only suitable in a few cases.
|
||||
///The btInMemorySerializer takes a shortcut and can be useful to create a deep-copy
|
||||
///of objects. There will be a demo on how to use the btInMemorySerializer.
|
||||
struct btInMemorySerializer : public btDefaultSerializer
|
||||
{
|
||||
btHashMap<btHashPtr,btChunk*> m_uid2ChunkPtr;
|
||||
btHashMap<btHashPtr,void*> m_orgPtr2UniqueDataPtr;
|
||||
btHashMap<btHashString,const void*> m_names2Ptr;
|
||||
btHashMap<btHashPtr,void*> m_skipPointers;
|
||||
|
||||
btBulletSerializedArrays m_arrays;
|
||||
|
||||
|
||||
virtual void startSerialization()
|
||||
{
|
||||
m_uid2ChunkPtr.clear();
|
||||
//todo: m_arrays.clear();
|
||||
btDefaultSerializer::startSerialization();
|
||||
}
|
||||
|
||||
btChunk* findChunkFromUniquePointer(void* uniquePointer)
|
||||
{
|
||||
btChunk** chkPtr = m_uid2ChunkPtr[uniquePointer];
|
||||
if (chkPtr)
|
||||
{
|
||||
return *chkPtr;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual void registerNameForPointer(const void* ptr, const char* name)
|
||||
{
|
||||
btDefaultSerializer::registerNameForPointer(ptr,name);
|
||||
m_names2Ptr.insert(name,ptr);
|
||||
}
|
||||
|
||||
virtual void finishSerialization()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void* getUniquePointer(void*oldPtr)
|
||||
{
|
||||
if (oldPtr==0)
|
||||
return 0;
|
||||
|
||||
// void* uniquePtr = getUniquePointer(oldPtr);
|
||||
btChunk* chunk = findChunkFromUniquePointer(oldPtr);
|
||||
if (chunk)
|
||||
{
|
||||
return chunk->m_oldPtr;
|
||||
} else
|
||||
{
|
||||
const char* n = (const char*) oldPtr;
|
||||
const void** ptr = m_names2Ptr[n];
|
||||
if (ptr)
|
||||
{
|
||||
return oldPtr;
|
||||
} else
|
||||
{
|
||||
void** ptr2 = m_skipPointers[oldPtr];
|
||||
if (ptr2)
|
||||
{
|
||||
return 0;
|
||||
} else
|
||||
{
|
||||
//If this assert hit, serialization happened in the wrong order
|
||||
// 'getUniquePointer'
|
||||
btAssert(0);
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return oldPtr;
|
||||
}
|
||||
|
||||
virtual void finalizeChunk(btChunk* chunk, const char* structType, int chunkCode,void* oldPtr)
|
||||
{
|
||||
if (!(m_serializationFlags&BT_SERIALIZE_NO_DUPLICATE_ASSERT))
|
||||
{
|
||||
btAssert(!findPointer(oldPtr));
|
||||
}
|
||||
|
||||
chunk->m_dna_nr = getReverseType(structType);
|
||||
chunk->m_chunkCode = chunkCode;
|
||||
//void* uniquePtr = getUniquePointer(oldPtr);
|
||||
m_chunkP.insert(oldPtr,oldPtr);//chunk->m_oldPtr);
|
||||
// chunk->m_oldPtr = uniquePtr;//oldPtr;
|
||||
|
||||
void* uid = findPointer(oldPtr);
|
||||
m_uid2ChunkPtr.insert(uid,chunk);
|
||||
|
||||
switch (chunk->m_chunkCode)
|
||||
{
|
||||
case BT_SOFTBODY_CODE:
|
||||
{
|
||||
#ifdef BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_softBodyDoubleData.push_back((btSoftBodyDoubleData*) chunk->m_oldPtr);
|
||||
#else
|
||||
m_arrays.m_softBodyFloatData.push_back((btSoftBodyFloatData*) chunk->m_oldPtr);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case BT_COLLISIONOBJECT_CODE:
|
||||
{
|
||||
#ifdef BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_collisionObjectDataDouble.push_back((btCollisionObjectDoubleData*)chunk->m_oldPtr);
|
||||
#else//BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_collisionObjectDataFloat.push_back((btCollisionObjectFloatData*)chunk->m_oldPtr);
|
||||
#endif //BT_USE_DOUBLE_PRECISION
|
||||
break;
|
||||
}
|
||||
case BT_RIGIDBODY_CODE:
|
||||
{
|
||||
#ifdef BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_rigidBodyDataDouble.push_back((btRigidBodyDoubleData*)chunk->m_oldPtr);
|
||||
#else
|
||||
m_arrays.m_rigidBodyDataFloat.push_back((btRigidBodyFloatData*)chunk->m_oldPtr);
|
||||
#endif//BT_USE_DOUBLE_PRECISION
|
||||
break;
|
||||
};
|
||||
case BT_CONSTRAINT_CODE:
|
||||
{
|
||||
#ifdef BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_constraintDataDouble.push_back(btTypedConstraintDoubleData*)chunk->m_oldPtr);
|
||||
#else
|
||||
m_arrays.m_constraintDataFloat.push_back((btTypedConstraintFloatData*)chunk->m_oldPtr);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
case BT_QUANTIZED_BVH_CODE:
|
||||
{
|
||||
#ifdef BT_USE_DOUBLE_PRECISION
|
||||
m_arrays.m_bvhsFloat.push_back((btQuantizedBvhDoubleData*) chunk->m_oldPtr);
|
||||
#else
|
||||
m_arrays.m_bvhsFloat.push_back((btQuantizedBvhFloatData*) chunk->m_oldPtr);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
case BT_SHAPE_CODE:
|
||||
{
|
||||
btCollisionShapeData* shapeData = (btCollisionShapeData*) chunk->m_oldPtr;
|
||||
m_arrays.m_colShapeData.push_back(shapeData);
|
||||
break;
|
||||
}
|
||||
case BT_TRIANLGE_INFO_MAP:
|
||||
case BT_ARRAY_CODE:
|
||||
case BT_SBMATERIAL_CODE:
|
||||
case BT_SBNODE_CODE:
|
||||
case BT_DYNAMICSWORLD_CODE:
|
||||
case BT_DNA_CODE:
|
||||
{
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int getNumChunks() const
|
||||
{
|
||||
return m_uid2ChunkPtr.size();
|
||||
}
|
||||
|
||||
const btChunk* getChunk(int chunkIndex) const
|
||||
{
|
||||
return *m_uid2ChunkPtr.getAtIndex(chunkIndex);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //BT_SERIALIZER_H
|
||||
|
||||
|
||||
Reference in New Issue
Block a user