ExporterMesh.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // Copyright (C) 2009-2017, 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 <cmath>
  7. enum class ComponentFormat : uint32_t
  8. {
  9. NONE,
  10. R8,
  11. R8G8,
  12. R8G8B8,
  13. R8G8B8A8,
  14. R16,
  15. R16G16,
  16. R16G16B16,
  17. R16G16B16A16,
  18. R32,
  19. R32G32,
  20. R32G32B32,
  21. R32G32B32A32,
  22. R10G10B10A2,
  23. COUNT
  24. };
  25. enum class FormatTransform : uint32_t
  26. {
  27. NONE,
  28. UNORM,
  29. SNORM,
  30. UINT,
  31. SINT,
  32. FLOAT,
  33. COUNT
  34. };
  35. struct Format
  36. {
  37. ComponentFormat m_components = ComponentFormat::NONE;
  38. FormatTransform m_transform = FormatTransform::NONE;
  39. };
  40. const uint32_t FLAG_QUADS = 1;
  41. struct Header
  42. {
  43. char m_magic[8]; ///< Magic word.
  44. uint32_t m_flags;
  45. uint32_t m_flags2;
  46. Format m_positionsFormat;
  47. Format m_normalsFormat;
  48. Format m_tangentsFormat;
  49. Format m_colorsFormat; ///< Vertex color.
  50. Format m_uvsFormat;
  51. Format m_boneWeightsFormat;
  52. Format m_boneIndicesFormat;
  53. Format m_indicesFormat; ///< Vertex indices.
  54. uint32_t m_totalIndicesCount;
  55. uint32_t m_totalVerticesCount;
  56. uint32_t m_uvsChannelCount;
  57. uint32_t m_subMeshCount;
  58. uint8_t m_padding[32];
  59. };
  60. struct SubMesh
  61. {
  62. uint32_t m_firstIndex = 0;
  63. uint32_t m_indicesCount = 0;
  64. };
  65. struct Vertex
  66. {
  67. float m_position[3];
  68. uint16_t m_uv[2];
  69. uint32_t m_normal;
  70. uint32_t m_tangent;
  71. };
  72. static uint16_t toF16(float f)
  73. {
  74. union Val32
  75. {
  76. int32_t i;
  77. float f;
  78. uint32_t u;
  79. };
  80. uint16_t out;
  81. Val32 v32;
  82. v32.f = f;
  83. int32_t i = v32.i;
  84. int32_t s = (i >> 16) & 0x00008000;
  85. int32_t e = ((i >> 23) & 0x000000ff) - (127 - 15);
  86. int32_t m = i & 0x007fffff;
  87. if(e <= 0)
  88. {
  89. if(e < -10)
  90. {
  91. out = 0;
  92. }
  93. else
  94. {
  95. m = (m | 0x00800000) >> (1 - e);
  96. if(m & 0x00001000)
  97. {
  98. m += 0x00002000;
  99. }
  100. out = s | (m >> 13);
  101. }
  102. }
  103. else if(e == 0xff - (127 - 15))
  104. {
  105. if(m == 0)
  106. {
  107. out = s | 0x7c00;
  108. }
  109. else
  110. {
  111. m >>= 13;
  112. out = s | 0x7c00 | m | (m == 0);
  113. }
  114. }
  115. else
  116. {
  117. if(m & 0x00001000)
  118. {
  119. m += 0x00002000;
  120. if(m & 0x00800000)
  121. {
  122. m = 0;
  123. e += 1;
  124. }
  125. }
  126. if(e > 30)
  127. {
  128. assert(0 && "Overflow");
  129. out = s | 0x7c00;
  130. }
  131. else
  132. {
  133. out = s | (e << 10) | (m >> 13);
  134. }
  135. }
  136. return out;
  137. }
  138. union SignedR10G10B10A10
  139. {
  140. struct
  141. {
  142. int m_x : 10;
  143. int m_y : 10;
  144. int m_z : 10;
  145. int m_w : 2;
  146. } m_unpacked;
  147. uint32_t m_packed;
  148. };
  149. uint32_t toR10G10B10A2Sint(float r, float g, float b, float a)
  150. {
  151. SignedR10G10B10A10 out;
  152. out.m_unpacked.m_x = int(round(r * 511.0));
  153. out.m_unpacked.m_y = int(round(g * 511.0));
  154. out.m_unpacked.m_z = int(round(b * 511.0));
  155. out.m_unpacked.m_w = int(round(a * 1.0));
  156. return out.m_packed;
  157. }
  158. void Exporter::exportMesh(const aiMesh& mesh, const aiMatrix4x4* transform, unsigned vertCountPerFace) const
  159. {
  160. std::string name = mesh.mName.C_Str();
  161. std::fstream file;
  162. LOGI("Exporting mesh %s", name.c_str());
  163. // Open file
  164. file.open(m_outputDirectory + name + ".ankimesh", std::ios::out | std::ios::binary);
  165. Header header;
  166. memset(&header, 0, sizeof(header));
  167. // Checks
  168. if(mesh.mNumFaces == 0)
  169. {
  170. ERROR("Incorrect face number");
  171. }
  172. if(mesh.mVertices == 0)
  173. {
  174. ERROR("Incorrect vertex count number");
  175. }
  176. if(!mesh.HasPositions())
  177. {
  178. ERROR("Missing positions");
  179. }
  180. if(!mesh.HasNormals())
  181. {
  182. ERROR("Missing normals");
  183. }
  184. if(!mesh.HasTangentsAndBitangents())
  185. {
  186. ERROR("Missing tangents");
  187. }
  188. if(!mesh.HasTextureCoords(0))
  189. {
  190. ERROR("Missing UVs");
  191. }
  192. // Write header
  193. static const char* magic = "ANKIMES3";
  194. memcpy(&header.m_magic, magic, 8);
  195. if(vertCountPerFace == 4)
  196. {
  197. header.m_flags = FLAG_QUADS;
  198. }
  199. header.m_positionsFormat.m_components = ComponentFormat::R32G32B32;
  200. header.m_positionsFormat.m_transform = FormatTransform::FLOAT;
  201. header.m_normalsFormat.m_components = ComponentFormat::R10G10B10A2;
  202. header.m_normalsFormat.m_transform = FormatTransform::SNORM;
  203. header.m_tangentsFormat.m_components = ComponentFormat::R10G10B10A2;
  204. header.m_tangentsFormat.m_transform = FormatTransform::SNORM;
  205. header.m_uvsFormat.m_components = ComponentFormat::R16G16;
  206. header.m_uvsFormat.m_transform = FormatTransform::FLOAT;
  207. header.m_indicesFormat.m_components = ComponentFormat::R16;
  208. header.m_indicesFormat.m_transform = FormatTransform::UINT;
  209. header.m_totalIndicesCount = mesh.mNumFaces * vertCountPerFace;
  210. header.m_totalVerticesCount = mesh.mNumVertices;
  211. header.m_uvsChannelCount = 1;
  212. header.m_subMeshCount = 1;
  213. file.write(reinterpret_cast<char*>(&header), sizeof(header));
  214. // Write sub meshes
  215. SubMesh smesh;
  216. smesh.m_firstIndex = 0;
  217. smesh.m_indicesCount = header.m_totalIndicesCount;
  218. file.write(reinterpret_cast<char*>(&smesh), sizeof(smesh));
  219. // Write indices
  220. for(unsigned i = 0; i < mesh.mNumFaces; i++)
  221. {
  222. const aiFace& face = mesh.mFaces[i];
  223. if(face.mNumIndices != vertCountPerFace)
  224. {
  225. ERROR("For some reason assimp returned wrong number of verts "
  226. "for a face (face.mNumIndices=%d). Probably degenerates in "
  227. "input file",
  228. face.mNumIndices);
  229. }
  230. for(unsigned j = 0; j < vertCountPerFace; j++)
  231. {
  232. uint32_t index32 = face.mIndices[j];
  233. if(index32 > 0xFFFF)
  234. {
  235. ERROR("Index too big");
  236. }
  237. uint16_t index = index32;
  238. file.write(reinterpret_cast<char*>(&index), sizeof(index));
  239. }
  240. }
  241. // Write vertices
  242. aiMatrix3x3 normalMat;
  243. if(transform)
  244. {
  245. normalMat = aiMatrix3x3(*transform);
  246. }
  247. for(unsigned i = 0; i < mesh.mNumVertices; i++)
  248. {
  249. aiVector3D pos = mesh.mVertices[i];
  250. aiVector3D n = mesh.mNormals[i];
  251. aiVector3D t = mesh.mTangents[i];
  252. aiVector3D b = mesh.mBitangents[i];
  253. const aiVector3D& uv = mesh.mTextureCoords[0][i];
  254. if(transform)
  255. {
  256. pos = (*transform) * pos;
  257. n = normalMat * n;
  258. t = normalMat * t;
  259. b = normalMat * b;
  260. }
  261. if(m_flipyz)
  262. {
  263. static const aiMatrix4x4 toLefthanded(1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1);
  264. pos = toLefthanded * pos;
  265. n = toLefthanded * n;
  266. t = toLefthanded * t;
  267. b = toLefthanded * b;
  268. }
  269. Vertex vert;
  270. // Position
  271. vert.m_position[0] = pos[0];
  272. vert.m_position[1] = pos[1];
  273. vert.m_position[2] = pos[2];
  274. // Tex coords
  275. vert.m_uv[0] = toF16(uv[0]);
  276. vert.m_uv[1] = toF16(uv[1]);
  277. // Normal
  278. vert.m_normal = toR10G10B10A2Sint(n[0], n[1], n[2], 0.0);
  279. // Tangent
  280. float w = ((n ^ t) * b < 0.0) ? 1.0 : -1.0;
  281. vert.m_tangent = toR10G10B10A2Sint(t[0], t[1], t[2], w);
  282. // Write
  283. file.write(reinterpret_cast<char*>(&vert), sizeof(vert));
  284. }
  285. }