RegisterTypes.cpp 8.2 KB

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