1 /* 2 Bullet Continuous Collision Detection and Physics Library 3 Copyright (c) 2003-2012 Erwin Coumans http://bulletphysics.org 4 5 This software is provided 'as-is', without any express or implied warranty. 6 In no event will the authors be held liable for any damages arising from the use of this software. 7 Permission is granted to anyone to use this software for any purpose, 8 including commercial applications, and to alter it and redistribute it freely, 9 subject to the following restrictions: 10 11 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. 12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 13 3. This notice may not be removed or altered from any source distribution. 14 */ 15 16 17 #ifndef BT_WORLD_IMPORTER_H 18 #define BT_WORLD_IMPORTER_H 19 20 #include "LinearMath/btTransform.h" 21 #include "LinearMath/btVector3.h" 22 #include "LinearMath/btAlignedObjectArray.h" 23 #include "LinearMath/btHashMap.h" 24 25 class btCollisionShape; 26 class btCollisionObject; 27 class btRigidBody; 28 class btTypedConstraint; 29 class btDynamicsWorld; 30 struct ConstraintInput; 31 class btRigidBodyColladaInfo; 32 struct btCollisionShapeData; 33 class btTriangleIndexVertexArray; 34 class btStridingMeshInterface; 35 struct btStridingMeshInterfaceData; 36 class btGImpactMeshShape; 37 class btOptimizedBvh; 38 struct btTriangleInfoMap; 39 class btBvhTriangleMeshShape; 40 class btPoint2PointConstraint; 41 class btHingeConstraint; 42 class btConeTwistConstraint; 43 class btGeneric6DofConstraint; 44 class btGeneric6DofSpringConstraint; 45 class btGeneric6DofSpring2Constraint; 46 class btSliderConstraint; 47 class btGearConstraint; 48 struct btContactSolverInfo; 49 struct btTypedConstraintData; 50 struct btTypedConstraintFloatData; 51 struct btTypedConstraintDoubleData; 52 53 struct btRigidBodyDoubleData; 54 struct btRigidBodyFloatData; 55 56 #ifdef BT_USE_DOUBLE_PRECISION 57 #define btRigidBodyData btRigidBodyDoubleData 58 #else 59 #define btRigidBodyData btRigidBodyFloatData 60 #endif//BT_USE_DOUBLE_PRECISION 61 62 63 class btWorldImporter 64 { 65 protected: 66 btDynamicsWorld* m_dynamicsWorld; 67 68 int m_verboseMode; 69 70 btAlignedObjectArray<btCollisionShape*> m_allocatedCollisionShapes; 71 btAlignedObjectArray<btCollisionObject*> m_allocatedRigidBodies; 72 btAlignedObjectArray<btTypedConstraint*> m_allocatedConstraints; 73 btAlignedObjectArray<btOptimizedBvh*> m_allocatedBvhs; 74 btAlignedObjectArray<btTriangleInfoMap*> m_allocatedTriangleInfoMaps; 75 btAlignedObjectArray<btTriangleIndexVertexArray*> m_allocatedTriangleIndexArrays; 76 btAlignedObjectArray<btStridingMeshInterfaceData*> m_allocatedbtStridingMeshInterfaceDatas; 77 78 btAlignedObjectArray<char*> m_allocatedNames; 79 80 btAlignedObjectArray<int*> m_indexArrays; 81 btAlignedObjectArray<short int*> m_shortIndexArrays; 82 btAlignedObjectArray<unsigned char*> m_charIndexArrays; 83 84 btAlignedObjectArray<btVector3FloatData*> m_floatVertexArrays; 85 btAlignedObjectArray<btVector3DoubleData*> m_doubleVertexArrays; 86 87 88 btHashMap<btHashPtr,btOptimizedBvh*> m_bvhMap; 89 btHashMap<btHashPtr,btTriangleInfoMap*> m_timMap; 90 91 btHashMap<btHashString,btCollisionShape*> m_nameShapeMap; 92 btHashMap<btHashString,btRigidBody*> m_nameBodyMap; 93 btHashMap<btHashString,btTypedConstraint*> m_nameConstraintMap; 94 btHashMap<btHashPtr,const char*> m_objectNameMap; 95 96 btHashMap<btHashPtr,btCollisionShape*> m_shapeMap; 97 btHashMap<btHashPtr,btCollisionObject*> m_bodyMap; 98 99 100 //methods 101 102 static btRigidBody& getFixedBody(); 103 104 char* duplicateName(const char* name); 105 106 btCollisionShape* convertCollisionShape( btCollisionShapeData* shapeData ); 107 108 void convertConstraintBackwardsCompatible281(btTypedConstraintData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion); 109 void convertConstraintFloat(btTypedConstraintFloatData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion); 110 void convertConstraintDouble(btTypedConstraintDoubleData* constraintData, btRigidBody* rbA, btRigidBody* rbB, int fileVersion); 111 void convertRigidBodyFloat(btRigidBodyFloatData* colObjData); 112 void convertRigidBodyDouble( btRigidBodyDoubleData* colObjData); 113 114 public: 115 116 btWorldImporter(btDynamicsWorld* world); 117 118 virtual ~btWorldImporter(); 119 120 ///delete all memory collision shapes, rigid bodies, constraints etc. allocated during the load. 121 ///make sure you don't use the dynamics world containing objects after you call this method 122 virtual void deleteAllData(); 123 124 void setVerboseMode(int verboseMode) 125 { 126 m_verboseMode = verboseMode; 127 } 128 129 int getVerboseMode() const 130 { 131 return m_verboseMode; 132 } 133 134 // query for data 135 int getNumCollisionShapes() const; 136 btCollisionShape* getCollisionShapeByIndex(int index); 137 int getNumRigidBodies() const; 138 btCollisionObject* getRigidBodyByIndex(int index) const; 139 int getNumConstraints() const; 140 btTypedConstraint* getConstraintByIndex(int index) const; 141 int getNumBvhs() const; 142 btOptimizedBvh* getBvhByIndex(int index) const; 143 int getNumTriangleInfoMaps() const; 144 btTriangleInfoMap* getTriangleInfoMapByIndex(int index) const; 145 146 // queris involving named objects 147 btCollisionShape* getCollisionShapeByName(const char* name); 148 btRigidBody* getRigidBodyByName(const char* name); 149 btTypedConstraint* getConstraintByName(const char* name); 150 const char* getNameForPointer(const void* ptr) const; 151 152 ///those virtuals are called by load and can be overridden by the user 153 154 virtual void setDynamicsWorldInfo(const btVector3& gravity, const btContactSolverInfo& solverInfo); 155 156 //bodies 157 virtual btRigidBody* createRigidBody(bool isDynamic, btScalar mass, const btTransform& startTransform, btCollisionShape* shape,const char* bodyName); 158 virtual btCollisionObject* createCollisionObject( const btTransform& startTransform, btCollisionShape* shape,const char* bodyName); 159 160 ///shapes 161 162 virtual btCollisionShape* createPlaneShape(const btVector3& planeNormal,btScalar planeConstant); 163 virtual btCollisionShape* createBoxShape(const btVector3& halfExtents); 164 virtual btCollisionShape* createSphereShape(btScalar radius); 165 virtual btCollisionShape* createCapsuleShapeX(btScalar radius, btScalar height); 166 virtual btCollisionShape* createCapsuleShapeY(btScalar radius, btScalar height); 167 virtual btCollisionShape* createCapsuleShapeZ(btScalar radius, btScalar height); 168 169 virtual btCollisionShape* createCylinderShapeX(btScalar radius,btScalar height); 170 virtual btCollisionShape* createCylinderShapeY(btScalar radius,btScalar height); 171 virtual btCollisionShape* createCylinderShapeZ(btScalar radius,btScalar height); 172 virtual btCollisionShape* createConeShapeX(btScalar radius,btScalar height); 173 virtual btCollisionShape* createConeShapeY(btScalar radius,btScalar height); 174 virtual btCollisionShape* createConeShapeZ(btScalar radius,btScalar height); 175 virtual class btTriangleIndexVertexArray* createTriangleMeshContainer(); 176 virtual btBvhTriangleMeshShape* createBvhTriangleMeshShape(btStridingMeshInterface* trimesh, btOptimizedBvh* bvh); 177 virtual btCollisionShape* createConvexTriangleMeshShape(btStridingMeshInterface* trimesh); 178 virtual btGImpactMeshShape* createGimpactShape(btStridingMeshInterface* trimesh); 179 virtual btStridingMeshInterfaceData* createStridingMeshInterfaceData(btStridingMeshInterfaceData* interfaceData); 180 181 virtual class btConvexHullShape* createConvexHullShape(); 182 virtual class btCompoundShape* createCompoundShape(); 183 virtual class btScaledBvhTriangleMeshShape* createScaledTrangleMeshShape(btBvhTriangleMeshShape* meshShape,const btVector3& localScalingbtBvhTriangleMeshShape); 184 185 virtual class btMultiSphereShape* createMultiSphereShape(const btVector3* positions,const btScalar* radi,int numSpheres); 186 187 virtual btTriangleIndexVertexArray* createMeshInterface(btStridingMeshInterfaceData& meshData); 188 189 ///acceleration and connectivity structures 190 virtual btOptimizedBvh* createOptimizedBvh(); 191 virtual btTriangleInfoMap* createTriangleInfoMap(); 192 193 ///constraints 194 virtual btPoint2PointConstraint* createPoint2PointConstraint(btRigidBody& rbA,btRigidBody& rbB, const btVector3& pivotInA,const btVector3& pivotInB); 195 virtual btPoint2PointConstraint* createPoint2PointConstraint(btRigidBody& rbA,const btVector3& pivotInA); 196 virtual btHingeConstraint* createHingeConstraint(btRigidBody& rbA,btRigidBody& rbB, const btTransform& rbAFrame, const btTransform& rbBFrame, bool useReferenceFrameA=false); 197 virtual btHingeConstraint* createHingeConstraint(btRigidBody& rbA,const btTransform& rbAFrame, bool useReferenceFrameA=false); 198 virtual btConeTwistConstraint* createConeTwistConstraint(btRigidBody& rbA,btRigidBody& rbB,const btTransform& rbAFrame, const btTransform& rbBFrame); 199 virtual btConeTwistConstraint* createConeTwistConstraint(btRigidBody& rbA,const btTransform& rbAFrame); 200 virtual btGeneric6DofConstraint* createGeneric6DofConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA); 201 virtual btGeneric6DofConstraint* createGeneric6DofConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameB); 202 virtual btGeneric6DofSpringConstraint* createGeneric6DofSpringConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA); 203 virtual btGeneric6DofSpring2Constraint* createGeneric6DofSpring2Constraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB, int rotateOrder ); 204 205 virtual btSliderConstraint* createSliderConstraint(btRigidBody& rbA, btRigidBody& rbB, const btTransform& frameInA, const btTransform& frameInB ,bool useLinearReferenceFrameA); 206 virtual btSliderConstraint* createSliderConstraint(btRigidBody& rbB, const btTransform& frameInB, bool useLinearReferenceFrameA); 207 virtual btGearConstraint* createGearConstraint(btRigidBody& rbA, btRigidBody& rbB, const btVector3& axisInA,const btVector3& axisInB, btScalar ratio); 208 209 210 211 212 }; 213 214 215 #endif //BT_WORLD_IMPORTER_H