AssbinExporter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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. #include "AssimpPCH.h"
  34. #include "assbin_chunks.h"
  35. #include "./../include/assimp/version.h"
  36. #include "ProcessHelper.h"
  37. #ifndef ASSIMP_BUILD_NO_EXPORT
  38. #ifndef ASSIMP_BUILD_NO_ASSBIN_EXPORTER
  39. using namespace Assimp;
  40. namespace Assimp {
  41. class AssbinChunkWriter : public IOStream
  42. {
  43. private:
  44. uint8_t* buffer;
  45. uint32_t magic;
  46. IOStream * container;
  47. uint32_t cur_size, cursor, initial;
  48. private:
  49. // -------------------------------------------------------------------
  50. void Grow(size_t need = 0)
  51. {
  52. size_t new_size = std::max(initial, std::max( need, cur_size+(cur_size>>1) ));
  53. const uint8_t* const old = buffer;
  54. buffer = new uint8_t[new_size];
  55. if (old) {
  56. memcpy(buffer,old,cur_size);
  57. delete[] old;
  58. }
  59. cur_size = new_size;
  60. }
  61. public:
  62. AssbinChunkWriter( IOStream * container, uint32_t magic, size_t initial = 4096)
  63. : initial(initial), buffer(NULL), cur_size(0), cursor(0), container(container), magic(magic)
  64. {
  65. }
  66. virtual ~AssbinChunkWriter()
  67. {
  68. if (container) {
  69. container->Write( &magic, sizeof(uint32_t), 1 );
  70. container->Write( &cursor, sizeof(uint32_t), 1 );
  71. container->Write( buffer, 1, cursor );
  72. }
  73. if (buffer) delete[] buffer;
  74. }
  75. // -------------------------------------------------------------------
  76. virtual size_t Read(void* pvBuffer,
  77. size_t pSize,
  78. size_t pCount) { return 0; };
  79. virtual aiReturn Seek(size_t pOffset,
  80. aiOrigin pOrigin) { return aiReturn_FAILURE; };
  81. virtual size_t Tell() const { return 0; };
  82. virtual void Flush() { };
  83. virtual size_t FileSize() const
  84. {
  85. return cursor;
  86. }
  87. // -------------------------------------------------------------------
  88. virtual size_t Write(const void* pvBuffer, size_t pSize, size_t pCount)
  89. {
  90. pSize *= pCount;
  91. if (cursor + pSize > cur_size) {
  92. Grow(cursor + pSize);
  93. }
  94. memcpy(buffer+cursor, pvBuffer, pSize);
  95. cursor += pSize;
  96. return pCount;
  97. }
  98. template <typename T>
  99. size_t Write(const T& v)
  100. {
  101. return Write( &v, sizeof(T), 1 );
  102. }
  103. // -----------------------------------------------------------------------------------
  104. // Serialize an aiString
  105. template <>
  106. inline uint32_t Write<aiString>(const aiString& s)
  107. {
  108. const uint32_t s2 = (uint32_t)s.length;
  109. Write(&s,4,1);
  110. Write(s.data,s2,1);
  111. return s2+4;
  112. }
  113. // -----------------------------------------------------------------------------------
  114. // Serialize an unsigned int as uint32_t
  115. template <>
  116. inline uint32_t Write<unsigned int>(const unsigned int& w)
  117. {
  118. const uint32_t t = (uint32_t)w;
  119. if (w > t) {
  120. // this shouldn't happen, integers in Assimp data structures never exceed 2^32
  121. printf("loss of data due to 64 -> 32 bit integer conversion");
  122. }
  123. Write(&t,4,1);
  124. return 4;
  125. }
  126. // -----------------------------------------------------------------------------------
  127. // Serialize an unsigned int as uint16_t
  128. template <>
  129. inline uint32_t Write<uint16_t>(const uint16_t& w)
  130. {
  131. Write(&w,2,1);
  132. return 2;
  133. }
  134. // -----------------------------------------------------------------------------------
  135. // Serialize a float
  136. template <>
  137. inline uint32_t Write<float>(const float& f)
  138. {
  139. BOOST_STATIC_ASSERT(sizeof(float)==4);
  140. Write(&f,4,1);
  141. return 4;
  142. }
  143. // -----------------------------------------------------------------------------------
  144. // Serialize a double
  145. template <>
  146. inline uint32_t Write<double>(const double& f)
  147. {
  148. BOOST_STATIC_ASSERT(sizeof(double)==8);
  149. Write(&f,8,1);
  150. return 8;
  151. }
  152. // -----------------------------------------------------------------------------------
  153. // Serialize a vec3
  154. template <>
  155. inline uint32_t Write<aiVector3D>(const aiVector3D& v)
  156. {
  157. uint32_t t = Write<float>(v.x);
  158. t += Write<float>(v.y);
  159. t += Write<float>(v.z);
  160. return t;
  161. }
  162. // -----------------------------------------------------------------------------------
  163. // Serialize a color value
  164. template <>
  165. inline uint32_t Write<aiColor4D>(const aiColor4D& v)
  166. {
  167. uint32_t t = Write<float>(v.r);
  168. t += Write<float>(v.g);
  169. t += Write<float>(v.b);
  170. t += Write<float>(v.a);
  171. return t;
  172. }
  173. // -----------------------------------------------------------------------------------
  174. // Serialize a quaternion
  175. template <>
  176. inline uint32_t Write<aiQuaternion>(const aiQuaternion& v)
  177. {
  178. uint32_t t = Write<float>(v.w);
  179. t += Write<float>(v.x);
  180. t += Write<float>(v.y);
  181. t += Write<float>(v.z);
  182. return 16;
  183. }
  184. // -----------------------------------------------------------------------------------
  185. // Serialize a vertex weight
  186. template <>
  187. inline uint32_t Write<aiVertexWeight>(const aiVertexWeight& v)
  188. {
  189. uint32_t t = Write<unsigned int>(v.mVertexId);
  190. return t+Write<float>(v.mWeight);
  191. }
  192. // -----------------------------------------------------------------------------------
  193. // Serialize a mat4x4
  194. template <>
  195. inline uint32_t Write<aiMatrix4x4>(const aiMatrix4x4& m)
  196. {
  197. for (unsigned int i = 0; i < 4;++i) {
  198. for (unsigned int i2 = 0; i2 < 4;++i2) {
  199. Write<float>(m[i][i2]);
  200. }
  201. }
  202. return 64;
  203. }
  204. // -----------------------------------------------------------------------------------
  205. // Serialize an aiVectorKey
  206. template <>
  207. inline uint32_t Write<aiVectorKey>(const aiVectorKey& v)
  208. {
  209. const uint32_t t = Write<double>(v.mTime);
  210. return t + Write<aiVector3D>(v.mValue);
  211. }
  212. // -----------------------------------------------------------------------------------
  213. // Serialize an aiQuatKey
  214. template <>
  215. inline uint32_t Write<aiQuatKey>(const aiQuatKey& v)
  216. {
  217. const uint32_t t = Write<double>(v.mTime);
  218. return t + Write<aiQuaternion>(v.mValue);
  219. }
  220. template <typename T>
  221. inline uint32_t WriteBounds(const T* in, unsigned int size)
  222. {
  223. T minc,maxc;
  224. ArrayBounds(in,size,minc,maxc);
  225. const uint32_t t = Write<T>(minc);
  226. return t + Write<T>(maxc);
  227. }
  228. };
  229. /*
  230. class AssbinChunkWriter
  231. {
  232. AssbinStream stream;
  233. uint32_t magic;
  234. public:
  235. AssbinChunkWriter( uint32_t _magic )
  236. {
  237. magic = _magic;
  238. }
  239. void AppendToStream( AssbinStream & _stream )
  240. {
  241. uint32_t s = stream.FileSize();
  242. _stream.Write( &magic, sizeof(uint32_t), 1 );
  243. _stream.Write( &s, sizeof(uint32_t), 1 );
  244. _stream.Write( stream.GetBuffer(), stream.FileSize(), 1 );
  245. }
  246. void AppendToStream( AssbinChunkWriter & _stream )
  247. {
  248. uint32_t s = stream.FileSize();
  249. _stream.WriteRaw( &magic, sizeof(uint32_t) );
  250. _stream.WriteRaw( &s, sizeof(uint32_t) );
  251. _stream.WriteRaw( stream.GetBuffer(), stream.FileSize() );
  252. }
  253. };
  254. */
  255. class AssbinExport
  256. {
  257. private:
  258. bool shortened;
  259. bool compressed;
  260. //AssbinStream stream;
  261. protected:
  262. template <typename T>
  263. size_t Write( IOStream * container, const T& v)
  264. {
  265. return container->Write( &v, sizeof(T), 1 );
  266. }
  267. // -----------------------------------------------------------------------------------
  268. void WriteBinaryNode( IOStream * container, const aiNode* node)
  269. {
  270. AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AINODE );
  271. chunk.Write<aiString>(node->mName);
  272. chunk.Write<aiMatrix4x4>(node->mTransformation);
  273. chunk.Write<unsigned int>(node->mNumChildren);
  274. chunk.Write<unsigned int>(node->mNumMeshes);
  275. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  276. chunk.Write<unsigned int>(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. chunk.Write<unsigned int>(tex->mWidth);
  287. chunk.Write<unsigned int>(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. chunk.Write<aiString>(b->mName);
  303. chunk.Write<unsigned int>(b->mNumWeights);
  304. chunk.Write<aiMatrix4x4>(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. chunk.WriteBounds(b->mWeights,b->mNumWeights);
  309. } // else write as usual
  310. else chunk.Write(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight));
  311. }
  312. // -----------------------------------------------------------------------------------
  313. void WriteBinaryMesh(IOStream * container, const aiMesh* mesh)
  314. {
  315. AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIMESH );
  316. chunk.Write<unsigned int>(mesh->mPrimitiveTypes);
  317. chunk.Write<unsigned int>(mesh->mNumVertices);
  318. chunk.Write<unsigned int>(mesh->mNumFaces);
  319. chunk.Write<unsigned int>(mesh->mNumBones);
  320. chunk.Write<unsigned int>(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. chunk.Write<unsigned int>(c);
  345. aiVector3D minVec, maxVec;
  346. if (mesh->mVertices) {
  347. if (shortened) {
  348. chunk.WriteBounds(mesh->mVertices,mesh->mNumVertices);
  349. } // else write as usual
  350. else chunk.Write(mesh->mVertices,1,12*mesh->mNumVertices);
  351. }
  352. if (mesh->mNormals) {
  353. if (shortened) {
  354. chunk.WriteBounds(mesh->mNormals,mesh->mNumVertices);
  355. } // else write as usual
  356. else chunk.Write(mesh->mNormals,1,12*mesh->mNumVertices);
  357. }
  358. if (mesh->mTangents && mesh->mBitangents) {
  359. if (shortened) {
  360. chunk.WriteBounds(mesh->mTangents,mesh->mNumVertices);
  361. chunk.WriteBounds(mesh->mBitangents,mesh->mNumVertices);
  362. } // else write as usual
  363. else {
  364. chunk.Write(mesh->mTangents,1,12*mesh->mNumVertices);
  365. chunk.Write(mesh->mBitangents,1,12*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. chunk.WriteBounds(mesh->mColors[n],mesh->mNumVertices);
  373. } // else write as usual
  374. else chunk.Write(mesh->mColors[n],16*mesh->mNumVertices,1);
  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. chunk.Write<unsigned int>(mesh->mNumUVComponents[n]);
  381. if (shortened) {
  382. chunk.WriteBounds(mesh->mTextureCoords[n],mesh->mNumVertices);
  383. } // else write as usual
  384. else chunk.Write(mesh->mTextureCoords[n],12*mesh->mNumVertices,1);
  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. chunk.Write<unsigned int>(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. chunk.Write<uint16_t>(f.mNumIndices);
  414. for (unsigned int a = 0; a < f.mNumIndices;++a) {
  415. if (mesh->mNumVertices < (1u<<16)) {
  416. chunk.Write<uint16_t>(f.mIndices[a]);
  417. }
  418. else chunk.Write<unsigned int>(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. chunk.Write<aiString>(prop->mKey);
  435. chunk.Write<unsigned int>(prop->mSemantic);
  436. chunk.Write<unsigned int>(prop->mIndex);
  437. chunk.Write<unsigned int>(prop->mDataLength);
  438. chunk.Write<unsigned int>((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. chunk.Write<unsigned int>(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. chunk.Write<aiString>(nd->mNodeName);
  455. chunk.Write<unsigned int>(nd->mNumPositionKeys);
  456. chunk.Write<unsigned int>(nd->mNumRotationKeys);
  457. chunk.Write<unsigned int>(nd->mNumScalingKeys);
  458. chunk.Write<unsigned int>(nd->mPreState);
  459. chunk.Write<unsigned int>(nd->mPostState);
  460. if (nd->mPositionKeys) {
  461. if (shortened) {
  462. chunk.WriteBounds(nd->mPositionKeys,nd->mNumPositionKeys);
  463. } // else write as usual
  464. else chunk.Write(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey));
  465. }
  466. if (nd->mRotationKeys) {
  467. if (shortened) {
  468. chunk.WriteBounds(nd->mRotationKeys,nd->mNumRotationKeys);
  469. } // else write as usual
  470. else chunk.Write(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey));
  471. }
  472. if (nd->mScalingKeys) {
  473. if (shortened) {
  474. chunk.WriteBounds(nd->mScalingKeys,nd->mNumScalingKeys);
  475. } // else write as usual
  476. else chunk.Write(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey));
  477. }
  478. }
  479. // -----------------------------------------------------------------------------------
  480. void WriteBinaryAnim( IOStream * container, const aiAnimation* anim )
  481. {
  482. AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AIANIMATION );
  483. chunk.Write<aiString> (anim->mName);
  484. chunk.Write<double> (anim->mDuration);
  485. chunk.Write<double> (anim->mTicksPerSecond);
  486. chunk.Write<unsigned int>(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. chunk.Write<aiString>(l->mName);
  497. chunk.Write<unsigned int>(l->mType);
  498. if (l->mType != aiLightSource_DIRECTIONAL) {
  499. chunk.Write<float>(l->mAttenuationConstant);
  500. chunk.Write<float>(l->mAttenuationLinear);
  501. chunk.Write<float>(l->mAttenuationQuadratic);
  502. }
  503. chunk.Write<aiVector3D>((const aiVector3D&)l->mColorDiffuse);
  504. chunk.Write<aiVector3D>((const aiVector3D&)l->mColorSpecular);
  505. chunk.Write<aiVector3D>((const aiVector3D&)l->mColorAmbient);
  506. if (l->mType == aiLightSource_SPOT) {
  507. chunk.Write<float>(l->mAngleInnerCone);
  508. chunk.Write<float>(l->mAngleOuterCone);
  509. }
  510. }
  511. // -----------------------------------------------------------------------------------
  512. void WriteBinaryCamera( IOStream * container, const aiCamera* cam )
  513. {
  514. AssbinChunkWriter chunk( container, ASSBIN_CHUNK_AICAMERA );
  515. chunk.Write<aiString>(cam->mName);
  516. chunk.Write<aiVector3D>(cam->mPosition);
  517. chunk.Write<aiVector3D>(cam->mLookAt);
  518. chunk.Write<aiVector3D>(cam->mUp);
  519. chunk.Write<float>(cam->mHorizontalFOV);
  520. chunk.Write<float>(cam->mClipPlaneNear);
  521. chunk.Write<float>(cam->mClipPlaneFar);
  522. chunk.Write<float>(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. chunk.Write<unsigned int>(scene->mFlags);
  530. chunk.Write<unsigned int>(scene->mNumMeshes);
  531. chunk.Write<unsigned int>(scene->mNumMaterials);
  532. chunk.Write<unsigned int>(scene->mNumAnimations);
  533. chunk.Write<unsigned int>(scene->mNumTextures);
  534. chunk.Write<unsigned int>(scene->mNumLights);
  535. chunk.Write<unsigned int>(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. {
  572. shortened = false;
  573. compressed = false;
  574. }
  575. // -----------------------------------------------------------------------------------
  576. // Write a binary model dump
  577. void WriteBinaryDump(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene)
  578. {
  579. IOStream * out = pIOSystem->Open( pFile, "wb" );
  580. if (!out) return;
  581. time_t tt = time(NULL);
  582. tm* p = gmtime(&tt);
  583. // header
  584. char s[64];
  585. memset( s, 0, 64 );
  586. #if _MSC_VER >= 1400
  587. sprintf_s(s,"ASSIMP.binary-dump.%s",asctime(p));
  588. #else
  589. snprintf(s,64,"ASSIMP.binary-dump.%s",asctime(p));
  590. #endif
  591. out->Write( s, 44, 1 );
  592. // == 44 bytes
  593. Write<unsigned int>( out, ASSBIN_VERSION_MAJOR );
  594. Write<unsigned int>( out, ASSBIN_VERSION_MINOR );
  595. Write<unsigned int>( out, aiGetVersionRevision() );
  596. Write<unsigned int>( out, aiGetCompileFlags() );
  597. Write<uint16_t>( out, shortened );
  598. Write<uint16_t>( out, compressed );
  599. // == 20 bytes
  600. //todo
  601. char buff[256];
  602. strncpy(buff,pFile,256);
  603. out->Write(buff,sizeof(char),256);
  604. char cmd[] = "\0";
  605. strncpy(buff,cmd,128);
  606. out->Write(buff,sizeof(char),128);
  607. // leave 64 bytes free for future extensions
  608. memset(buff,0xcd,64);
  609. out->Write(buff,sizeof(char),64);
  610. // == 435 bytes
  611. // ==== total header size: 512 bytes
  612. ai_assert( out->Tell() == ASSBIN_HEADER_LENGTH );
  613. // Up to here the data is uncompressed. For compressed files, the rest
  614. // is compressed using standard DEFLATE from zlib.
  615. WriteBinaryScene( out, pScene );
  616. pIOSystem->Close( out );
  617. }
  618. };
  619. void ExportSceneAssbin(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene)
  620. {
  621. AssbinExport exporter;
  622. exporter.WriteBinaryDump( pFile, pIOSystem, pScene );
  623. }
  624. } // end of namespace Assimp
  625. #endif // ASSIMP_BUILD_NO_ASSBIN_EXPORTER
  626. #endif // ASSIMP_BUILD_NO_EXPORT