hkpConvexPieceMeshBuilder.h
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:9k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /* 
  2.  * 
  3.  * Confidential Information of Telekinesys Research Limited (t/a Havok). Not for disclosure or distribution without Havok's
  4.  * prior written consent. This software contains code, techniques and know-how which is confidential and proprietary to Havok.
  5.  * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2009 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
  6.  * 
  7.  */
  8. #ifndef HK_INTERNAL_CONVEXPIECEMESH_BUILDER_H
  9. #define HK_INTERNAL_CONVEXPIECEMESH_BUILDER_H
  10. #include <Common/Base/hkBase.h>
  11. #include <Common/Base/Container/Array/hkObjectArray.h>
  12. #include <Common/Base/Container/PointerMap/hkPointerMap.h>
  13. #include <Physics/Collide/Agent/hkpProcessCollisionInput.h>
  14. #include <Physics/Internal/Collide/ConvexPieceMesh/hkpConvexPieceStreamData.h>
  15. #include <Physics/Collide/Shape/Compound/Collection/hkpShapeCollection.h>
  16. #include <Physics/Collide/Agent/Query/hkpLinearCastCollisionInput.h>
  17. class hkpClosestCdPointCollector;
  18. class hkpTriangleShape;
  19. class hkpMeshShape;
  20. //#include <hkvisualize/shape/hkDisplayGeometry.h>
  21. /// This is a helper class which allows the hkpConvexPieceMeshBuilder to query the material of individual childShapes 
  22. /// of the hkCollectionShape being converted to convex pieces. hkpConvexPieceMeshBuilder only merges childShapes
  23. /// having the same material information.
  24. class hkpShapeCollectionMaterialMediator
  25. {
  26. public:
  27. virtual ~hkpShapeCollectionMaterialMediator() { }
  28. /// Gets material ID for the child shape specified.
  29. virtual hkUint32 getMaterialOfShapeKey(hkpShapeKey key) const = 0;
  30. };
  31. /// This implementation returns hkpMeshMaterial::m_filterInfo associated with child shapes.
  32. class hkpDefaultShapeCollectionMaterialMediator : public hkpShapeCollectionMaterialMediator
  33. {
  34. public:
  35. hkpDefaultShapeCollectionMaterialMediator(hkpMeshShape* meshShape) ;
  36. ~hkpDefaultShapeCollectionMaterialMediator();
  37. /// Gets the hkpMeshMaterial::m_filterInfo of the specified child shape.
  38. hkUint32 getMaterialOfShapeKey(hkpShapeKey key) const;
  39. private:
  40. hkpMeshShape* m_meshShape;
  41. };
  42. ///
  43. /// Given a triangle based mesh, this class creates a simplified set of convex pieces 
  44. /// representing the same mesh.
  45. ///
  46. ///
  47. /// The output can be passed to the hkpConvexPieceMeshShape which can then be wrapped in a
  48. /// hkpMoppBvTreeShape and used as the simulation mesh in a game.
  49. /// 
  50. class hkpConvexPieceMeshBuilder: public hkReferencedObject
  51. {
  52. public:
  53. HK_DECLARE_CLASS_ALLOCATOR(HK_MEMORY_CLASS_CDINFO);
  54. /// Constructs a new hkpConvexPieceMeshBuilder.
  55. hkpConvexPieceMeshBuilder();
  56. /// Destructs any mem allocated etc
  57. ~hkpConvexPieceMeshBuilder();
  58. /// Creates a convex representation of the input mesh.
  59. /// The displayMesh must be a shapeCollection that returns 
  60. /// children of type "hkpTriangleShape".
  61. /// 
  62. /// The maximum number of triangles in each convex piece can be
  63. /// specified here also.  If maxTrianglesPerConvexPiece is -1, the max number
  64. /// of vertices that will fit in the shape buffer will be used.
  65. void convexifyLandscape( const hkpShapeCollection* inputMesh, hkpCollisionInput* collisionInput, hkpConvexPieceStreamData& convexPieceStreamDataOut, hkpShapeCollectionMaterialMediator* materialMediator = HK_NULL, int maxVerticesPerConvexPiece = -1 );
  66. protected:
  67. struct TriangleInfo
  68. {
  69. HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR(HK_MEMORY_CLASS_CDINFO, TriangleInfo);
  70. char m_flags;
  71. hkBool m_vertsUsed[3];
  72. //hkUint16 m_lightInfo; /*HK_TRIANGLELIGHT_INFO*/
  73. // members used by the lighting algorithm
  74. int m_convexPieceOffset;
  75. };
  76. ///
  77. struct BuilderInput
  78. {
  79. HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR(HK_MEMORY_CLASS_CDINFO, BuilderInput);
  80. hkpCollidable* m_moppCollidable;
  81. hkpCollidable* m_sphereCollidable;
  82. hkReal m_sphereRadius;
  83. hkTransform* m_sphereTransform;
  84. const hkpShapeCollection* m_meshShape;
  85. const hkpShapeCollectionMaterialMediator* m_materialMediator;
  86. hkPointerMap<hkpShapeKey, hkUint32> m_shapeKeyToTriangleInfoIndex;
  87. hkArray< struct TriangleInfo > m_triangleInfo;
  88. struct hkpLinearCastCollisionInput m_collisionInput;
  89. BuilderInput() : m_moppCollidable(HK_NULL), m_sphereCollidable(HK_NULL),
  90. m_sphereRadius(0.0f), m_sphereTransform(HK_NULL) {}
  91. ~BuilderInput() 
  92. {
  93. m_moppCollidable->getShape()->removeReference();
  94. delete m_moppCollidable;
  95. delete m_sphereCollidable;
  96. delete m_sphereTransform;
  97. }
  98. };
  99. /// Internal struct used to represent a convex piece.
  100. struct ConvexPiece
  101. {
  102. HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR(HK_MEMORY_CLASS_CDINFO, ConvexPiece);
  103. ///
  104. hkArray<hkpShapeKey> m_triangles;
  105. /// We store the vertices used in the convex piece here, since these
  106. /// are generated by calculating the convex hull of the convex piece.
  107. hkArray<hkVector4> m_vertices;
  108. /// Set true if this convex piece is not to be modified, e.g. it has
  109. /// been determined that this is the largest convex piece possible
  110. /// containing any of the triangles it has.
  111. hkBool m_fixedConvexPiece;
  112. /// There is also a bitfield in the triangleInfo for each triangle
  113. /// that indicates which vertices in that triangle are used here.
  114. char m_flags;
  115. ConvexPiece() : m_fixedConvexPiece(false), m_flags(0) {}
  116. };
  117. protected:
  118. hkBool isEqualNormal( const hkpTriangleShape* triangle, const hkpTriangleShape* neighbourTriangle, hkVector4& triNormal );
  119. void linearCastAgainstCentroid( hkpConvexPieceMeshBuilder::BuilderInput& input, const hkVector4& source, const hkpShapeKey& triKey, hkpClosestCdPointCollector& collector );
  120. void calcCentroidAndNormal( hkpConvexPieceMeshBuilder::BuilderInput& input, const hkpShapeKey& triKey, hkVector4& centroid, hkVector4& triNormal );
  121. hkBool canAssimilateVertex( const hkpShapeCollection* meshShape, const ConvexPiece& convexPiece, const hkVector4& vertex,
  122. const hkVector4& edge0, const hkVector4& edge1, const hkVector4& triNormal );
  123. hkBool notEdgeVert( const hkVector4& vert1, const hkVector4& vert2, const hkVector4& vert3 );
  124. hkBool canJoinConvexShapeToConvexShape( const hkpShapeCollection* meshShape, const hkpShapeCollectionMaterialMediator* materialMediator,
  125. const ConvexPiece& convexPiece, const ConvexPiece& neighbourConvexPiece );
  126. static inline TriangleInfo* getTriangleInfo(BuilderInput& input, hkpShapeKey key);
  127. void buildBuilderInput(const hkpShapeCollection* inputMesh, hkpCollisionInput* collisionInput, hkpConvexPieceMeshBuilder::BuilderInput& inputOut, hkpShapeCollectionMaterialMediator* materialMeidator = HK_NULL);
  128. void getConvexPieceAabb( BuilderInput& input, const ConvexPiece& convexPiece, const hkTransform& localToWorld, hkReal tolerance, hkAabb& out ) const;
  129. /// Checks if the m_fixedConvexPiece flag has been set in the convex piece this triangle is in.
  130. hkBool isFixedTriangle( BuilderInput& input, const hkpShapeKey key );
  131. hkBool isNotDegenerateTriangle( BuilderInput& input, const hkpShapeKey key );
  132. hkBool isGoodTriangle( BuilderInput& input, const hkpShapeKey key );
  133. void joinFlatTriangles( BuilderInput& input );
  134. void filterCollinearVertices( BuilderInput& input, ConvexPiece& convexPiece );
  135. void generatePlaneEquationsForConvexPiece( BuilderInput& input, ConvexPiece& convexPiece, hkArray< hkVector4 >& planeEquations, hkBool runExtraCollinearFilter );
  136. protected:
  137. //
  138. // Optimised bit stream structure.
  139. //
  140. void convertArraysToStream( hkpConvexPieceMeshBuilder::BuilderInput& input,
  141. hkpConvexPieceStreamData& convexPieceMeshOut );
  142. int calcStreamSize( const hkArray< ConvexPiece* >& convexPieces );
  143. /// Build the stream data from the input arrays.
  144. void buildStreamData( hkpConvexPieceMeshBuilder::BuilderInput& input,
  145. const hkArray< ConvexPiece* >& convexPieces, 
  146. hkArray< hkUint32 >& stream, 
  147. hkArray< hkUint32 >& convexPieceOffsets,
  148. hkArray< hkUint32 >& convexPieceSingleTriangles );
  149. void deallocatePieces();
  150. // Intermediate structure used during construction of convex pieces.
  151. hkArray<struct ConvexPiece*> m_convexPieces;
  152. int m_maxVerticesPerConvexPiece;
  153. public:
  154. hkpConvexPieceMeshBuilder( hkFinishLoadedObjectFlag flag ) {}
  155. };
  156. #endif // HK_INTERNAL_CONVEXPIECEMESH_BUILDER_H
  157. /*
  158. * Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20090216)
  159. * Confidential Information of Havok.  (C) Copyright 1999-2009
  160. * Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
  161. * Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
  162. * rights, and intellectual property rights in the Havok software remain in
  163. * Havok and/or its suppliers.
  164. * Use of this software for evaluation purposes is subject to and indicates
  165. * acceptance of the End User licence Agreement for this product. A copy of
  166. * the license is included with this software and is also available at www.havok.com/tryhavok.
  167. */