ExporterMesh.cpp 6.6 KB

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