ExporterMesh.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 = (maxPositionDistance < 2.0) ? anki::Format::R16G16B16_SFLOAT : anki::Format::R32G32B32_SFLOAT;
  156. posa.m_relativeOffset = 0;
  157. posa.m_scale = 1.0;
  158. // Normals
  159. auto& na = header.m_vertexAttributes[anki::VertexAttributeLocation::NORMAL];
  160. na.m_bufferBinding = 1;
  161. na.m_format = anki::Format::A2B10G10R10_SNORM_PACK32;
  162. na.m_relativeOffset = 0;
  163. na.m_scale = 1.0;
  164. // Tangents
  165. auto& ta = header.m_vertexAttributes[anki::VertexAttributeLocation::TANGENT];
  166. ta.m_bufferBinding = 1;
  167. ta.m_format = anki::Format::A2B10G10R10_SNORM_PACK32;
  168. ta.m_relativeOffset = sizeof(uint32_t);
  169. ta.m_scale = 1.0;
  170. // UVs
  171. auto& uva = header.m_vertexAttributes[anki::VertexAttributeLocation::UV];
  172. uva.m_bufferBinding = 1;
  173. if(minUvDistance >= 0.0 && maxUvDistance <= 1.0)
  174. {
  175. uva.m_format = anki::Format::R16G16_UNORM;
  176. }
  177. else
  178. {
  179. uva.m_format = anki::Format::R16G16_SFLOAT;
  180. }
  181. uva.m_relativeOffset = sizeof(uint32_t) * 2;
  182. uva.m_scale = 1.0;
  183. // Bone weight
  184. if(hasBoneWeights)
  185. {
  186. auto& bidxa = header.m_vertexAttributes[anki::VertexAttributeLocation::BONE_INDICES];
  187. bidxa.m_bufferBinding = 2;
  188. bidxa.m_format = anki::Format::R16G16B16A16_UINT;
  189. bidxa.m_relativeOffset = 0;
  190. bidxa.m_scale = 1.0;
  191. auto& wa = header.m_vertexAttributes[anki::VertexAttributeLocation::BONE_WEIGHTS];
  192. wa.m_bufferBinding = 2;
  193. wa.m_format = anki::Format::R8G8B8A8_UNORM;
  194. wa.m_relativeOffset = sizeof(uint16_t) * 4;
  195. wa.m_scale = 1.0;
  196. }
  197. }
  198. // Arange the attributes into vert buffers
  199. {
  200. header.m_vertexBufferCount = 2;
  201. // First buff has positions
  202. const auto& posa = header.m_vertexAttributes[anki::VertexAttributeLocation::POSITION];
  203. if(posa.m_format == anki::Format::R32G32B32_SFLOAT)
  204. {
  205. header.m_vertexBuffers[0].m_vertexStride = sizeof(float) * 3;
  206. }
  207. else
  208. {
  209. header.m_vertexBuffers[0].m_vertexStride = sizeof(uint16_t) * 3;
  210. }
  211. // 2nd buff has normal + tangent + texcoords
  212. header.m_vertexBuffers[1].m_vertexStride = sizeof(uint32_t) * 2 + sizeof(uint16_t) * 2;
  213. // 3rd has bone weights
  214. if(hasBoneWeights)
  215. {
  216. header.m_vertexBuffers[2].m_vertexStride = sizeof(WeightVertex);
  217. ++header.m_vertexBufferCount;
  218. }
  219. }
  220. // Write some other header stuff
  221. {
  222. memcpy(&header.m_magic[0], anki::MeshBinaryFile::MAGIC, 8);
  223. header.m_flags = (vertCountPerFace == 4) ? anki::MeshBinaryFile::Flag::QUAD : anki::MeshBinaryFile::Flag::NONE;
  224. header.m_indexType = anki::IndexType::U16;
  225. header.m_totalIndexCount = mesh.mNumFaces * vertCountPerFace;
  226. header.m_totalVertexCount = mesh.mNumVertices;
  227. header.m_subMeshCount = 1;
  228. header.m_aabbMin = aabbMin;
  229. header.m_aabbMax = aabbMax;
  230. }
  231. // Open file
  232. std::fstream file;
  233. file.open(m_outputDirectory + name + ".ankimesh", std::ios::out | std::ios::binary);
  234. // Write header
  235. file.write(reinterpret_cast<char*>(&header), sizeof(header));
  236. // Write sub meshes
  237. {
  238. anki::MeshBinaryFile::SubMesh smesh;
  239. smesh.m_firstIndex = 0;
  240. smesh.m_indexCount = header.m_totalIndexCount;
  241. smesh.m_aabbMin = aabbMin;
  242. smesh.m_aabbMax = aabbMax;
  243. file.write(reinterpret_cast<char*>(&smesh), sizeof(smesh));
  244. }
  245. // Write indices
  246. for(unsigned i = 0; i < mesh.mNumFaces; i++)
  247. {
  248. const aiFace& face = mesh.mFaces[i];
  249. if(face.mNumIndices != vertCountPerFace)
  250. {
  251. ERROR("For some reason assimp returned wrong number of verts for a face (face.mNumIndices=%d). Probably"
  252. "degenerates in input file",
  253. face.mNumIndices);
  254. }
  255. for(unsigned j = 0; j < vertCountPerFace; j++)
  256. {
  257. uint32_t index32 = face.mIndices[j];
  258. if(index32 > 0xFFFF)
  259. {
  260. ERROR("Index too big");
  261. }
  262. uint16_t index = index32;
  263. file.write(reinterpret_cast<char*>(&index), sizeof(index));
  264. }
  265. }
  266. // Write first vert buffer
  267. {
  268. const auto& posa = header.m_vertexAttributes[anki::VertexAttributeLocation::POSITION];
  269. if(posa.m_format == anki::Format::R32G32B32_SFLOAT)
  270. {
  271. file.write(reinterpret_cast<char*>(&positions[0]), positions.size() * sizeof(positions[0]));
  272. }
  273. else if(posa.m_format == anki::Format::R16G16B16_SFLOAT)
  274. {
  275. std::vector<uint16_t> pos16;
  276. pos16.resize(positions.size());
  277. for(unsigned i = 0; i < positions.size(); ++i)
  278. {
  279. pos16[i] = anki::F16(positions[i]).toU16();
  280. }
  281. file.write(reinterpret_cast<char*>(&pos16[0]), pos16.size() * sizeof(pos16[0]));
  282. }
  283. else
  284. {
  285. assert(0);
  286. }
  287. }
  288. // Write 2nd vert buffer
  289. {
  290. struct Vert
  291. {
  292. uint32_t m_n;
  293. uint32_t m_t;
  294. uint16_t m_uv[2];
  295. };
  296. std::vector<Vert> verts;
  297. verts.resize(mesh.mNumVertices);
  298. for(unsigned i = 0; i < mesh.mNumVertices; ++i)
  299. {
  300. const auto& inVert = ntVerts[i];
  301. verts[i].m_n = anki::packColorToR10G10B10A2SNorm(inVert.m_n[0], inVert.m_n[1], inVert.m_n[2], 0.0);
  302. verts[i].m_t =
  303. anki::packColorToR10G10B10A2SNorm(inVert.m_t[0], inVert.m_t[1], inVert.m_t[2], inVert.m_t[3]);
  304. const float uv[2] = {inVert.m_uv[0], inVert.m_uv[1]};
  305. const anki::Format uvfmt = header.m_vertexAttributes[anki::VertexAttributeLocation::UV].m_format;
  306. if(uvfmt == anki::Format::R16G16_UNORM)
  307. {
  308. assert(uv[0] <= 1.0 && uv[0] >= 0.0 && uv[1] <= 1.0 && uv[1] >= 0.0);
  309. verts[i].m_uv[0] = uv[0] * 0xFFFF;
  310. verts[i].m_uv[1] = uv[1] * 0xFFFF;
  311. }
  312. else if(uvfmt == anki::Format::R16G16_SFLOAT)
  313. {
  314. verts[i].m_uv[0] = anki::F16(uv[0]).toU16();
  315. verts[i].m_uv[1] = anki::F16(uv[1]).toU16();
  316. }
  317. else
  318. {
  319. assert(0);
  320. }
  321. }
  322. file.write(reinterpret_cast<char*>(&verts[0]), verts.size() * sizeof(verts[0]));
  323. }
  324. // Write 3rd vert buffer
  325. if(hasBoneWeights)
  326. {
  327. file.write(reinterpret_cast<char*>(&bweights[0]), bweights.size() * sizeof(bweights[0]));
  328. }
  329. }