RegisterTypes.cpp 8.7 KB

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