RegisterTypes.cpp 8.2 KB

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