ExporterMesh.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. || !mesh.HasNormals()
  184. || !mesh.HasTangentsAndBitangents()
  185. || !mesh.HasTextureCoords(0))
  186. {
  187. ERROR("Missing attribute");
  188. }
  189. // Write header
  190. static const char* magic = "ANKIMES2";
  191. memcpy(&header.m_magic, magic, 8);
  192. header.m_positionsFormat.m_components = ComponentFormat::R32G32B32;
  193. header.m_positionsFormat.m_transform = FormatTransform::FLOAT;
  194. header.m_normalsFormat.m_components = ComponentFormat::R10G10B10A2;
  195. header.m_normalsFormat.m_transform = FormatTransform::SNORM;
  196. header.m_tangentsFormat.m_components = ComponentFormat::R10G10B10A2;
  197. header.m_tangentsFormat.m_transform = FormatTransform::SNORM;
  198. header.m_uvsFormat.m_components = ComponentFormat::R16G16;
  199. header.m_uvsFormat.m_transform = FormatTransform::FLOAT;
  200. header.m_indicesFormat.m_components = ComponentFormat::R16;
  201. header.m_indicesFormat.m_transform = FormatTransform::UINT;
  202. header.m_totalIndicesCount = mesh.mNumFaces * 3;
  203. header.m_totalVerticesCount = mesh.mNumVertices;
  204. header.m_uvsChannelCount = 1;
  205. header.m_subMeshCount = 1;
  206. file.write(reinterpret_cast<char*>(&header), sizeof(header));
  207. // Write sub meshes
  208. SubMesh smesh;
  209. smesh.m_firstIndex = 0;
  210. smesh.m_indicesCount = header.m_totalIndicesCount;
  211. file.write(reinterpret_cast<char*>(&smesh), sizeof(smesh));
  212. // Write indices
  213. for(unsigned i = 0; i < mesh.mNumFaces; i++)
  214. {
  215. const aiFace& face = mesh.mFaces[i];
  216. if(face.mNumIndices != 3)
  217. {
  218. ERROR("For some reason assimp returned wrong number of verts "
  219. "for a face (face.mNumIndices=%d). Probably degenerates in "
  220. "input file", face.mNumIndices);
  221. }
  222. for(unsigned j = 0; j < 3; j++)
  223. {
  224. uint32_t index32 = face.mIndices[j];
  225. if(index32 > 0xFFFF)
  226. {
  227. ERROR("Index too big");
  228. }
  229. uint16_t index = index32;
  230. file.write(reinterpret_cast<char*>(&index), sizeof(index));
  231. }
  232. }
  233. // Write vertices
  234. aiMatrix3x3 normalMat;
  235. if(transform)
  236. {
  237. normalMat = aiMatrix3x3(*transform);
  238. }
  239. for(unsigned i = 0; i < mesh.mNumVertices; i++)
  240. {
  241. aiVector3D pos = mesh.mVertices[i];
  242. aiVector3D n = mesh.mNormals[i];
  243. aiVector3D t = mesh.mTangents[i];
  244. aiVector3D b = mesh.mBitangents[i];
  245. const aiVector3D& uv = mesh.mTextureCoords[0][i];
  246. if(transform)
  247. {
  248. pos = (*transform) * pos;
  249. n = normalMat * n;
  250. t = normalMat * t;
  251. b = normalMat * b;
  252. }
  253. if(m_flipyz)
  254. {
  255. static const aiMatrix4x4 toLefthanded(
  256. 1, 0, 0, 0,
  257. 0, 0, 1, 0,
  258. 0, -1, 0, 0,
  259. 0, 0, 0, 1);
  260. pos = toLefthanded * pos;
  261. n = toLefthanded * n;
  262. t = toLefthanded * t;
  263. b = toLefthanded * b;
  264. }
  265. Vertex vert;
  266. // Position
  267. vert.m_position[0] = pos[0];
  268. vert.m_position[1] = pos[1];
  269. vert.m_position[2] = pos[2];
  270. // Normal
  271. vert.m_normal = toR10G10B10A2Sint(n[0], n[1], n[2], 0.0);
  272. // Tangent
  273. float w = ((n ^ t) * b < 0.0) ? 1.0 : -1.0;
  274. vert.m_tangent = toR10G10B10A2Sint(t[0], t[1], t[2], w);
  275. // Tex coords
  276. vert.m_uv[0] = toF16(uv[0]);
  277. vert.m_uv[1] = toF16(uv[1]);
  278. // Write
  279. file.write(reinterpret_cast<char*>(&vert), sizeof(vert));
  280. }
  281. }