CmMeshData.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 Provides a convenient way of setting mesh positions.
  52. *
  53. * @param elements Pointer to pre-allocated array of positions. Any previous position data will be deleted.
  54. * @param numElements Number of elements in the elements array.
  55. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  56. * internally be represented as a single vertex buffer.
  57. *
  58. * @note MeshData will take ownership of the provided memory, and will delete it upon
  59. * destruction or when you replace it with other data.
  60. */
  61. void setPositions(Vector2* elements, UINT32 numElements, UINT32 streamIdx = 0);
  62. /**
  63. * @brief Provides a convenient way of setting mesh positions.
  64. *
  65. * @param elements Pointer to pre-allocated array of positions. Any previous position data will be deleted.
  66. * @param numElements Number of elements in the elements array.
  67. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  68. * internally be represented as a single vertex buffer.
  69. *
  70. * @note MeshData will take ownership of the provided memory, and will delete it upon
  71. * destruction or when you replace it with other data.
  72. */
  73. void setPositions(Vector3* elements, UINT32 numElements, UINT32 streamIdx = 0);
  74. /**
  75. * @brief Provides a convenient way of setting mesh positions.
  76. *
  77. * @param elements Pointer to pre-allocated array of positions. Any previous position data will be deleted.
  78. * @param numElements Number of elements in the elements array.
  79. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  80. * internally be represented as a single vertex buffer.
  81. *
  82. * @note MeshData will take ownership of the provided memory, and will delete it upon
  83. * destruction or when you replace it with other data.
  84. *
  85. */
  86. void setPositions(Vector4* elements, UINT32 numElements, UINT32 streamIdx = 0);
  87. /**
  88. * @brief Provides a convenient way of setting mesh normals.
  89. *
  90. * @param elements Pointer to pre-allocated array of normals. Any previous normal data will be deleted.
  91. * @param numElements Number of elements in the elements array.
  92. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  93. * internally be represented as a single vertex buffer.
  94. *
  95. * @note MeshData will take ownership of the provided memory, and will delete it upon
  96. * destruction or when you replace it with other data.
  97. */
  98. void setNormals(Vector3* elements, UINT32 numElements, UINT32 streamIdx = 0);
  99. /**
  100. * @brief Provides a convenient way of setting mesh tangents.
  101. *
  102. * @param elements Pointer to pre-allocated array of tangents. Any previous tangent data will be deleted.
  103. * @param numElements Number of elements in the elements array.
  104. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  105. * internally be represented as a single vertex buffer.
  106. *
  107. * @note MeshData will take ownership of the provided memory, and will delete it upon
  108. * destruction or when you replace it with other data.
  109. */
  110. void setTangents(Vector3* elements, UINT32 numElements, UINT32 streamIdx = 0);
  111. /**
  112. * @brief Provides a convenient way of setting mesh tangents.
  113. *
  114. * @param elements Pointer to pre-allocated array of tangents. Any previous tangent data will be deleted.
  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. * @note MeshData will take ownership of the provided memory, and will delete it upon
  120. * destruction or when you replace it with other data.
  121. */
  122. void setTangents(Vector4* elements, UINT32 numElements, UINT32 streamIdx = 0);
  123. /**
  124. * @brief Provides a convenient way of setting mesh bitangents.
  125. *
  126. * @param elements Pointer to pre-allocated array of bitangents. Any previous bitangent data will be deleted.
  127. * @param numElements Number of elements in the elements array.
  128. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  129. * internally be represented as a single vertex buffer.
  130. *
  131. * @note MeshData will take ownership of the provided memory, and will delete it upon
  132. * destruction or when you replace it with other data.
  133. */
  134. void setBitangents(Vector3* elements, UINT32 numElements, UINT32 streamIdx = 0);
  135. /**
  136. * @brief Provides a convenient way of setting mesh texture coordinates.
  137. *
  138. * @param elements Pointer to pre-allocated array of texture coordinates. Any previous uv0 data will be deleted.
  139. * @param numElements Number of elements in the elements array.
  140. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  141. * internally be represented as a single vertex buffer.
  142. *
  143. * @note MeshData will take ownership of the provided memory, and will delete it upon
  144. * destruction or when you replace it with other data.
  145. */
  146. void setUV0(Vector2* elements, UINT32 numElements, UINT32 streamIdx = 0);
  147. /**
  148. * @brief Provides a convenient way of setting mesh texture coordinates.
  149. *
  150. * @param elements Pointer to pre-allocated array of texture coordinates. Any previous uv1 data will be deleted.
  151. * @param numElements Number of elements in the elements array.
  152. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  153. * internally be represented as a single vertex buffer.
  154. *
  155. * @note MeshData will take ownership of the provided memory, and will delete it upon
  156. * destruction or when you replace it with other data.
  157. */
  158. void setUV1(Vector2* elements, UINT32 numElements, UINT32 streamIdx = 0);
  159. /**
  160. * @brief Provides a convenient way of setting mesh colors.
  161. *
  162. * @param elements Pointer to pre-allocated array of colors. Any previous color data will be deleted.
  163. * @param numElements Number of elements in the elements array.
  164. * @param streamIdx (optional) Zero-based index of the stream. Each stream will
  165. * internally be represented as a single vertex buffer.
  166. *
  167. * @note MeshData will take ownership of the provided memory, and will delete it upon
  168. * destruction or when you replace it with other data.
  169. */
  170. void setColors(Color* elements, UINT32 numElements, UINT32 streamIdx = 0);
  171. /**
  172. * @brief Adds (or replaces) a new set of vertex element data. Anything that was previously
  173. * present at the same data slot is removed.
  174. *
  175. * @param type Type of the vertex element. Determines size.
  176. * @param semantic Semantic that allows the engine to connect the data to a shader input slot.
  177. * @param elements Allocated array of elements. Total size should be size of element type * number of elements. Any previous data at the same slot will be deleted.
  178. * @param numElements Number of elements in the array.
  179. * @param semanticIdx (optional) If there are multiple semantics with the same name, use different index to differentiate between them.
  180. * @param streamIdx (optional) Zero-based index of the stream. Each stream will internally be represented as a single vertex buffer.
  181. */
  182. void setVertexElementData(VertexElementType type, VertexElementSemantic semantic, UINT8* elements, UINT32 numElements, UINT32 semanticIdx = 0, UINT32 streamIdx = 0);
  183. /**
  184. * @brief Sets a list of indices for the specified sub mesh. Any indexes previously
  185. * set for the sub mesh are deleted.
  186. *
  187. * @param indices If non-null, the indices.
  188. * @param numIndices Number of indices.
  189. * @param subMesh (optional) the sub mesh.
  190. */
  191. void setIndices(UINT32* indices, UINT32 numIndices, UINT32 subMesh = 0);
  192. /**
  193. * @brief Sets a list of indices for the specified sub mesh. Any indexes previously
  194. * set for the sub mesh are deleted.
  195. *
  196. * @param indices If non-null, the indices.
  197. * @param numIndices Number of indices.
  198. * @param subMesh (optional) the sub mesh.
  199. */
  200. void setIndices(UINT16* indices, UINT32 numIndices, UINT32 subMesh = 0);
  201. /**
  202. * @brief Query if we have vertex data for the specified semantic.
  203. */
  204. bool hasElement(VertexElementSemantic semantic, UINT32 semanticIdx = 0, UINT32 streamIdx = 0) const;
  205. /**
  206. * @brief Creates a new vertex declaration based on set vertex elements.
  207. */
  208. VertexDeclarationPtr createDeclaration() const;
  209. UINT32 getNumSubmeshes() const { return (UINT32)mIndices.size(); }
  210. UINT32 getNumVertices() const;
  211. UINT32 getNumIndices(UINT32 subMesh) const;
  212. UINT16* getIndices16(UINT32 subMesh) const;
  213. UINT32* getIndices32(UINT32 subMesh) const;
  214. vector<VertexElement>::type getVertexElements() const;
  215. MeshData::VertexElementData& getVertElemData(VertexElementType type, VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx);
  216. UINT32 getIndexElementSize()
  217. {
  218. return mIndexType == IndexBuffer::IT_32BIT ? sizeof(UINT32) : sizeof(UINT16);
  219. }
  220. static MeshDataPtr combine(const vector<MeshDataPtr>::type& elements);
  221. private:
  222. friend class Mesh;
  223. vector<IndexElementData>::type mIndices;
  224. map<UINT32, vector<VertexElementData>::type>::type mVertexData;
  225. IndexBuffer::IndexType mIndexType;
  226. void clearIfItExists(VertexElementType type, VertexElementSemantic semantic, UINT32 semanticIdx, UINT32 streamIdx);
  227. /************************************************************************/
  228. /* SERIALIZATION */
  229. /************************************************************************/
  230. public:
  231. friend class MeshDataRTTI;
  232. static RTTITypeBase* getRTTIStatic();
  233. virtual RTTITypeBase* getRTTI() const;
  234. };
  235. }