AssbinExporter.cpp 28 KB

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