CmMesh.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmMeshBase.h"
  4. #include "CmMeshData.h"
  5. #include "CmVertexData.h"
  6. #include "CmIndexData.h"
  7. #include "CmDrawOps.h"
  8. #include "CmSubMesh.h"
  9. namespace BansheeEngine
  10. {
  11. /**
  12. * @brief Primary class for holding geometry. Stores data in the form of a vertex
  13. * buffers and optionally index buffer, which may be bound to the pipeline for drawing.
  14. * May contain multiple sub-meshes.
  15. */
  16. class CM_EXPORT Mesh : public MeshBase
  17. {
  18. public:
  19. virtual ~Mesh();
  20. /**
  21. * @copydoc GpuResource::writeSubresource
  22. */
  23. virtual void writeSubresource(UINT32 subresourceIdx, const GpuResourceData& data, bool discardEntireBuffer);
  24. /**
  25. * @copydoc GpuResource::readSubresource
  26. */
  27. virtual void readSubresource(UINT32 subresourceIdx, GpuResourceData& data);
  28. /**
  29. * @brief Allocates a buffer you may use for storage when reading a subresource. You
  30. * need to allocate such a buffer if you are calling "readSubresource".
  31. *
  32. * @note This method is thread safe.
  33. */
  34. MeshDataPtr allocateSubresourceBuffer(UINT32 subresourceIdx) const;
  35. /**
  36. * @brief TODO - Currently does nothing. But normally it should provide a way to map subresource index to
  37. * a specific submesh or buffer stream. Right now you can only work with entire mesh at once, not its subsets.
  38. */
  39. void mapFromSubresourceIdx(UINT32 subresourceIdx) const {}
  40. /**
  41. * @brief TODO - Currently does nothing. But normally it should provide a way to map submesh or stream index to
  42. * a specific subresource index. Right now you can only work with entire mesh at once, not its subsets.
  43. */
  44. UINT32 mapToSubresourceIdx() const { return 0; }
  45. /**
  46. * @brief Returns an axis aligned bounding box of the geometry contained in the vertex buffers for all submeshes.
  47. */
  48. const AABox& getBounds() const;
  49. /**
  50. * @brief Returns an axis aligned bounding box of the geometry contained in the specific sub-mesh.
  51. */
  52. const AABox& getBounds(UINT32 submeshIdx) const;
  53. /**
  54. * @copydoc MeshBase::getVertexData
  55. */
  56. virtual std::shared_ptr<VertexData> _getVertexData() const;
  57. /**
  58. * @copydoc MeshBase::getIndexData
  59. */
  60. virtual std::shared_ptr<IndexData> _getIndexData() const;
  61. /**
  62. * @brief Returns a dummy mesh, containing just one triangle. Don't modify the returned mesh.
  63. */
  64. static HMesh dummy();
  65. protected:
  66. friend class MeshManager;
  67. Mesh(UINT32 numVertices, UINT32 numIndices,
  68. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  69. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  70. Mesh(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const MeshDataPtr& initialMeshData,
  71. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  72. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  73. Mesh(const MeshDataPtr& initialMeshData,
  74. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  75. std::shared_ptr<VertexData> mVertexData; // Core thread
  76. std::shared_ptr<IndexData> mIndexData; // Core thread
  77. VertexDataDescPtr mVertexDesc; // Immutable
  78. MeshBufferType mBufferType; // Immutable
  79. IndexBuffer::IndexType mIndexType; // Immutable
  80. MeshDataPtr mTempInitialMeshData; // Immutable
  81. /**
  82. * @copydoc Resource::initialize_internal()
  83. */
  84. virtual void initialize_internal();
  85. /**
  86. * @copydoc Resource::destroy_internal()
  87. */
  88. virtual void destroy_internal();
  89. /************************************************************************/
  90. /* SERIALIZATION */
  91. /************************************************************************/
  92. private:
  93. Mesh(); // Serialization only
  94. public:
  95. friend class MeshRTTI;
  96. static RTTITypeBase* getRTTIStatic();
  97. virtual RTTITypeBase* getRTTI() const;
  98. /************************************************************************/
  99. /* STATICS */
  100. /************************************************************************/
  101. public:
  102. /**
  103. * @brief Creates a new empty mesh.
  104. *
  105. * @param numVertices Number of vertices in the mesh.
  106. * @param numIndices Number of indices in the mesh.
  107. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  108. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  109. * at least partially matches the input description of the currently bound vertex GPU program.
  110. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  111. * dynamic. This parameter affects performance.
  112. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  113. * where three indices represent a single triangle.
  114. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  115. * the number of vertices limited by the size.
  116. */
  117. static HMesh create(UINT32 numVertices, UINT32 numIndices,
  118. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  119. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  120. /**
  121. * @brief Creates a new mesh and immediately writes some data to the mesh. This is faster than writing
  122. * the data in a separate step after creation.
  123. *
  124. * @param numVertices Number of vertices in the mesh.
  125. * @param numIndices Number of indices in the mesh.
  126. * @param vertexDesc Vertex description structure that describes how are vertices organized in the
  127. * vertex buffer. When binding a mesh to the pipeline you must ensure vertex description
  128. * at least partially matches the input description of the currently bound vertex GPU program.
  129. * @param initialMeshData Vertex and index data used for initializing the mesh. Caller must ensure the data vertex and index buffers
  130. * match the ones in the mesh, however the data might only write to a certain subset of the mesh, it does not
  131. * have to write to all of it.
  132. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  133. * dynamic. This parameter affects performance.
  134. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  135. * where three indices represent a single triangle.
  136. * @param indexType Size of indices, use smaller size for better performance, however be careful not to go over
  137. * the number of vertices limited by the size.
  138. */
  139. static HMesh create(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const MeshDataPtr& initialMeshData,
  140. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  141. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  142. /**
  143. * @brief Creates a new mesh from an existing mesh data. Created mesh will match the vertex and index buffers described
  144. * by the mesh data exactly.
  145. *
  146. * @param initialMeshData Vertex and index data used for initializing the mesh.
  147. * @param bufferType Specify static for buffers you don't plan on updating other reading from often. Otherwise specify
  148. * dynamic. This parameter affects performance.
  149. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  150. * where three indices represent a single triangle.
  151. */
  152. static HMesh create(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  153. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  154. /**
  155. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, MeshBufferType, DrawOperationType, IndexBuffer::IndexType)
  156. *
  157. * @note Internal method. Use "create" for normal use.
  158. */
  159. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices,
  160. const VertexDataDescPtr& vertexDesc, MeshBufferType bufferType = MeshBufferType::Static,
  161. DrawOperationType drawOp = DOT_TRIANGLE_LIST, IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  162. /**
  163. * @copydoc create(UINT32, UINT32, const VertexDataDescPtr&, const MeshDataPtr&, MeshBufferType, DrawOperationType, IndexBuffer::IndexType)
  164. *
  165. * @note Internal method. Use "create" for normal use.
  166. */
  167. static MeshPtr _createPtr(UINT32 numVertices, UINT32 numIndices, const VertexDataDescPtr& vertexDesc, const MeshDataPtr& initialMeshData,
  168. MeshBufferType bufferType = MeshBufferType::Static, DrawOperationType drawOp = DOT_TRIANGLE_LIST,
  169. IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  170. /**
  171. * @copydoc create(const MeshDataPtr&, MeshBufferType, DrawOperationType)
  172. *
  173. * @note Internal method. Use "create" for normal use.
  174. */
  175. static MeshPtr _createPtr(const MeshDataPtr& initialMeshData, MeshBufferType bufferType = MeshBufferType::Static,
  176. DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  177. };
  178. }