WriteDumb.cpp 42 KB

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