WriteDumb.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. "todo assimp dumb help";
  41. // -----------------------------------------------------------------------------------
  42. // Compress a binary dump file (beginning at offset head_size)
  43. void CompressBinaryDump(const char* file, unsigned int head_size)
  44. {
  45. // for simplicity ... copy the file into memory again and compress it there
  46. FILE* p = ::fopen(file,"r");
  47. ::fseek(p,0,SEEK_END);
  48. const unsigned int size = (unsigned int)::ftell(p);
  49. ::fseek(p,0,SEEK_SET);
  50. if (size<head_size) {
  51. ::fclose(p);
  52. return;
  53. }
  54. uint8_t* data = new uint8_t[size];
  55. ::fread(data,1,size,p);
  56. uLongf out_size = (uLongf)((size-head_size) * 1.001 + 12.);
  57. uint8_t* out = new uint8_t[out_size];
  58. compress2(out,&out_size,data+head_size,size-head_size,9);
  59. ::fclose(p);
  60. p = ::fopen(file,"w");
  61. ::fwrite(data,head_size,1,p);
  62. ::fwrite(&out_size,sizeof(uLongf),1,p); // write size of uncompressed data
  63. ::fwrite(out,out_size,1,p);
  64. ::fclose(p);
  65. delete[] data;
  66. delete[] out;
  67. }
  68. // -----------------------------------------------------------------------------------
  69. // Write a magic start value for each serialized data structure
  70. inline void WriteMagic(const char* msg, FILE* out)
  71. {
  72. ::fwrite(msg,3,1,out);
  73. }
  74. // -----------------------------------------------------------------------------------
  75. // Serialize an aiString
  76. inline void WriteAiString(const aiString& s, FILE* out)
  77. {
  78. const uint32_t s2 = (uint32_t)s.length;
  79. ::fwrite(&s,4,1,out);
  80. ::fwrite(s.data,s2,1,out);
  81. }
  82. // -----------------------------------------------------------------------------------
  83. // Serialize an unsigned int as uint32_t
  84. inline void WriteInteger(unsigned int w, FILE* out)
  85. {
  86. const uint32_t t = (uint32_t)w;
  87. ::fwrite(&t,4,1,out);
  88. }
  89. // -----------------------------------------------------------------------------------
  90. // Serialize an unsigned int as uint16_t
  91. inline void WriteShort(unsigned int w, FILE* out)
  92. {
  93. const uint16_t t = (uint16_t)w;
  94. ::fwrite(&t,2,1,out);
  95. }
  96. // -----------------------------------------------------------------------------------
  97. // Serialize a float
  98. inline void WriteFloat(float f, FILE* out)
  99. {
  100. ::fwrite(&f,4,1,out);
  101. }
  102. // -----------------------------------------------------------------------------------
  103. // Serialize a double
  104. inline void WriteDouble(double f, FILE* out)
  105. {
  106. ::fwrite(&f,8,1,out);
  107. }
  108. // -----------------------------------------------------------------------------------
  109. // Serialize a vec3
  110. inline void WriteVec3(const aiVector3D& v, FILE* out)
  111. {
  112. ::fwrite(&v,12,1,out);
  113. }
  114. // -----------------------------------------------------------------------------------
  115. // Serialize a mat4x4
  116. inline void WriteMat4x4(const aiMatrix4x4& m, FILE* out)
  117. {
  118. for (unsigned int i = 0; i < 4;++i)
  119. for (unsigned int i2 = 0; i2 < 4;++i2)
  120. WriteFloat(m[i][i2],out);
  121. }
  122. // -----------------------------------------------------------------------------------
  123. // Write a single node in a binary dump
  124. void WriteBinaryNode(const aiNode* node, FILE* out)
  125. {
  126. WriteMagic("#ND",out);
  127. WriteAiString(node->mName,out);
  128. WriteMat4x4(node->mTransformation,out);
  129. WriteInteger(node->mNumMeshes,out);
  130. for (unsigned int i = 0; i < node->mNumMeshes;++i)
  131. WriteInteger(node->mMeshes[i],out);
  132. WriteInteger(node->mNumChildren,out);
  133. for (unsigned int i = 0; i < node->mNumChildren;++i)
  134. WriteBinaryNode(node->mChildren[i],out);
  135. }
  136. // -----------------------------------------------------------------------------------
  137. // Write the min/max values of an array of Ts to the file
  138. template <typename T>
  139. inline void WriteBounds(const T* in, unsigned int size, FILE* out)
  140. {
  141. T minc,maxc;
  142. ArrayBounds(in,size,minc,maxc);
  143. ::fwrite(&minc,sizeof(T),1,out);
  144. ::fwrite(&maxc,sizeof(T),1,out);
  145. }
  146. // -----------------------------------------------------------------------------------
  147. // Write a binary model dump
  148. void WriteBinaryDump(const aiScene* scene, FILE* out, const char* src, const char* cmd,
  149. bool shortened, bool compressed, ImportData& imp)
  150. {
  151. time_t tt = ::time(NULL);
  152. tm* p = ::gmtime(&tt);
  153. // header
  154. ::fprintf(out,"ASSIMP.binary-dump.%s.",::asctime(p));
  155. // == 45 bytes
  156. WriteInteger(aiGetVersionMajor(),out);
  157. WriteInteger(aiGetVersionMinor(),out);
  158. WriteInteger(aiGetVersionRevision(),out);
  159. WriteInteger(aiGetCompileFlags(),out);
  160. WriteShort(shortened,out);
  161. WriteShort(compressed,out);
  162. // == 20 bytes
  163. char buff[256];
  164. ::strncpy(buff,src,256);
  165. ::fwrite(buff,256,1,out);
  166. ::strncpy(buff,cmd,128);
  167. ::fwrite(buff,128,1,out);
  168. // leave 41 bytes free for future extensions
  169. ::memset(buff,0xcd,41);
  170. ::fwrite(buff,32,1,out);
  171. // == 435 bytes
  172. // ==== total header size: 500 bytes
  173. // Up to here the data is uncompressed. For compressed files, the rest
  174. // is compressed using standard DEFLATE from zlib.
  175. // basic scene information
  176. WriteInteger(scene->mFlags,out);
  177. WriteInteger(scene->mNumAnimations,out);
  178. WriteInteger(scene->mNumTextures,out);
  179. WriteInteger(scene->mNumMaterials,out);
  180. WriteInteger(scene->mNumCameras,out);
  181. WriteInteger(scene->mNumLights,out);
  182. WriteInteger(scene->mNumMeshes,out);
  183. // write node graph
  184. WriteBinaryNode(scene->mRootNode,out);
  185. // write materials
  186. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  187. const aiMaterial* mat = scene->mMaterials[i];
  188. WriteMagic("#MA",out);
  189. WriteInteger(mat->mNumProperties,out);
  190. for (unsigned int a = 0; a < mat->mNumProperties;++a) {
  191. const aiMaterialProperty* prop = mat->mProperties[a];
  192. WriteMagic("#MP",out);
  193. WriteAiString(prop->mKey,out);
  194. WriteInteger(prop->mSemantic,out);
  195. WriteInteger(prop->mIndex,out);
  196. WriteInteger(prop->mDataLength,out);
  197. ::fwrite(prop->mData,prop->mDataLength,1,out);
  198. }
  199. }
  200. // write cameras
  201. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  202. const aiCamera* cam = scene->mCameras[i];
  203. WriteMagic("#CA",out);
  204. WriteAiString(cam->mName,out);
  205. WriteVec3(cam->mPosition,out);
  206. WriteVec3(cam->mLookAt,out);
  207. WriteVec3(cam->mUp,out);
  208. WriteFloat(cam->mClipPlaneNear,out);
  209. WriteFloat(cam->mClipPlaneFar,out);
  210. WriteFloat(cam->mHorizontalFOV,out);
  211. WriteFloat(cam->mAspect,out);
  212. }
  213. // write lights
  214. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  215. const aiLight* l = scene->mLights[i];
  216. WriteMagic("#LI",out);
  217. WriteAiString(l->mName,out);
  218. WriteInteger(l->mType,out);
  219. WriteVec3((const aiVector3D&)l->mColorDiffuse,out);
  220. WriteVec3((const aiVector3D&)l->mColorSpecular,out);
  221. WriteVec3((const aiVector3D&)l->mColorAmbient,out);
  222. if (l->mType != aiLightSource_DIRECTIONAL) {
  223. WriteVec3(l->mPosition,out);
  224. WriteFloat(l->mAttenuationLinear,out);
  225. WriteFloat(l->mAttenuationConstant,out);
  226. WriteFloat(l->mAttenuationQuadratic,out);
  227. }
  228. if (l->mType != aiLightSource_POINT) {
  229. WriteVec3(l->mDirection,out);
  230. }
  231. if (l->mType == aiLightSource_SPOT) {
  232. WriteFloat(l->mAttenuationConstant,out);
  233. WriteFloat(l->mAttenuationQuadratic,out);
  234. }
  235. }
  236. // write all animations
  237. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  238. const aiAnimation* anim = scene->mAnimations[i];
  239. WriteMagic("#AN",out);
  240. WriteAiString (anim->mName,out);
  241. WriteDouble (anim->mTicksPerSecond,out);
  242. WriteDouble (anim->mDuration,out);
  243. WriteInteger(anim->mNumChannels,out);
  244. for (unsigned int a = 0; a < anim->mNumChannels;++a) {
  245. const aiNodeAnim* nd = anim->mChannels[a];
  246. WriteMagic("#NA",out);
  247. WriteAiString(nd->mNodeName,out);
  248. WriteInteger(nd->mPreState,out);
  249. WriteInteger(nd->mPostState,out);
  250. WriteInteger(nd->mNumPositionKeys,out);
  251. WriteInteger(nd->mNumRotationKeys,out);
  252. WriteInteger(nd->mNumScalingKeys,out);
  253. if (nd->mPositionKeys) {
  254. if (shortened) {
  255. WriteBounds(nd->mPositionKeys,nd->mNumPositionKeys,out);
  256. } // else write as usual
  257. else ::fwrite(nd->mPositionKeys,sizeof(aiVectorKey),nd->mNumPositionKeys,out);
  258. }
  259. if (nd->mRotationKeys) {
  260. if (shortened) {
  261. WriteBounds(nd->mRotationKeys,nd->mNumRotationKeys,out);
  262. } // else write as usual
  263. else ::fwrite(nd->mRotationKeys,sizeof(aiQuatKey),nd->mNumRotationKeys,out);
  264. }
  265. if (nd->mScalingKeys) {
  266. if (shortened) {
  267. WriteBounds(nd->mScalingKeys,nd->mNumScalingKeys,out);
  268. } // else write as usual
  269. else ::fwrite(nd->mScalingKeys,sizeof(aiVectorKey),nd->mNumScalingKeys,out);
  270. }
  271. }
  272. }
  273. // write all meshes
  274. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  275. const aiMesh* mesh = scene->mMeshes[i];
  276. WriteMagic("#ME",out);
  277. WriteInteger(mesh->mPrimitiveTypes,out);
  278. WriteInteger(mesh->mNumBones,out);
  279. WriteInteger(mesh->mNumFaces,out);
  280. WriteInteger(mesh->mNumVertices,out);
  281. WriteInteger(mesh->mMaterialIndex,out);
  282. // write bones
  283. if (mesh->mNumBones) {
  284. for (unsigned int a = 0; a < mesh->mNumBones;++a) {
  285. const aiBone* b = mesh->mBones[a];
  286. WriteMagic("#BN",out);
  287. WriteAiString(b->mName,out);
  288. WriteMat4x4(b->mOffsetMatrix,out);
  289. WriteInteger(b->mNumWeights,out);
  290. // for the moment we write dumb min/max values for the bones, too.
  291. // maybe I'll add a better, hash-like solution later
  292. if (shortened) {
  293. WriteBounds(b->mWeights,b->mNumWeights,out);
  294. } // else write as usual
  295. else ::fwrite(b->mWeights,sizeof(aiVertexWeight),b->mNumWeights,out);
  296. }
  297. }
  298. // write faces. There are no floating-point calculations involved
  299. // in these, so we can write a simple hash over the face data
  300. // to the dump file. We generate a single 32 Bit hash for 512 faces
  301. // using Assimp's standard hashing function.
  302. if (shortened) {
  303. unsigned int processed = 0;
  304. for (unsigned int job;job = std::min(mesh->mNumFaces-processed,512u);processed += job) {
  305. unsigned int hash = 0;
  306. for (unsigned int a = 0; a < job;++a) {
  307. const aiFace& f = mesh->mFaces[processed+a];
  308. hash = SuperFastHash((const char*)&f.mNumIndices,sizeof(unsigned int),hash);
  309. hash = SuperFastHash((const char*) f.mIndices,f.mNumIndices*sizeof(unsigned int),hash);
  310. }
  311. WriteInteger(hash,out);
  312. }
  313. }
  314. else // else write as usual
  315. {
  316. for (unsigned int i = 0; i < mesh->mNumFaces;++i) {
  317. const aiFace& f = mesh->mFaces[i];
  318. WriteInteger(f.mNumIndices,out);
  319. for (unsigned int a = 0; a < f.mNumIndices;++a)
  320. WriteInteger(f.mIndices[a],out);
  321. }
  322. }
  323. // first of all, write bits for all existent vertex components
  324. unsigned int c = 0;
  325. if (mesh->mVertices)
  326. c |= 1;
  327. if (mesh->mNormals)
  328. c |= 2;
  329. if (mesh->mTangents && mesh->mBitangents)
  330. c |= 4;
  331. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
  332. if (!mesh->mTextureCoords[n])break;
  333. c |= (8 << n);
  334. }
  335. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
  336. if (!mesh->mColors[n])break;
  337. c |= (16 << n);
  338. }
  339. WriteInteger(c,out);
  340. aiVector3D minVec, maxVec;
  341. if (mesh->mVertices) {
  342. if (shortened) {
  343. WriteBounds(mesh->mVertices,mesh->mNumVertices,out);
  344. } // else write as usual
  345. else ::fwrite(mesh->mVertices,12*mesh->mNumVertices,1,out);
  346. }
  347. if (mesh->mNormals) {
  348. if (shortened) {
  349. WriteBounds(mesh->mNormals,mesh->mNumVertices,out);
  350. } // else write as usual
  351. else ::fwrite(mesh->mNormals,12*mesh->mNumVertices,1,out);
  352. }
  353. if (mesh->mTangents && mesh->mBitangents) {
  354. if (shortened) {
  355. WriteBounds(mesh->mTangents,mesh->mNumVertices,out);
  356. WriteBounds(mesh->mBitangents,mesh->mNumVertices,out);
  357. } // else write as usual
  358. else {
  359. ::fwrite(mesh->mTangents,12*mesh->mNumVertices,1,out);
  360. ::fwrite(mesh->mBitangents,12*mesh->mNumVertices,1,out);
  361. }
  362. }
  363. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_TEXTURECOORDS;++n) {
  364. if (!mesh->mTextureCoords[n])break;
  365. // write number of UV components
  366. WriteInteger(mesh->mNumUVComponents[n],out);
  367. if (shortened) {
  368. WriteBounds(mesh->mTextureCoords[n],mesh->mNumVertices,out);
  369. } // else write as usual
  370. else ::fwrite(mesh->mTextureCoords[n],12*mesh->mNumVertices,1,out);
  371. }
  372. for (unsigned int n = 0; n < AI_MAX_NUMBER_OF_COLOR_SETS;++n) {
  373. if (!mesh->mColors[n])
  374. break;
  375. if (shortened) {
  376. WriteBounds(mesh->mColors[n],mesh->mNumVertices,out);
  377. } // else write as usual
  378. else ::fwrite(mesh->mColors[n],16*mesh->mNumVertices,1,out);
  379. }
  380. }
  381. }
  382. // -----------------------------------------------------------------------------------
  383. // Convert a name to standard XML format
  384. void ConvertName(aiString& out, const aiString& in)
  385. {
  386. out.length = 0;
  387. for (unsigned int i = 0; i < in.length; ++i) {
  388. switch (in.data[i]) {
  389. case '<':
  390. out.Append("&lt;");break;
  391. case '>':
  392. out.Append("&gt;");break;
  393. case '&':
  394. out.Append("&amp;");break;
  395. case '\"':
  396. out.Append("&quot;");break;
  397. case '\'':
  398. out.Append("&apos;");break;
  399. default:
  400. out.data[out.length++] = in.data[i];
  401. }
  402. }
  403. out.data[out.length] = 0;
  404. }
  405. // -----------------------------------------------------------------------------------
  406. // Write a single node as text dump
  407. void WriteNode(const aiNode* node, FILE* out, unsigned int depth)
  408. {
  409. char prefix[512];
  410. for (unsigned int i = 0; i < depth;++i)
  411. prefix[i] = '\t';
  412. prefix[depth] = '\0';
  413. const aiMatrix4x4& m = node->mTransformation;
  414. aiString name;
  415. ConvertName(name,node->mName);
  416. ::fprintf(out,"%s<Node name=\"%s\"> \n"
  417. "%s\t<Matrix4 name=\"trafo\" > \n"
  418. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  419. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  420. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  421. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  422. "%s\t</Matrix4> \n",
  423. prefix,name.data,prefix,
  424. prefix,m.a1,m.a2,m.a3,m.a4,
  425. prefix,m.b1,m.b2,m.b3,m.b4,
  426. prefix,m.c1,m.c2,m.c3,m.c4,
  427. prefix,m.d1,m.d2,m.d3,m.d4,prefix);
  428. if (node->mNumMeshes) {
  429. ::fprintf(out, "%s\t<MeshRefs num=\"%i\">\n%s\t",
  430. prefix,node->mNumMeshes,prefix);
  431. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  432. ::fprintf(out,"%i ",node->mMeshes[i]);
  433. }
  434. ::fprintf(out,"\n%s\t</MeshRefs>\n",prefix);
  435. }
  436. ::fprintf(out,"%s\t<Integer name=\"num_children\">%i</Integer>\n",
  437. prefix,node->mNumChildren);
  438. for (unsigned int i = 0; i < node->mNumChildren;++i)
  439. WriteNode(node->mChildren[i],out,depth+1);
  440. ::fprintf(out,"%s</Node>\n",prefix);
  441. }
  442. // -----------------------------------------------------------------------------------
  443. // Write a text model dump
  444. void WriteDump(const aiScene* scene, FILE* out, const char* src, const char* cmd, bool shortened)
  445. {
  446. time_t tt = ::time(NULL);
  447. tm* p = ::gmtime(&tt);
  448. aiString name;
  449. // write header
  450. ::fprintf(out,
  451. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  452. "<ASSIMP >\n\n"
  453. "<!-- XML Model dump produced by assimp dump\n"
  454. " Library version: %i.%i.%i\n"
  455. " Source: %s\n"
  456. " Command line: %s\n"
  457. " %s\n"
  458. "-->"
  459. " \n\n"
  460. "<Scene NumberOfMeshes=\"%i\" NumberOfMaterials=\"%i\" NumberOfTextures=\"%i\" NumberOfCameras=\"%i\" NumberOfLights=\"%i\" NumberOfAnimations=\"%i\">\n",
  461. aiGetVersionMajor(),aiGetVersionMinor(),aiGetVersionRevision(),src,cmd,::asctime(p),
  462. scene->mNumMeshes, scene->mNumMaterials,scene->mNumTextures,
  463. scene->mNumCameras,scene->mNumLights,scene->mNumAnimations);
  464. // write the node graph
  465. WriteNode(scene->mRootNode, out, 1);
  466. // write cameras
  467. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  468. aiCamera* cam = scene->mCameras[i];
  469. ConvertName(name,cam->mName);
  470. // camera header
  471. ::fprintf(out,"\t<Camera parent=\"%s\">\n"
  472. "\t\t<Vector3 name=\"up\" > %0 8f %0 8f %0 8f </Vector3>\n"
  473. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n"
  474. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  475. "\t\t<Float name=\"fov\" > %f </Float>\n"
  476. "\t\t<Float name=\"aspect\" > %f </Float>\n"
  477. "\t\t<Float name=\"near_clip\" > %f </Float>\n"
  478. "\t\t<Float name=\"far_clip\" > %f </Float>\n"
  479. "\t</Camera>\n",
  480. name.data,
  481. cam->mUp.x,cam->mUp.y,cam->mUp.z,
  482. cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
  483. cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
  484. cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
  485. }
  486. // write lights
  487. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  488. aiLight* l = scene->mLights[i];
  489. ConvertName(name,l->mName);
  490. // light header
  491. ::fprintf(out,"\t<Light parent=\"%s\"> type=\"%s\"\n"
  492. "\t\t<Vector3 name=\"diffuse\" > %0 8f %0 8f %0 8f </Vector3>\n"
  493. "\t\t<Vector3 name=\"specular\" > %0 8f %0 8f %0 8f </Vector3>\n"
  494. "\t\t<Vector3 name=\"ambient\" > %0 8f %0 8f %0 8f </Vector3>\n",
  495. name.data,
  496. (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
  497. (l->mType == aiLightSource_POINT ? "point" : "spot" )),
  498. l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
  499. l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
  500. l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
  501. if (l->mType != aiLightSource_DIRECTIONAL) {
  502. ::fprintf(out,
  503. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  504. "\t\t<Float name=\"atten_cst\" > %f </Float>\n"
  505. "\t\t<Float name=\"atten_lin\" > %f </Float>\n"
  506. "\t\t<Float name=\"atten_sqr\" > %f </Float>\n",
  507. l->mPosition.x,l->mPosition.y,l->mPosition.z,
  508. l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
  509. }
  510. if (l->mType != aiLightSource_POINT) {
  511. ::fprintf(out,
  512. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n",
  513. l->mDirection.x,l->mDirection.y,l->mDirection.z);
  514. }
  515. if (l->mType == aiLightSource_SPOT) {
  516. ::fprintf(out,
  517. "\t\t<Float name=\"cone_out\" > %f </Float>\n"
  518. "\t\t<Float name=\"cone_inn\" > %f </Float>\n",
  519. l->mAngleOuterCone,l->mAngleInnerCone);
  520. }
  521. ::fprintf(out,"\t</Light>\n");
  522. }
  523. // write textures
  524. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  525. aiTexture* tex = scene->mTextures[i];
  526. bool compressed = (tex->mHeight == 0);
  527. // mesh header
  528. ::fprintf(out,"\t<Texture> \n"
  529. "\t\t<Integer name=\"width\" > %i </Integer>\n",
  530. "\t\t<Integer name=\"height\" > %i </Integer>\n",
  531. "\t\t<Boolean name=\"compressed\" > %s </Boolean>\n",
  532. (compressed ? -1 : tex->mWidth),(compressed ? -1 : tex->mHeight),
  533. (compressed ? "true" : "false"));
  534. if (compressed) {
  535. ::fprintf(out,"\t\t<Data length=\"%i\"> %i \n",tex->mWidth);
  536. if (!shortened) {
  537. for (unsigned int n = 0; n < tex->mWidth;++n) {
  538. ::fprintf(out,"\t\t\t%2x",tex->pcData[n]);
  539. if (n && !(n % 50))
  540. ::fprintf(out,"\n");
  541. }
  542. }
  543. }
  544. else if (!shortened){
  545. ::fprintf(out,"\t\t<Data length=\"%i\"> %i \n",tex->mWidth*tex->mHeight*4);
  546. const unsigned int width = (unsigned int)log10((double)std::max(tex->mHeight,tex->mWidth))+1;
  547. for (unsigned int y = 0; y < tex->mHeight;++y) {
  548. for (unsigned int x = 0; x < tex->mWidth;++x) {
  549. aiTexel* tx = tex->pcData + y*tex->mWidth+x;
  550. unsigned int r = tx->r,g=tx->g,b=tx->b,a=tx->a;
  551. ::fprintf(out,"\t\t\t%2x %2x %2x %2x",r,g,b,a);
  552. // group by four for readibility
  553. if (0 == (x+y*tex->mWidth) % 4)
  554. ::fprintf(out,"\n");
  555. }
  556. }
  557. }
  558. ::fprintf(out,"\t\t</Data>\n\t</Texture>\n");
  559. }
  560. // write materials
  561. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  562. const aiMaterial* mat = scene->mMaterials[i];
  563. ::fprintf(out,
  564. "\t<Material NumberOfProperties=\"%i\">\n",mat->mNumProperties);
  565. for (unsigned int n = 0; n < mat->mNumProperties;++n) {
  566. const aiMaterialProperty* prop = mat->mProperties[n];
  567. const char* sz = "";
  568. if (prop->mType == aiPTI_Float)
  569. sz = "float";
  570. else if (prop->mType == aiPTI_Integer)
  571. sz = "integer";
  572. else if (prop->mType == aiPTI_String)
  573. sz = "string";
  574. else if (prop->mType == aiPTI_Buffer)
  575. sz = "binary_buffer";
  576. ::fprintf(out,
  577. "\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%i\"",
  578. prop->mKey.data, sz,
  579. TextureTypeToString((aiTextureType)prop->mSemantic),prop->mIndex);
  580. if (prop->mType == aiPTI_Float) {
  581. ::fprintf(out,
  582. " size=\"%i\">\n\t\t\t",
  583. prop->mDataLength/sizeof(float));
  584. for (unsigned int p = 0; p < prop->mDataLength/sizeof(float);++p)
  585. ::fprintf(out,"%f ",*((float*)(prop->mData+p*sizeof(float))));
  586. }
  587. else if (prop->mType == aiPTI_Integer) {
  588. ::fprintf(out,
  589. " size=\"%i\">\n\t\t\t",
  590. prop->mDataLength/sizeof(int));
  591. for (unsigned int p = 0; p < prop->mDataLength/sizeof(int);++p)
  592. ::fprintf(out,"%i ",*((int*)(prop->mData+p*sizeof(int))));
  593. }
  594. else if (prop->mType == aiPTI_Buffer) {
  595. ::fprintf(out,
  596. " size=\"%i\">\n\t\t\t",
  597. prop->mDataLength);
  598. for (unsigned int p = 0; p < prop->mDataLength;++p) {
  599. ::fprintf(out,"%2x ",prop->mData[p]);
  600. if (p && 0 == p%30)
  601. ::fprintf(out,"\n\t\t\t");
  602. }
  603. }
  604. else if (prop->mType == aiPTI_String) {
  605. ::fprintf(out,">\n\t\t\t\"%s\"",prop->mData+4 /* skip length */);
  606. }
  607. ::fprintf(out,"\n\t\t</MatProperty>\n");
  608. }
  609. ::fprintf(out,"\t</Material>\n");
  610. }
  611. // write animations
  612. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  613. aiAnimation* anim = scene->mAnimations[i];
  614. // anim header
  615. ConvertName(name,anim->mName);
  616. ::fprintf(out,"\t<Animation name=\"%s\">\n"
  617. "\t\t<Integer name=\"num_chan\" > %i </Integer>\n"
  618. "\t\t<Float name=\"duration\" > %e </Float>\n"
  619. "\t\t<Float name=\"tick_cnt\" > %e </Float>\n",
  620. name.data, anim->mNumChannels,anim->mDuration, anim->mTicksPerSecond);
  621. // write bone animation channels
  622. for (unsigned int n = 0; n < anim->mNumChannels;++n) {
  623. aiNodeAnim* nd = anim->mChannels[n];
  624. // node anim header
  625. ConvertName(name,nd->mNodeName);
  626. ::fprintf(out,"\t\t<Channel node=\"%s\">\n"
  627. "\t\t\t<Integer name=\"num_pos_keys\" > %i </Integer>\n"
  628. "\t\t\t<Integer name=\"num_scl_keys\" > %i </Integer>\n"
  629. "\t\t\t<Integer name=\"num_rot_keys\" > %i </Integer>\n",
  630. name.data,nd->mNumPositionKeys,nd->mNumScalingKeys,nd->mNumRotationKeys);
  631. if (!shortened) {
  632. // write position keys
  633. for (unsigned int a = 0; a < nd->mNumPositionKeys;++a) {
  634. aiVectorKey* vc = nd->mPositionKeys+a;
  635. ::fprintf(out,"\t\t\t<PositionKey time=\"%e\">\n"
  636. "\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t</PositionKey>\n",
  637. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,a);
  638. }
  639. // write scaling keys
  640. for (unsigned int a = 0; a < nd->mNumScalingKeys;++a) {
  641. aiVectorKey* vc = nd->mScalingKeys+a;
  642. ::fprintf(out,"\t\t\t<ScalingKey time=\"%e\">\n"
  643. "\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t</ScalingKey>\n",
  644. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,a);
  645. }
  646. // write rotation keys
  647. for (unsigned int a = 0; a < nd->mNumRotationKeys;++a) {
  648. aiQuatKey* vc = nd->mRotationKeys+a;
  649. ::fprintf(out,"\t\t\t<RotationKey time=\"%e\">\n"
  650. "\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t</RotationKey>\n",
  651. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,vc->mValue.w,a);
  652. }
  653. }
  654. ::fprintf(out,"\t\t</Channel>\n",n);
  655. }
  656. ::fprintf(out,"\t</Animation>\n",i);
  657. }
  658. // write meshes
  659. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  660. aiMesh* mesh = scene->mMeshes[i];
  661. const unsigned int width = (unsigned int)log10((double)mesh->mNumVertices)+1;
  662. // mesh header
  663. ::fprintf(out,"\t<Mesh types=\"%s %s %s %s\">\n"
  664. "\t\t<Integer name=\"num_verts\" > %i </Integer>\n"
  665. "\t\t<Integer name=\"num_faces\" > %i </Integer>\n"
  666. "\t\t<Integer name=\"mat_index\" > %i </Integer>\n",
  667. (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
  668. (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
  669. (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
  670. (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
  671. mesh->mNumVertices,mesh->mNumFaces,mesh->mMaterialIndex);
  672. // bones
  673. for (unsigned int n = 0; n < mesh->mNumBones;++n) {
  674. aiBone* bone = mesh->mBones[n];
  675. ConvertName(name,bone->mName);
  676. // bone header
  677. ::fprintf(out,"\t\t<Bone name=\"%s\">\n"
  678. "\t\t\t<Matrix4 name=\"offset\" > \n"
  679. "\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  680. "\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  681. "\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  682. "\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  683. "\t\t\t</Matrix4> \n"
  684. "\t\t\t<Integer name=\"num_weights\" > %i </Integer>\n",
  685. name.data,
  686. bone->mOffsetMatrix.a1,bone->mOffsetMatrix.a2,bone->mOffsetMatrix.a3,bone->mOffsetMatrix.a4,
  687. bone->mOffsetMatrix.b1,bone->mOffsetMatrix.b2,bone->mOffsetMatrix.b3,bone->mOffsetMatrix.b4,
  688. bone->mOffsetMatrix.c1,bone->mOffsetMatrix.c2,bone->mOffsetMatrix.c3,bone->mOffsetMatrix.c4,
  689. bone->mOffsetMatrix.d1,bone->mOffsetMatrix.d2,bone->mOffsetMatrix.d3,bone->mOffsetMatrix.d4,
  690. bone->mNumWeights);
  691. if (!shortened) {
  692. // bone weights
  693. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  694. aiVertexWeight* wght = bone->mWeights+a;
  695. ::fprintf(out,"\t\t\t<VertexWeight index=\"%i\">\n\t\t\t\t%f\n\t\t\t</VertexWeight>\n",
  696. wght->mVertexId,wght->mWeight);
  697. }
  698. }
  699. ::fprintf(out,"\t\t</Bone>\n",n);
  700. }
  701. // faces
  702. if (!shortened) {
  703. for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
  704. aiFace& f = mesh->mFaces[n];
  705. ::fprintf(out,"\t\t<Face num_indices=\"%i\">\n"
  706. "\t\t\t",f.mNumIndices);
  707. for (unsigned int j = 0; j < f.mNumIndices;++j)
  708. ::fprintf(out,"%i ",f.mIndices[j]);
  709. ::fprintf(out,"\n\t\t</Face>\n");
  710. }
  711. }
  712. // vertex positions
  713. if (mesh->HasPositions()) {
  714. ::fprintf(out,"\t\t<Positions> \n");
  715. if (!shortened) {
  716. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  717. ::fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  718. mesh->mVertices[n].x,
  719. mesh->mVertices[n].y,
  720. mesh->mVertices[n].z);
  721. }
  722. }
  723. else {
  724. }
  725. ::fprintf(out,"\t\t</Positions>\n");
  726. }
  727. // vertex normals
  728. if (mesh->HasNormals()) {
  729. ::fprintf(out,"\t\t<Normals> \n");
  730. if (!shortened) {
  731. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  732. ::fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  733. mesh->mNormals[n].x,
  734. mesh->mNormals[n].y,
  735. mesh->mNormals[n].z);
  736. }
  737. }
  738. else {
  739. }
  740. ::fprintf(out,"\t\t</Normals>\n");
  741. }
  742. // vertex tangents and bitangents
  743. if (mesh->HasTangentsAndBitangents()) {
  744. ::fprintf(out,"\t\t<Tangents> \n");
  745. if (!shortened) {
  746. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  747. ::fprintf(out,"\t\t%0 8f %0 8f %0 8f \t %0 8f %0 8f %0 8f\n",
  748. mesh->mTangents[n].x,
  749. mesh->mTangents[n].y,
  750. mesh->mTangents[n].z,
  751. mesh->mBitangents[n].x,
  752. mesh->mBitangents[n].y,
  753. mesh->mBitangents[n].z);
  754. }
  755. }
  756. else {
  757. }
  758. ::fprintf(out,"\t\t</Tangents>\n");
  759. }
  760. // texture coordinates
  761. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
  762. if (!mesh->mTextureCoords[a])
  763. break;
  764. ::fprintf(out,"\t\t<TextureCoords set=\"%i\" num_components=\"%i\"> \n",a,mesh->mNumUVComponents[a]);
  765. if (!shortened) {
  766. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  767. ::fprintf(out,"\t\t%0 8f %0 8f %0 8f\n",
  768. mesh->mTextureCoords[a][n].x,
  769. mesh->mTextureCoords[a][n].y,
  770. mesh->mTextureCoords[a][n].z);
  771. }
  772. }
  773. else {
  774. }
  775. ::fprintf(out,"\t\t</TextureCoords>\n");
  776. }
  777. // vertex colors
  778. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
  779. if (!mesh->mColors[a])
  780. break;
  781. //::fprintf(out,"\t\t<Colors set=\"%i\"> \n",a);
  782. if (!shortened) {
  783. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  784. ::fprintf(out,"\t\t%0 8f %0 8f %0 8f %0 8f\n",
  785. mesh->mColors[a][n].r,
  786. mesh->mColors[a][n].g,
  787. mesh->mColors[a][n].b,
  788. mesh->mColors[a][n].a);
  789. }
  790. }
  791. else {
  792. }
  793. ::fprintf(out,"\t\t</Color>\n");
  794. }
  795. ::fprintf(out,"\t</Mesh>\n");
  796. }
  797. ::fprintf(out,"</Scene>\n</ASSIMP>");
  798. }
  799. // -----------------------------------------------------------------------------------
  800. int Assimp_Dump (const char** params, unsigned int num)
  801. {
  802. if (num < 1) {
  803. ::printf("assimp dump: Invalid number of arguments. See \'assimp dump --help\'\r\n");
  804. return 1;
  805. }
  806. // --help
  807. if (!::strcmp( params[0], "-h") || !::strcmp( params[0], "--help") || !::strcmp( params[0], "-?") ) {
  808. printf(AICMD_MSG_DUMP_HELP);
  809. return 0;
  810. }
  811. // asssimp dump in out [options]
  812. if (num < 1) {
  813. ::printf("assimp dump: Invalid number of arguments. See \'assimp dump --help\'\r\n");
  814. return 1;
  815. }
  816. std::string in = std::string(params[0]);
  817. std::string out = (num > 1 ? std::string(params[1]) : std::string("-"));
  818. // store full command line
  819. std::string cmd;
  820. for (unsigned int i = (out[0] == '-' ? 1 : 2); i < num;++i) {
  821. if (!params[i])continue;
  822. cmd.append(params[i]);
  823. cmd.append(" ");
  824. }
  825. // get import flags
  826. ImportData import;
  827. ProcessStandardArguments(import,params+1,num-1);
  828. bool binary = false, shortened = false,compressed=false;
  829. // process other flags
  830. for (unsigned int i = 1; i < num;++i) {
  831. if (!params[i])continue;
  832. if (!::strcmp( params[i], "-b") || !::strcmp( params[i], "--binary")) {
  833. binary = true;
  834. }
  835. else if (!::strcmp( params[i], "-s") || !::strcmp( params[i], "--short")) {
  836. shortened = true;
  837. }
  838. else if (!::strcmp( params[i], "-z") || !::strcmp( params[i], "--compressed")) {
  839. compressed = true;
  840. }
  841. else if (i > 2 || params[i][0] == '-') {
  842. ::printf("Unknown parameter: %s\n",params[i]);
  843. return 10;
  844. }
  845. }
  846. if (out[0] == '-') {
  847. // take file name from input file
  848. std::string::size_type s = in.find_last_of('.');
  849. if (s == std::string::npos)
  850. s = in.length();
  851. out = in.substr(0,s);
  852. out.append((binary ? ".assbin" : ".assxml"));
  853. if (shortened && binary)
  854. out.append(".regress");
  855. }
  856. // import the main model
  857. const aiScene* scene = ImportModel(import,in);
  858. if (!scene) {
  859. ::printf("assimp dump: Unable to load input file %s\n",in.c_str());
  860. return 5;
  861. }
  862. // open the output file and build the dump
  863. FILE* o = ::fopen(out.c_str(),(binary ? "wb" : "wt"));
  864. if (!o) {
  865. ::printf("assimp dump: Unable to open output file %s\n",out.c_str());
  866. return 12;
  867. }
  868. if (binary)
  869. WriteBinaryDump (scene,o,in.c_str(),cmd.c_str(),shortened,compressed,import);
  870. else WriteDump (scene,o,in.c_str(),cmd.c_str(),shortened);
  871. ::fclose(o);
  872. if (compressed && binary)
  873. CompressBinaryDump(out.c_str(),500);
  874. ::printf("assimp dump: Wrote output dump %s\n",out.c_str());
  875. return 0;
  876. }