ExporterMesh.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright (C) 2009-2018, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "Exporter.h"
  6. #include <anki/resource/MeshLoader.h>
  7. #include <anki/Math.h>
  8. #include <cmath>
  9. #include <cfloat>
  10. void Exporter::exportMesh(const aiMesh& mesh, const aiMatrix4x4* transform, unsigned vertCountPerFace) const
  11. {
  12. std::string name = mesh.mName.C_Str();
  13. LOGI("Exporting mesh %s", name.c_str());
  14. const bool hasBoneWeights = mesh.mNumBones > 0;
  15. anki::MeshBinaryFile::Header header;
  16. memset(&header, 0, sizeof(header));
  17. // Checks
  18. if(mesh.mNumFaces == 0)
  19. {
  20. ERROR("Incorrect face number");
  21. }
  22. if(mesh.mVertices == 0)
  23. {
  24. ERROR("Incorrect vertex count number");
  25. }
  26. if(!mesh.HasPositions())
  27. {
  28. ERROR("Missing positions");
  29. }
  30. if(!mesh.HasNormals())
  31. {
  32. ERROR("Missing normals");
  33. }
  34. if(!mesh.HasTangentsAndBitangents())
  35. {
  36. ERROR("Missing tangents");
  37. }
  38. if(!mesh.HasTextureCoords(0))
  39. {
  40. ERROR("Missing UVs");
  41. }
  42. //
  43. // Gather the attributes
  44. //
  45. struct WeightVertex
  46. {
  47. uint16_t m_boneIndices[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
  48. uint8_t m_weights[4] = {0, 0, 0, 0};
  49. };
  50. std::vector<WeightVertex> bweights;
  51. std::vector<float> positions;
  52. struct NTVertex
  53. {
  54. float m_n[3];
  55. float m_t[4];
  56. float m_uv[2];
  57. };
  58. std::vector<NTVertex> ntVerts;
  59. float maxPositionDistance = 0.0; // Distance of positions from zero
  60. float maxUvDistance = -FLT_MAX, minUvDistance = FLT_MAX;
  61. anki::Vec3 aabbMin(anki::MAX_F32), aabbMax(anki::MIN_F32);
  62. {
  63. const aiMatrix3x3 normalMat = (transform) ? aiMatrix3x3(*transform) : aiMatrix3x3();
  64. const unsigned vertCount = mesh.mNumVertices;
  65. positions.resize(vertCount * 3);
  66. ntVerts.resize(vertCount);
  67. for(unsigned i = 0; i < vertCount; i++)
  68. {
  69. aiVector3D pos = mesh.mVertices[i];
  70. aiVector3D n = mesh.mNormals[i];
  71. aiVector3D t = mesh.mTangents[i];
  72. aiVector3D b = mesh.mBitangents[i];
  73. const aiVector3D& uv = mesh.mTextureCoords[0][i];
  74. if(transform)
  75. {
  76. pos = (*transform) * pos;
  77. n = normalMat * n;
  78. t = normalMat * t;
  79. b = normalMat * b;
  80. }
  81. if(m_flipyz)
  82. {
  83. static const aiMatrix4x4 toLefthanded(1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1);
  84. pos = toLefthanded * pos;
  85. n = toLefthanded * n;
  86. t = toLefthanded * t;
  87. b = toLefthanded * b;
  88. }
  89. positions[i * 3 + 0] = pos.x;
  90. positions[i * 3 + 1] = pos.y;
  91. positions[i * 3 + 2] = pos.z;
  92. for(int d = 0; d < 3; ++d)
  93. {
  94. maxPositionDistance = std::max<float>(maxPositionDistance, fabs(pos[d]));
  95. aabbMin[d] = std::min(aabbMin[d], pos[d]);
  96. aabbMax[d] = std::max(aabbMax[d], pos[d]);
  97. }
  98. ntVerts[i].m_n[0] = n.x;
  99. ntVerts[i].m_n[1] = n.y;
  100. ntVerts[i].m_n[2] = n.z;
  101. ntVerts[i].m_t[0] = t.x;
  102. ntVerts[i].m_t[1] = t.y;
  103. ntVerts[i].m_t[2] = t.z;
  104. ntVerts[i].m_t[3] = ((n ^ t) * b < 0.0) ? 1.0 : -1.0;
  105. ntVerts[i].m_uv[0] = uv.x;
  106. ntVerts[i].m_uv[1] = uv.y;
  107. maxUvDistance = std::max(maxUvDistance, std::max(uv.x, uv.y));
  108. minUvDistance = std::min(minUvDistance, std::min(uv.x, uv.y));
  109. }
  110. if(hasBoneWeights)
  111. {
  112. bweights.resize(vertCount);
  113. for(unsigned i = 0; i < mesh.mNumBones; ++i)
  114. {
  115. const aiBone& bone = *mesh.mBones[i];
  116. for(unsigned j = 0; j < bone.mNumWeights; ++j)
  117. {
  118. const aiVertexWeight& aiWeight = bone.mWeights[j];
  119. assert(aiWeight.mVertexId < bweights.size());
  120. WeightVertex& vert = bweights[aiWeight.mVertexId];
  121. unsigned idx;
  122. if(vert.m_boneIndices[0] == 0xFFFF)
  123. {
  124. idx = 0;
  125. }
  126. else if(vert.m_boneIndices[1] == 0xFFFF)
  127. {
  128. idx = 1;
  129. }
  130. else if(vert.m_boneIndices[2] == 0xFFFF)
  131. {
  132. idx = 2;
  133. }
  134. else if(vert.m_boneIndices[3] == 0xFFFF)
  135. {
  136. idx = 3;
  137. }
  138. else
  139. {
  140. ERROR("Vertex has more than 4 bone weights");
  141. }
  142. vert.m_boneIndices[idx] = i;
  143. vert.m_weights[idx] = aiWeight.mWeight * 0xFF;
  144. }
  145. }
  146. }
  147. // Bump aabbMax a bit
  148. aabbMax += anki::EPSILON * 10.0f;
  149. }
  150. // Chose the formats of the attributes
  151. {
  152. // Positions
  153. auto& posa = header.m_vertexAttributes[anki::VertexAttributeLocation::POSITION];
  154. posa.m_bufferBinding = 0;
  155. posa.m_format =
  156. (maxPositionDistance < 2.0) ? anki::Format::R16G16B16A16_SFLOAT : anki::Format::R32G32B32_SFLOAT;
  157. posa.m_relativeOffset = 0;
  158. posa.m_scale = 1.0;
  159. // Normals
  160. auto& na = header.m_vertexAttributes[anki::VertexAttributeLocation::NORMAL];
  161. na.m_bufferBinding = 1;
  162. na.m_format = anki::Format::A2B10G10R10_SNORM_PACK32;
  163. na.m_relativeOffset = 0;
  164. na.m_scale = 1.0;
  165. // Tangents
  166. auto& ta = header.m_vertexAttributes[anki::VertexAttributeLocation::TANGENT];
  167. ta.m_bufferBinding = 1;
  168. ta.m_format = anki::Format::A2B10G10R10_SNORM_PACK32;
  169. ta.m_relativeOffset = sizeof(uint32_t);
  170. ta.m_scale = 1.0;
  171. // UVs
  172. auto& uva = header.m_vertexAttributes[anki::VertexAttributeLocation::UV];
  173. uva.m_bufferBinding = 1;
  174. if(minUvDistance >= 0.0 && maxUvDistance <= 1.0)
  175. {
  176. uva.m_format = anki::Format::R16G16_UNORM;
  177. }
  178. else
  179. {
  180. uva.m_format = anki::Format::R16G16_SFLOAT;
  181. }
  182. uva.m_relativeOffset = sizeof(uint32_t) * 2;
  183. uva.m_scale = 1.0;
  184. // Bone weight
  185. if(hasBoneWeights)
  186. {
  187. auto& bidxa = header.m_vertexAttributes[anki::VertexAttributeLocation::BONE_INDICES];
  188. bidxa.m_bufferBinding = 2;
  189. bidxa.m_format = anki::Format::R16G16B16A16_UINT;
  190. bidxa.m_relativeOffset = 0;
  191. bidxa.m_scale = 1.0;
  192. auto& wa = header.m_vertexAttributes[anki::VertexAttributeLocation::BONE_WEIGHTS];
  193. wa.m_bufferBinding = 2;
  194. wa.m_format = anki::Format::R8G8B8A8_UNORM;
  195. wa.m_relativeOffset = sizeof(uint16_t) * 4;
  196. wa.m_scale = 1.0;
  197. }
  198. }
  199. // Arange the attributes into vert buffers
  200. {
  201. header.m_vertexBufferCount = 2;
  202. // First buff has positions
  203. const auto& posa = header.m_vertexAttributes[anki::VertexAttributeLocation::POSITION];
  204. if(posa.m_format == anki::Format::R32G32B32_SFLOAT)
  205. {
  206. header.m_vertexBuffers[0].m_vertexStride = sizeof(float) * 3;
  207. }
  208. else if(posa.m_format == anki::Format::R16G16B16A16_SFLOAT)
  209. {
  210. header.m_vertexBuffers[0].m_vertexStride = sizeof(uint16_t) * 4;
  211. }
  212. else
  213. {
  214. assert(0);
  215. }
  216. // 2nd buff has normal + tangent + texcoords
  217. header.m_vertexBuffers[1].m_vertexStride = sizeof(uint32_t) * 2 + sizeof(uint16_t) * 2;
  218. // 3rd has bone weights
  219. if(hasBoneWeights)
  220. {
  221. header.m_vertexBuffers[2].m_vertexStride = sizeof(WeightVertex);
  222. ++header.m_vertexBufferCount;
  223. }
  224. }
  225. // Write some other header stuff
  226. {
  227. memcpy(&header.m_magic[0], anki::MeshBinaryFile::MAGIC, 8);
  228. header.m_flags = (vertCountPerFace == 4) ? anki::MeshBinaryFile::Flag::QUAD : anki::MeshBinaryFile::Flag::NONE;
  229. header.m_indexType = anki::IndexType::U16;
  230. header.m_totalIndexCount = mesh.mNumFaces * vertCountPerFace;
  231. header.m_totalVertexCount = mesh.mNumVertices;
  232. header.m_subMeshCount = 1;
  233. header.m_aabbMin = aabbMin;
  234. header.m_aabbMax = aabbMax;
  235. }
  236. // Open file
  237. std::fstream file;
  238. file.open(m_outputDirectory + name + ".ankimesh", std::ios::out | std::ios::binary);
  239. // Write header
  240. file.write(reinterpret_cast<char*>(&header), sizeof(header));
  241. // Write sub meshes
  242. {
  243. anki::MeshBinaryFile::SubMesh smesh;
  244. smesh.m_firstIndex = 0;
  245. smesh.m_indexCount = header.m_totalIndexCount;
  246. smesh.m_aabbMin = aabbMin;
  247. smesh.m_aabbMax = aabbMax;
  248. file.write(reinterpret_cast<char*>(&smesh), sizeof(smesh));
  249. }
  250. // Write indices
  251. for(unsigned i = 0; i < mesh.mNumFaces; i++)
  252. {
  253. const aiFace& face = mesh.mFaces[i];
  254. if(face.mNumIndices != vertCountPerFace)
  255. {
  256. ERROR("For some reason assimp returned wrong number of verts for a face (face.mNumIndices=%d). Probably"
  257. "degenerates in input file",
  258. face.mNumIndices);
  259. }
  260. for(unsigned j = 0; j < vertCountPerFace; j++)
  261. {
  262. uint32_t index32 = face.mIndices[j];
  263. if(index32 > 0xFFFF)
  264. {
  265. ERROR("Index too big");
  266. }
  267. uint16_t index = index32;
  268. file.write(reinterpret_cast<char*>(&index), sizeof(index));
  269. }
  270. }
  271. // Write first vert buffer
  272. {
  273. const auto& posa = header.m_vertexAttributes[anki::VertexAttributeLocation::POSITION];
  274. if(posa.m_format == anki::Format::R32G32B32_SFLOAT)
  275. {
  276. file.write(reinterpret_cast<char*>(&positions[0]), positions.size() * sizeof(positions[0]));
  277. }
  278. else if(posa.m_format == anki::Format::R16G16B16A16_SFLOAT)
  279. {
  280. std::vector<uint16_t> pos16;
  281. pos16.resize(mesh.mNumVertices * 4);
  282. const float* p32 = &positions[0];
  283. const float* p32end = p32 + positions.size();
  284. uint16_t* p16 = &pos16[0];
  285. while(p32 != p32end)
  286. {
  287. p16[0] = anki::F16(p32[0]).toU16();
  288. p16[1] = anki::F16(p32[1]).toU16();
  289. p16[2] = anki::F16(p32[2]).toU16();
  290. p16[3] = anki::F16(0.0f).toU16();
  291. p32 += 3;
  292. p16 += 4;
  293. }
  294. file.write(reinterpret_cast<char*>(&pos16[0]), pos16.size() * sizeof(pos16[0]));
  295. }
  296. else
  297. {
  298. assert(0);
  299. }
  300. }
  301. // Write 2nd vert buffer
  302. {
  303. struct Vert
  304. {
  305. uint32_t m_n;
  306. uint32_t m_t;
  307. uint16_t m_uv[2];
  308. };
  309. std::vector<Vert> verts;
  310. verts.resize(mesh.mNumVertices);
  311. for(unsigned i = 0; i < mesh.mNumVertices; ++i)
  312. {
  313. const auto& inVert = ntVerts[i];
  314. verts[i].m_n = anki::packColorToR10G10B10A2SNorm(inVert.m_n[0], inVert.m_n[1], inVert.m_n[2], 0.0);
  315. verts[i].m_t =
  316. anki::packColorToR10G10B10A2SNorm(inVert.m_t[0], inVert.m_t[1], inVert.m_t[2], inVert.m_t[3]);
  317. const float uv[2] = {inVert.m_uv[0], inVert.m_uv[1]};
  318. const anki::Format uvfmt = header.m_vertexAttributes[anki::VertexAttributeLocation::UV].m_format;
  319. if(uvfmt == anki::Format::R16G16_UNORM)
  320. {
  321. assert(uv[0] <= 1.0 && uv[0] >= 0.0 && uv[1] <= 1.0 && uv[1] >= 0.0);
  322. verts[i].m_uv[0] = uv[0] * 0xFFFF;
  323. verts[i].m_uv[1] = uv[1] * 0xFFFF;
  324. }
  325. else if(uvfmt == anki::Format::R16G16_SFLOAT)
  326. {
  327. verts[i].m_uv[0] = anki::F16(uv[0]).toU16();
  328. verts[i].m_uv[1] = anki::F16(uv[1]).toU16();
  329. }
  330. else
  331. {
  332. assert(0);
  333. }
  334. }
  335. file.write(reinterpret_cast<char*>(&verts[0]), verts.size() * sizeof(verts[0]));
  336. }
  337. // Write 3rd vert buffer
  338. if(hasBoneWeights)
  339. {
  340. file.write(reinterpret_cast<char*>(&bweights[0]), bweights.size() * sizeof(bweights[0]));
  341. }
  342. }