WriteDumb.cpp 43 KB

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