MeshLoader.h 2.8 KB

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