BsMeshBase.h 5.8 KB

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