OPC_BaseModel.h 10 KB

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