ExporterMesh.cpp 6.4 KB

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