MeshLoader.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "anki/resource/MeshLoader.h"
  6. #include "anki/util/File.h"
  7. namespace anki {
  8. //==============================================================================
  9. MeshLoader::~MeshLoader()
  10. {
  11. // WARNING: Watch the order of deallocation. Reverse of the deallocation to
  12. // have successful cleanups
  13. m_verts.destroy(m_alloc);
  14. m_indices.destroy(m_alloc);
  15. m_subMeshes.destroy(m_alloc);
  16. }
  17. //==============================================================================
  18. Error MeshLoader::load(
  19. GenericMemoryPoolAllocator<U8> alloc,
  20. const CString& filename)
  21. {
  22. m_alloc = alloc;
  23. Error err = loadInternal(filename);
  24. if(err)
  25. {
  26. ANKI_LOGE("Failed to load mesh %s", &filename[0]);
  27. }
  28. return err;
  29. }
  30. //==============================================================================
  31. Error MeshLoader::loadInternal(const CString& filename)
  32. {
  33. Error err = ErrorCode::NONE;
  34. File file;
  35. err = file.open(filename,
  36. File::OpenFlag::READ | File::OpenFlag::BINARY
  37. | File::OpenFlag::LITTLE_ENDIAN);
  38. if(err) return err;
  39. // Load header
  40. err = file.read(&m_header, sizeof(m_header));
  41. if(err) return err;
  42. //
  43. // Check header
  44. //
  45. if(memcmp(&m_header.m_magic[0], "ANKIMES2", 8) != 0)
  46. {
  47. ANKI_LOGE("Wrong magic word");
  48. return ErrorCode::USER_DATA;
  49. }
  50. if(checkFormat(m_header.m_positionsFormat, "positions", true)
  51. || checkFormat(m_header.m_normalsFormat, "normals", true)
  52. || checkFormat(m_header.m_tangentsFormat, "tangents", true)
  53. || checkFormat(m_header.m_colorsFormat, "colors", false)
  54. || checkFormat(m_header.m_uvsFormat, "UVs", true)
  55. || checkFormat(m_header.m_boneWeightsFormat, "bone weights", false)
  56. || checkFormat(m_header.m_boneIndicesFormat, "bone ids", false)
  57. || checkFormat(m_header.m_indicesFormat, "indices format", true))
  58. {
  59. return ErrorCode::USER_DATA;
  60. }
  61. // Check positions
  62. if(m_header.m_positionsFormat.m_components != ComponentFormat::R32G32B32
  63. || m_header.m_positionsFormat.m_transform != FormatTransform::FLOAT)
  64. {
  65. ANKI_LOGE("Incorrect/unsupported positions format");
  66. return ErrorCode::USER_DATA;
  67. }
  68. // Check normals
  69. if(m_header.m_normalsFormat.m_components != ComponentFormat::R10G10B10A2
  70. || m_header.m_normalsFormat.m_transform != FormatTransform::SNORM)
  71. {
  72. ANKI_LOGE("Incorrect/unsupported normals format");
  73. return ErrorCode::USER_DATA;
  74. }
  75. // Check tangents
  76. if(m_header.m_tangentsFormat.m_components != ComponentFormat::R10G10B10A2
  77. || m_header.m_tangentsFormat.m_transform != FormatTransform::SNORM)
  78. {
  79. ANKI_LOGE("Incorrect/unsupported tangents format");
  80. return ErrorCode::USER_DATA;
  81. }
  82. // Check colors
  83. if(m_header.m_colorsFormat.m_components != ComponentFormat::NONE
  84. || m_header.m_colorsFormat.m_transform != FormatTransform::NONE)
  85. {
  86. ANKI_LOGE("Incorrect/unsupported color format");
  87. return ErrorCode::USER_DATA;
  88. }
  89. // Check UVs
  90. if(m_header.m_uvsFormat.m_components != ComponentFormat::R16G16
  91. || m_header.m_uvsFormat.m_transform != FormatTransform::FLOAT)
  92. {
  93. ANKI_LOGE("Incorrect/unsupported UVs format");
  94. return ErrorCode::USER_DATA;
  95. }
  96. Bool hasBoneInfo = false;
  97. if(m_header.m_boneWeightsFormat.m_components != ComponentFormat::NONE)
  98. {
  99. // Has bone info
  100. hasBoneInfo = true;
  101. // Bone weights
  102. if(m_header.m_boneWeightsFormat.m_components
  103. != ComponentFormat::R8G8B8A8
  104. || m_header.m_boneWeightsFormat.m_transform
  105. != FormatTransform::UNORM)
  106. {
  107. ANKI_LOGE("Incorrect/unsupported UVs format");
  108. return ErrorCode::USER_DATA;
  109. }
  110. // Bone indices
  111. if(m_header.m_boneIndicesFormat.m_components
  112. != ComponentFormat::R16G16B16A16
  113. || m_header.m_boneIndicesFormat.m_transform
  114. != FormatTransform::UINT)
  115. {
  116. ANKI_LOGE("Incorrect/unsupported UVs format");
  117. return ErrorCode::USER_DATA;
  118. }
  119. }
  120. else
  121. {
  122. // No bone info
  123. // Bone weights
  124. if(m_header.m_boneWeightsFormat.m_components
  125. != ComponentFormat::NONE
  126. || m_header.m_boneWeightsFormat.m_transform
  127. != FormatTransform::NONE)
  128. {
  129. ANKI_LOGE("Incorrect/unsupported UVs format");
  130. return ErrorCode::USER_DATA;
  131. }
  132. // Bone indices
  133. if(m_header.m_boneIndicesFormat.m_components
  134. != ComponentFormat::NONE
  135. || m_header.m_boneIndicesFormat.m_transform
  136. != FormatTransform::NONE)
  137. {
  138. ANKI_LOGE("Incorrect/unsupported UVs format");
  139. return ErrorCode::USER_DATA;
  140. }
  141. }
  142. // Check indices
  143. if(m_header.m_totalIndicesCount < 3
  144. || (m_header.m_totalIndicesCount % 3) != 0
  145. || m_header.m_totalIndicesCount > MAX_U16
  146. || m_header.m_indicesFormat.m_components != ComponentFormat::R16
  147. || m_header.m_indicesFormat.m_transform != FormatTransform::UINT)
  148. {
  149. // Only 16bit indices are supported for now
  150. ANKI_LOGE("Incorrect/unsuported index info");
  151. return ErrorCode::USER_DATA;
  152. }
  153. // Check other
  154. if(m_header.m_totalVerticesCount == 0)
  155. {
  156. ANKI_LOGE("Incorrect/unsuported vertex count");
  157. return ErrorCode::USER_DATA;
  158. }
  159. if(m_header.m_uvsChannelCount != 1)
  160. {
  161. ANKI_LOGE("Incorrect/unsuported UVs channel count");
  162. return ErrorCode::USER_DATA;
  163. }
  164. if(m_header.m_subMeshCount == 0)
  165. {
  166. ANKI_LOGE("Incorrect/unsuported submesh count");
  167. return ErrorCode::USER_DATA;
  168. }
  169. //
  170. // Read submesh info
  171. //
  172. err = m_subMeshes.create(m_alloc, m_header.m_subMeshCount);
  173. if(err) return err;
  174. err = file.read(&m_subMeshes[0], m_subMeshes.getSizeInBytes());
  175. if(err) return err;
  176. // Checks
  177. U idxSum = 0;
  178. for(U i = 0; i < m_subMeshes.getSize(); i++)
  179. {
  180. const SubMesh& sm = m_subMeshes[0];
  181. if(sm.m_firstIndex != idxSum
  182. || sm.m_indicesCount < 3)
  183. {
  184. ANKI_LOGE("Incorrect sub mesh info");
  185. return ErrorCode::USER_DATA;
  186. }
  187. idxSum += sm.m_indicesCount;
  188. }
  189. if(idxSum != m_header.m_totalIndicesCount)
  190. {
  191. ANKI_LOGE("Incorrect sub mesh info");
  192. return ErrorCode::USER_DATA;
  193. }
  194. //
  195. // Read indices
  196. //
  197. err = m_indices.create(m_alloc, m_header.m_totalIndicesCount * sizeof(U16));
  198. if(err) return err;
  199. err = file.read(&m_indices[0], m_indices.getSizeInBytes());
  200. if(err) return err;
  201. //
  202. // Read vertices
  203. //
  204. m_vertSize =
  205. 3 * sizeof(F32) // pos
  206. + 1 * sizeof(U32) // norm
  207. + 1 * sizeof(U32) // tang
  208. + 2 * sizeof(U16) // uvs
  209. + ((hasBoneInfo) ? (4 * sizeof(U8) + 4 * sizeof(U16)) : 0);
  210. err = m_verts.create(m_alloc, m_header.m_totalVerticesCount * m_vertSize);
  211. if(err) return err;
  212. err = file.read(&m_verts[0], m_verts.getSizeInBytes());
  213. if(err) return err;
  214. return err;
  215. }
  216. //==============================================================================
  217. Error MeshLoader::checkFormat(
  218. const Format& fmt,
  219. const CString& attrib,
  220. Bool cannotBeEmpty)
  221. {
  222. if(fmt.m_components >= ComponentFormat::COUNT)
  223. {
  224. ANKI_LOGE("Incorrect component format for %s", &attrib[0]);
  225. return ErrorCode::USER_DATA;
  226. }
  227. if(fmt.m_transform >= FormatTransform::COUNT)
  228. {
  229. ANKI_LOGE("Incorrect format transform for %s", &attrib[0]);
  230. return ErrorCode::USER_DATA;
  231. }
  232. if(cannotBeEmpty)
  233. {
  234. if(fmt.m_components == ComponentFormat::NONE)
  235. {
  236. ANKI_LOGE("Format cannot be zero for %s", &attrib[0]);
  237. return ErrorCode::USER_DATA;
  238. }
  239. if(fmt.m_transform == FormatTransform::NONE)
  240. {
  241. ANKI_LOGE("Transform cannot be zero for %s", &attrib[0]);
  242. return ErrorCode::USER_DATA;
  243. }
  244. }
  245. return ErrorCode::NONE;
  246. }
  247. } // end namespace anki