BsMesh.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. #include "BsMeshBase.h"
  7. #include "BsMeshData.h"
  8. #include "BsVertexData.h"
  9. #include "BsDrawOps.h"
  10. #include "BsSubMesh.h"
  11. #include "BsBounds.h"
  12. namespace BansheeEngine
  13. {
  14. /**
  15. * @brief Primary class for holding geometry. Stores data in the form of a vertex
  16. * buffers and optionally index buffer, which may be bound to the pipeline for drawing.
  17. * May contain multiple sub-meshes.
  18. */
  19. class BS_CORE_EXPORT Mesh : public MeshBase
  20. {
  21. public:
  22. virtual ~Mesh();
  23. /**
  24. * @copydoc MeshBase::initialize
  25. */
  26. virtual void initialize();
  27. /**
  28. * @copydoc GpuResource::_writeSubresourceSim
  29. */
  30. virtual void _writeSubresourceSim(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  31. /**
  32. * @copydoc GpuResource::writeSubresource
  33. */
  34. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  35. /**
  36. * @copydoc GpuResource::readSubresource
  37. */
  38. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  39. /**
  40. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  41. * need to allocate such a buffer if you are calling "readSubresource".
  42. *
  43. * @param subresourceIdx Only 0 is supported. You can only update entire mesh at once.
  44. *
  45. * @note This method is thread safe.
  46. */
  47. MeshDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  48. /**
  49. * @brief Returns bounds of the geometry contained in the vertex buffers for all sub-meshes.
  50. */
  51. const Bounds& getBounds() const { return mBounds; }
  52. /**
  53. * @copydoc MeshBase::getVertexData
  54. */
  55. virtual std::shared_ptr<VertexData> _getVertexData() const;
  56. /**
  57. * @copydoc MeshBase::getIndexData
  58. */
  59. virtual IndexBufferPtr _getIndexBuffer() const;
  60. /**
  61. * @brief Returns a dummy mesh, containing just one triangle. Don't modify the returned mesh.
  62. */
  63. static HMesh dummy();
  64. /************************************************************************/
  65. /* CORE PROXY */
  66. /************************************************************************/
  67. /**
  68. * @copydoc MeshBase::_createProxy
  69. */
  70. MeshProxyPtr _createProxy(UINT32 subMeshIdx);
  71. protected:
  72. friend class MeshManager;
  73. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  74. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  75. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  76. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc,
  77. const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static,
  78. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  79. Mesh(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  80. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  81. Mesh(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  82. std::shared_ptr<VertexData> mVertexData; // Core thread
  83. IndexBufferPtr mIndexBuffer; // Core thread
  84. Bounds mBounds; // Core thread
  85. VertexDataDescPtr mVertexDesc; // Immutable
  86. MeshBufferType mBufferType; // Immutable
  87. IndexBuffer::IndexType mIndexType; // Immutable
  88. MeshDataPtr mTempInitialMeshData; // Immutable
  89. /**
  90. * @copydoc Resource::initialize_internal()
  91. */
  92. virtual void initialize_internal();
  93. /**
  94. * @copydoc Resource::destroy_internal()
  95. */
  96. virtual void destroy_internal();
  97. /**
  98. * @brief Updates bounds by calculating them from the vertices in the provided mesh data object.
  99. */
  100. void updateBounds(const MeshData& meshData);
  101. /**
  102. * @brief Calculates bounds surrounding the vertices in the provided buffer.
  103. *
  104. * @param verticesPtr Pointer to the buffer containing the positions of vertices to calculate bounds for.
  105. * @param numVertices Number of vertices in the provided buffer.
  106. * @param stride How many bytes are needed to advance from one vertex to another.
  107. */
  108. Bounds calculateBounds(UINT8* verticesPtr, UINT32 numVertices, UINT32 stride) const;
  109. /************************************************************************/
  110. /* SERIALIZATION */
  111. /************************************************************************/
  112. private:
  113. Mesh(); // Serialization only
  114. public:
  115. friend class MeshRTTI;
  116. static RTTITypeBase* getRTTIStatic();
  117. virtual RTTITypeBase* getRTTI() const;
  118. /************************************************************************/
  119. /* STATICS */
  120. /************************************************************************/
  121. public:
  122. /**
  123. * @brief Creates a new empty mesh. Created mesh will have no sub-meshes.
  124. *
  125. * @param numVertices Number of vertices in the mesh.
  126. * @param numIndices Number of indices in the mesh.
  127. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  128. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  129. * at least partially matches the input description of the currently bound vertex GPU program.
  130. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  131. * dynamic. This parameter affects performance.
  132. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  133. * where three indices represent a single triangle.
  134. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  135. * the number of vertices limited by the size.
  136. */
  137. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  138. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  139. /**
  140. * @brief Creates a new empty mesh. Created mesh will have specified sub-meshes you may render independently.
  141. *
  142. * @param numVertices Number of vertices in the mesh.
  143. * @param numIndices Number of indices in the mesh.
  144. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  145. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  146. * at least partially matches the input description of the currently bound vertex GPU program.
  147. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  148. * Sub-meshes may be rendered independently.
  149. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  150. * dynamic. This parameter affects performance.
  151. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  152. * the number of vertices limited by the size.
  153. */
  154. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  155. MeshBufferType bufferType = MeshBufferType::Static, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  156. /**
  157. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  158. * by the mesh data exactly. Created mesh will have no sub-meshes.
  159. *
  160. * @param initialMeshData Vertex and index data used for initializing the mesh.
  161. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  162. * dynamic. This parameter affects performance.
  163. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  164. * where three indices represent a single triangle.
  165. */
  166. static HMesh create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  167. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  168. /**
  169. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  170. * by the mesh data exactly. Created mesh will have specified sub-meshes you may render independently.
  171. *
  172. * @param initialMeshData Vertex and index data used for initializing the mesh.
  173. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  174. * Sub-meshes may be rendered independently.
  175. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  176. * dynamic. This parameter affects performance.
  177. */
  178. static HMesh create(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes, MeshBufferType bufferType = MeshBufferType::Static);
  179. /**
  180. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, MeshBufferType, DrawOperationType, IndexBuffer::IndexType)
  181. *
  182. * @note Internal method. Use "create" for normal use.
  183. */
  184. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  185. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  186. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  187. /**
  188. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, const Vector<SubMesh>&, MeshBufferType, IndexBuffer::IndexType)
  189. *
  190. * @note Internal method. Use "create" for normal use.
  191. */
  192. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  193. const VertexDataDescPtr& vertexDesc, const Vector<SubMesh>& subMeshes,
  194. MeshBufferType bufferType = MeshBufferType::Static, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  195. /**
  196. * @copydoc create(const MeshDataPtr&, MeshBufferType, DrawOperationType)
  197. *
  198. * @note Internal method. Use "create" for normal use.
  199. */
  200. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  201. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  202. /**
  203. * @copydoc create(const MeshDataPtr&, const Vector<SubMesh>&, MeshBufferType)
  204. *
  205. * @note Internal method. Use "create" for normal use.
  206. */
  207. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, const Vector<SubMesh>& subMeshes,
  208. MeshBufferType bufferType = MeshBufferType::Static);
  209. };
  210. }