25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1393 lines
42 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file WriteTextDumb.cpp
  35. * @brief Implementation of the 'assimp dump' utility
  36. */
  37. #include "Main.h"
  38. #include "../code/ProcessHelper.h"
  39. const char* AICMD_MSG_DUMP_HELP =
  40. "assimp dump <model> [<out>] [-b] [-s] [-z] [common parameters]\n"
  41. "\t -b Binary output \n"
  42. "\t -s Shortened \n"
  43. "\t -z Compressed \n"
  44. "\t[See the assimp_cmd docs for a full list of all common parameters] \n"
  45. "\t -cfast Fast post processing preset, runs just a few important steps \n"
  46. "\t -cdefault Default post processing: runs all recommended steps\n"
  47. "\t -cfull Fires almost all post processing steps \n"
  48. ;
  49. #include "../../code/assbin_chunks.h"
  50. FILE* out = NULL;
  51. bool shortened = false;
  52. // -----------------------------------------------------------------------------------
  53. // Compress a binary dump file (beginning at offset head_size)
  54. void CompressBinaryDump(const char* file, unsigned int head_size)
  55. {
  56. // for simplicity ... copy the file into memory again and compress it there
  57. FILE* p = fopen(file,"r");
  58. fseek(p,0,SEEK_END);
  59. const uint32_t size = ftell(p);
  60. fseek(p,0,SEEK_SET);
  61. if (size<head_size) {
  62. fclose(p);
  63. return;
  64. }
  65. uint8_t* data = new uint8_t[size];
  66. fread(data,1,size,p);
  67. uLongf out_size = (uLongf)((size-head_size) * 1.001 + 12.);
  68. uint8_t* out = new uint8_t[out_size];
  69. compress2(out,&out_size,data+head_size,size-head_size,9);
  70. fclose(p);
  71. p = fopen(file,"w");
  72. fwrite(data,head_size,1,p);
  73. fwrite(&out_size,4,1,p); // write size of uncompressed data
  74. fwrite(out,out_size,1,p);
  75. fclose(p);
  76. delete[] data;
  77. delete[] out;
  78. }
  79. // -----------------------------------------------------------------------------------
  80. // Write a magic start value for each serialized data structure
  81. inline uint32_t WriteMagic(uint32_t magic)
  82. {
  83. fwrite(&magic,4,1,out);
  84. fwrite(&magic,4,1,out);
  85. return ftell(out)-4;
  86. }
  87. // use template specializations rather than regular overloading to be able to
  88. // explicitly select the right 'overload' to leave no doubts on what is called,
  89. // retaining the possibility of letting the compiler select.
  90. template <typename T> uint32_t Write(const T&);
  91. // -----------------------------------------------------------------------------------
  92. // Serialize an aiString
  93. template <>
  94. inline uint32_t Write<aiString>(const aiString& s)
  95. {
  96. const uint32_t s2 = (uint32_t)s.length;
  97. fwrite(&s,4,1,out);
  98. fwrite(s.data,s2,1,out);
  99. return s2+4;
  100. }
  101. // -----------------------------------------------------------------------------------
  102. // Serialize an unsigned int as uint32_t
  103. template <>
  104. inline uint32_t Write<unsigned int>(const unsigned int& w)
  105. {
  106. const uint32_t t = (uint32_t)w;
  107. if (w > t) {
  108. // this shouldn't happen, integers in Assimp data structures never exceed 2^32
  109. printf("loss of data due to 64 -> 32 bit integer conversion");
  110. }
  111. fwrite(&t,4,1,out);
  112. return 4;
  113. }
  114. // -----------------------------------------------------------------------------------
  115. // Serialize an unsigned int as uint16_t
  116. template <>
  117. inline uint32_t Write<uint16_t>(const uint16_t& w)
  118. {
  119. fwrite(&w,2,1,out);
  120. return 2;
  121. }
  122. // -----------------------------------------------------------------------------------
  123. // Serialize a float
  124. template <>
  125. inline uint32_t Write<float>(const float& f)
  126. {
  127. BOOST_STATIC_ASSERT(sizeof(float)==4);
  128. fwrite(&f,4,1,out);
  129. return 4;
  130. }
  131. // -----------------------------------------------------------------------------------
  132. // Serialize a double
  133. template <>
  134. inline uint32_t Write<double>(const double& f)
  135. {
  136. BOOST_STATIC_ASSERT(sizeof(double)==8);
  137. fwrite(&f,8,1,out);
  138. return 8;
  139. }
  140. // -----------------------------------------------------------------------------------
  141. // Serialize a vec3
  142. template <>
  143. inline uint32_t Write<aiVector3D>(const aiVector3D& v)
  144. {
  145. uint32_t t = Write<float>(v.x);
  146. t += Write<float>(v.y);
  147. t += Write<float>(v.z);
  148. return t;
  149. }
  150. // -----------------------------------------------------------------------------------
  151. // Serialize a color value
  152. template <>
  153. inline uint32_t Write<aiColor4D>(const aiColor4D& v)
  154. {
  155. uint32_t t = Write<float>(v.r);
  156. t += Write<float>(v.g);
  157. t += Write<float>(v.b);
  158. t += Write<float>(v.a);
  159. return t;
  160. }
  161. // -----------------------------------------------------------------------------------
  162. // Serialize a quaternion
  163. template <>
  164. inline uint32_t Write<aiQuaternion>(const aiQuaternion& v)
  165. {
  166. uint32_t t = Write<float>(v.w);
  167. t += Write<float>(v.x);
  168. t += Write<float>(v.y);
  169. t += Write<float>(v.z);
  170. return 16;
  171. }
  172. // -----------------------------------------------------------------------------------
  173. // Serialize a vertex weight
  174. template <>
  175. inline uint32_t Write<aiVertexWeight>(const aiVertexWeight& v)
  176. {
  177. uint32_t t = Write<unsigned int>(v.mVertexId);
  178. return t+Write<float>(v.mWeight);
  179. }
  180. // -----------------------------------------------------------------------------------
  181. // Serialize a mat4x4
  182. template <>
  183. inline uint32_t Write<aiMatrix4x4>(const aiMatrix4x4& m)
  184. {
  185. for (unsigned int i = 0; i < 4;++i) {
  186. for (unsigned int i2 = 0; i2 < 4;++i2) {
  187. Write<float>(m[i][i2]);
  188. }
  189. }
  190. return 64;
  191. }
  192. // -----------------------------------------------------------------------------------
  193. // Serialize an aiVectorKey
  194. template <>
  195. inline uint32_t Write<aiVectorKey>(const aiVectorKey& v)
  196. {
  197. const uint32_t t = Write<double>(v.mTime);
  198. return t + Write<aiVector3D>(v.mValue);
  199. }
  200. // -----------------------------------------------------------------------------------
  201. // Serialize an aiQuatKey
  202. template <>
  203. inline uint32_t Write<aiQuatKey>(const aiQuatKey& v)
  204. {
  205. const uint32_t t = Write<double>(v.mTime);
  206. return t + Write<aiQuaternion>(v.mValue);
  207. }
  208. // -----------------------------------------------------------------------------------
  209. // Write the min/max values of an array of Ts to the file
  210. template <typename T>
  211. inline uint32_t WriteBounds(const T* in, unsigned int size)
  212. {
  213. T minc,maxc;
  214. Assimp::ArrayBounds(in,size,minc,maxc);
  215. const uint32_t t = Write<T>(minc);
  216. return t + Write<T>(maxc);
  217. }
  218. // -----------------------------------------------------------------------------------
  219. void ChangeInteger(uint32_t ofs,uint32_t n)
  220. {
  221. const uint32_t cur = ftell(out);
  222. fseek(out,ofs,SEEK_SET);
  223. fwrite(&n,4,1,out);
  224. fseek(out,cur,SEEK_SET);
  225. }
  226. // -----------------------------------------------------------------------------------
  227. uint32_t WriteBinaryNode(const aiNode* node)
  228. {
  229. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AINODE);
  230. len += Write<aiString>(node->mName);
  231. len += Write<aiMatrix4x4>(node->mTransformation);
  232. len += Write<unsigned int>(node->mNumChildren);
  233. len += Write<unsigned int>(node->mNumMeshes);
  234. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  235. len += Write<unsigned int>(node->mMeshes[i]);
  236. }
  237. for (unsigned int i = 0; i < node->mNumChildren;++i) {
  238. len += WriteBinaryNode(node->mChildren[i])+8;
  239. }
  240. ChangeInteger(old,len);
  241. return len;
  242. }
  243. // -----------------------------------------------------------------------------------
  244. uint32_t WriteBinaryTexture(const aiTexture* tex)
  245. {
  246. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AITEXTURE);
  247. len += Write<unsigned int>(tex->mWidth);
  248. len += Write<unsigned int>(tex->mHeight);
  249. len += fwrite(tex->achFormatHint,1,4,out);
  250. if(!shortened) {
  251. if (!tex->mHeight) {
  252. len += fwrite(tex->pcData,1,tex->mWidth,out);
  253. }
  254. else {
  255. len += fwrite(tex->pcData,1,tex->mWidth*tex->mHeight*4,out);
  256. }
  257. }
  258. ChangeInteger(old,len);
  259. return len;
  260. }
  261. // -----------------------------------------------------------------------------------
  262. uint32_t WriteBinaryBone(const aiBone* b)
  263. {
  264. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AIBONE);
  265. len += Write<aiString>(b->mName);
  266. len += Write<unsigned int>(b->mNumWeights);
  267. len += Write<aiMatrix4x4>(b->mOffsetMatrix);
  268. // for the moment we write dumb min/max values for the bones, too.
  269. // maybe I'll add a better, hash-like solution later
  270. if (shortened) {
  271. len += WriteBounds(b->mWeights,b->mNumWeights);
  272. } // else write as usual
  273. else len += fwrite(b->mWeights,1,b->mNumWeights*sizeof(aiVertexWeight),out);
  274. ChangeInteger(old,len);
  275. return len;
  276. }
  277. // -----------------------------------------------------------------------------------
  278. uint32_t WriteBinaryMesh(const aiMesh* mesh)
  279. {
  280. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AIMESH);
  281. len += Write<unsigned int>(mesh->mPrimitiveTypes);
  282. len += Write<unsigned int>(mesh->mNumVertices);
  283. len += Write<unsigned int>(mesh->mNumFaces);
  284. len += Write<unsigned int>(mesh->mNumBones);
  285. len += Write<unsigned int>(mesh->mMaterialIndex);
  286. // first of all, write bits for all existent vertex components
  287. unsigned int c = 0;
  288. if (mesh->mVertices) {
  289. c |= ASSBIN_MESH_HAS_POSITIONS;
  290. }
  291. if (mesh->mNormals) {
  292. c |= ASSBIN_MESH_HAS_NORMALS;
  293. }
  294. if (mesh->mTangents && mesh->mBitangents) {
  295. c |= ASSBIN_MESH_HAS_TANGENTS_AND_BITANGENTS;
  296. }
  297. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
  298. if (!mesh->mTextureCoords[n]) {
  299. break;
  300. }
  301. c |= ASSBIN_MESH_HAS_TEXCOORD(n);
  302. }
  303. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
  304. if (!mesh->mColors[n]) {
  305. break;
  306. }
  307. c |= ASSBIN_MESH_HAS_COLOR(n);
  308. }
  309. len += Write<unsigned int>(c);
  310. aiVector3D minVec, maxVec;
  311. if (mesh->mVertices) {
  312. if (shortened) {
  313. len += WriteBounds(mesh->mVertices,mesh->mNumVertices);
  314. } // else write as usual
  315. else len += fwrite(mesh->mVertices,1,12*mesh->mNumVertices,out);
  316. }
  317. if (mesh->mNormals) {
  318. if (shortened) {
  319. len += WriteBounds(mesh->mNormals,mesh->mNumVertices);
  320. } // else write as usual
  321. else len += fwrite(mesh->mNormals,1,12*mesh->mNumVertices,out);
  322. }
  323. if (mesh->mTangents && mesh->mBitangents) {
  324. if (shortened) {
  325. len += WriteBounds(mesh->mTangents,mesh->mNumVertices);
  326. len += WriteBounds(mesh->mBitangents,mesh->mNumVertices);
  327. } // else write as usual
  328. else {
  329. len += fwrite(mesh->mTangents,1,12*mesh->mNumVertices,out);
  330. len += fwrite(mesh->mBitangents,1,12*mesh->mNumVertices,out);
  331. }
  332. }
  333. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
  334. if (!mesh->mColors[n])
  335. break;
  336. if (shortened) {
  337. len += WriteBounds(mesh->mColors[n],mesh->mNumVertices);
  338. } // else write as usual
  339. else len += fwrite(mesh->mColors[n],16*mesh->mNumVertices,1,out);
  340. }
  341. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
  342. if (!mesh->mTextureCoords[n])
  343. break;
  344. // write number of UV components
  345. len += Write<unsigned int>(mesh->mNumUVComponents[n]);
  346. if (shortened) {
  347. len += WriteBounds(mesh->mTextureCoords[n],mesh->mNumVertices);
  348. } // else write as usual
  349. else len += fwrite(mesh->mTextureCoords[n],12*mesh->mNumVertices,1,out);
  350. }
  351. // write faces. There are no floating-point calculations involved
  352. // in these, so we can write a simple hash over the face data
  353. // to the dump file. We generate a single 32 Bit hash for 512 faces
  354. // using Assimp's standard hashing function.
  355. if (shortened) {
  356. unsigned int processed = 0;
  357. for (unsigned int job;(job = std::min(mesh->mNumFaces-processed,512u));processed += job) {
  358. uint32_t hash = 0;
  359. for (unsigned int a = 0; a < job;++a) {
  360. const aiFace& f = mesh->mFaces[processed+a];
  361. uint32_t tmp = f.mNumIndices;
  362. hash = SuperFastHash(reinterpret_cast<const char*>(&tmp),sizeof tmp,hash);
  363. for (unsigned int i = 0; i < f.mNumIndices; ++i) {
  364. BOOST_STATIC_ASSERT(AI_MAX_VERTICES <= 0xffffffff);
  365. tmp = static_cast<uint32_t>( f.mIndices[i] );
  366. hash = SuperFastHash(reinterpret_cast<const char*>(&tmp),sizeof tmp,hash);
  367. }
  368. }
  369. len += Write<unsigned int>(hash);
  370. }
  371. }
  372. else // else write as usual
  373. {
  374. // if there are less than 2^16 vertices, we can simply use 16 bit integers ...
  375. for (unsigned int i = 0; i < mesh->mNumFaces;++i) {
  376. const aiFace& f = mesh->mFaces[i];
  377. BOOST_STATIC_ASSERT(AI_MAX_FACE_INDICES <= 0xffff);
  378. len += Write<uint16_t>(f.mNumIndices);
  379. for (unsigned int a = 0; a < f.mNumIndices;++a) {
  380. if (mesh->mNumVertices < (1u<<16)) {
  381. len += Write<uint16_t>(f.mIndices[a]);
  382. }
  383. else len += Write<unsigned int>(f.mIndices[a]);
  384. }
  385. }
  386. }
  387. // write bones
  388. if (mesh->mNumBones) {
  389. for (unsigned int a = 0; a < mesh->mNumBones;++a) {
  390. const aiBone* b = mesh->mBones[a];
  391. len += WriteBinaryBone(b)+8;
  392. }
  393. }
  394. ChangeInteger(old,len);
  395. return len;
  396. }
  397. // -----------------------------------------------------------------------------------
  398. uint32_t WriteBinaryMaterialProperty(const aiMaterialProperty* prop)
  399. {
  400. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AIMATERIALPROPERTY);
  401. len += Write<aiString>(prop->mKey);
  402. len += Write<unsigned int>(prop->mSemantic);
  403. len += Write<unsigned int>(prop->mIndex);
  404. len += Write<unsigned int>(prop->mDataLength);
  405. len += Write<unsigned int>((unsigned int)prop->mType);
  406. len += fwrite(prop->mData,1,prop->mDataLength,out);
  407. ChangeInteger(old,len);
  408. return len;
  409. }
  410. // -----------------------------------------------------------------------------------
  411. uint32_t WriteBinaryMaterial(const aiMaterial* mat)
  412. {
  413. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AIMATERIAL);
  414. len += Write<unsigned int>(mat->mNumProperties);
  415. for (unsigned int i = 0; i < mat->mNumProperties;++i) {
  416. len += WriteBinaryMaterialProperty(mat->mProperties[i])+8;
  417. }
  418. ChangeInteger(old,len);
  419. return len;
  420. }
  421. // -----------------------------------------------------------------------------------
  422. uint32_t WriteBinaryNodeAnim(const aiNodeAnim* nd)
  423. {
  424. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AINODEANIM);
  425. len += Write<aiString>(nd->mNodeName);
  426. len += Write<unsigned int>(nd->mNumPositionKeys);
  427. len += Write<unsigned int>(nd->mNumRotationKeys);
  428. len += Write<unsigned int>(nd->mNumScalingKeys);
  429. len += Write<unsigned int>(nd->mPreState);
  430. len += Write<unsigned int>(nd->mPostState);
  431. if (nd->mPositionKeys) {
  432. if (shortened) {
  433. len += WriteBounds(nd->mPositionKeys,nd->mNumPositionKeys);
  434. } // else write as usual
  435. else len += fwrite(nd->mPositionKeys,1,nd->mNumPositionKeys*sizeof(aiVectorKey),out);
  436. }
  437. if (nd->mRotationKeys) {
  438. if (shortened) {
  439. len += WriteBounds(nd->mRotationKeys,nd->mNumRotationKeys);
  440. } // else write as usual
  441. else len += fwrite(nd->mRotationKeys,1,nd->mNumRotationKeys*sizeof(aiQuatKey),out);
  442. }
  443. if (nd->mScalingKeys) {
  444. if (shortened) {
  445. len += WriteBounds(nd->mScalingKeys,nd->mNumScalingKeys);
  446. } // else write as usual
  447. else len += fwrite(nd->mScalingKeys,1,nd->mNumScalingKeys*sizeof(aiVectorKey),out);
  448. }
  449. ChangeInteger(old,len);
  450. return len;
  451. }
  452. // -----------------------------------------------------------------------------------
  453. uint32_t WriteBinaryAnim(const aiAnimation* anim)
  454. {
  455. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AIANIMATION);
  456. len += Write<aiString> (anim->mName);
  457. len += Write<double> (anim->mDuration);
  458. len += Write<double> (anim->mTicksPerSecond);
  459. len += Write<unsigned int>(anim->mNumChannels);
  460. for (unsigned int a = 0; a < anim->mNumChannels;++a) {
  461. const aiNodeAnim* nd = anim->mChannels[a];
  462. len += WriteBinaryNodeAnim(nd)+8;
  463. }
  464. ChangeInteger(old,len);
  465. return len;
  466. }
  467. // -----------------------------------------------------------------------------------
  468. uint32_t WriteBinaryLight(const aiLight* l)
  469. {
  470. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AILIGHT);
  471. len += Write<aiString>(l->mName);
  472. len += Write<unsigned int>(l->mType);
  473. if (l->mType != aiLightSource_DIRECTIONAL) {
  474. len += Write<float>(l->mAttenuationConstant);
  475. len += Write<float>(l->mAttenuationLinear);
  476. len += Write<float>(l->mAttenuationQuadratic);
  477. }
  478. len += Write<aiVector3D>((const aiVector3D&)l->mColorDiffuse);
  479. len += Write<aiVector3D>((const aiVector3D&)l->mColorSpecular);
  480. len += Write<aiVector3D>((const aiVector3D&)l->mColorAmbient);
  481. if (l->mType == aiLightSource_SPOT) {
  482. len += Write<float>(l->mAngleInnerCone);
  483. len += Write<float>(l->mAngleOuterCone);
  484. }
  485. ChangeInteger(old,len);
  486. return len;
  487. }
  488. // -----------------------------------------------------------------------------------
  489. uint32_t WriteBinaryCamera(const aiCamera* cam)
  490. {
  491. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AICAMERA);
  492. len += Write<aiString>(cam->mName);
  493. len += Write<aiVector3D>(cam->mPosition);
  494. len += Write<aiVector3D>(cam->mLookAt);
  495. len += Write<aiVector3D>(cam->mUp);
  496. len += Write<float>(cam->mHorizontalFOV);
  497. len += Write<float>(cam->mClipPlaneNear);
  498. len += Write<float>(cam->mClipPlaneFar);
  499. len += Write<float>(cam->mAspect);
  500. ChangeInteger(old,len);
  501. return len;
  502. }
  503. // -----------------------------------------------------------------------------------
  504. uint32_t WriteBinaryScene(const aiScene* scene)
  505. {
  506. uint32_t len = 0, old = WriteMagic(ASSBIN_CHUNK_AISCENE);
  507. // basic scene information
  508. len += Write<unsigned int>(scene->mFlags);
  509. len += Write<unsigned int>(scene->mNumMeshes);
  510. len += Write<unsigned int>(scene->mNumMaterials);
  511. len += Write<unsigned int>(scene->mNumAnimations);
  512. len += Write<unsigned int>(scene->mNumTextures);
  513. len += Write<unsigned int>(scene->mNumLights);
  514. len += Write<unsigned int>(scene->mNumCameras);
  515. // write node graph
  516. len += WriteBinaryNode(scene->mRootNode)+8;
  517. // write all meshes
  518. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  519. const aiMesh* mesh = scene->mMeshes[i];
  520. len += WriteBinaryMesh(mesh)+8;
  521. }
  522. // write materials
  523. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  524. const aiMaterial* mat = scene->mMaterials[i];
  525. len += WriteBinaryMaterial(mat)+8;
  526. }
  527. // write all animations
  528. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  529. const aiAnimation* anim = scene->mAnimations[i];
  530. len += WriteBinaryAnim(anim)+8;
  531. }
  532. // write all textures
  533. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  534. const aiTexture* mesh = scene->mTextures[i];
  535. len += WriteBinaryTexture(mesh)+8;
  536. }
  537. // write lights
  538. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  539. const aiLight* l = scene->mLights[i];
  540. len += WriteBinaryLight(l)+8;
  541. }
  542. // write cameras
  543. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  544. const aiCamera* cam = scene->mCameras[i];
  545. len += WriteBinaryCamera(cam)+8;
  546. }
  547. ChangeInteger(old,len);
  548. return len;
  549. }
  550. // -----------------------------------------------------------------------------------
  551. // Write a binary model dump
  552. void WriteBinaryDump(const aiScene* scene, FILE* _out, const char* src, const char* cmd,
  553. bool _shortened, bool compressed, ImportData& imp)
  554. {
  555. out = _out;
  556. shortened = _shortened;
  557. time_t tt = time(NULL);
  558. tm* p = gmtime(&tt);
  559. // header
  560. fprintf(out,"ASSIMP.binary-dump.%s",asctime(p));
  561. // == 44 bytes
  562. Write<unsigned int>(ASSBIN_VERSION_MAJOR);
  563. Write<unsigned int>(ASSBIN_VERSION_MINOR);
  564. Write<unsigned int>(aiGetVersionRevision());
  565. Write<unsigned int>(aiGetCompileFlags());
  566. Write<uint16_t>(shortened);
  567. Write<uint16_t>(compressed);
  568. // == 20 bytes
  569. char buff[256];
  570. strncpy(buff,src,256);
  571. fwrite(buff,256,1,out);
  572. strncpy(buff,cmd,128);
  573. fwrite(buff,128,1,out);
  574. // leave 64 bytes free for future extensions
  575. memset(buff,0xcd,64);
  576. fwrite(buff,64,1,out);
  577. // == 435 bytes
  578. // ==== total header size: 512 bytes
  579. ai_assert(ftell(out)==ASSBIN_HEADER_LENGTH);
  580. // Up to here the data is uncompressed. For compressed files, the rest
  581. // is compressed using standard DEFLATE from zlib.
  582. WriteBinaryScene(scene);
  583. }
  584. // -----------------------------------------------------------------------------------
  585. // Convert a name to standard XML format
  586. void ConvertName(aiString& out, const aiString& in)
  587. {
  588. out.length = 0;
  589. for (unsigned int i = 0; i < in.length; ++i) {
  590. switch (in.data[i]) {
  591. case '<':
  592. out.Append("&lt;");break;
  593. case '>':
  594. out.Append("&gt;");break;
  595. case '&':
  596. out.Append("&amp;");break;
  597. case '\"':
  598. out.Append("&quot;");break;
  599. case '\'':
  600. out.Append("&apos;");break;
  601. default:
  602. out.data[out.length++] = in.data[i];
  603. }
  604. }
  605. out.data[out.length] = 0;
  606. }
  607. // -----------------------------------------------------------------------------------
  608. // Write a single node as text dump
  609. void WriteNode(const aiNode* node, FILE* out, unsigned int depth)
  610. {
  611. char prefix[512];
  612. for (unsigned int i = 0; i < depth;++i)
  613. prefix[i] = '\t';
  614. prefix[depth] = '\0';
  615. const aiMatrix4x4& m = node->mTransformation;
  616. aiString name;
  617. ConvertName(name,node->mName);
  618. fprintf(out,"%s<Node name=\"%s\"> \n"
  619. "%s\t<Matrix4> \n"
  620. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  621. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  622. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  623. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  624. "%s\t</Matrix4> \n",
  625. prefix,name.data,prefix,
  626. prefix,m.a1,m.a2,m.a3,m.a4,
  627. prefix,m.b1,m.b2,m.b3,m.b4,
  628. prefix,m.c1,m.c2,m.c3,m.c4,
  629. prefix,m.d1,m.d2,m.d3,m.d4,prefix);
  630. if (node->mNumMeshes) {
  631. fprintf(out, "%s\t<MeshRefs num=\"%i\">\n%s\t",
  632. prefix,node->mNumMeshes,prefix);
  633. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  634. fprintf(out,"%i ",node->mMeshes[i]);
  635. }
  636. fprintf(out,"\n%s\t</MeshRefs>\n",prefix);
  637. }
  638. if (node->mNumChildren) {
  639. fprintf(out,"%s\t<NodeList num=\"%i\">\n",
  640. prefix,node->mNumChildren);
  641. for (unsigned int i = 0; i < node->mNumChildren;++i) {
  642. WriteNode(node->mChildren[i],out,depth+2);
  643. }
  644. fprintf(out,"%s\t</NodeList>\n",prefix);
  645. }
  646. fprintf(out,"%s</Node>\n",prefix);
  647. }
  648. // -------------------------------------------------------------------------------
  649. const char* TextureTypeToString(aiTextureType in)
  650. {
  651. switch (in)
  652. {
  653. case aiTextureType_NONE:
  654. return "n/a";
  655. case aiTextureType_DIFFUSE:
  656. return "Diffuse";
  657. case aiTextureType_SPECULAR:
  658. return "Specular";
  659. case aiTextureType_AMBIENT:
  660. return "Ambient";
  661. case aiTextureType_EMISSIVE:
  662. return "Emissive";
  663. case aiTextureType_OPACITY:
  664. return "Opacity";
  665. case aiTextureType_NORMALS:
  666. return "Normals";
  667. case aiTextureType_HEIGHT:
  668. return "Height";
  669. case aiTextureType_SHININESS:
  670. return "Shininess";
  671. case aiTextureType_DISPLACEMENT:
  672. return "Displacement";
  673. case aiTextureType_LIGHTMAP:
  674. return "Lightmap";
  675. case aiTextureType_REFLECTION:
  676. return "Reflection";
  677. case aiTextureType_UNKNOWN:
  678. return "Unknown";
  679. default:
  680. break;
  681. }
  682. ai_assert(false);
  683. return "BUG";
  684. }
  685. // -----------------------------------------------------------------------------------
  686. // Some chuncks of text will need to be encoded for XML
  687. // http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string#5665377
  688. static std::string encodeXML(const std::string& data) {
  689. std::string buffer;
  690. buffer.reserve(data.size());
  691. for(size_t pos = 0; pos != data.size(); ++pos) {
  692. switch(data[pos]) {
  693. case '&': buffer.append("&amp;"); break;
  694. case '\"': buffer.append("&quot;"); break;
  695. case '\'': buffer.append("&apos;"); break;
  696. case '<': buffer.append("&lt;"); break;
  697. case '>': buffer.append("&gt;"); break;
  698. default: buffer.append(&data[pos], 1); break;
  699. }
  700. }
  701. return buffer;
  702. }
  703. // -----------------------------------------------------------------------------------
  704. // Write a text model dump
  705. void WriteDump(const aiScene* scene, FILE* out, const char* src, const char* cmd, bool shortened)
  706. {
  707. time_t tt = ::time(NULL);
  708. tm* p = ::gmtime(&tt);
  709. std::string c = cmd;
  710. std::string::size_type s;
  711. // https://sourceforge.net/tracker/?func=detail&aid=3167364&group_id=226462&atid=1067632
  712. // -- not allowed in XML comments
  713. while((s = c.find("--")) != std::string::npos) {
  714. c[s] = '?';
  715. }
  716. aiString name;
  717. // write header
  718. fprintf(out,
  719. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  720. "<ASSIMP format_id=\"1\">\n\n"
  721. "<!-- XML Model dump produced by assimp dump\n"
  722. " Library version: %i.%i.%i\n"
  723. " Source: %s\n"
  724. " Command line: %s\n"
  725. " %s\n"
  726. "-->"
  727. " \n\n"
  728. "<Scene flags=\"%i\" postprocessing=\"%i\">\n",
  729. aiGetVersionMajor(),aiGetVersionMinor(),aiGetVersionRevision(),src,c.c_str(),asctime(p),
  730. scene->mFlags,
  731. 0 /*globalImporter->GetEffectivePostProcessing()*/);
  732. // write the node graph
  733. WriteNode(scene->mRootNode, out, 0);
  734. #if 0
  735. // write cameras
  736. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  737. aiCamera* cam = scene->mCameras[i];
  738. ConvertName(name,cam->mName);
  739. // camera header
  740. fprintf(out,"\t<Camera parent=\"%s\">\n"
  741. "\t\t<Vector3 name=\"up\" > %0 8f %0 8f %0 8f </Vector3>\n"
  742. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n"
  743. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  744. "\t\t<Float name=\"fov\" > %f </Float>\n"
  745. "\t\t<Float name=\"aspect\" > %f </Float>\n"
  746. "\t\t<Float name=\"near_clip\" > %f </Float>\n"
  747. "\t\t<Float name=\"far_clip\" > %f </Float>\n"
  748. "\t</Camera>\n",
  749. name.data,
  750. cam->mUp.x,cam->mUp.y,cam->mUp.z,
  751. cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
  752. cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
  753. cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
  754. }
  755. // write lights
  756. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  757. aiLight* l = scene->mLights[i];
  758. ConvertName(name,l->mName);
  759. // light header
  760. fprintf(out,"\t<Light parent=\"%s\"> type=\"%s\"\n"
  761. "\t\t<Vector3 name=\"diffuse\" > %0 8f %0 8f %0 8f </Vector3>\n"
  762. "\t\t<Vector3 name=\"specular\" > %0 8f %0 8f %0 8f </Vector3>\n"
  763. "\t\t<Vector3 name=\"ambient\" > %0 8f %0 8f %0 8f </Vector3>\n",
  764. name.data,
  765. (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
  766. (l->mType == aiLightSource_POINT ? "point" : "spot" )),
  767. l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
  768. l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
  769. l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
  770. if (l->mType != aiLightSource_DIRECTIONAL) {
  771. fprintf(out,
  772. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  773. "\t\t<Float name=\"atten_cst\" > %f </Float>\n"
  774. "\t\t<Float name=\"atten_lin\" > %f </Float>\n"
  775. "\t\t<Float name=\"atten_sqr\" > %f </Float>\n",
  776. l->mPosition.x,l->mPosition.y,l->mPosition.z,
  777. l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
  778. }
  779. if (l->mType != aiLightSource_POINT) {
  780. fprintf(out,
  781. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n",
  782. l->mDirection.x,l->mDirection.y,l->mDirection.z);
  783. }
  784. if (l->mType == aiLightSource_SPOT) {
  785. fprintf(out,
  786. "\t\t<Float name=\"cone_out\" > %f </Float>\n"
  787. "\t\t<Float name=\"cone_inn\" > %f </Float>\n",
  788. l->mAngleOuterCone,l->mAngleInnerCone);
  789. }
  790. fprintf(out,"\t</Light>\n");
  791. }
  792. #endif
  793. // write textures
  794. if (scene->mNumTextures) {
  795. fprintf(out,"<TextureList num=\"%i\">\n",scene->mNumTextures);
  796. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  797. aiTexture* tex = scene->mTextures[i];
  798. bool compressed = (tex->mHeight == 0);
  799. // mesh header
  800. fprintf(out,"\t<Texture width=\"%i\" height=\"%i\" compressed=\"%s\"> \n",
  801. (compressed ? -1 : tex->mWidth),(compressed ? -1 : tex->mHeight),
  802. (compressed ? "true" : "false"));
  803. if (compressed) {
  804. fprintf(out,"\t\t<Data length=\"%i\"> \n",tex->mWidth);
  805. if (!shortened) {
  806. for (unsigned int n = 0; n < tex->mWidth;++n) {
  807. fprintf(out,"\t\t\t%2x",reinterpret_cast<uint8_t*>(tex->pcData)[n]);
  808. if (n && !(n % 50)) {
  809. fprintf(out,"\n");
  810. }
  811. }
  812. }
  813. }
  814. else if (!shortened){
  815. fprintf(out,"\t\t<Data length=\"%i\"> \n",tex->mWidth*tex->mHeight*4);
  816. // const unsigned int width = (unsigned int)log10((double)std::max(tex->mHeight,tex->mWidth))+1;
  817. for (unsigned int y = 0; y < tex->mHeight;++y) {
  818. for (unsigned int x = 0; x < tex->mWidth;++x) {
  819. aiTexel* tx = tex->pcData + y*tex->mWidth+x;
  820. unsigned int r = tx->r,g=tx->g,b=tx->b,a=tx->a;
  821. fprintf(out,"\t\t\t%2x %2x %2x %2x",r,g,b,a);
  822. // group by four for readibility
  823. if (0 == (x+y*tex->mWidth) % 4)
  824. fprintf(out,"\n");
  825. }
  826. }
  827. }
  828. fprintf(out,"\t\t</Data>\n\t</Texture>\n");
  829. }
  830. fprintf(out,"</TextureList>\n");
  831. }
  832. // write materials
  833. if (scene->mNumMaterials) {
  834. fprintf(out,"<MaterialList num=\"%i\">\n",scene->mNumMaterials);
  835. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  836. const aiMaterial* mat = scene->mMaterials[i];
  837. fprintf(out,"\t<Material>\n");
  838. fprintf(out,"\t\t<MatPropertyList num=\"%i\">\n",mat->mNumProperties);
  839. for (unsigned int n = 0; n < mat->mNumProperties;++n) {
  840. const aiMaterialProperty* prop = mat->mProperties[n];
  841. const char* sz = "";
  842. if (prop->mType == aiPTI_Float) {
  843. sz = "float";
  844. }
  845. else if (prop->mType == aiPTI_Integer) {
  846. sz = "integer";
  847. }
  848. else if (prop->mType == aiPTI_String) {
  849. sz = "string";
  850. }
  851. else if (prop->mType == aiPTI_Buffer) {
  852. sz = "binary_buffer";
  853. }
  854. fprintf(out,"\t\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%i\"",
  855. prop->mKey.data, sz,
  856. ::TextureTypeToString((aiTextureType)prop->mSemantic),prop->mIndex);
  857. if (prop->mType == aiPTI_Float) {
  858. fprintf(out," size=\"%i\">\n\t\t\t\t",
  859. static_cast<int>(prop->mDataLength/sizeof(float)));
  860. for (unsigned int p = 0; p < prop->mDataLength/sizeof(float);++p) {
  861. fprintf(out,"%f ",*((float*)(prop->mData+p*sizeof(float))));
  862. }
  863. }
  864. else if (prop->mType == aiPTI_Integer) {
  865. fprintf(out," size=\"%i\">\n\t\t\t\t",
  866. static_cast<int>(prop->mDataLength/sizeof(int)));
  867. for (unsigned int p = 0; p < prop->mDataLength/sizeof(int);++p) {
  868. fprintf(out,"%i ",*((int*)(prop->mData+p*sizeof(int))));
  869. }
  870. }
  871. else if (prop->mType == aiPTI_Buffer) {
  872. fprintf(out," size=\"%i\">\n\t\t\t\t",
  873. static_cast<int>(prop->mDataLength));
  874. for (unsigned int p = 0; p < prop->mDataLength;++p) {
  875. fprintf(out,"%2x ",prop->mData[p]);
  876. if (p && 0 == p%30) {
  877. fprintf(out,"\n\t\t\t\t");
  878. }
  879. }
  880. }
  881. else if (prop->mType == aiPTI_String) {
  882. fprintf(out,">\n\t\t\t\t\"%s\"",encodeXML(prop->mData+4).c_str() /* skip length */);
  883. }
  884. fprintf(out,"\n\t\t\t</MatProperty>\n");
  885. }
  886. fprintf(out,"\t\t</MatPropertyList>\n");
  887. fprintf(out,"\t</Material>\n");
  888. }
  889. fprintf(out,"</MaterialList>\n");
  890. }
  891. // write animations
  892. if (scene->mNumAnimations) {
  893. fprintf(out,"<AnimationList num=\"%i\">\n",scene->mNumAnimations);
  894. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  895. aiAnimation* anim = scene->mAnimations[i];
  896. // anim header
  897. ConvertName(name,anim->mName);
  898. fprintf(out,"\t<Animation name=\"%s\" duration=\"%e\" tick_cnt=\"%e\">\n",
  899. name.data, anim->mDuration, anim->mTicksPerSecond);
  900. // write bone animation channels
  901. if (anim->mNumChannels) {
  902. fprintf(out,"\t\t<NodeAnimList num=\"%i\">\n",anim->mNumChannels);
  903. for (unsigned int n = 0; n < anim->mNumChannels;++n) {
  904. aiNodeAnim* nd = anim->mChannels[n];
  905. // node anim header
  906. ConvertName(name,nd->mNodeName);
  907. fprintf(out,"\t\t\t<NodeAnim node=\"%s\">\n",name.data);
  908. if (!shortened) {
  909. // write position keys
  910. if (nd->mNumPositionKeys) {
  911. fprintf(out,"\t\t\t\t<PositionKeyList num=\"%i\">\n",nd->mNumPositionKeys);
  912. for (unsigned int a = 0; a < nd->mNumPositionKeys;++a) {
  913. aiVectorKey* vc = nd->mPositionKeys+a;
  914. fprintf(out,"\t\t\t\t\t<PositionKey time=\"%e\">\n"
  915. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</PositionKey>\n",
  916. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  917. }
  918. fprintf(out,"\t\t\t\t</PositionKeyList>\n");
  919. }
  920. // write scaling keys
  921. if (nd->mNumScalingKeys) {
  922. fprintf(out,"\t\t\t\t<ScalingKeyList num=\"%i\">\n",nd->mNumScalingKeys);
  923. for (unsigned int a = 0; a < nd->mNumScalingKeys;++a) {
  924. aiVectorKey* vc = nd->mScalingKeys+a;
  925. fprintf(out,"\t\t\t\t\t<ScalingKey time=\"%e\">\n"
  926. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</ScalingKey>\n",
  927. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  928. }
  929. fprintf(out,"\t\t\t\t</ScalingKeyList>\n");
  930. }
  931. // write rotation keys
  932. if (nd->mNumRotationKeys) {
  933. fprintf(out,"\t\t\t\t<RotationKeyList num=\"%i\">\n",nd->mNumRotationKeys);
  934. for (unsigned int a = 0; a < nd->mNumRotationKeys;++a) {
  935. aiQuatKey* vc = nd->mRotationKeys+a;
  936. fprintf(out,"\t\t\t\t\t<RotationKey time=\"%e\">\n"
  937. "\t\t\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t\t\t</RotationKey>\n",
  938. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,vc->mValue.w);
  939. }
  940. fprintf(out,"\t\t\t\t</RotationKeyList>\n");
  941. }
  942. }
  943. fprintf(out,"\t\t\t</NodeAnim>\n");
  944. }
  945. fprintf(out,"\t\t</NodeAnimList>\n");
  946. }
  947. fprintf(out,"\t</Animation>\n");
  948. }
  949. fprintf(out,"</AnimationList>\n");
  950. }
  951. // write meshes
  952. if (scene->mNumMeshes) {
  953. fprintf(out,"<MeshList num=\"%i\">\n",scene->mNumMeshes);
  954. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  955. aiMesh* mesh = scene->mMeshes[i];
  956. // const unsigned int width = (unsigned int)log10((double)mesh->mNumVertices)+1;
  957. // mesh header
  958. fprintf(out,"\t<Mesh types=\"%s %s %s %s\" material_index=\"%i\">\n",
  959. (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
  960. (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
  961. (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
  962. (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
  963. mesh->mMaterialIndex);
  964. // bones
  965. if (mesh->mNumBones) {
  966. fprintf(out,"\t\t<BoneList num=\"%i\">\n",mesh->mNumBones);
  967. for (unsigned int n = 0; n < mesh->mNumBones;++n) {
  968. aiBone* bone = mesh->mBones[n];
  969. ConvertName(name,bone->mName);
  970. // bone header
  971. fprintf(out,"\t\t\t<Bone name=\"%s\">\n"
  972. "\t\t\t\t<Matrix4> \n"
  973. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  974. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  975. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  976. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  977. "\t\t\t\t</Matrix4> \n",
  978. name.data,
  979. bone->mOffsetMatrix.a1,bone->mOffsetMatrix.a2,bone->mOffsetMatrix.a3,bone->mOffsetMatrix.a4,
  980. bone->mOffsetMatrix.b1,bone->mOffsetMatrix.b2,bone->mOffsetMatrix.b3,bone->mOffsetMatrix.b4,
  981. bone->mOffsetMatrix.c1,bone->mOffsetMatrix.c2,bone->mOffsetMatrix.c3,bone->mOffsetMatrix.c4,
  982. bone->mOffsetMatrix.d1,bone->mOffsetMatrix.d2,bone->mOffsetMatrix.d3,bone->mOffsetMatrix.d4);
  983. if (!shortened && bone->mNumWeights) {
  984. fprintf(out,"\t\t\t\t<WeightList num=\"%i\">\n",bone->mNumWeights);
  985. // bone weights
  986. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  987. aiVertexWeight* wght = bone->mWeights+a;
  988. fprintf(out,"\t\t\t\t\t<Weight index=\"%i\">\n\t\t\t\t\t\t%f\n\t\t\t\t\t</Weight>\n",
  989. wght->mVertexId,wght->mWeight);
  990. }
  991. fprintf(out,"\t\t\t\t</WeightList>\n");
  992. }
  993. fprintf(out,"\t\t\t</Bone>\n");
  994. }
  995. fprintf(out,"\t\t</BoneList>\n");
  996. }
  997. // faces
  998. if (!shortened && mesh->mNumFaces) {
  999. fprintf(out,"\t\t<FaceList num=\"%i\">\n",mesh->mNumFaces);
  1000. for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
  1001. aiFace& f = mesh->mFaces[n];
  1002. fprintf(out,"\t\t\t<Face num=\"%i\">\n"
  1003. "\t\t\t\t",f.mNumIndices);
  1004. for (unsigned int j = 0; j < f.mNumIndices;++j)
  1005. fprintf(out,"%i ",f.mIndices[j]);
  1006. fprintf(out,"\n\t\t\t</Face>\n");
  1007. }
  1008. fprintf(out,"\t\t</FaceList>\n");
  1009. }
  1010. // vertex positions
  1011. if (mesh->HasPositions()) {
  1012. fprintf(out,"\t\t<Positions num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  1013. if (!shortened) {
  1014. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1015. fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  1016. mesh->mVertices[n].x,
  1017. mesh->mVertices[n].y,
  1018. mesh->mVertices[n].z);
  1019. }
  1020. }
  1021. fprintf(out,"\t\t</Positions>\n");
  1022. }
  1023. // vertex normals
  1024. if (mesh->HasNormals()) {
  1025. fprintf(out,"\t\t<Normals num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  1026. if (!shortened) {
  1027. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1028. fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  1029. mesh->mNormals[n].x,
  1030. mesh->mNormals[n].y,
  1031. mesh->mNormals[n].z);
  1032. }
  1033. }
  1034. else {
  1035. }
  1036. fprintf(out,"\t\t</Normals>\n");
  1037. }
  1038. // vertex tangents and bitangents
  1039. if (mesh->HasTangentsAndBitangents()) {
  1040. fprintf(out,"\t\t<Tangents num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  1041. if (!shortened) {
  1042. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1043. fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  1044. mesh->mTangents[n].x,
  1045. mesh->mTangents[n].y,
  1046. mesh->mTangents[n].z);
  1047. }
  1048. }
  1049. fprintf(out,"\t\t</Tangents>\n");
  1050. fprintf(out,"\t\t<Bitangents num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  1051. if (!shortened) {
  1052. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1053. fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  1054. mesh->mBitangents[n].x,
  1055. mesh->mBitangents[n].y,
  1056. mesh->mBitangents[n].z);
  1057. }
  1058. }
  1059. fprintf(out,"\t\t</Bitangents>\n");
  1060. }
  1061. // texture coordinates
  1062. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
  1063. if (!mesh->mTextureCoords[a])
  1064. break;
  1065. fprintf(out,"\t\t<TextureCoords num=\"%i\" set=\"%i\" num_components=\"%i\"> \n",mesh->mNumVertices,
  1066. a,mesh->mNumUVComponents[a]);
  1067. if (!shortened) {
  1068. if (mesh->mNumUVComponents[a] == 3) {
  1069. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1070. fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  1071. mesh->mTextureCoords[a][n].x,
  1072. mesh->mTextureCoords[a][n].y,
  1073. mesh->mTextureCoords[a][n].z);
  1074. }
  1075. }
  1076. else {
  1077. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1078. fprintf(out,"\t\t%0 8f %0 8f\n",
  1079. mesh->mTextureCoords[a][n].x,
  1080. mesh->mTextureCoords[a][n].y);
  1081. }
  1082. }
  1083. }
  1084. fprintf(out,"\t\t</TextureCoords>\n");
  1085. }
  1086. // vertex colors
  1087. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
  1088. if (!mesh->mColors[a])
  1089. break;
  1090. fprintf(out,"\t\t<Colors num=\"%i\" set=\"%i\" num_components=\"4\"> \n",mesh->mNumVertices,a);
  1091. if (!shortened) {
  1092. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  1093. fprintf(out,"\t\t%0 8f %0 8f %0 8f %0 8f\n",
  1094. mesh->mColors[a][n].r,
  1095. mesh->mColors[a][n].g,
  1096. mesh->mColors[a][n].b,
  1097. mesh->mColors[a][n].a);
  1098. }
  1099. }
  1100. fprintf(out,"\t\t</Colors>\n");
  1101. }
  1102. fprintf(out,"\t</Mesh>\n");
  1103. }
  1104. fprintf(out,"</MeshList>\n");
  1105. }
  1106. fprintf(out,"</Scene>\n</ASSIMP>");
  1107. }
  1108. // -----------------------------------------------------------------------------------
  1109. int Assimp_Dump (const char* const* params, unsigned int num)
  1110. {
  1111. const char* fail = "assimp dump: Invalid number of arguments. "
  1112. "See \'assimp dump --help\'\r\n";
  1113. if (num < 1) {
  1114. printf("%s", fail);
  1115. return 1;
  1116. }
  1117. // --help
  1118. if (!strcmp( params[0], "-h") || !strcmp( params[0], "--help") || !strcmp( params[0], "-?") ) {
  1119. printf("%s",AICMD_MSG_DUMP_HELP);
  1120. return 0;
  1121. }
  1122. // asssimp dump in out [options]
  1123. if (num < 1) {
  1124. printf("%s", fail);
  1125. return 1;
  1126. }
  1127. std::string in = std::string(params[0]);
  1128. std::string out = (num > 1 ? std::string(params[1]) : std::string("-"));
  1129. // store full command line
  1130. std::string cmd;
  1131. for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num;++i) {
  1132. if (!params[i])continue;
  1133. cmd.append(params[i]);
  1134. cmd.append(" ");
  1135. }
  1136. // get import flags
  1137. ImportData import;
  1138. ProcessStandardArguments(import,params+1,num-1);
  1139. bool binary = false, shortened = false,compressed=false;
  1140. // process other flags
  1141. for (unsigned int i = 1; i < num;++i) {
  1142. if (!params[i])continue;
  1143. if (!strcmp( params[i], "-b") || !strcmp( params[i], "--binary")) {
  1144. binary = true;
  1145. }
  1146. else if (!strcmp( params[i], "-s") || !strcmp( params[i], "--short")) {
  1147. shortened = true;
  1148. }
  1149. else if (!strcmp( params[i], "-z") || !strcmp( params[i], "--compressed")) {
  1150. compressed = true;
  1151. }
  1152. #if 0
  1153. else if (i > 2 || params[i][0] == '-') {
  1154. ::printf("Unknown parameter: %s\n",params[i]);
  1155. return 10;
  1156. }
  1157. #endif
  1158. }
  1159. if (out[0] == '-') {
  1160. // take file name from input file
  1161. std::string::size_type s = in.find_last_of('.');
  1162. if (s == std::string::npos) {
  1163. s = in.length();
  1164. }
  1165. out = in.substr(0,s);
  1166. out.append((binary ? ".assbin" : ".assxml"));
  1167. if (shortened && binary) {
  1168. out.append(".regress");
  1169. }
  1170. }
  1171. // import the main model
  1172. const aiScene* scene = ImportModel(import,in);
  1173. if (!scene) {
  1174. printf("assimp dump: Unable to load input file %s\n",in.c_str());
  1175. return 5;
  1176. }
  1177. // open the output file and build the dump
  1178. FILE* o = ::fopen(out.c_str(),(binary ? "wb" : "wt"));
  1179. if (!o) {
  1180. printf("assimp dump: Unable to open output file %s\n",out.c_str());
  1181. return 12;
  1182. }
  1183. if (binary) {
  1184. WriteBinaryDump (scene,o,in.c_str(),cmd.c_str(),shortened,compressed,import);
  1185. }
  1186. else WriteDump (scene,o,in.c_str(),cmd.c_str(),shortened);
  1187. fclose(o);
  1188. if (compressed && binary) {
  1189. CompressBinaryDump(out.c_str(),ASSBIN_HEADER_LENGTH);
  1190. }
  1191. printf("assimp dump: Wrote output dump %s\n",out.c_str());
  1192. return 0;
  1193. }