OPC_BaseModel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /*
  3. * OPCODE - Optimized Collision Detection
  4. * Copyright (C) 2001 Pierre Terdiman
  5. * Homepage: http://www.codercorner.com/Opcode.htm
  6. */
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. /**
  10. * Contains base model interface.
  11. * \file OPC_BaseModel.h
  12. * \author Pierre Terdiman
  13. * \date May, 18, 2003
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_BASEMODEL_H__
  19. #define __OPC_BASEMODEL_H__
  20. //! Model creation structure
  21. struct OPCODE_API OPCODECREATE
  22. {
  23. //! Constructor
  24. inline_ OPCODECREATE():
  25. mIMesh(null),
  26. mSettings(SPLIT_SPLATTER_POINTS | SPLIT_GEOM_CENTER),
  27. mNoLeaf(true),
  28. mQuantized(true),
  29. #ifdef __MESHMERIZER_H__
  30. mCollisionHull(false),
  31. #endif // __MESHMERIZER_H__
  32. mKeepOriginal(false),
  33. mCanRemap(false)
  34. {
  35. }
  36. inline_ OPCODECREATE(MeshInterface *IMesh, const BuildSettings &Settings, bool NoLeaf, bool Quantized):
  37. mIMesh(IMesh),
  38. mSettings(Settings),
  39. mNoLeaf(NoLeaf),
  40. mQuantized(Quantized),
  41. #ifdef __MESHMERIZER_H__
  42. mCollisionHull(false),
  43. #endif // __MESHMERIZER_H__
  44. mKeepOriginal(false),
  45. mCanRemap(false)
  46. {
  47. }
  48. MeshInterface* mIMesh; //!< Mesh interface (access to triangles & vertices) (*)
  49. BuildSettings mSettings; //!< Builder's settings
  50. bool mNoLeaf; //!< true => discard leaf nodes (else use a normal tree)
  51. bool mQuantized; //!< true => quantize the tree (else use a normal tree)
  52. #ifdef __MESHMERIZER_H__
  53. bool mCollisionHull; //!< true => use convex hull + GJK
  54. #endif // __MESHMERIZER_H__
  55. bool mKeepOriginal; //!< true => keep a copy of the original tree (debug purpose)
  56. bool mCanRemap; //!< true => allows OPCODE to reorganize client arrays
  57. // (*) This pointer is saved internally and used by OPCODE until collision structures are released,
  58. // so beware of the object's lifetime.
  59. };
  60. enum ModelFlag
  61. {
  62. OPC_QUANTIZED = (1<<0), //!< Compressed/uncompressed tree
  63. OPC_NO_LEAF = (1<<1), //!< Leaf/NoLeaf tree
  64. OPC_SINGLE_NODE = (1<<2) //!< Special case for 1-node models
  65. };
  66. class OPCODE_API BaseModel
  67. {
  68. public:
  69. // Constructor/Destructor
  70. BaseModel();
  71. virtual ~BaseModel();
  72. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  73. /**
  74. * Builds a collision model.
  75. * \param create [in] model creation structure
  76. * \return true if success
  77. */
  78. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  79. virtual bool Build(const OPCODECREATE& create) = 0;
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. /**
  82. * Gets the number of bytes used by the tree.
  83. * \return amount of bytes used
  84. */
  85. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  86. virtual udword GetUsedBytes() const = 0;
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  88. /**
  89. * Refits the collision model. This can be used to handle dynamic meshes. Usage is:
  90. * 1. modify your mesh vertices (keep the topology constant!)
  91. * 2. refit the tree (call this method)
  92. * \return true if success
  93. */
  94. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. virtual bool Refit();
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. /**
  98. * Gets the source tree.
  99. * \return generic tree
  100. */
  101. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  102. inline_ const AABBTree* GetSourceTree() const { return mSource; }
  103. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. /**
  105. * Gets the tree.
  106. * \return the collision tree
  107. */
  108. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  109. inline_ const AABBOptimizedTree* GetTree() const { return mTree; }
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  111. /**
  112. * Gets the tree.
  113. * \return the collision tree
  114. */
  115. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  116. inline_ AABBOptimizedTree* GetTree() { return mTree; }
  117. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  118. /**
  119. * Gets the number of nodes in the tree.
  120. * Should be 2*N-1 for normal trees and N-1 for optimized ones.
  121. * \return number of nodes
  122. */
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  124. inline_ udword GetNbNodes() const { return mTree->GetNbNodes(); }
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126. /**
  127. * Checks whether the tree has leaf nodes or not.
  128. * \return true if the tree has leaf nodes (normal tree), else false (optimized tree)
  129. */
  130. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  131. inline_ BOOL HasLeafNodes() const { return !(mModelCode & OPC_NO_LEAF); }
  132. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. /**
  134. * Checks whether the tree is quantized or not.
  135. * \return true if the tree is quantized
  136. */
  137. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  138. inline_ BOOL IsQuantized() const { return mModelCode & OPC_QUANTIZED; }
  139. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  140. /**
  141. * Checks whether the model has a single node or not. This special case must be handled separately.
  142. * \return true if the model has only 1 node
  143. */
  144. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  145. inline_ BOOL HasSingleNode() const { return mModelCode & OPC_SINGLE_NODE; }
  146. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  147. /**
  148. * Gets the model's code.
  149. * \return model's code
  150. */
  151. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  152. inline_ udword GetModelCode() const { return mModelCode; }
  153. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  154. /**
  155. * Gets the mesh interface.
  156. * \return mesh interface
  157. */
  158. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159. inline_ const MeshInterface* GetMeshInterface() const { return mIMesh; }
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. /**
  162. * Sets the mesh interface.
  163. * \param imesh [in] mesh interface
  164. */
  165. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. inline_ void SetMeshInterface(const MeshInterface* imesh) { mIMesh = imesh; }
  167. protected:
  168. const MeshInterface* mIMesh; //!< User-defined mesh interface
  169. udword mModelCode; //!< Model code = combination of ModelFlag(s)
  170. AABBTree* mSource; //!< Original source tree
  171. AABBOptimizedTree* mTree; //!< Optimized tree owned by the model
  172. // Internal methods
  173. void ReleaseBase();
  174. bool CreateTree(bool no_leaf, bool quantized);
  175. };
  176. #endif //__OPC_BASEMODEL_H__