BsMesh.h 9.8 KB

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