ExporterMesh.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 "Exporter.h"
  6. //==============================================================================
  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. struct Header
  41. {
  42. char m_magic[8]; ///< Magic word.
  43. uint32_t m_flags;
  44. uint32_t m_flags2;
  45. Format m_positionsFormat;
  46. Format m_normalsFormat;
  47. Format m_tangentsFormat;
  48. Format m_colorsFormat; ///< Vertex color.
  49. Format m_uvsFormat;
  50. Format m_boneWeightsFormat;
  51. Format m_boneIndicesFormat;
  52. Format m_indicesFormat; ///< Vertex indices.
  53. uint32_t m_totalIndicesCount;
  54. uint32_t m_totalVerticesCount;
  55. uint32_t m_uvsChannelCount;
  56. uint32_t m_subMeshCount;
  57. uint8_t m_padding[32];
  58. };
  59. struct SubMesh
  60. {
  61. uint32_t m_firstIndex = 0;
  62. uint32_t m_indicesCount = 0;
  63. };
  64. struct Vertex
  65. {
  66. float m_position[3];
  67. uint32_t m_normal;
  68. uint32_t m_tangent;
  69. uint16_t m_uv[2];
  70. };
  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. //==============================================================================
  139. union SignedR10G10B10A10
  140. {
  141. struct
  142. {
  143. int m_x: 10;
  144. int m_y: 10;
  145. int m_z: 10;
  146. int m_w: 2;
  147. } m_unpacked;
  148. uint32_t m_packed;
  149. };
  150. //==============================================================================
  151. uint32_t toR10G10B10A2Sint(float r, float g, float b, float a)
  152. {
  153. SignedR10G10B10A10 out;
  154. out.m_unpacked.m_x = int(round(r * 511.0));
  155. out.m_unpacked.m_y = int(round(g * 511.0));
  156. out.m_unpacked.m_z = int(round(b * 511.0));
  157. out.m_unpacked.m_w = int(round(a * 1.0));
  158. return out.m_packed;
  159. }
  160. //==============================================================================
  161. void Exporter::exportMesh(
  162. const aiMesh& mesh,
  163. const aiMatrix4x4* transform) const
  164. {
  165. std::string name = mesh.mName.C_Str();
  166. std::fstream file;
  167. LOGI("Exporting mesh %s", name.c_str());
  168. // Open file
  169. file.open(m_outputDirectory + name + ".ankimesh",
  170. std::ios::out | std::ios::binary);
  171. Header header;
  172. memset(&header, 0, sizeof(header));
  173. // Checks
  174. if(mesh.mNumFaces == 0)
  175. {
  176. ERROR("Incorrect face number");
  177. }
  178. if(mesh.mVertices == 0)
  179. {
  180. ERROR("Incorrect vertex count number");
  181. }
  182. if(!mesh.HasPositions())
  183. {
  184. ERROR("Missing positions");
  185. }
  186. if(!mesh.HasNormals())
  187. {
  188. ERROR("Missing normals");
  189. }
  190. if(!mesh.HasTangentsAndBitangents())
  191. {
  192. ERROR("Missing tangents");
  193. }
  194. if(!mesh.HasTextureCoords(0))
  195. {
  196. ERROR("Missing UVs");
  197. }
  198. // Write header
  199. static const char* magic = "ANKIMES2";
  200. memcpy(&header.m_magic, magic, 8);
  201. header.m_positionsFormat.m_components = ComponentFormat::R32G32B32;
  202. header.m_positionsFormat.m_transform = FormatTransform::FLOAT;
  203. header.m_normalsFormat.m_components = ComponentFormat::R10G10B10A2;
  204. header.m_normalsFormat.m_transform = FormatTransform::SNORM;
  205. header.m_tangentsFormat.m_components = ComponentFormat::R10G10B10A2;
  206. header.m_tangentsFormat.m_transform = FormatTransform::SNORM;
  207. header.m_uvsFormat.m_components = ComponentFormat::R16G16;
  208. header.m_uvsFormat.m_transform = FormatTransform::FLOAT;
  209. header.m_indicesFormat.m_components = ComponentFormat::R16;
  210. header.m_indicesFormat.m_transform = FormatTransform::UINT;
  211. header.m_totalIndicesCount = mesh.mNumFaces * 3;
  212. header.m_totalVerticesCount = mesh.mNumVertices;
  213. header.m_uvsChannelCount = 1;
  214. header.m_subMeshCount = 1;
  215. file.write(reinterpret_cast<char*>(&header), sizeof(header));
  216. // Write sub meshes
  217. SubMesh smesh;
  218. smesh.m_firstIndex = 0;
  219. smesh.m_indicesCount = header.m_totalIndicesCount;
  220. file.write(reinterpret_cast<char*>(&smesh), sizeof(smesh));
  221. // Write indices
  222. for(unsigned i = 0; i < mesh.mNumFaces; i++)
  223. {
  224. const aiFace& face = mesh.mFaces[i];
  225. if(face.mNumIndices != 3)
  226. {
  227. ERROR("For some reason assimp returned wrong number of verts "
  228. "for a face (face.mNumIndices=%d). Probably degenerates in "
  229. "input file", face.mNumIndices);
  230. }
  231. for(unsigned j = 0; j < 3; j++)
  232. {
  233. uint32_t index32 = face.mIndices[j];
  234. if(index32 > 0xFFFF)
  235. {
  236. ERROR("Index too big");
  237. }
  238. uint16_t index = index32;
  239. file.write(reinterpret_cast<char*>(&index), sizeof(index));
  240. }
  241. }
  242. // Write vertices
  243. aiMatrix3x3 normalMat;
  244. if(transform)
  245. {
  246. normalMat = aiMatrix3x3(*transform);
  247. }
  248. for(unsigned i = 0; i < mesh.mNumVertices; i++)
  249. {
  250. aiVector3D pos = mesh.mVertices[i];
  251. aiVector3D n = mesh.mNormals[i];
  252. aiVector3D t = mesh.mTangents[i];
  253. aiVector3D b = mesh.mBitangents[i];
  254. const aiVector3D& uv = mesh.mTextureCoords[0][i];
  255. if(transform)
  256. {
  257. pos = (*transform) * pos;
  258. n = normalMat * n;
  259. t = normalMat * t;
  260. b = normalMat * b;
  261. }
  262. if(m_flipyz)
  263. {
  264. static const aiMatrix4x4 toLefthanded(
  265. 1, 0, 0, 0,
  266. 0, 0, 1, 0,
  267. 0, -1, 0, 0,
  268. 0, 0, 0, 1);
  269. pos = toLefthanded * pos;
  270. n = toLefthanded * n;
  271. t = toLefthanded * t;
  272. b = toLefthanded * b;
  273. }
  274. Vertex vert;
  275. // Position
  276. vert.m_position[0] = pos[0];
  277. vert.m_position[1] = pos[1];
  278. vert.m_position[2] = pos[2];
  279. // Normal
  280. vert.m_normal = toR10G10B10A2Sint(n[0], n[1], n[2], 0.0);
  281. // Tangent
  282. float w = ((n ^ t) * b < 0.0) ? 1.0 : -1.0;
  283. vert.m_tangent = toR10G10B10A2Sint(t[0], t[1], t[2], w);
  284. // Tex coords
  285. vert.m_uv[0] = toF16(uv[0]);
  286. vert.m_uv[1] = toF16(uv[1]);
  287. // Write
  288. file.write(reinterpret_cast<char*>(&vert), sizeof(vert));
  289. }
  290. }