From 9c68ef62b59a72392802e9c313b3d565e9b616d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Sun, 30 Jan 2022 20:03:44 +0100 Subject: [PATCH] Havok: Add hkpConvexTransformShape and hkQsTransform (stub) --- lib/hkStubs/CMakeLists.txt | 4 + .../Base/Math/QsTransform/hkQsTransform.h | 5 + .../Base/Math/QsTransform/hkQsTransformf.h | 41 +++++++ .../Base/Math/QsTransform/hkQsTransformf.inl | 49 +++++++++ .../Base/Math/Quaternion/hkQuaternionf.h | 1 + .../Base/Math/Quaternion/hkQuaternionf.inl | 4 + lib/hkStubs/Havok/Common/Base/Math/hkMath.h | 4 + .../Havok/Common/Base/Types/hkBaseDefs.h | 2 + .../ConvexTransform/hkpConvexTransformShape.h | 104 ++++++++++++++++++ .../Collide/Shape/Convex/hkpConvexShape.h | 21 ++++ 10 files changed, 235 insertions(+) create mode 100644 lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransform.h create mode 100644 lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.h create mode 100644 lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.inl create mode 100644 lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h diff --git a/lib/hkStubs/CMakeLists.txt b/lib/hkStubs/CMakeLists.txt index fe6295dc..9f36a461 100644 --- a/lib/hkStubs/CMakeLists.txt +++ b/lib/hkStubs/CMakeLists.txt @@ -21,6 +21,9 @@ add_library(hkStubs OBJECT Havok/Common/Base/Math/Matrix/hkRotationf.h Havok/Common/Base/Math/Matrix/hkTransform.h Havok/Common/Base/Math/Matrix/hkTransformf.h + Havok/Common/Base/Math/QsTransform/hkQsTransform.h + Havok/Common/Base/Math/QsTransform/hkQsTransformf.h + Havok/Common/Base/Math/QsTransform/hkQsTransformf.inl Havok/Common/Base/Math/Quaternion/hkQuaternion.h Havok/Common/Base/Math/Quaternion/hkQuaternionf.h Havok/Common/Base/Math/Quaternion/hkQuaternionf.inl @@ -111,6 +114,7 @@ add_library(hkStubs OBJECT Havok/Physics2012/Collide/Shape/Convex/hkpConvexShape.h Havok/Physics2012/Collide/Shape/Convex/Box/hkpBoxShape.h Havok/Physics2012/Collide/Shape/Convex/Capsule/hkpCapsuleShape.h + Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h Havok/Physics2012/Collide/Shape/HeightField/hkpSphereRepShape.h Havok/Physics2012/Collide/Shape/Query/hkpRayShapeCollectionFilter.h Havok/Physics2012/Collide/Shape/Query/hkpShapeRayCastInput.h diff --git a/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransform.h b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransform.h new file mode 100644 index 00000000..e1b725dd --- /dev/null +++ b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransform.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +using hkQsTransform = hkQsTransformf; diff --git a/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.h b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.h new file mode 100644 index 00000000..72f8d268 --- /dev/null +++ b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.h @@ -0,0 +1,41 @@ +#pragma once + +class hkQsTransformf { +public: + // Initialisation tags. + enum class IdentityInitializer {}; + enum class ZeroInitializer {}; + + HK_FORCE_INLINE hkQsTransformf() = default; + + // NOLINTNEXTLINE(google-explicit-constructor) + HK_FORCE_INLINE hkQsTransformf(IdentityInitializer init); + + // NOLINTNEXTLINE(google-explicit-constructor) + HK_FORCE_INLINE hkQsTransformf(ZeroInitializer init); + + HK_FORCE_INLINE hkQsTransformf(hkVector4fParameter translation, hkQuaternionfParameter rotation, + hkVector4fParameter scale); + + HK_FORCE_INLINE hkQsTransformf(hkVector4fParameter translation, + hkQuaternionfParameter rotation); + + HK_FORCE_INLINE hkQsTransformf(const hkQsTransformf& other); + + HK_FORCE_INLINE const hkVector4f& getTranslation() const; + HK_FORCE_INLINE const hkQuaternionf& getRotation() const; + HK_FORCE_INLINE const hkVector4f& getScale() const; + + HK_FORCE_INLINE void setIdentity(); + HK_FORCE_INLINE void setZero(); + + void setFromTransformNoScale(const hkTransformf& transform); + void copyToTransformNoScale(hkTransformf& transformOut) const; + void setFromTransform(const hkTransformf& transform); + void setFromTransform(const hkQTransformf& qt); + void copyToTransform(hkTransformf& transformOut) const; + + hkVector4f m_translation; + hkQuaternionf m_rotation; + hkVector4f m_scale; +}; diff --git a/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.inl b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.inl new file mode 100644 index 00000000..886d0652 --- /dev/null +++ b/lib/hkStubs/Havok/Common/Base/Math/QsTransform/hkQsTransformf.inl @@ -0,0 +1,49 @@ +#pragma once + +inline hkQsTransformf::hkQsTransformf(IdentityInitializer) { + setIdentity(); +} + +inline hkQsTransformf::hkQsTransformf(ZeroInitializer) { + setZero(); +} + +inline hkQsTransformf::hkQsTransformf(hkVector4fParameter translation, + hkQuaternionfParameter rotation, hkVector4fParameter scale) + : m_translation(translation), m_rotation(rotation), m_scale(scale) {} + +inline hkQsTransformf::hkQsTransformf(hkVector4fParameter translation, + hkQuaternionfParameter rotation) + : m_translation(translation), m_rotation(rotation) { + m_scale = hkVector4f::getConstant(); +} + +inline hkQsTransformf::hkQsTransformf(const hkQsTransformf& other) { + m_translation = other.m_translation; + m_rotation = other.m_rotation; + m_scale = other.m_scale; +} + +inline const hkVector4f& hkQsTransformf::getTranslation() const { + return m_translation; +} + +inline const hkQuaternionf& hkQsTransformf::getRotation() const { + return m_rotation; +} + +inline const hkVector4f& hkQsTransformf::getScale() const { + return m_scale; +} + +inline void hkQsTransformf::setIdentity() { + m_translation.setZero(); + m_rotation.setIdentity(); + m_scale = hkVector4f::getConstant(); +} + +inline void hkQsTransformf::setZero() { + m_translation.setZero(); + m_rotation.m_vec.setZero(); + m_scale.setZero(); +} diff --git a/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.h b/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.h index c2e2e172..5785ce36 100644 --- a/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.h +++ b/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.h @@ -36,6 +36,7 @@ public: HK_FORCE_INLINE void normalize(); HK_FORCE_INLINE static const hkQuaternionf& getIdentity(); + HK_FORCE_INLINE void setIdentity(); hkVector4f m_vec; }; diff --git a/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.inl b/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.inl index 72c9a026..6e2319d0 100644 --- a/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.inl +++ b/lib/hkStubs/Havok/Common/Base/Math/Quaternion/hkQuaternionf.inl @@ -98,3 +98,7 @@ inline void hkQuaternionf::normalize() { inline const hkQuaternionf& hkQuaternionf::getIdentity() { return reinterpret_cast(g_vectorfConstants[HK_QUADREAL_0001]); } + +inline void hkQuaternionf::setIdentity() { + m_vec = hkVector4f::getConstant(); +} diff --git a/lib/hkStubs/Havok/Common/Base/Math/hkMath.h b/lib/hkStubs/Havok/Common/Base/Math/hkMath.h index 32c42ab1..83c24e8a 100644 --- a/lib/hkStubs/Havok/Common/Base/Math/hkMath.h +++ b/lib/hkStubs/Havok/Common/Base/Math/hkMath.h @@ -16,6 +16,8 @@ class hkQuaternionf; class hkMatrix3f; class hkRotationf; class hkTransformf; +class hkQTransformf; +class hkQsTransformf; // Type aliases using hkVector4fParameter = const hkVector4f&; @@ -31,12 +33,14 @@ using hkQuaternionfParameter = const hkQuaternionf&; #include #include #include +#include // Implementations #include #include #include #include +#include // clang-format on diff --git a/lib/hkStubs/Havok/Common/Base/Types/hkBaseDefs.h b/lib/hkStubs/Havok/Common/Base/Types/hkBaseDefs.h index de378d7a..6b3f87da 100644 --- a/lib/hkStubs/Havok/Common/Base/Types/hkBaseDefs.h +++ b/lib/hkStubs/Havok/Common/Base/Types/hkBaseDefs.h @@ -5,3 +5,5 @@ #define HK_NEVER_INLINE __attribute__((noinline)) #define HK_VISIBILITY_HIDDEN __attribute__((visibility("hidden"))) + +#define HK_RESTRICT __restrict diff --git a/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h new file mode 100644 index 00000000..8e5570f2 --- /dev/null +++ b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h @@ -0,0 +1,104 @@ +#pragma once + +#include + +class hkpConvexTransformShape : public hkpConvexTransformShapeBase { +public: + HK_DECLARE_CLASS_ALLOCATOR(hkpConvexTransformShape) + HK_DECLARE_REFLECTION() + HKCD_DECLARE_SHAPE_TYPE(hkcdShapeType::CONVEX_TRANSFORM) + + HK_FORCE_INLINE hkpConvexTransformShape() {} + + hkpConvexTransformShape( + const hkpConvexShape* childShape, const hkTransform& transform, + hkpShapeContainer::ReferencePolicy ref = hkpShapeContainer::REFERENCE_POLICY_INCREMENT); + + hkpConvexTransformShape( + const hkpConvexShape* childShape, const hkQsTransform& transform, + hkpShapeContainer::ReferencePolicy ref = hkpShapeContainer::REFERENCE_POLICY_INCREMENT); + + explicit hkpConvexTransformShape(hkFinishLoadedObjectFlag flag); + + void setTransform(const hkQsTransform& transform); + inline void setTransform(const hkTransform& transform); + inline void getTransform(hkTransform* HK_RESTRICT transform) const; + inline const hkQsTransform& getQsTransform() const; + + inline const hkVector4& getExtraScale() const; + + inline const hkpConvexShape* getChildShape() const; + + inline void transformVertex(hkVector4Parameter localVertex, + hkVector4* HK_RESTRICT vertexOut) const; + + void getSupportingVertex(hkVector4Parameter direction, + hkcdVertex& supportingVertexOut) const override; + + void convertVertexIdsToVertices(const hkpVertexId* ids, int numIds, + hkcdVertex* verticesOut) const override; + + void getCentre(hkVector4& centreOut) const override; + + int getNumCollisionSpheres() const override; + + const hkSphere* getCollisionSpheres(hkSphere* sphereBuffer) const override; + + void getAabb(const hkTransform& localToWorld, hkReal tolerance, hkAabb& out) const override; + + hkBool castRay(const hkpShapeRayCastInput& input, + hkpShapeRayCastOutput& results) const override; + + void castRayWithCollector(const hkpShapeRayCastInput& input, const hkpCdBody& cdBody, + hkpRayHitCollector& collector) const override; + + const hkpShapeContainer* getContainer() const override; + + int calcSizeForSpu(const CalcSizeForSpuInput& input, int spuBufferSizeLeft) const override; + + void getFirstVertex(hkVector4& v) const override; + +protected: + hkQsTransform m_transform; + hkVector4 m_extraScale; +}; + +inline void hkpConvexTransformShape::setTransform(const hkTransform& transform) { + m_transform.setFromTransformNoScale(transform); + m_extraScale.setZero(); +} + +inline void hkpConvexTransformShape::getTransform(hkTransform* transform) const { + m_transform.copyToTransformNoScale(*transform); +} + +inline const hkQsTransform& hkpConvexTransformShape::getQsTransform() const { + return m_transform; +} + +inline const hkVector4& hkpConvexTransformShape::getExtraScale() const { + return m_extraScale; +} + +inline const hkpConvexShape* hkpConvexTransformShape::getChildShape() const { + return static_cast(m_childShape.getChild()); +} + +inline void hkpConvexTransformShape::transformVertex(const hkVector4f& localVertex, + hkVector4* vertexOut) const { + hkVector4 center; + center.set(m_transform.m_translation.getW(), m_transform.m_scale.getW(), m_extraScale.getW(), + hkSimdReal::getConstant()); + + hkVector4 relVertex; + relVertex.setSub(localVertex, center); + vertexOut->setMul(localVertex, m_transform.getScale()); + vertexOut->addMul(m_extraScale, relVertex); + + vertexOut->_setRotatedDir(m_transform.getRotation(), *vertexOut); + vertexOut->add(m_transform.getTranslation()); +} + +inline int hkpConvexTransformShape::getNumCollisionSpheres() const { + return getChildShape()->getNumCollisionSpheres(); +} diff --git a/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/hkpConvexShape.h b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/hkpConvexShape.h index c53c92c3..3c8d5e51 100644 --- a/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/hkpConvexShape.h +++ b/lib/hkStubs/Havok/Physics2012/Collide/Shape/Convex/hkpConvexShape.h @@ -3,6 +3,7 @@ #include #include #include +#include extern hkReal hkConvexShapeDefaultRadius; @@ -47,6 +48,26 @@ protected: hkReal m_radius; }; +class hkpConvexTransformShapeBase : public hkpConvexShape { +public: + HK_DECLARE_CLASS_ALLOCATOR(hkpConvexTransformShapeBase) + HK_DECLARE_REFLECTION() + +public: + hkpConvexTransformShapeBase() {} + + explicit hkpConvexTransformShapeBase(hkFinishLoadedObjectFlag flag) + : hkpConvexShape(flag), m_childShape(flag) {} + +protected: + hkpConvexTransformShapeBase(ShapeType type, hkReal radius, const hkpConvexShape* childShape, + hkpShapeContainer::ReferencePolicy ref); + void getChildShapeFromPpu(int thisShapeSize) const; + + hkpSingleShapeContainer m_childShape; + mutable int m_childShapeSizeForSpu; +}; + inline const hkReal& hkpConvexShape::getRadius() const { return m_radius; }