AssbinExporter.cpp 30 KB

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