BsMeshBase.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsResource.h"
  4. #include "BsBounds.h"
  5. #include "BsDrawOps.h"
  6. #include "BsSubMesh.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Type of mesh dirty flags
  11. */
  12. enum class MeshDirtyFlag
  13. {
  14. Mesh = 0x01, /**< Internal mesh data is dirty. */
  15. Proxy = 0x02 /**< Active proxy needs to be updated. */
  16. };
  17. /**
  18. * @brief Type of buffers used by a mesh. These options usually affect performance and
  19. * you should specify static if you don't plan on modifying the mesh often,
  20. * otherwise specify dynamic.
  21. */
  22. enum class MeshBufferType
  23. {
  24. Static,
  25. Dynamic
  26. };
  27. /**
  28. * @brief Contains various properties describing a mesh.
  29. */
  30. class BS_CORE_EXPORT MeshProperties
  31. {
  32. public:
  33. MeshProperties();
  34. MeshProperties(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp);
  35. MeshProperties(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  36. /**
  37. * @brief Retrieves a sub-mesh containing data used for rendering a
  38. * certain portion of this mesh. If no sub-meshes are specified manually
  39. * a special sub-mesh containing all indices is returned.
  40. */
  41. const SubMesh& getSubMesh(UINT32 subMeshIdx = 0) const;
  42. /**
  43. * @brief Retrieves a total number of sub-meshes in this mesh.
  44. */
  45. UINT32 getNumSubMeshes() const;
  46. /**
  47. * @brief Returns maximum number of vertices the mesh may store.
  48. */
  49. UINT32 getNumVertices() const { return mNumVertices; }
  50. /**
  51. * @brief Returns maximum number of indices the mesh may store.
  52. */
  53. UINT32 getNumIndices() const { return mNumIndices; }
  54. /**
  55. * @brief Returns bounds of the geometry contained in the vertex buffers for all sub-meshes.
  56. */
  57. const Bounds& getBounds() const { return mBounds; }
  58. protected:
  59. friend class MeshBase;
  60. friend class MeshCoreBase;
  61. friend class Mesh;
  62. friend class MeshCore;
  63. friend class TransientMesh;
  64. friend class TransientMeshCore;
  65. friend class MeshBaseRTTI;
  66. Vector<SubMesh> mSubMeshes;
  67. UINT32 mNumVertices;
  68. UINT32 mNumIndices;
  69. Bounds mBounds;
  70. };
  71. /**
  72. * @brief Core version of a class used as a basis for all implemenations of meshes.
  73. *
  74. * @see MeshBase
  75. *
  76. * @note Core thread.
  77. */
  78. class BS_CORE_EXPORT MeshCoreBase : public CoreObjectCore
  79. {
  80. public:
  81. MeshCoreBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  82. virtual ~MeshCoreBase() { }
  83. /**
  84. * @brief Get vertex data used for rendering.
  85. */
  86. virtual SPtr<VertexData> getVertexData() const = 0;
  87. /**
  88. * @brief Get index data used for rendering.
  89. */
  90. virtual SPtr<IndexBufferCore> getIndexBuffer() const = 0;
  91. /**
  92. * @brief Returns an offset into the vertex buffers that is returned
  93. * by getVertexData that signifies where this meshes vertices
  94. * begin.
  95. *
  96. * @note Used when multiple meshes share the same buffers.
  97. */
  98. virtual UINT32 getVertexOffset() const { return 0; }
  99. /**
  100. * @brief Returns an offset into the index buffer that is returned
  101. * by getIndexData that signifies where this meshes indices
  102. * begin.
  103. *
  104. * @note Used when multiple meshes share the same buffers.
  105. */
  106. virtual UINT32 getIndexOffset() const { return 0; }
  107. /**
  108. * @brief Called whenever this mesh starts being used on the GPU.
  109. *
  110. * @note Needs to be called after all commands referencing this
  111. * mesh have been sent to the GPU.
  112. *
  113. * Internal method.
  114. */
  115. virtual void _notifyUsedOnGPU() { }
  116. /**
  117. * @brief Returns properties that contain information about the mesh.
  118. */
  119. const MeshProperties& getProperties() const { return mProperties; }
  120. protected:
  121. /**
  122. * @copydoc CoreObjectCore::syncFromCore
  123. */
  124. virtual CoreSyncData syncFromCore(FrameAlloc* allocator);
  125. /**
  126. * @copydoc CoreObjectCore::syncToCore
  127. */
  128. virtual void syncToCore(const CoreSyncData& data);
  129. MeshProperties mProperties;
  130. };
  131. /**
  132. * @brief Base class all mesh implementations derive from. Meshes hold geometry information,
  133. * normally in the form of one or several index or vertex buffers. Different mesh implementations
  134. * might choose to manage those buffers differently.
  135. *
  136. * @note Sim thread.
  137. */
  138. class BS_CORE_EXPORT MeshBase : public Resource
  139. {
  140. public:
  141. /**
  142. * @brief Constructs a new mesh with no sub-meshes.
  143. *
  144. * @param numVertices Number of vertices in the mesh.
  145. * @param numIndices Number of indices in the mesh.
  146. * @param drawOp Determines how should the provided indices be interpreted by the pipeline. Default option is triangles,
  147. * where three indices represent a single triangle.
  148. */
  149. MeshBase(UINT32 numVertices, UINT32 numIndices, DrawOperationType drawOp = DOT_TRIANGLE_LIST);
  150. /**
  151. * @brief Constructs a new mesh with one or multiple sub-meshes. (When using just one sub-mesh it is equivalent
  152. * to using the other overload).
  153. *
  154. * @param numVertices Number of vertices in the mesh.
  155. * @param numIndices Number of indices in the mesh.
  156. * @param subMeshes Defines how are indices separated into sub-meshes, and how are those sub-meshes rendered.
  157. */
  158. MeshBase(UINT32 numVertices, UINT32 numIndices, const Vector<SubMesh>& subMeshes);
  159. virtual ~MeshBase();
  160. /**
  161. * @brief Returns properties that contain information about the mesh.
  162. */
  163. const MeshProperties& getProperties() const { return mProperties; }
  164. /**
  165. * @brief Retrieves a core implementation of a mesh usable only from the
  166. * core thread.
  167. */
  168. SPtr<MeshCoreBase> getCore() const;
  169. protected:
  170. /**
  171. * @copydoc CoreObject::syncToCore
  172. */
  173. virtual CoreSyncData syncToCore(FrameAlloc* allocator);
  174. /**
  175. * @copydoc CoreObject::syncFromCore
  176. */
  177. virtual void syncFromCore(const CoreSyncData& data);
  178. MeshProperties mProperties;
  179. /************************************************************************/
  180. /* SERIALIZATION */
  181. /************************************************************************/
  182. private:
  183. MeshBase() { } // Serialization only
  184. public:
  185. friend class MeshBaseRTTI;
  186. static RTTITypeBase* getRTTIStatic();
  187. virtual RTTITypeBase* getRTTI() const;
  188. };
  189. }