AssbinExporter.cpp 28 KB

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