WriteDumb.cpp 42 KB

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