MeshLoader.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/resource/Common.h>
  7. #include <anki/Math.h>
  8. #include <anki/util/Enum.h>
  9. namespace anki
  10. {
  11. /// @addtogroup resource
  12. /// @{
  13. /// Mesh data. This class loads the mesh file and the Mesh class loads it to
  14. /// the GPU.
  15. class MeshLoader
  16. {
  17. public:
  18. /// Type of the components.
  19. enum class ComponentFormat : U32
  20. {
  21. NONE,
  22. R8,
  23. R8G8,
  24. R8G8B8,
  25. R8G8B8A8,
  26. R16,
  27. R16G16,
  28. R16G16B16,
  29. R16G16B16A16,
  30. R32,
  31. R32G32,
  32. R32G32B32,
  33. R32G32B32A32,
  34. R10G10B10A2,
  35. COUNT
  36. };
  37. enum class FormatTransform : U32
  38. {
  39. NONE,
  40. UNORM,
  41. SNORM,
  42. UINT,
  43. SINT,
  44. FLOAT,
  45. COUNT
  46. };
  47. struct Format
  48. {
  49. ComponentFormat m_components = ComponentFormat::NONE;
  50. FormatTransform m_transform = FormatTransform::NONE;
  51. };
  52. enum class Flag : U32
  53. {
  54. NONE = 0,
  55. QUADS = 1 << 0
  56. };
  57. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(Flag, friend);
  58. struct Header
  59. {
  60. Array<U8, 8> m_magic; ///< Magic word.
  61. U32 m_flags;
  62. U32 m_flags2;
  63. Format m_positionsFormat;
  64. Format m_normalsFormat;
  65. Format m_tangentsFormat;
  66. Format m_colorsFormat; ///< Vertex color.
  67. Format m_uvsFormat;
  68. Format m_boneWeightsFormat;
  69. Format m_boneIndicesFormat;
  70. Format m_indicesFormat; ///< Vertex indices.
  71. U32 m_totalIndicesCount;
  72. U32 m_totalVerticesCount;
  73. /// Number of UV sets. Eg one for normal diffuse and another for
  74. /// lightmaps.
  75. U32 m_uvsChannelCount;
  76. U32 m_subMeshCount;
  77. U8 m_padding[32];
  78. };
  79. static_assert(sizeof(Header) == 128, "Check size of struct");
  80. class SubMesh
  81. {
  82. public:
  83. U32 m_firstIndex = 0;
  84. U32 m_indicesCount = 0;
  85. };
  86. MeshLoader(ResourceManager* manager)
  87. : m_manager(manager)
  88. {
  89. }
  90. ~MeshLoader();
  91. ANKI_USE_RESULT Error load(const ResourceFilename& filename);
  92. const Header& getHeader() const
  93. {
  94. ANKI_ASSERT(isLoaded());
  95. return m_header;
  96. }
  97. const U8* getVertexData() const
  98. {
  99. ANKI_ASSERT(isLoaded());
  100. return &m_verts[0];
  101. }
  102. PtrSize getVertexDataSize() const
  103. {
  104. ANKI_ASSERT(isLoaded());
  105. return m_verts.getSizeInBytes();
  106. }
  107. PtrSize getVertexSize() const
  108. {
  109. ANKI_ASSERT(isLoaded());
  110. return m_vertSize;
  111. }
  112. const U8* getIndexData() const
  113. {
  114. ANKI_ASSERT(isLoaded());
  115. return &m_indices[0];
  116. }
  117. PtrSize getIndexDataSize() const
  118. {
  119. ANKI_ASSERT(isLoaded());
  120. return m_indices.getSizeInBytes();
  121. }
  122. Bool hasBoneInfo() const
  123. {
  124. ANKI_ASSERT(isLoaded());
  125. return m_header.m_boneWeightsFormat.m_components
  126. != ComponentFormat::NONE;
  127. }
  128. private:
  129. template<typename T>
  130. using MDArray = DArray<T>;
  131. ResourceManager* m_manager;
  132. Header m_header;
  133. MDArray<U8> m_verts;
  134. MDArray<U8> m_indices;
  135. MDArray<SubMesh> m_subMeshes;
  136. U8 m_vertSize = 0;
  137. Bool isLoaded() const
  138. {
  139. return m_verts.getSize() > 0;
  140. }
  141. static ANKI_USE_RESULT Error checkFormat(
  142. const Format& fmt, const CString& attrib, Bool cannotBeEmpty);
  143. };
  144. /// @}
  145. } // end namespace anki