AssbinExporter.cpp 30 KB

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