AssbinExporter.cpp 23 KB

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