AssbinFileWriter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file AssbinFileWriter.cpp
  34. * @brief Implementation of Assbin file writer.
  35. */
  36. #include "AssbinFileWriter.h"
  37. #include "Common/assbin_chunks.h"
  38. #include "PostProcessing/ProcessHelper.h"
  39. #include <assimp/Exceptional.h>
  40. #include <assimp/version.h>
  41. #include <assimp/IOStream.hpp>
  42. #include "zlib.h"
  43. #include <ctime>
  44. #if _MSC_VER
  45. #pragma warning(push)
  46. #pragma warning(disable : 4706)
  47. #endif // _MSC_VER
  48. namespace Assimp {
  49. template <typename T>
  50. size_t Write(IOStream *stream, const T &v) {
  51. return stream->Write(&v, sizeof(T), 1);
  52. }
  53. // -----------------------------------------------------------------------------------
  54. // Serialize an aiString
  55. template <>
  56. inline size_t Write<aiString>(IOStream *stream, const aiString &s) {
  57. const size_t s2 = (uint32_t)s.length;
  58. stream->Write(&s, 4, 1);
  59. stream->Write(s.data, s2, 1);
  60. return s2 + 4;
  61. }
  62. // -----------------------------------------------------------------------------------
  63. // Serialize an unsigned int as uint32_t
  64. template <>
  65. inline size_t Write<unsigned int>(IOStream *stream, const unsigned int &w) {
  66. const uint32_t t = (uint32_t)w;
  67. if (w > t) {
  68. // this shouldn't happen, integers in Assimp data structures never exceed 2^32
  69. throw DeadlyExportError("loss of data due to 64 -> 32 bit integer conversion");
  70. }
  71. stream->Write(&t, 4, 1);
  72. return 4;
  73. }
  74. // -----------------------------------------------------------------------------------
  75. // Serialize an unsigned int as uint16_t
  76. template <>
  77. inline size_t Write<uint16_t>(IOStream *stream, const uint16_t &w) {
  78. static_assert(sizeof(uint16_t) == 2, "sizeof(uint16_t)==2");
  79. stream->Write(&w, 2, 1);
  80. return 2;
  81. }
  82. // -----------------------------------------------------------------------------------
  83. // Serialize a float
  84. template <>
  85. inline size_t Write<float>(IOStream *stream, const float &f) {
  86. static_assert(sizeof(float) == 4, "sizeof(float)==4");
  87. stream->Write(&f, 4, 1);
  88. return 4;
  89. }
  90. // -----------------------------------------------------------------------------------
  91. // Serialize a double
  92. template <>
  93. inline size_t Write<double>(IOStream *stream, const double &f) {
  94. static_assert(sizeof(double) == 8, "sizeof(double)==8");
  95. stream->Write(&f, 8, 1);
  96. return 8;
  97. }
  98. // -----------------------------------------------------------------------------------
  99. // Serialize a vec3
  100. template <>
  101. inline size_t Write<aiVector3D>(IOStream *stream, const aiVector3D &v) {
  102. size_t t = Write<ai_real>(stream, v.x);
  103. t += Write<float>(stream, v.y);
  104. t += Write<float>(stream, v.z);
  105. return t;
  106. }
  107. // -----------------------------------------------------------------------------------
  108. // Serialize a color value
  109. template <>
  110. inline size_t Write<aiColor3D>(IOStream *stream, const aiColor3D &v) {
  111. size_t t = Write<ai_real>(stream, v.r);
  112. t += Write<float>(stream, v.g);
  113. t += Write<float>(stream, v.b);
  114. return t;
  115. }
  116. // -----------------------------------------------------------------------------------
  117. // Serialize a color value
  118. template <>
  119. inline size_t Write<aiColor4D>(IOStream *stream, const aiColor4D &v) {
  120. size_t t = Write<ai_real>(stream, v.r);
  121. t += Write<float>(stream, v.g);
  122. t += Write<float>(stream, v.b);
  123. t += Write<float>(stream, v.a);
  124. return t;
  125. }
  126. // -----------------------------------------------------------------------------------
  127. // Serialize a quaternion
  128. template <>
  129. inline size_t Write<aiQuaternion>(IOStream *stream, const aiQuaternion &v) {
  130. size_t t = Write<ai_real>(stream, v.w);
  131. t += Write<float>(stream, v.x);
  132. t += Write<float>(stream, v.y);
  133. t += Write<float>(stream, v.z);
  134. ai_assert(t == 16);
  135. return t;
  136. }
  137. // -----------------------------------------------------------------------------------
  138. // Serialize a vertex weight
  139. template <>
  140. inline size_t Write<aiVertexWeight>(IOStream *stream, const aiVertexWeight &v) {
  141. size_t t = Write<unsigned int>(stream, v.mVertexId);
  142. return t + Write<float>(stream, v.mWeight);
  143. }
  144. constexpr size_t MatrixSize = 64;
  145. // -----------------------------------------------------------------------------------
  146. // Serialize a mat4x4
  147. template <>
  148. inline size_t Write<aiMatrix4x4>(IOStream *stream, const aiMatrix4x4 &m) {
  149. for (unsigned int i = 0; i < 4; ++i) {
  150. for (unsigned int i2 = 0; i2 < 4; ++i2) {
  151. Write<ai_real>(stream, m[i][i2]);
  152. }
  153. }
  154. return MatrixSize;
  155. }
  156. // -----------------------------------------------------------------------------------
  157. // Serialize an aiVectorKey
  158. template <>
  159. inline size_t Write<aiVectorKey>(IOStream *stream, const aiVectorKey &v) {
  160. const size_t t = Write<double>(stream, v.mTime);
  161. return t + Write<aiVector3D>(stream, v.mValue);
  162. }
  163. // -----------------------------------------------------------------------------------
  164. // Serialize an aiQuatKey
  165. template <>
  166. inline size_t Write<aiQuatKey>(IOStream *stream, const aiQuatKey &v) {
  167. const size_t t = Write<double>(stream, v.mTime);
  168. return t + Write<aiQuaternion>(stream, v.mValue);
  169. }
  170. template <typename T>
  171. inline size_t WriteBounds(IOStream *stream, const T *in, unsigned int size) {
  172. T minc, maxc;
  173. ArrayBounds(in, size, minc, maxc);
  174. const size_t t = Write<T>(stream, minc);
  175. return t + Write<T>(stream, maxc);
  176. }
  177. // We use this to write out non-byte arrays so that we write using the specializations.
  178. // This way we avoid writing out extra bytes that potentially come from struct alignment.
  179. template <typename T>
  180. inline size_t WriteArray(IOStream *stream, const T *in, unsigned int size) {
  181. size_t n = 0;
  182. for (unsigned int i = 0; i < size; i++)
  183. n += Write<T>(stream, in[i]);
  184. return n;
  185. }
  186. // ----------------------------------------------------------------------------------
  187. /** @class AssbinChunkWriter
  188. * @brief Chunk writer mechanism for the .assbin file structure
  189. *
  190. * This is a standard in-memory IOStream (most of the code is based on BlobIOStream),
  191. * the difference being that this takes another IOStream as a "container" in the
  192. * constructor, and when it is destroyed, it appends the magic number, the chunk size,
  193. * and the chunk contents to the container stream. This allows relatively easy chunk
  194. * chunk construction, even recursively.
  195. */
  196. class AssbinChunkWriter : public IOStream {
  197. private:
  198. uint8_t *buffer;
  199. uint32_t magic;
  200. IOStream *container;
  201. size_t cur_size, cursor, initial;
  202. private:
  203. // -------------------------------------------------------------------
  204. void Grow(size_t need = 0) {
  205. size_t new_size = std::max(initial, std::max(need, cur_size + (cur_size >> 1)));
  206. const uint8_t *const old = buffer;
  207. buffer = new uint8_t[new_size];
  208. if (old) {
  209. memcpy(buffer, old, cur_size);
  210. delete[] old;
  211. }
  212. cur_size = new_size;
  213. }
  214. public:
  215. AssbinChunkWriter(IOStream *container, uint32_t magic, size_t initial = 4096) :
  216. buffer(nullptr),
  217. magic(magic),
  218. container(container),
  219. cur_size(0),
  220. cursor(0),
  221. initial(initial) {
  222. // empty
  223. }
  224. ~AssbinChunkWriter() override {
  225. if (container) {
  226. container->Write(&magic, sizeof(uint32_t), 1);
  227. container->Write(&cursor, sizeof(uint32_t), 1);
  228. container->Write(buffer, 1, cursor);
  229. }
  230. if (buffer) delete[] buffer;
  231. }
  232. void *GetBufferPointer() { return buffer; }
  233. size_t Read(void * /*pvBuffer*/, size_t /*pSize*/, size_t /*pCount*/) override {
  234. return 0;
  235. }
  236. aiReturn Seek(size_t /*pOffset*/, aiOrigin /*pOrigin*/) override {
  237. return aiReturn_FAILURE;
  238. }
  239. size_t Tell() const override {
  240. return cursor;
  241. }
  242. void Flush() override {
  243. // not implemented
  244. }
  245. size_t FileSize() const override {
  246. return cursor;
  247. }
  248. size_t Write(const void *pvBuffer, size_t pSize, size_t pCount) override {
  249. pSize *= pCount;
  250. if (cursor + pSize > cur_size) {
  251. Grow(cursor + pSize);
  252. }
  253. memcpy(buffer + cursor, pvBuffer, pSize);
  254. cursor += pSize;
  255. return pCount;
  256. }
  257. };
  258. // ----------------------------------------------------------------------------------
  259. /** @class AssbinFileWriter
  260. * @brief Assbin file writer class
  261. *
  262. * This class writes an .assbin file, and is responsible for the file layout.
  263. */
  264. class AssbinFileWriter {
  265. private:
  266. bool shortened;
  267. bool compressed;
  268. protected:
  269. // -----------------------------------------------------------------------------------
  270. void WriteBinaryNode(IOStream *container, const aiNode *node) {
  271. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AINODE);
  272. unsigned int nb_metadata = (node->mMetaData != nullptr ? node->mMetaData->mNumProperties : 0);
  273. Write<aiString>(&chunk, node->mName);
  274. Write<aiMatrix4x4>(&chunk, node->mTransformation);
  275. Write<unsigned int>(&chunk, node->mNumChildren);
  276. Write<unsigned int>(&chunk, node->mNumMeshes);
  277. Write<unsigned int>(&chunk, nb_metadata);
  278. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  279. Write<unsigned int>(&chunk, node->mMeshes[i]);
  280. }
  281. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  282. WriteBinaryNode(&chunk, node->mChildren[i]);
  283. }
  284. for (unsigned int i = 0; i < nb_metadata; ++i) {
  285. const aiString &key = node->mMetaData->mKeys[i];
  286. aiMetadataType type = node->mMetaData->mValues[i].mType;
  287. void *value = node->mMetaData->mValues[i].mData;
  288. Write<aiString>(&chunk, key);
  289. Write<uint16_t>(&chunk, (uint16_t)type);
  290. switch (type) {
  291. case AI_BOOL:
  292. Write<bool>(&chunk, *((bool *)value));
  293. break;
  294. case AI_INT32:
  295. Write<int32_t>(&chunk, *((int32_t *)value));
  296. break;
  297. case AI_UINT64:
  298. Write<uint64_t>(&chunk, *((uint64_t *)value));
  299. break;
  300. case AI_FLOAT:
  301. Write<float>(&chunk, *((float *)value));
  302. break;
  303. case AI_DOUBLE:
  304. Write<double>(&chunk, *((double *)value));
  305. break;
  306. case AI_AISTRING:
  307. Write<aiString>(&chunk, *((aiString *)value));
  308. break;
  309. case AI_AIVECTOR3D:
  310. Write<aiVector3D>(&chunk, *((aiVector3D *)value));
  311. break;
  312. #ifdef SWIG
  313. case FORCE_32BIT:
  314. #endif // SWIG
  315. default:
  316. break;
  317. }
  318. }
  319. }
  320. // -----------------------------------------------------------------------------------
  321. void WriteBinaryTexture(IOStream *container, const aiTexture *tex) {
  322. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AITEXTURE);
  323. Write<unsigned int>(&chunk, tex->mWidth);
  324. Write<unsigned int>(&chunk, tex->mHeight);
  325. // Write the texture format, but don't include the null terminator.
  326. chunk.Write(tex->achFormatHint, sizeof(char), HINTMAXTEXTURELEN - 1);
  327. if (!shortened) {
  328. if (!tex->mHeight) {
  329. chunk.Write(tex->pcData, 1, tex->mWidth);
  330. } else {
  331. chunk.Write(tex->pcData, 1, tex->mWidth * tex->mHeight * 4);
  332. }
  333. }
  334. }
  335. // -----------------------------------------------------------------------------------
  336. void WriteBinaryBone(IOStream *container, const aiBone *b) {
  337. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIBONE);
  338. Write<aiString>(&chunk, b->mName);
  339. Write<unsigned int>(&chunk, b->mNumWeights);
  340. Write<aiMatrix4x4>(&chunk, b->mOffsetMatrix);
  341. // for the moment we write dumb min/max values for the bones, too.
  342. // maybe I'll add a better, hash-like solution later
  343. if (shortened) {
  344. WriteBounds(&chunk, b->mWeights, b->mNumWeights);
  345. } // else write as usual
  346. else
  347. WriteArray<aiVertexWeight>(&chunk, b->mWeights, b->mNumWeights);
  348. }
  349. // -----------------------------------------------------------------------------------
  350. void WriteBinaryMesh(IOStream *container, const aiMesh *mesh) {
  351. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMESH);
  352. Write<unsigned int>(&chunk, mesh->mPrimitiveTypes);
  353. Write<unsigned int>(&chunk, mesh->mNumVertices);
  354. Write<unsigned int>(&chunk, mesh->mNumFaces);
  355. Write<unsigned int>(&chunk, mesh->mNumBones);
  356. Write<unsigned int>(&chunk, mesh->mMaterialIndex);
  357. // first of all, write bits for all existent vertex components
  358. unsigned int c = 0;
  359. if (mesh->mVertices) {
  360. c |= ASSBIN_MESH_HAS_POSITIONS;
  361. }
  362. if (mesh->mNormals) {
  363. c |= ASSBIN_MESH_HAS_NORMALS;
  364. }
  365. if (mesh->mTangents && mesh->mBitangents) {
  366. c |= ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS;
  367. }
  368. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++n) {
  369. if (!mesh->mTextureCoords[n]) {
  370. break;
  371. }
  372. c |= ASSBIN_MESH_HAS_TEXCOORD(n);
  373. }
  374. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++n) {
  375. if (!mesh->mColors[n]) {
  376. break;
  377. }
  378. c |= ASSBIN_MESH_HAS_COLOR(n);
  379. }
  380. Write<unsigned int>(&chunk, c);
  381. aiVector3D minVec, maxVec;
  382. if (mesh->mVertices) {
  383. if (shortened) {
  384. WriteBounds(&chunk, mesh->mVertices, mesh->mNumVertices);
  385. } // else write as usual
  386. else
  387. WriteArray<aiVector3D>(&chunk, mesh->mVertices, mesh->mNumVertices);
  388. }
  389. if (mesh->mNormals) {
  390. if (shortened) {
  391. WriteBounds(&chunk, mesh->mNormals, mesh->mNumVertices);
  392. } // else write as usual
  393. else
  394. WriteArray<aiVector3D>(&chunk, mesh->mNormals, mesh->mNumVertices);
  395. }
  396. if (mesh->mTangents && mesh->mBitangents) {
  397. if (shortened) {
  398. WriteBounds(&chunk, mesh->mTangents, mesh->mNumVertices);
  399. WriteBounds(&chunk, mesh->mBitangents, mesh->mNumVertices);
  400. } // else write as usual
  401. else {
  402. WriteArray<aiVector3D>(&chunk, mesh->mTangents, mesh->mNumVertices);
  403. WriteArray<aiVector3D>(&chunk, mesh->mBitangents, mesh->mNumVertices);
  404. }
  405. }
  406. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS; ++n) {
  407. if (!mesh->mColors[n])
  408. break;
  409. if (shortened) {
  410. WriteBounds(&chunk, mesh->mColors[n], mesh->mNumVertices);
  411. } // else write as usual
  412. else
  413. WriteArray<aiColor4D>(&chunk, mesh->mColors[n], mesh->mNumVertices);
  414. }
  415. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++n) {
  416. if (!mesh->mTextureCoords[n])
  417. break;
  418. // write number of UV components
  419. Write<unsigned int>(&chunk, mesh->mNumUVComponents[n]);
  420. if (shortened) {
  421. WriteBounds(&chunk, mesh->mTextureCoords[n], mesh->mNumVertices);
  422. } // else write as usual
  423. else
  424. WriteArray<aiVector3D>(&chunk, mesh->mTextureCoords[n], mesh->mNumVertices);
  425. }
  426. // write faces. There are no floating-point calculations involved
  427. // in these, so we can write a simple hash over the face data
  428. // to the dump file. We generate a single 32 Bit hash for 512 faces
  429. // using Assimp's standard hashing function.
  430. if (shortened) {
  431. unsigned int processed = 0;
  432. for (unsigned int job; (job = std::min(mesh->mNumFaces - processed, 512u)); processed += job) {
  433. uint32_t hash = 0;
  434. for (unsigned int a = 0; a < job; ++a) {
  435. const aiFace &f = mesh->mFaces[processed + a];
  436. uint32_t tmp = f.mNumIndices;
  437. hash = SuperFastHash(reinterpret_cast<const char *>(&tmp), sizeof tmp, hash);
  438. for (unsigned int i = 0; i < f.mNumIndices; ++i) {
  439. static_assert(AI_MAX_VERTICES <= 0xffffffff, "AI_MAX_VERTICES <= 0xffffffff");
  440. tmp = static_cast<uint32_t>(f.mIndices[i]);
  441. hash = SuperFastHash(reinterpret_cast<const char *>(&tmp), sizeof tmp, hash);
  442. }
  443. }
  444. Write<unsigned int>(&chunk, hash);
  445. }
  446. } else // else write as usual
  447. {
  448. // if there are less than 2^16 vertices, we can simply use 16 bit integers ...
  449. for (unsigned int i = 0; i < mesh->mNumFaces; ++i) {
  450. const aiFace &f = mesh->mFaces[i];
  451. static_assert(AI_MAX_FACE_INDICES <= 0xffff, "AI_MAX_FACE_INDICES <= 0xffff");
  452. Write<uint16_t>(&chunk, static_cast<uint16_t>(f.mNumIndices));
  453. for (unsigned int a = 0; a < f.mNumIndices; ++a) {
  454. if (mesh->mNumVertices < (1u << 16)) {
  455. Write<uint16_t>(&chunk, static_cast<uint16_t>(f.mIndices[a]));
  456. } else {
  457. Write<unsigned int>(&chunk, f.mIndices[a]);
  458. }
  459. }
  460. }
  461. }
  462. // write bones
  463. if (mesh->mNumBones) {
  464. for (unsigned int a = 0; a < mesh->mNumBones; ++a) {
  465. const aiBone *b = mesh->mBones[a];
  466. WriteBinaryBone(&chunk, b);
  467. }
  468. }
  469. }
  470. // -----------------------------------------------------------------------------------
  471. void WriteBinaryMaterialProperty(IOStream *container, const aiMaterialProperty *prop) {
  472. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMATERIALPROPERTY);
  473. Write<aiString>(&chunk, prop->mKey);
  474. Write<unsigned int>(&chunk, prop->mSemantic);
  475. Write<unsigned int>(&chunk, prop->mIndex);
  476. Write<unsigned int>(&chunk, prop->mDataLength);
  477. Write<unsigned int>(&chunk, (unsigned int)prop->mType);
  478. chunk.Write(prop->mData, 1, prop->mDataLength);
  479. }
  480. // -----------------------------------------------------------------------------------
  481. void WriteBinaryMaterial(IOStream *container, const aiMaterial *mat) {
  482. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIMATERIAL);
  483. Write<unsigned int>(&chunk, mat->mNumProperties);
  484. for (unsigned int i = 0; i < mat->mNumProperties; ++i) {
  485. WriteBinaryMaterialProperty(&chunk, mat->mProperties[i]);
  486. }
  487. }
  488. // -----------------------------------------------------------------------------------
  489. void WriteBinaryNodeAnim(IOStream *container, const aiNodeAnim *nd) {
  490. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AINODEANIM);
  491. Write<aiString>(&chunk, nd->mNodeName);
  492. Write<unsigned int>(&chunk, nd->mNumPositionKeys);
  493. Write<unsigned int>(&chunk, nd->mNumRotationKeys);
  494. Write<unsigned int>(&chunk, nd->mNumScalingKeys);
  495. Write<unsigned int>(&chunk, nd->mPreState);
  496. Write<unsigned int>(&chunk, nd->mPostState);
  497. if (nd->mPositionKeys) {
  498. if (shortened) {
  499. WriteBounds(&chunk, nd->mPositionKeys, nd->mNumPositionKeys);
  500. } // else write as usual
  501. else
  502. WriteArray<aiVectorKey>(&chunk, nd->mPositionKeys, nd->mNumPositionKeys);
  503. }
  504. if (nd->mRotationKeys) {
  505. if (shortened) {
  506. WriteBounds(&chunk, nd->mRotationKeys, nd->mNumRotationKeys);
  507. } // else write as usual
  508. else
  509. WriteArray<aiQuatKey>(&chunk, nd->mRotationKeys, nd->mNumRotationKeys);
  510. }
  511. if (nd->mScalingKeys) {
  512. if (shortened) {
  513. WriteBounds(&chunk, nd->mScalingKeys, nd->mNumScalingKeys);
  514. } // else write as usual
  515. else
  516. WriteArray<aiVectorKey>(&chunk, nd->mScalingKeys, nd->mNumScalingKeys);
  517. }
  518. }
  519. // -----------------------------------------------------------------------------------
  520. void WriteBinaryAnim(IOStream *container, const aiAnimation *anim) {
  521. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AIANIMATION);
  522. Write<aiString>(&chunk, anim->mName);
  523. Write<double>(&chunk, anim->mDuration);
  524. Write<double>(&chunk, anim->mTicksPerSecond);
  525. Write<unsigned int>(&chunk, anim->mNumChannels);
  526. for (unsigned int a = 0; a < anim->mNumChannels; ++a) {
  527. const aiNodeAnim *nd = anim->mChannels[a];
  528. WriteBinaryNodeAnim(&chunk, nd);
  529. }
  530. }
  531. // -----------------------------------------------------------------------------------
  532. void WriteBinaryLight(IOStream *container, const aiLight *l) {
  533. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AILIGHT);
  534. Write<aiString>(&chunk, l->mName);
  535. Write<unsigned int>(&chunk, l->mType);
  536. Write<aiVector3D>(&chunk, l->mPosition);
  537. Write<aiVector3D>(&chunk, l->mDirection);
  538. Write<aiVector3D>(&chunk, l->mUp);
  539. if (l->mType != aiLightSource_DIRECTIONAL) {
  540. Write<float>(&chunk, l->mAttenuationConstant);
  541. Write<float>(&chunk, l->mAttenuationLinear);
  542. Write<float>(&chunk, l->mAttenuationQuadratic);
  543. }
  544. Write<aiColor3D>(&chunk, l->mColorDiffuse);
  545. Write<aiColor3D>(&chunk, l->mColorSpecular);
  546. Write<aiColor3D>(&chunk, l->mColorAmbient);
  547. if (l->mType == aiLightSource_SPOT) {
  548. Write<float>(&chunk, l->mAngleInnerCone);
  549. Write<float>(&chunk, l->mAngleOuterCone);
  550. }
  551. }
  552. // -----------------------------------------------------------------------------------
  553. void WriteBinaryCamera(IOStream *container, const aiCamera *cam) {
  554. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AICAMERA);
  555. Write<aiString>(&chunk, cam->mName);
  556. Write<aiVector3D>(&chunk, cam->mPosition);
  557. Write<aiVector3D>(&chunk, cam->mLookAt);
  558. Write<aiVector3D>(&chunk, cam->mUp);
  559. Write<float>(&chunk, cam->mHorizontalFOV);
  560. Write<float>(&chunk, cam->mClipPlaneNear);
  561. Write<float>(&chunk, cam->mClipPlaneFar);
  562. Write<float>(&chunk, cam->mAspect);
  563. }
  564. // -----------------------------------------------------------------------------------
  565. void WriteBinaryScene(IOStream *container, const aiScene *scene) {
  566. AssbinChunkWriter chunk(container, ASSBIN_CHUNK_AISCENE);
  567. // basic scene information
  568. Write<unsigned int>(&chunk, scene->mFlags);
  569. Write<unsigned int>(&chunk, scene->mNumMeshes);
  570. Write<unsigned int>(&chunk, scene->mNumMaterials);
  571. Write<unsigned int>(&chunk, scene->mNumAnimations);
  572. Write<unsigned int>(&chunk, scene->mNumTextures);
  573. Write<unsigned int>(&chunk, scene->mNumLights);
  574. Write<unsigned int>(&chunk, scene->mNumCameras);
  575. // write node graph
  576. WriteBinaryNode(&chunk, scene->mRootNode);
  577. // write all meshes
  578. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  579. const aiMesh *mesh = scene->mMeshes[i];
  580. WriteBinaryMesh(&chunk, mesh);
  581. }
  582. // write materials
  583. for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
  584. const aiMaterial *mat = scene->mMaterials[i];
  585. WriteBinaryMaterial(&chunk, mat);
  586. }
  587. // write all animations
  588. for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
  589. const aiAnimation *anim = scene->mAnimations[i];
  590. WriteBinaryAnim(&chunk, anim);
  591. }
  592. // write all textures
  593. for (unsigned int i = 0; i < scene->mNumTextures; ++i) {
  594. const aiTexture *mesh = scene->mTextures[i];
  595. WriteBinaryTexture(&chunk, mesh);
  596. }
  597. // write lights
  598. for (unsigned int i = 0; i < scene->mNumLights; ++i) {
  599. const aiLight *l = scene->mLights[i];
  600. WriteBinaryLight(&chunk, l);
  601. }
  602. // write cameras
  603. for (unsigned int i = 0; i < scene->mNumCameras; ++i) {
  604. const aiCamera *cam = scene->mCameras[i];
  605. WriteBinaryCamera(&chunk, cam);
  606. }
  607. }
  608. public:
  609. AssbinFileWriter(bool shortened, bool compressed) :
  610. shortened(shortened), compressed(compressed) {
  611. }
  612. // -----------------------------------------------------------------------------------
  613. // Write a binary model dump
  614. void WriteBinaryDump(const char *pFile, const char *cmd, IOSystem *pIOSystem, const aiScene *pScene) {
  615. IOStream *out = pIOSystem->Open(pFile, "wb");
  616. if (!out)
  617. throw std::runtime_error("Unable to open output file " + std::string(pFile) + '\n');
  618. auto CloseIOStream = [&]() {
  619. if (out) {
  620. pIOSystem->Close(out);
  621. out = nullptr; // Ensure this is only done once.
  622. }
  623. };
  624. try {
  625. time_t tt = time(nullptr);
  626. #if _WIN32
  627. tm *p = gmtime(&tt);
  628. #else
  629. struct tm now;
  630. tm *p = gmtime_r(&tt, &now);
  631. #endif
  632. // header
  633. char s[64];
  634. memset(s, 0, 64);
  635. #if _MSC_VER >= 1400
  636. sprintf_s(s, "ASSIMP.binary-dump.%s", asctime(p));
  637. #else
  638. ai_snprintf(s, 64, "ASSIMP.binary-dump.%s", asctime(p));
  639. #endif
  640. out->Write(s, 44, 1);
  641. // == 44 bytes
  642. Write<unsigned int>(out, ASSBIN_VERSION_MAJOR);
  643. Write<unsigned int>(out, ASSBIN_VERSION_MINOR);
  644. Write<unsigned int>(out, aiGetVersionRevision());
  645. Write<unsigned int>(out, aiGetCompileFlags());
  646. Write<uint16_t>(out, shortened);
  647. Write<uint16_t>(out, compressed);
  648. // == 20 bytes
  649. char buff[256] = { 0 };
  650. ai_snprintf(buff, 256, "%s", pFile);
  651. out->Write(buff, sizeof(char), 256);
  652. memset(buff, 0, sizeof(buff));
  653. ai_snprintf(buff, 128, "%s", cmd);
  654. out->Write(buff, sizeof(char), 128);
  655. // leave 64 bytes free for future extensions
  656. memset(buff, 0xcd, 64);
  657. out->Write(buff, sizeof(char), 64);
  658. // == 435 bytes
  659. // ==== total header size: 512 bytes
  660. ai_assert(out->Tell() == ASSBIN_HEADER_LENGTH);
  661. // Up to here the data is uncompressed. For compressed files, the rest
  662. // is compressed using standard DEFLATE from zlib.
  663. if (compressed) {
  664. AssbinChunkWriter uncompressedStream(nullptr, 0);
  665. WriteBinaryScene(&uncompressedStream, pScene);
  666. uLongf uncompressedSize = static_cast<uLongf>(uncompressedStream.Tell());
  667. uLongf compressedSize = (uLongf)compressBound(uncompressedSize);
  668. uint8_t *compressedBuffer = new uint8_t[compressedSize];
  669. int res = compress2(compressedBuffer, &compressedSize, (const Bytef *)uncompressedStream.GetBufferPointer(), uncompressedSize, 9);
  670. if (res != Z_OK) {
  671. delete[] compressedBuffer;
  672. throw DeadlyExportError("Compression failed.");
  673. }
  674. out->Write(&uncompressedSize, sizeof(uint32_t), 1);
  675. out->Write(compressedBuffer, sizeof(char), compressedSize);
  676. delete[] compressedBuffer;
  677. } else {
  678. WriteBinaryScene(out, pScene);
  679. }
  680. CloseIOStream();
  681. } catch (...) {
  682. CloseIOStream();
  683. throw;
  684. }
  685. }
  686. };
  687. void DumpSceneToAssbin(
  688. const char *pFile, const char *cmd, IOSystem *pIOSystem,
  689. const aiScene *pScene, bool shortened, bool compressed) {
  690. AssbinFileWriter fileWriter(shortened, compressed);
  691. fileWriter.WriteBinaryDump(pFile, cmd, pIOSystem, pScene);
  692. }
  693. #if _MSC_VER
  694. #pragma warning(pop)
  695. #endif // _MSC_VER
  696. } // end of namespace Assimp