OPC_MeshInterface.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 a mesh interface.
  11. * \file OPC_MeshInterface.h
  12. * \author Pierre Terdiman
  13. * \date November, 27, 2002
  14. */
  15. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. // Include Guard
  18. #ifndef __OPC_MESHINTERFACE_H__
  19. #define __OPC_MESHINTERFACE_H__
  20. struct VertexPointers
  21. {
  22. const Point* Vertex[3];
  23. udword MatIdx;
  24. VertexPointers() { MatIdx=-1; }
  25. bool BackfaceCulling(const Point& source)
  26. {
  27. const Point& p0 = *Vertex[0];
  28. const Point& p1 = *Vertex[1];
  29. const Point& p2 = *Vertex[2];
  30. // Compute normal direction
  31. Point Normal = (p2 - p1)^(p0 - p1);
  32. // Backface culling
  33. return (Normal | (source - p0)) >= 0.0f;
  34. }
  35. };
  36. #ifdef OPC_USE_CALLBACKS
  37. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  38. /**
  39. * User-callback, called by OPCODE to request vertices from the app.
  40. * \param triangle_index [in] face index for which the system is requesting the vertices
  41. * \param triangle [out] triangle's vertices (must be provided by the user)
  42. * \param user_data [in] user-defined data from SetCallback()
  43. */
  44. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. typedef void (*RequestCallback) (udword triangle_index, VertexPointers& triangle, void* user_data);
  46. #endif
  47. class OPCODE_API MeshInterface
  48. {
  49. public:
  50. // Constructor / Destructor
  51. MeshInterface();
  52. ~MeshInterface();
  53. // Common settings
  54. inline_ udword GetNbTriangles() const { return mNbTris; }
  55. inline_ udword GetNbVertices() const { return mNbVerts; }
  56. inline_ void SetNbTriangles(udword nb) { mNbTris = nb; }
  57. inline_ void SetNbVertices(udword nb) { mNbVerts = nb; }
  58. #ifdef OPC_USE_CALLBACKS
  59. // Callback settings
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61. /**
  62. * Callback control: setups object callback. Must provide triangle-vertices for a given triangle index.
  63. * \param callback [in] user-defined callback
  64. * \param user_data [in] user-defined data
  65. * \return true if success
  66. */
  67. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. bool SetCallback(RequestCallback callback, void* user_data);
  69. inline_ void* GetUserData() const { return mUserData; }
  70. inline_ RequestCallback GetCallback() const { return mObjCallback; }
  71. #else
  72. // Pointers settings
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  74. /**
  75. * Pointers control: setups object pointers. Must provide access to faces and vertices for a given object.
  76. * \param tris [in] pointer to triangles
  77. * \param verts [in] pointer to vertices
  78. * \return true if success
  79. */
  80. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  81. bool SetPointers(const IndexedTriangle* tris, const Point* verts);
  82. inline_ const IndexedTriangle* GetTris() const { return mTris; }
  83. inline_ const Point* GetVerts() const { return mVerts; }
  84. #ifdef OPC_USE_STRIDE
  85. // Strides settings
  86. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. /**
  88. * Strides control
  89. * \param tri_stride [in] size of a triangle in bytes. The first sizeof(IndexedTriangle) bytes are used to get vertex indices.
  90. * \param vertex_stride [in] size of a vertex in bytes. The first sizeof(Point) bytes are used to get vertex position.
  91. * \return true if success
  92. */
  93. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  94. bool SetStrides(udword tri_stride=sizeof(IndexedTriangle), udword vertex_stride=sizeof(Point));
  95. inline_ udword GetTriStride() const { return mTriStride; }
  96. inline_ udword GetVertexStride() const { return mVertexStride; }
  97. #endif
  98. #endif
  99. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. /**
  101. * Fetches a triangle given a triangle index.
  102. * \param vp [out] required triangle's vertex pointers
  103. * \param index [in] triangle index
  104. */
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106. inline_ void GetTriangle(VertexPointers& vp, udword index) const
  107. {
  108. #ifdef OPC_USE_CALLBACKS
  109. (mObjCallback)(index, vp, mUserData);
  110. #else
  111. #ifdef OPC_USE_STRIDE
  112. const IndexedTriangle* T = (const IndexedTriangle*)(((ubyte*)mTris) + index * mTriStride);
  113. vp.Vertex[0] = (const Point*)(((ubyte*)mVerts) + T->mVRef[0] * mVertexStride);
  114. vp.Vertex[1] = (const Point*)(((ubyte*)mVerts) + T->mVRef[1] * mVertexStride);
  115. vp.Vertex[2] = (const Point*)(((ubyte*)mVerts) + T->mVRef[2] * mVertexStride);
  116. vp.MatIdx = T->mMatIdx;
  117. #else
  118. const IndexedTriangle* T = &mTris[index];
  119. vp.Vertex[0] = &mVerts[T->mVRef[0]];
  120. vp.Vertex[1] = &mVerts[T->mVRef[1]];
  121. vp.Vertex[2] = &mVerts[T->mVRef[2]];
  122. vp.MatIdx = T->mMatIdx;
  123. #endif
  124. #endif
  125. }
  126. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. /**
  128. * Remaps client's mesh according to a permutation.
  129. * \param nb_indices [in] number of indices in the permutation (will be checked against number of triangles)
  130. * \param permutation [in] list of triangle indices
  131. * \return true if success
  132. */
  133. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  134. bool RemapClient(udword nb_indices, const udword* permutation) const;
  135. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  136. /**
  137. * Checks the mesh interface is valid, i.e. things have been setup correctly.
  138. * \return true if valid
  139. */
  140. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  141. bool IsValid() const;
  142. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. /**
  144. * Checks the mesh itself is valid.
  145. * Currently we only look for degenerate faces.
  146. * \return number of degenerate faces
  147. */
  148. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  149. udword CheckTopology() const;
  150. private:
  151. udword mNbTris; //!< Number of triangles in the input model
  152. udword mNbVerts; //!< Number of vertices in the input model
  153. #ifdef OPC_USE_CALLBACKS
  154. // User callback
  155. void* mUserData; //!< User-defined data sent to callback
  156. RequestCallback mObjCallback; //!< Object callback
  157. #else
  158. // User pointers
  159. const IndexedTriangle* mTris; //!< Array of indexed triangles
  160. const Point* mVerts; //!< Array of vertices
  161. #ifdef OPC_USE_STRIDE
  162. udword mTriStride; //!< Possible triangle stride in bytes [Opcode 1.3]
  163. udword mVertexStride; //!< Possible vertex stride in bytes [Opcode 1.3]
  164. #endif
  165. #endif
  166. };
  167. #endif //__OPC_MESHINTERFACE_H__