RegisterTypes.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/RegisterTypes.h>
  6. #include <Jolt/Core/Factory.h>
  7. #include <Jolt/Core/RTTI.h>
  8. #include <Jolt/Core/TickCounter.h>
  9. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  10. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/PlaneShape.h>
  12. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  13. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  14. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  15. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  16. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  17. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  18. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  19. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  20. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  21. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  22. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  23. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  24. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  25. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  26. #include <Jolt/Physics/SoftBody/SoftBodyShape.h>
  27. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, Skeleton)
  28. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SkeletalAnimation)
  29. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, CompoundShapeSettings)
  30. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, StaticCompoundShapeSettings)
  31. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, MutableCompoundShapeSettings)
  32. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, TriangleShapeSettings)
  33. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PlaneShapeSettings)
  34. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SphereShapeSettings)
  35. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, BoxShapeSettings)
  36. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, CapsuleShapeSettings)
  37. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, TaperedCapsuleShapeSettings)
  38. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, CylinderShapeSettings)
  39. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, ScaledShapeSettings)
  40. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, MeshShapeSettings)
  41. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, ConvexHullShapeSettings)
  42. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, HeightFieldShapeSettings)
  43. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, RotatedTranslatedShapeSettings)
  44. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, OffsetCenterOfMassShapeSettings)
  45. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, RagdollSettings)
  46. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PointConstraintSettings)
  47. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SixDOFConstraintSettings)
  48. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SliderConstraintSettings)
  49. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SwingTwistConstraintSettings)
  50. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, DistanceConstraintSettings)
  51. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, HingeConstraintSettings)
  52. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, FixedConstraintSettings)
  53. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, ConeConstraintSettings)
  54. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintSettings)
  55. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintPath)
  56. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintPathHermite)
  57. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, VehicleConstraintSettings)
  58. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, WheeledVehicleControllerSettings)
  59. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, RackAndPinionConstraintSettings)
  60. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GearConstraintSettings)
  61. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PulleyConstraintSettings)
  62. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, MotorSettings)
  63. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PhysicsScene)
  64. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PhysicsMaterial)
  65. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GroupFilter)
  66. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GroupFilterTable)
  67. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, BodyCreationSettings)
  68. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SoftBodyCreationSettings)
  69. JPH_NAMESPACE_BEGIN
  70. bool VerifyJoltVersionIDInternal(uint64 inVersionID)
  71. {
  72. return inVersionID == JPH_VERSION_ID;
  73. }
  74. void RegisterTypesInternal(uint64 inVersionID)
  75. {
  76. // Version check
  77. if (!VerifyJoltVersionIDInternal(inVersionID))
  78. {
  79. Trace("Version mismatch, make sure you compile the client code with the same Jolt version and compiler definitions!");
  80. uint64 mismatch = JPH_VERSION_ID ^ inVersionID;
  81. auto check_bit = [mismatch](int inBit, const char *inLabel) { if (mismatch & (uint64(1) << (inBit + 23))) Trace("Mismatching define %s.", inLabel); };
  82. check_bit(1, "JPH_DOUBLE_PRECISION");
  83. check_bit(2, "JPH_CROSS_PLATFORM_DETERMINISTIC");
  84. check_bit(3, "JPH_FLOATING_POINT_EXCEPTIONS_ENABLED");
  85. check_bit(4, "JPH_PROFILE_ENABLED");
  86. check_bit(5, "JPH_EXTERNAL_PROFILE");
  87. check_bit(6, "JPH_DEBUG_RENDERER");
  88. check_bit(7, "JPH_DISABLE_TEMP_ALLOCATOR");
  89. check_bit(8, "JPH_DISABLE_CUSTOM_ALLOCATOR");
  90. check_bit(9, "JPH_OBJECT_LAYER_BITS");
  91. check_bit(10, "JPH_ENABLE_ASSERTS");
  92. check_bit(11, "JPH_OBJECT_STREAM");
  93. std::abort();
  94. }
  95. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  96. JPH_ASSERT(Allocate != nullptr && Reallocate != nullptr && Free != nullptr && AlignedAllocate != nullptr && AlignedFree != nullptr, "Need to supply an allocator first or call RegisterDefaultAllocator()");
  97. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  98. JPH_ASSERT(Factory::sInstance != nullptr, "Need to create a factory first!");
  99. // Initialize dispatcher
  100. CollisionDispatch::sInit();
  101. // Register base classes first so that we can specialize them later
  102. CompoundShape::sRegister();
  103. ConvexShape::sRegister();
  104. // Register compounds before others so that we can specialize them later (register them in reverse order of collision complexity)
  105. MutableCompoundShape::sRegister();
  106. StaticCompoundShape::sRegister();
  107. // Leaf classes
  108. TriangleShape::sRegister();
  109. PlaneShape::sRegister();
  110. SphereShape::sRegister();
  111. BoxShape::sRegister();
  112. CapsuleShape::sRegister();
  113. TaperedCapsuleShape::sRegister();
  114. CylinderShape::sRegister();
  115. MeshShape::sRegister();
  116. ConvexHullShape::sRegister();
  117. HeightFieldShape::sRegister();
  118. SoftBodyShape::sRegister();
  119. // Register these last because their collision functions are simple so we want to execute them first (register them in reverse order of collision complexity)
  120. RotatedTranslatedShape::sRegister();
  121. OffsetCenterOfMassShape::sRegister();
  122. ScaledShape::sRegister();
  123. // Create list of all types
  124. const RTTI *types[] = {
  125. JPH_RTTI(SkeletalAnimation),
  126. JPH_RTTI(Skeleton),
  127. JPH_RTTI(CompoundShapeSettings),
  128. JPH_RTTI(StaticCompoundShapeSettings),
  129. JPH_RTTI(MutableCompoundShapeSettings),
  130. JPH_RTTI(TriangleShapeSettings),
  131. JPH_RTTI(PlaneShapeSettings),
  132. JPH_RTTI(SphereShapeSettings),
  133. JPH_RTTI(BoxShapeSettings),
  134. JPH_RTTI(CapsuleShapeSettings),
  135. JPH_RTTI(TaperedCapsuleShapeSettings),
  136. JPH_RTTI(CylinderShapeSettings),
  137. JPH_RTTI(ScaledShapeSettings),
  138. JPH_RTTI(MeshShapeSettings),
  139. JPH_RTTI(ConvexHullShapeSettings),
  140. JPH_RTTI(HeightFieldShapeSettings),
  141. JPH_RTTI(RotatedTranslatedShapeSettings),
  142. JPH_RTTI(OffsetCenterOfMassShapeSettings),
  143. JPH_RTTI(RagdollSettings),
  144. JPH_RTTI(PointConstraintSettings),
  145. JPH_RTTI(SixDOFConstraintSettings),
  146. JPH_RTTI(SliderConstraintSettings),
  147. JPH_RTTI(SwingTwistConstraintSettings),
  148. JPH_RTTI(DistanceConstraintSettings),
  149. JPH_RTTI(HingeConstraintSettings),
  150. JPH_RTTI(FixedConstraintSettings),
  151. JPH_RTTI(ConeConstraintSettings),
  152. JPH_RTTI(PathConstraintSettings),
  153. JPH_RTTI(VehicleConstraintSettings),
  154. JPH_RTTI(WheeledVehicleControllerSettings),
  155. JPH_RTTI(PathConstraintPath),
  156. JPH_RTTI(PathConstraintPathHermite),
  157. JPH_RTTI(RackAndPinionConstraintSettings),
  158. JPH_RTTI(GearConstraintSettings),
  159. JPH_RTTI(PulleyConstraintSettings),
  160. JPH_RTTI(MotorSettings),
  161. JPH_RTTI(PhysicsScene),
  162. JPH_RTTI(PhysicsMaterial),
  163. JPH_RTTI(PhysicsMaterialSimple),
  164. JPH_RTTI(GroupFilter),
  165. JPH_RTTI(GroupFilterTable),
  166. JPH_RTTI(BodyCreationSettings),
  167. JPH_RTTI(SoftBodyCreationSettings)
  168. };
  169. // Register them all
  170. Factory::sInstance->Register(types, (uint)size(types));
  171. // Initialize default physics material
  172. if (PhysicsMaterial::sDefault == nullptr)
  173. PhysicsMaterial::sDefault = new PhysicsMaterialSimple("Default", Color::sGrey);
  174. }
  175. void UnregisterTypes()
  176. {
  177. // Unregister all types
  178. if (Factory::sInstance != nullptr)
  179. Factory::sInstance->Clear();
  180. // Delete default physics material
  181. PhysicsMaterial::sDefault = nullptr;
  182. }
  183. JPH_NAMESPACE_END