AssxmlFileWriter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file AssxmlFileWriter.cpp
  34. * @brief Implementation of Assxml file writer.
  35. */
  36. #include "AssxmlFileWriter.h"
  37. #include "PostProcessing/ProcessHelper.h"
  38. #include <assimp/version.h>
  39. #include <assimp/IOStream.hpp>
  40. #include <assimp/IOSystem.hpp>
  41. #include <assimp/Exporter.hpp>
  42. #include <stdarg.h>
  43. #ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  44. # include <zlib.h>
  45. #else
  46. # include <contrib/zlib/zlib.h>
  47. #endif
  48. #include <time.h>
  49. #include <stdio.h>
  50. #include <memory>
  51. using namespace Assimp;
  52. namespace Assimp {
  53. namespace AssxmlFileWriter {
  54. // -----------------------------------------------------------------------------------
  55. static int ioprintf( IOStream * io, const char *format, ... ) {
  56. using namespace std;
  57. if ( nullptr == io ) {
  58. return -1;
  59. }
  60. static const int Size = 4096;
  61. char sz[ Size ];
  62. ::memset( sz, '\0', Size );
  63. va_list va;
  64. va_start( va, format );
  65. const unsigned int nSize = vsnprintf( sz, Size-1, format, va );
  66. ai_assert( nSize < Size );
  67. va_end( va );
  68. io->Write( sz, sizeof(char), nSize );
  69. return nSize;
  70. }
  71. // -----------------------------------------------------------------------------------
  72. // Convert a name to standard XML format
  73. static void ConvertName(aiString& out, const aiString& in) {
  74. out.length = 0;
  75. for (unsigned int i = 0; i < in.length; ++i) {
  76. switch (in.data[i]) {
  77. case '<':
  78. out.Append("&lt;");break;
  79. case '>':
  80. out.Append("&gt;");break;
  81. case '&':
  82. out.Append("&amp;");break;
  83. case '\"':
  84. out.Append("&quot;");break;
  85. case '\'':
  86. out.Append("&apos;");break;
  87. default:
  88. out.data[out.length++] = in.data[i];
  89. }
  90. }
  91. out.data[out.length] = 0;
  92. }
  93. // -----------------------------------------------------------------------------------
  94. // Write a single node as text dump
  95. static void WriteNode(const aiNode* node, IOStream * io, unsigned int depth) {
  96. char prefix[512];
  97. for (unsigned int i = 0; i < depth;++i)
  98. prefix[i] = '\t';
  99. prefix[depth] = '\0';
  100. const aiMatrix4x4& m = node->mTransformation;
  101. aiString name;
  102. ConvertName(name,node->mName);
  103. ioprintf(io,"%s<Node name=\"%s\"> \n"
  104. "%s\t<Matrix4> \n"
  105. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  106. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  107. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  108. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  109. "%s\t</Matrix4> \n",
  110. prefix,name.data,prefix,
  111. prefix,m.a1,m.a2,m.a3,m.a4,
  112. prefix,m.b1,m.b2,m.b3,m.b4,
  113. prefix,m.c1,m.c2,m.c3,m.c4,
  114. prefix,m.d1,m.d2,m.d3,m.d4,prefix);
  115. if (node->mNumMeshes) {
  116. ioprintf(io, "%s\t<MeshRefs num=\"%u\">\n%s\t",
  117. prefix,node->mNumMeshes,prefix);
  118. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  119. ioprintf(io,"%u ",node->mMeshes[i]);
  120. }
  121. ioprintf(io,"\n%s\t</MeshRefs>\n",prefix);
  122. }
  123. if (node->mNumChildren) {
  124. ioprintf(io,"%s\t<NodeList num=\"%u\">\n",
  125. prefix,node->mNumChildren);
  126. for (unsigned int i = 0; i < node->mNumChildren;++i) {
  127. WriteNode(node->mChildren[i],io,depth+2);
  128. }
  129. ioprintf(io,"%s\t</NodeList>\n",prefix);
  130. }
  131. ioprintf(io,"%s</Node>\n",prefix);
  132. }
  133. // -----------------------------------------------------------------------------------
  134. // Some chuncks of text will need to be encoded for XML
  135. // http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string#5665377
  136. static std::string encodeXML(const std::string& data) {
  137. std::string buffer;
  138. buffer.reserve(data.size());
  139. for(size_t pos = 0; pos != data.size(); ++pos) {
  140. switch(data[pos]) {
  141. case '&': buffer.append("&amp;"); break;
  142. case '\"': buffer.append("&quot;"); break;
  143. case '\'': buffer.append("&apos;"); break;
  144. case '<': buffer.append("&lt;"); break;
  145. case '>': buffer.append("&gt;"); break;
  146. default: buffer.append(&data[pos], 1); break;
  147. }
  148. }
  149. return buffer;
  150. }
  151. // -----------------------------------------------------------------------------------
  152. // Write a text model dump
  153. static
  154. void WriteDump(const char* pFile, const char* cmd, const aiScene* scene, IOStream* io, bool shortened) {
  155. time_t tt = ::time( NULL );
  156. #if _WIN32
  157. tm* p = gmtime(&tt);
  158. #else
  159. struct tm now;
  160. tm* p = gmtime_r(&tt, &now);
  161. #endif
  162. ai_assert(nullptr != p);
  163. std::string c = cmd;
  164. std::string::size_type s;
  165. // https://sourceforge.net/tracker/?func=detail&aid=3167364&group_id=226462&atid=1067632
  166. // -- not allowed in XML comments
  167. while((s = c.find("--")) != std::string::npos) {
  168. c[s] = '?';
  169. }
  170. // write header
  171. std::string header(
  172. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  173. "<ASSIMP format_id=\"1\">\n\n"
  174. "<!-- XML Model dump produced by assimp dump\n"
  175. " Library version: %u.%u.%u\n"
  176. " Source: %s\n"
  177. " Command line: %s\n"
  178. " %s\n"
  179. "-->"
  180. " \n\n"
  181. "<Scene flags=\"%u\" postprocessing=\"%u\">\n"
  182. );
  183. const unsigned int majorVersion( aiGetVersionMajor() );
  184. const unsigned int minorVersion( aiGetVersionMinor() );
  185. const unsigned int rev( aiGetVersionRevision() );
  186. const char *curtime( asctime( p ) );
  187. ioprintf( io, header.c_str(), majorVersion, minorVersion, rev, pFile, c.c_str(), curtime, scene->mFlags, 0u );
  188. // write the node graph
  189. WriteNode(scene->mRootNode, io, 0);
  190. #if 0
  191. // write cameras
  192. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  193. aiCamera* cam = scene->mCameras[i];
  194. ConvertName(name,cam->mName);
  195. // camera header
  196. ioprintf(io,"\t<Camera parent=\"%s\">\n"
  197. "\t\t<Vector3 name=\"up\" > %0 8f %0 8f %0 8f </Vector3>\n"
  198. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n"
  199. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  200. "\t\t<Float name=\"fov\" > %f </Float>\n"
  201. "\t\t<Float name=\"aspect\" > %f </Float>\n"
  202. "\t\t<Float name=\"near_clip\" > %f </Float>\n"
  203. "\t\t<Float name=\"far_clip\" > %f </Float>\n"
  204. "\t</Camera>\n",
  205. name.data,
  206. cam->mUp.x,cam->mUp.y,cam->mUp.z,
  207. cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
  208. cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
  209. cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
  210. }
  211. // write lights
  212. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  213. aiLight* l = scene->mLights[i];
  214. ConvertName(name,l->mName);
  215. // light header
  216. ioprintf(io,"\t<Light parent=\"%s\"> type=\"%s\"\n"
  217. "\t\t<Vector3 name=\"diffuse\" > %0 8f %0 8f %0 8f </Vector3>\n"
  218. "\t\t<Vector3 name=\"specular\" > %0 8f %0 8f %0 8f </Vector3>\n"
  219. "\t\t<Vector3 name=\"ambient\" > %0 8f %0 8f %0 8f </Vector3>\n",
  220. name.data,
  221. (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
  222. (l->mType == aiLightSource_POINT ? "point" : "spot" )),
  223. l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
  224. l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
  225. l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
  226. if (l->mType != aiLightSource_DIRECTIONAL) {
  227. ioprintf(io,
  228. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  229. "\t\t<Float name=\"atten_cst\" > %f </Float>\n"
  230. "\t\t<Float name=\"atten_lin\" > %f </Float>\n"
  231. "\t\t<Float name=\"atten_sqr\" > %f </Float>\n",
  232. l->mPosition.x,l->mPosition.y,l->mPosition.z,
  233. l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
  234. }
  235. if (l->mType != aiLightSource_POINT) {
  236. ioprintf(io,
  237. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n",
  238. l->mDirection.x,l->mDirection.y,l->mDirection.z);
  239. }
  240. if (l->mType == aiLightSource_SPOT) {
  241. ioprintf(io,
  242. "\t\t<Float name=\"cone_out\" > %f </Float>\n"
  243. "\t\t<Float name=\"cone_inn\" > %f </Float>\n",
  244. l->mAngleOuterCone,l->mAngleInnerCone);
  245. }
  246. ioprintf(io,"\t</Light>\n");
  247. }
  248. #endif
  249. aiString name;
  250. // write textures
  251. if (scene->mNumTextures) {
  252. ioprintf(io,"<TextureList num=\"%u\">\n",scene->mNumTextures);
  253. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  254. aiTexture* tex = scene->mTextures[i];
  255. bool compressed = (tex->mHeight == 0);
  256. // mesh header
  257. ioprintf(io,"\t<Texture width=\"%u\" height=\"%u\" compressed=\"%s\"> \n",
  258. (compressed ? -1 : tex->mWidth),(compressed ? -1 : tex->mHeight),
  259. (compressed ? "true" : "false"));
  260. if (compressed) {
  261. ioprintf(io,"\t\t<Data length=\"%u\"> \n",tex->mWidth);
  262. if (!shortened) {
  263. for (unsigned int n = 0; n < tex->mWidth;++n) {
  264. ioprintf(io,"\t\t\t%2x",reinterpret_cast<uint8_t*>(tex->pcData)[n]);
  265. if (n && !(n % 50)) {
  266. ioprintf(io,"\n");
  267. }
  268. }
  269. }
  270. }
  271. else if (!shortened){
  272. ioprintf(io,"\t\t<Data length=\"%u\"> \n",tex->mWidth*tex->mHeight*4);
  273. // const unsigned int width = (unsigned int)std::log10((double)std::max(tex->mHeight,tex->mWidth))+1;
  274. for (unsigned int y = 0; y < tex->mHeight;++y) {
  275. for (unsigned int x = 0; x < tex->mWidth;++x) {
  276. aiTexel* tx = tex->pcData + y*tex->mWidth+x;
  277. unsigned int r = tx->r,g=tx->g,b=tx->b,a=tx->a;
  278. ioprintf(io,"\t\t\t%2x %2x %2x %2x",r,g,b,a);
  279. // group by four for readability
  280. if ( 0 == ( x + y*tex->mWidth ) % 4 ) {
  281. ioprintf( io, "\n" );
  282. }
  283. }
  284. }
  285. }
  286. ioprintf(io,"\t\t</Data>\n\t</Texture>\n");
  287. }
  288. ioprintf(io,"</TextureList>\n");
  289. }
  290. // write materials
  291. if (scene->mNumMaterials) {
  292. ioprintf(io,"<MaterialList num=\"%u\">\n",scene->mNumMaterials);
  293. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  294. const aiMaterial* mat = scene->mMaterials[i];
  295. ioprintf(io,"\t<Material>\n");
  296. ioprintf(io,"\t\t<MatPropertyList num=\"%u\">\n",mat->mNumProperties);
  297. for (unsigned int n = 0; n < mat->mNumProperties;++n) {
  298. const aiMaterialProperty* prop = mat->mProperties[n];
  299. const char* sz = "";
  300. if (prop->mType == aiPTI_Float) {
  301. sz = "float";
  302. }
  303. else if (prop->mType == aiPTI_Integer) {
  304. sz = "integer";
  305. }
  306. else if (prop->mType == aiPTI_String) {
  307. sz = "string";
  308. }
  309. else if (prop->mType == aiPTI_Buffer) {
  310. sz = "binary_buffer";
  311. }
  312. ioprintf(io,"\t\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%u\"",
  313. prop->mKey.data, sz,
  314. ::TextureTypeToString((aiTextureType)prop->mSemantic),prop->mIndex);
  315. if (prop->mType == aiPTI_Float) {
  316. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  317. static_cast<int>(prop->mDataLength/sizeof(float)));
  318. for (unsigned int pp = 0; pp < prop->mDataLength/sizeof(float);++pp) {
  319. ioprintf(io,"%f ",*((float*)(prop->mData+pp*sizeof(float))));
  320. }
  321. }
  322. else if (prop->mType == aiPTI_Integer) {
  323. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  324. static_cast<int>(prop->mDataLength/sizeof(int)));
  325. for (unsigned int pp = 0; pp < prop->mDataLength/sizeof(int);++pp) {
  326. ioprintf(io,"%i ",*((int*)(prop->mData+pp*sizeof(int))));
  327. }
  328. }
  329. else if (prop->mType == aiPTI_Buffer) {
  330. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  331. static_cast<int>(prop->mDataLength));
  332. for (unsigned int pp = 0; pp< prop->mDataLength;++pp) {
  333. ioprintf(io,"%2x ",prop->mData[pp]);
  334. if (pp && 0 == pp%30) {
  335. ioprintf(io,"\n\t\t\t\t");
  336. }
  337. }
  338. }
  339. else if (prop->mType == aiPTI_String) {
  340. ioprintf(io,">\n\t\t\t\t\"%s\"",encodeXML(prop->mData+4).c_str() /* skip length */);
  341. }
  342. ioprintf(io,"\n\t\t\t</MatProperty>\n");
  343. }
  344. ioprintf(io,"\t\t</MatPropertyList>\n");
  345. ioprintf(io,"\t</Material>\n");
  346. }
  347. ioprintf(io,"</MaterialList>\n");
  348. }
  349. // write animations
  350. if (scene->mNumAnimations) {
  351. ioprintf(io,"<AnimationList num=\"%u\">\n",scene->mNumAnimations);
  352. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  353. aiAnimation* anim = scene->mAnimations[i];
  354. // anim header
  355. ConvertName(name,anim->mName);
  356. ioprintf(io,"\t<Animation name=\"%s\" duration=\"%e\" tick_cnt=\"%e\">\n",
  357. name.data, anim->mDuration, anim->mTicksPerSecond);
  358. // write bone animation channels
  359. if (anim->mNumChannels) {
  360. ioprintf(io,"\t\t<NodeAnimList num=\"%u\">\n",anim->mNumChannels);
  361. for (unsigned int n = 0; n < anim->mNumChannels;++n) {
  362. aiNodeAnim* nd = anim->mChannels[n];
  363. // node anim header
  364. ConvertName(name,nd->mNodeName);
  365. ioprintf(io,"\t\t\t<NodeAnim node=\"%s\">\n",name.data);
  366. if (!shortened) {
  367. // write position keys
  368. if (nd->mNumPositionKeys) {
  369. ioprintf(io,"\t\t\t\t<PositionKeyList num=\"%u\">\n",nd->mNumPositionKeys);
  370. for (unsigned int a = 0; a < nd->mNumPositionKeys;++a) {
  371. aiVectorKey* vc = nd->mPositionKeys+a;
  372. ioprintf(io,"\t\t\t\t\t<PositionKey time=\"%e\">\n"
  373. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</PositionKey>\n",
  374. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  375. }
  376. ioprintf(io,"\t\t\t\t</PositionKeyList>\n");
  377. }
  378. // write scaling keys
  379. if (nd->mNumScalingKeys) {
  380. ioprintf(io,"\t\t\t\t<ScalingKeyList num=\"%u\">\n",nd->mNumScalingKeys);
  381. for (unsigned int a = 0; a < nd->mNumScalingKeys;++a) {
  382. aiVectorKey* vc = nd->mScalingKeys+a;
  383. ioprintf(io,"\t\t\t\t\t<ScalingKey time=\"%e\">\n"
  384. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</ScalingKey>\n",
  385. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  386. }
  387. ioprintf(io,"\t\t\t\t</ScalingKeyList>\n");
  388. }
  389. // write rotation keys
  390. if (nd->mNumRotationKeys) {
  391. ioprintf(io,"\t\t\t\t<RotationKeyList num=\"%u\">\n",nd->mNumRotationKeys);
  392. for (unsigned int a = 0; a < nd->mNumRotationKeys;++a) {
  393. aiQuatKey* vc = nd->mRotationKeys+a;
  394. ioprintf(io,"\t\t\t\t\t<RotationKey time=\"%e\">\n"
  395. "\t\t\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t\t\t</RotationKey>\n",
  396. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,vc->mValue.w);
  397. }
  398. ioprintf(io,"\t\t\t\t</RotationKeyList>\n");
  399. }
  400. }
  401. ioprintf(io,"\t\t\t</NodeAnim>\n");
  402. }
  403. ioprintf(io,"\t\t</NodeAnimList>\n");
  404. }
  405. ioprintf(io,"\t</Animation>\n");
  406. }
  407. ioprintf(io,"</AnimationList>\n");
  408. }
  409. // write meshes
  410. if (scene->mNumMeshes) {
  411. ioprintf(io,"<MeshList num=\"%u\">\n",scene->mNumMeshes);
  412. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  413. aiMesh* mesh = scene->mMeshes[i];
  414. // const unsigned int width = (unsigned int)std::log10((double)mesh->mNumVertices)+1;
  415. // mesh header
  416. ioprintf(io,"\t<Mesh types=\"%s %s %s %s\" material_index=\"%u\">\n",
  417. (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
  418. (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
  419. (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
  420. (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
  421. mesh->mMaterialIndex);
  422. // bones
  423. if (mesh->mNumBones) {
  424. ioprintf(io,"\t\t<BoneList num=\"%u\">\n",mesh->mNumBones);
  425. for (unsigned int n = 0; n < mesh->mNumBones;++n) {
  426. aiBone* bone = mesh->mBones[n];
  427. ConvertName(name,bone->mName);
  428. // bone header
  429. ioprintf(io,"\t\t\t<Bone name=\"%s\">\n"
  430. "\t\t\t\t<Matrix4> \n"
  431. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  432. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  433. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  434. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  435. "\t\t\t\t</Matrix4> \n",
  436. name.data,
  437. bone->mOffsetMatrix.a1,bone->mOffsetMatrix.a2,bone->mOffsetMatrix.a3,bone->mOffsetMatrix.a4,
  438. bone->mOffsetMatrix.b1,bone->mOffsetMatrix.b2,bone->mOffsetMatrix.b3,bone->mOffsetMatrix.b4,
  439. bone->mOffsetMatrix.c1,bone->mOffsetMatrix.c2,bone->mOffsetMatrix.c3,bone->mOffsetMatrix.c4,
  440. bone->mOffsetMatrix.d1,bone->mOffsetMatrix.d2,bone->mOffsetMatrix.d3,bone->mOffsetMatrix.d4);
  441. if (!shortened && bone->mNumWeights) {
  442. ioprintf(io,"\t\t\t\t<WeightList num=\"%u\">\n",bone->mNumWeights);
  443. // bone weights
  444. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  445. aiVertexWeight* wght = bone->mWeights+a;
  446. ioprintf(io,"\t\t\t\t\t<Weight index=\"%u\">\n\t\t\t\t\t\t%f\n\t\t\t\t\t</Weight>\n",
  447. wght->mVertexId,wght->mWeight);
  448. }
  449. ioprintf(io,"\t\t\t\t</WeightList>\n");
  450. }
  451. ioprintf(io,"\t\t\t</Bone>\n");
  452. }
  453. ioprintf(io,"\t\t</BoneList>\n");
  454. }
  455. // faces
  456. if (!shortened && mesh->mNumFaces) {
  457. ioprintf(io,"\t\t<FaceList num=\"%u\">\n",mesh->mNumFaces);
  458. for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
  459. aiFace& f = mesh->mFaces[n];
  460. ioprintf(io,"\t\t\t<Face num=\"%u\">\n"
  461. "\t\t\t\t",f.mNumIndices);
  462. for (unsigned int j = 0; j < f.mNumIndices;++j)
  463. ioprintf(io,"%u ",f.mIndices[j]);
  464. ioprintf(io,"\n\t\t\t</Face>\n");
  465. }
  466. ioprintf(io,"\t\t</FaceList>\n");
  467. }
  468. // vertex positions
  469. if (mesh->HasPositions()) {
  470. ioprintf(io,"\t\t<Positions num=\"%u\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  471. if (!shortened) {
  472. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  473. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  474. mesh->mVertices[n].x,
  475. mesh->mVertices[n].y,
  476. mesh->mVertices[n].z);
  477. }
  478. }
  479. ioprintf(io,"\t\t</Positions>\n");
  480. }
  481. // vertex normals
  482. if (mesh->HasNormals()) {
  483. ioprintf(io,"\t\t<Normals num=\"%u\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  484. if (!shortened) {
  485. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  486. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  487. mesh->mNormals[n].x,
  488. mesh->mNormals[n].y,
  489. mesh->mNormals[n].z);
  490. }
  491. }
  492. ioprintf(io,"\t\t</Normals>\n");
  493. }
  494. // vertex tangents and bitangents
  495. if (mesh->HasTangentsAndBitangents()) {
  496. ioprintf(io,"\t\t<Tangents num=\"%u\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  497. if (!shortened) {
  498. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  499. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  500. mesh->mTangents[n].x,
  501. mesh->mTangents[n].y,
  502. mesh->mTangents[n].z);
  503. }
  504. }
  505. ioprintf(io,"\t\t</Tangents>\n");
  506. ioprintf(io,"\t\t<Bitangents num=\"%u\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  507. if (!shortened) {
  508. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  509. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  510. mesh->mBitangents[n].x,
  511. mesh->mBitangents[n].y,
  512. mesh->mBitangents[n].z);
  513. }
  514. }
  515. ioprintf(io,"\t\t</Bitangents>\n");
  516. }
  517. // texture coordinates
  518. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
  519. if (!mesh->mTextureCoords[a])
  520. break;
  521. ioprintf(io,"\t\t<TextureCoords num=\"%u\" set=\"%u\" num_components=\"%u\"> \n",mesh->mNumVertices,
  522. a,mesh->mNumUVComponents[a]);
  523. if (!shortened) {
  524. if (mesh->mNumUVComponents[a] == 3) {
  525. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  526. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  527. mesh->mTextureCoords[a][n].x,
  528. mesh->mTextureCoords[a][n].y,
  529. mesh->mTextureCoords[a][n].z);
  530. }
  531. }
  532. else {
  533. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  534. ioprintf(io,"\t\t%0 8f %0 8f\n",
  535. mesh->mTextureCoords[a][n].x,
  536. mesh->mTextureCoords[a][n].y);
  537. }
  538. }
  539. }
  540. ioprintf(io,"\t\t</TextureCoords>\n");
  541. }
  542. // vertex colors
  543. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
  544. if (!mesh->mColors[a])
  545. break;
  546. ioprintf(io,"\t\t<Colors num=\"%u\" set=\"%u\" num_components=\"4\"> \n",mesh->mNumVertices,a);
  547. if (!shortened) {
  548. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  549. ioprintf(io,"\t\t%0 8f %0 8f %0 8f %0 8f\n",
  550. mesh->mColors[a][n].r,
  551. mesh->mColors[a][n].g,
  552. mesh->mColors[a][n].b,
  553. mesh->mColors[a][n].a);
  554. }
  555. }
  556. ioprintf(io,"\t\t</Colors>\n");
  557. }
  558. ioprintf(io,"\t</Mesh>\n");
  559. }
  560. ioprintf(io,"</MeshList>\n");
  561. }
  562. ioprintf(io,"</Scene>\n</ASSIMP>");
  563. }
  564. } // end of namespace AssxmlFileWriter
  565. void DumpSceneToAssxml(
  566. const char* pFile, const char* cmd, IOSystem* pIOSystem,
  567. const aiScene* pScene, bool shortened) {
  568. std::unique_ptr<IOStream> file(pIOSystem->Open(pFile, "wt"));
  569. if (!file.get()) {
  570. throw std::runtime_error("Unable to open output file " + std::string(pFile) + '\n');
  571. }
  572. AssxmlFileWriter::WriteDump(pFile, cmd, pScene, file.get(), shortened);
  573. }
  574. } // end of namespace Assimp