CmMeshData.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmIReflectable.h"
  4. #include "CmVertexBuffer.h"
  5. #include "CmIndexBuffer.h"
  6. #include "CmVertexDeclaration.h"
  7. namespace CamelotEngine
  8. {
  9. class CM_EXPORT MeshData : public IReflectable
  10. {
  11. public:
  12. struct VertexElementData : public IReflectable
  13. {
  14. VertexElementData(VertexElementType type, VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx, UINT8* _data, UINT32 numElements)
  15. :data(_data), elementCount(numElements), element(streamIdx, 0, type, semantic, semanticIdx)
  16. { }
  17. UINT8* data;
  18. UINT32 elementCount;
  19. VertexElement element;
  20. /************************************************************************/
  21. /* SERIALIZATION */
  22. /************************************************************************/
  23. public:
  24. VertexElementData()
  25. :data(nullptr), elementCount(0)
  26. {} // Serialization only constructor
  27. friend class VertexElementDataRTTI;
  28. static RTTITypeBase* getRTTIStatic();
  29. virtual RTTITypeBase* getRTTI() const;
  30. };
  31. struct IndexElementData : public IReflectable
  32. {
  33. IndexElementData()
  34. :numIndices(0), subMesh(0), elementSize(0), indices(nullptr)
  35. { }
  36. UINT8* indices;
  37. UINT32 numIndices;
  38. UINT32 elementSize;
  39. UINT32 subMesh;
  40. /************************************************************************/
  41. /* SERIALIZATION */
  42. /************************************************************************/
  43. public:
  44. friend class IndexElementDataRTTI;
  45. static RTTITypeBase* getRTTIStatic();
  46. virtual RTTITypeBase* getRTTI() const;
  47. };
  48. MeshData(IndexBuffer::IndexType indexType = IndexBuffer::IT_32BIT);
  49. ~MeshData();
  50. /**
  51. * @brief Allocates a buffer for holding a specified amount of vertex positions, and returns a
  52. * pointer the user can use to populate the buffer. If a previous set of data exists
  53. * in this same stream slot, it will be deleted.
  54. *
  55. * @param numElements Number of elements in the elements array.
  56. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  57. * internally be represented as a single vertex buffer.
  58. */
  59. Vector2* addPositionsVec2(UINT32 numElements, UINT32 streamIdx = 0);
  60. /**
  61. * @brief Allocates a buffer for holding a specified amount of vertex positions, and returns a
  62. * pointer the user can use to populate the buffer. If a previous set of data exists
  63. * in this same stream slot, it will be deleted.
  64. *
  65. * @param numElements Number of elements in the elements array.
  66. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  67. * internally be represented as a single vertex buffer.
  68. */
  69. Vector3* addPositionsVec3(UINT32 numElements, UINT32 streamIdx = 0);
  70. /**
  71. * @brief Allocates a buffer for holding a specified amount of vertex positions, and returns a
  72. * pointer the user can use to populate the buffer. If a previous set of data exists
  73. * in this same stream slot, it will be deleted.
  74. *
  75. * @param numElements Number of elements in the elements array.
  76. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  77. * internally be represented as a single vertex buffer.
  78. */
  79. Vector4* addPositionsVec4(UINT32 numElements, UINT32 streamIdx = 0);
  80. /**
  81. * @brief Allocates a buffer for holding a specified amount of vertex normals, and returns a
  82. * pointer the user can use to populate the buffer. If a previous set of data exists
  83. * in this same stream slot, it will be deleted.
  84. *
  85. * @param numElements Number of elements in the elements array.
  86. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  87. * internally be represented as a single vertex buffer.
  88. */
  89. Vector3* addNormals(UINT32 numElements, UINT32 streamIdx = 0);
  90. /**
  91. * @brief Allocates a buffer for holding a specified amount of vertex tangents, and returns a
  92. * pointer the user can use to populate the buffer. If a previous set of data exists
  93. * in this same stream slot, it will be deleted.
  94. *
  95. * @param numElements Number of elements in the elements array.
  96. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  97. * internally be represented as a single vertex buffer.
  98. */
  99. Vector3* addTangentsVec3(UINT32 numElements, UINT32 streamIdx = 0);
  100. /**
  101. * @brief Allocates a buffer for holding a specified amount of vertex tangents, and returns a
  102. * pointer the user can use to populate the buffer. If a previous set of data exists
  103. * in this same stream slot, it will be deleted.
  104. *
  105. * @param numElements Number of elements in the elements array.
  106. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  107. * internally be represented as a single vertex buffer.
  108. */
  109. Vector4* addTangentsVec4(UINT32 numElements, UINT32 streamIdx = 0);
  110. /**
  111. * @brief Allocates a buffer for holding a specified amount of vertex bitangents, and returns a
  112. * pointer the user can use to populate the buffer. If a previous set of data exists
  113. * in this same stream slot, it will be deleted.
  114. *
  115. * @param numElements Number of elements in the elements array.
  116. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  117. * internally be represented as a single vertex buffer.
  118. */
  119. Vector3* addBitangents(UINT32 numElements, UINT32 streamIdx = 0);
  120. /**
  121. * @brief Allocates a buffer for holding a specified amount of vertex texture coordinates,
  122. * and returns a pointer the user can use to populate the buffer. If a previous set
  123. * of data exists in this same stream slot, it will be deleted.
  124. *
  125. * @param numElements Number of elements in the elements array.
  126. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  127. * internally be represented as a single vertex buffer.
  128. */
  129. Vector2* addUV0(UINT32 numElements, UINT32 streamIdx = 0);
  130. /**
  131. * @brief Allocates a buffer for holding a specified amount of vertex texture coordinates,
  132. * and returns a pointer the user can use to populate the buffer. If a previous set
  133. * of data exists in this same stream slot, it will be deleted.
  134. *
  135. * @param numElements Number of elements in the elements array.
  136. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  137. * internally be represented as a single vertex buffer.
  138. */
  139. Vector2* addUV1(UINT32 numElements, UINT32 streamIdx = 0);
  140. /**
  141. * @brief Allocates a buffer for holding a specified amount of vertex colors, and returns a
  142. * pointer the user can use to populate the buffer. If a previous set of data exists
  143. * in this same stream slot, it will be deleted.
  144. *
  145. * @param numElements Number of elements in the elements array.
  146. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  147. * internally be represented as a single vertex buffer.
  148. */
  149. Color* addColorsFloat(UINT32 numElements, UINT32 streamIdx = 0);
  150. /**
  151. * @brief Allocates a buffer for holding a specified amount of vertex colors, and returns a
  152. * pointer the user can use to populate the buffer. If a previous set of data exists
  153. * in this same stream slot, it will be deleted.
  154. *
  155. * @param numElements Number of elements in the elements array.
  156. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  157. * internally be represented as a single vertex buffer.
  158. */
  159. UINT32* addColorsDWORD(UINT32 numElements, UINT32 streamIdx = 0);
  160. /**
  161. * @brief Allocates a buffer for holding a specified amount of vertex data, and returns a
  162. * pointer the user can use to populate the buffer. Anything that was previously
  163. * present at the same data slot is removed.
  164. *
  165. * @param type Type of the vertex element. Determines size.
  166. * @param semantic Semantic that allows the engine to connect the data to a shader input slot.
  167. * @param numElements Number of elements in the array.
  168. * @param semanticIdx (optional) If there are multiple semantics with the same name, use different index to differentiate between them.
  169. * @param streamIdx (optional) Zero-based index of the stream. Each stream will internally be represented as a single vertex buffer.
  170. */
  171. UINT8* addVertexElementData(VertexElementType type, VertexElementSemantic semantic, UINT32 numElements, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  172. /**
  173. * @brief Allocates buffer for the indices for the specified sub mesh, and returns a
  174. * pointer the user can use to populate the buffer. Any indexes previously
  175. * set for the sub mesh are deleted.
  176. *
  177. * @param numIndices Number of indices.
  178. * @param subMesh (optional) the sub mesh.
  179. */
  180. UINT32* addIndices32(UINT32 numIndices, UINT32 subMesh = 0);
  181. /**
  182. * @brief Allocates buffer for the indices for the specified sub mesh, and returns a
  183. * pointer the user can use to populate the buffer. Any indexes previously
  184. * set for the sub mesh are deleted.
  185. *
  186. * @param numIndices Number of indices.
  187. * @param subMesh (optional) the sub mesh.
  188. */
  189. UINT16* addIndices16(UINT32 numIndices, UINT32 subMesh = 0);
  190. /**
  191. * @brief Query if we have vertex data for the specified semantic.
  192. */
  193. bool hasElement(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0) const;
  194. /**
  195. * @brief Creates a new vertex declaration based on set vertex elements.
  196. */
  197. VertexDeclarationPtr createDeclaration() const;
  198. UINT32 getNumSubmeshes() const { return (UINT32)mIndices.size(); }
  199. UINT32 getNumVertices() const;
  200. UINT32 getNumIndices(UINT32 subMesh) const;
  201. UINT16* getIndices16(UINT32 subMesh) const;
  202. UINT32* getIndices32(UINT32 subMesh) const;
  203. vector<VertexElement>::type getVertexElements() const;
  204. MeshData::VertexElementData& getVertElemData(VertexElementType type, VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx);
  205. UINT32 getIndexElementSize()
  206. {
  207. return mIndexType == IndexBuffer::IT_32BIT ? sizeof(UINT32) : sizeof(UINT16);
  208. }
  209. static MeshDataPtr combine(const vector<MeshDataPtr>::type& elements);
  210. private:
  211. friend class Mesh;
  212. vector<IndexElementData>::type mIndices;
  213. map<UINT32, vector<VertexElementData>::type>::type mVertexData;
  214. IndexBuffer::IndexType mIndexType;
  215. void clearIfItExists(VertexElementType type, VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx);
  216. /************************************************************************/
  217. /* SERIALIZATION */
  218. /************************************************************************/
  219. public:
  220. friend class MeshDataRTTI;
  221. static RTTITypeBase* getRTTIStatic();
  222. virtual RTTITypeBase* getRTTI() const;
  223. };
  224. }