AssbinExporter.cpp 23 KB

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