b3TriangleIndexVertexArray.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #ifndef B3_TRIANGLE_INDEX_VERTEX_ARRAY_H
  14. #define B3_TRIANGLE_INDEX_VERTEX_ARRAY_H
  15. #include "b3StridingMeshInterface.h"
  16. #include "Bullet3Common/b3AlignedObjectArray.h"
  17. #include "Bullet3Common/b3Scalar.h"
  18. ///The b3IndexedMesh indexes a single vertex and index array. Multiple b3IndexedMesh objects can be passed into a b3TriangleIndexVertexArray using addIndexedMesh.
  19. ///Instead of the number of indices, we pass the number of triangles.
  20. B3_ATTRIBUTE_ALIGNED16(struct)
  21. b3IndexedMesh
  22. {
  23. B3_DECLARE_ALIGNED_ALLOCATOR();
  24. int m_numTriangles;
  25. const unsigned char* m_triangleIndexBase;
  26. // Size in byte of the indices for one triangle (3*sizeof(index_type) if the indices are tightly packed)
  27. int m_triangleIndexStride;
  28. int m_numVertices;
  29. const unsigned char* m_vertexBase;
  30. // Size of a vertex, in bytes
  31. int m_vertexStride;
  32. // The index type is set when adding an indexed mesh to the
  33. // b3TriangleIndexVertexArray, do not set it manually
  34. PHY_ScalarType m_indexType;
  35. // The vertex type has a default type similar to Bullet's precision mode (float or double)
  36. // but can be set manually if you for example run Bullet with double precision but have
  37. // mesh data in single precision..
  38. PHY_ScalarType m_vertexType;
  39. b3IndexedMesh()
  40. : m_indexType(PHY_INTEGER),
  41. #ifdef B3_USE_DOUBLE_PRECISION
  42. m_vertexType(PHY_DOUBLE)
  43. #else // B3_USE_DOUBLE_PRECISION
  44. m_vertexType(PHY_FLOAT)
  45. #endif // B3_USE_DOUBLE_PRECISION
  46. {
  47. }
  48. };
  49. typedef b3AlignedObjectArray<b3IndexedMesh> IndexedMeshArray;
  50. ///The b3TriangleIndexVertexArray allows to access multiple triangle meshes, by indexing into existing triangle/index arrays.
  51. ///Additional meshes can be added using addIndexedMesh
  52. ///No duplcate is made of the vertex/index data, it only indexes into external vertex/index arrays.
  53. ///So keep those arrays around during the lifetime of this b3TriangleIndexVertexArray.
  54. B3_ATTRIBUTE_ALIGNED16(class)
  55. b3TriangleIndexVertexArray : public b3StridingMeshInterface
  56. {
  57. protected:
  58. IndexedMeshArray m_indexedMeshes;
  59. int m_pad[2];
  60. mutable int m_hasAabb; // using int instead of bool to maintain alignment
  61. mutable b3Vector3 m_aabbMin;
  62. mutable b3Vector3 m_aabbMax;
  63. public:
  64. B3_DECLARE_ALIGNED_ALLOCATOR();
  65. b3TriangleIndexVertexArray() : m_hasAabb(0)
  66. {
  67. }
  68. virtual ~b3TriangleIndexVertexArray();
  69. //just to be backwards compatible
  70. b3TriangleIndexVertexArray(int numTriangles, int* triangleIndexBase, int triangleIndexStride, int numVertices, b3Scalar* vertexBase, int vertexStride);
  71. void addIndexedMesh(const b3IndexedMesh& mesh, PHY_ScalarType indexType = PHY_INTEGER)
  72. {
  73. m_indexedMeshes.push_back(mesh);
  74. m_indexedMeshes[m_indexedMeshes.size() - 1].m_indexType = indexType;
  75. }
  76. virtual void getLockedVertexIndexBase(unsigned char** vertexbase, int& numverts, PHY_ScalarType& type, int& vertexStride, unsigned char** indexbase, int& indexstride, int& numfaces, PHY_ScalarType& indicestype, int subpart = 0);
  77. virtual void getLockedReadOnlyVertexIndexBase(const unsigned char** vertexbase, int& numverts, PHY_ScalarType& type, int& vertexStride, const unsigned char** indexbase, int& indexstride, int& numfaces, PHY_ScalarType& indicestype, int subpart = 0) const;
  78. /// unLockVertexBase finishes the access to a subpart of the triangle mesh
  79. /// make a call to unLockVertexBase when the read and write access (using getLockedVertexIndexBase) is finished
  80. virtual void unLockVertexBase(int subpart) { (void)subpart; }
  81. virtual void unLockReadOnlyVertexBase(int subpart) const { (void)subpart; }
  82. /// getNumSubParts returns the number of separate subparts
  83. /// each subpart has a continuous array of vertices and indices
  84. virtual int getNumSubParts() const
  85. {
  86. return (int)m_indexedMeshes.size();
  87. }
  88. IndexedMeshArray& getIndexedMeshArray()
  89. {
  90. return m_indexedMeshes;
  91. }
  92. const IndexedMeshArray& getIndexedMeshArray() const
  93. {
  94. return m_indexedMeshes;
  95. }
  96. virtual void preallocateVertices(int numverts) { (void)numverts; }
  97. virtual void preallocateIndices(int numindices) { (void)numindices; }
  98. virtual bool hasPremadeAabb() const;
  99. virtual void setPremadeAabb(const b3Vector3& aabbMin, const b3Vector3& aabbMax) const;
  100. virtual void getPremadeAabb(b3Vector3 * aabbMin, b3Vector3 * aabbMax) const;
  101. };
  102. #endif //B3_TRIANGLE_INDEX_VERTEX_ARRAY_H