AssxmlFileWriter.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2024, 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/Exporter.hpp>
  40. #include <assimp/IOStream.hpp>
  41. #include <assimp/IOSystem.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 <stdio.h>
  49. #include <time.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;");
  79. break;
  80. case '>':
  81. out.Append("&gt;");
  82. break;
  83. case '&':
  84. out.Append("&amp;");
  85. break;
  86. case '\"':
  87. out.Append("&quot;");
  88. break;
  89. case '\'':
  90. out.Append("&apos;");
  91. break;
  92. default:
  93. out.data[out.length++] = in.data[i];
  94. }
  95. }
  96. out.data[out.length] = 0;
  97. }
  98. // -----------------------------------------------------------------------------------
  99. // Write a single node as text dump
  100. static void WriteNode(const aiNode *node, IOStream *io, unsigned int depth) {
  101. char prefix[512];
  102. for (unsigned int i = 0; i < depth; ++i)
  103. prefix[i] = '\t';
  104. prefix[depth] = '\0';
  105. const aiMatrix4x4 &m = node->mTransformation;
  106. aiString name;
  107. ConvertName(name, node->mName);
  108. ioprintf(io, "%s<Node name=\"%s\"> \n"
  109. "%s\t<Matrix4> \n"
  110. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  111. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  112. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  113. "%s\t\t%0 6f %0 6f %0 6f %0 6f\n"
  114. "%s\t</Matrix4> \n",
  115. prefix, name.data, prefix,
  116. prefix, m.a1, m.a2, m.a3, m.a4,
  117. prefix, m.b1, m.b2, m.b3, m.b4,
  118. prefix, m.c1, m.c2, m.c3, m.c4,
  119. prefix, m.d1, m.d2, m.d3, m.d4, prefix);
  120. if (node->mNumMeshes) {
  121. ioprintf(io, "%s\t<MeshRefs num=\"%u\">\n%s\t",
  122. prefix, node->mNumMeshes, prefix);
  123. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  124. ioprintf(io, "%u ", node->mMeshes[i]);
  125. }
  126. ioprintf(io, "\n%s\t</MeshRefs>\n", prefix);
  127. }
  128. if (node->mNumChildren) {
  129. ioprintf(io, "%s\t<NodeList num=\"%u\">\n",
  130. prefix, node->mNumChildren);
  131. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  132. WriteNode(node->mChildren[i], io, depth + 2);
  133. }
  134. ioprintf(io, "%s\t</NodeList>\n", prefix);
  135. }
  136. ioprintf(io, "%s</Node>\n", prefix);
  137. }
  138. // -----------------------------------------------------------------------------------
  139. // Some chunks of text will need to be encoded for XML
  140. // http://stackoverflow.com/questions/5665231/most-efficient-way-to-escape-xml-html-in-c-string#5665377
  141. static std::string encodeXML(const std::string &data) {
  142. std::string buffer;
  143. buffer.reserve(data.size());
  144. for (size_t pos = 0; pos != data.size(); ++pos) {
  145. switch (data[pos]) {
  146. case '&': buffer.append("&amp;"); break;
  147. case '\"': buffer.append("&quot;"); break;
  148. case '\'': buffer.append("&apos;"); break;
  149. case '<': buffer.append("&lt;"); break;
  150. case '>': buffer.append("&gt;"); break;
  151. default: buffer.append(&data[pos], 1); break;
  152. }
  153. }
  154. return buffer;
  155. }
  156. // -----------------------------------------------------------------------------------
  157. // Write a text model dump
  158. static void WriteDump(const char *pFile, const char *cmd, const aiScene *scene, IOStream *io, bool shortened) {
  159. time_t tt = ::time(nullptr);
  160. #if _WIN32
  161. tm *p = gmtime(&tt);
  162. #else
  163. struct tm now;
  164. tm *p = gmtime_r(&tt, &now);
  165. #endif
  166. ai_assert(nullptr != p);
  167. std::string c = cmd;
  168. std::string::size_type s;
  169. // https://sourceforge.net/tracker/?func=detail&aid=3167364&group_id=226462&atid=1067632
  170. // -- not allowed in XML comments
  171. while ((s = c.find("--")) != std::string::npos) {
  172. c[s] = '?';
  173. }
  174. // write header
  175. std::string header(
  176. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  177. "<ASSIMP format_id=\"1\">\n\n"
  178. "<!-- XML Model dump produced by assimp dump\n"
  179. " Library version: %u.%u.%u\n"
  180. " Source: %s\n"
  181. " Command line: %s\n"
  182. " %s\n"
  183. "-->"
  184. " \n\n"
  185. "<Scene flags=\"%u\" postprocessing=\"%u\">\n");
  186. const unsigned int majorVersion(aiGetVersionMajor());
  187. const unsigned int minorVersion(aiGetVersionMinor());
  188. const unsigned int rev(aiGetVersionRevision());
  189. const char *curtime(asctime(p));
  190. ioprintf(io, header.c_str(), majorVersion, minorVersion, rev, pFile, c.c_str(), curtime, scene->mFlags, 0u);
  191. // write the node graph
  192. WriteNode(scene->mRootNode, io, 0);
  193. #if 0
  194. // write cameras
  195. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  196. aiCamera* cam = scene->mCameras[i];
  197. ConvertName(name,cam->mName);
  198. // camera header
  199. ioprintf(io,"\t<Camera parent=\"%s\">\n"
  200. "\t\t<Vector3 name=\"up\" > %0 8f %0 8f %0 8f </Vector3>\n"
  201. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n"
  202. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  203. "\t\t<Float name=\"fov\" > %f </Float>\n"
  204. "\t\t<Float name=\"aspect\" > %f </Float>\n"
  205. "\t\t<Float name=\"near_clip\" > %f </Float>\n"
  206. "\t\t<Float name=\"far_clip\" > %f </Float>\n"
  207. "\t</Camera>\n",
  208. name.data,
  209. cam->mUp.x,cam->mUp.y,cam->mUp.z,
  210. cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
  211. cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
  212. cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
  213. }
  214. // write lights
  215. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  216. aiLight* l = scene->mLights[i];
  217. ConvertName(name,l->mName);
  218. // light header
  219. ioprintf(io,"\t<Light parent=\"%s\"> type=\"%s\"\n"
  220. "\t\t<Vector3 name=\"diffuse\" > %0 8f %0 8f %0 8f </Vector3>\n"
  221. "\t\t<Vector3 name=\"specular\" > %0 8f %0 8f %0 8f </Vector3>\n"
  222. "\t\t<Vector3 name=\"ambient\" > %0 8f %0 8f %0 8f </Vector3>\n",
  223. name.data,
  224. (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
  225. (l->mType == aiLightSource_POINT ? "point" : "spot" )),
  226. l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
  227. l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
  228. l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
  229. if (l->mType != aiLightSource_DIRECTIONAL) {
  230. ioprintf(io,
  231. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  232. "\t\t<Float name=\"atten_cst\" > %f </Float>\n"
  233. "\t\t<Float name=\"atten_lin\" > %f </Float>\n"
  234. "\t\t<Float name=\"atten_sqr\" > %f </Float>\n",
  235. l->mPosition.x,l->mPosition.y,l->mPosition.z,
  236. l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
  237. }
  238. if (l->mType != aiLightSource_POINT) {
  239. ioprintf(io,
  240. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n",
  241. l->mDirection.x,l->mDirection.y,l->mDirection.z);
  242. }
  243. if (l->mType == aiLightSource_SPOT) {
  244. ioprintf(io,
  245. "\t\t<Float name=\"cone_out\" > %f </Float>\n"
  246. "\t\t<Float name=\"cone_inn\" > %f </Float>\n",
  247. l->mAngleOuterCone,l->mAngleInnerCone);
  248. }
  249. ioprintf(io,"\t</Light>\n");
  250. }
  251. #endif
  252. aiString name;
  253. // write textures
  254. if (scene->mNumTextures) {
  255. ioprintf(io, "<TextureList num=\"%u\">\n", scene->mNumTextures);
  256. for (unsigned int i = 0; i < scene->mNumTextures; ++i) {
  257. aiTexture *tex = scene->mTextures[i];
  258. bool compressed = (tex->mHeight == 0);
  259. // mesh header
  260. ioprintf(io, "\t<Texture width=\"%u\" height=\"%u\" compressed=\"%s\"> \n",
  261. (compressed ? -1 : tex->mWidth), (compressed ? -1 : tex->mHeight),
  262. (compressed ? "true" : "false"));
  263. if (compressed) {
  264. ioprintf(io, "\t\t<Data length=\"%u\"> \n", tex->mWidth);
  265. if (!shortened) {
  266. for (unsigned int n = 0; n < tex->mWidth; ++n) {
  267. ioprintf(io, "\t\t\t%2x", reinterpret_cast<uint8_t *>(tex->pcData)[n]);
  268. if (n && !(n % 50)) {
  269. ioprintf(io, "\n");
  270. }
  271. }
  272. }
  273. } else if (!shortened) {
  274. ioprintf(io, "\t\t<Data length=\"%u\"> \n", tex->mWidth * tex->mHeight * 4);
  275. // const unsigned int width = (unsigned int)std::log10((double)std::max(tex->mHeight,tex->mWidth))+1;
  276. for (unsigned int y = 0; y < tex->mHeight; ++y) {
  277. for (unsigned int x = 0; x < tex->mWidth; ++x) {
  278. aiTexel *tx = tex->pcData + y * tex->mWidth + x;
  279. unsigned int r = tx->r, g = tx->g, b = tx->b, a = tx->a;
  280. ioprintf(io, "\t\t\t%2x %2x %2x %2x", r, g, b, a);
  281. // group by four for readability
  282. if (0 == (x + y * tex->mWidth) % 4) {
  283. ioprintf(io, "\n");
  284. }
  285. }
  286. }
  287. }
  288. ioprintf(io, "\t\t</Data>\n\t</Texture>\n");
  289. }
  290. ioprintf(io, "</TextureList>\n");
  291. }
  292. // write materials
  293. if (scene->mNumMaterials) {
  294. ioprintf(io, "<MaterialList num=\"%u\">\n", scene->mNumMaterials);
  295. for (unsigned int i = 0; i < scene->mNumMaterials; ++i) {
  296. const aiMaterial *mat = scene->mMaterials[i];
  297. ioprintf(io, "\t<Material>\n");
  298. ioprintf(io, "\t\t<MatPropertyList num=\"%u\">\n", mat->mNumProperties);
  299. for (unsigned int n = 0; n < mat->mNumProperties; ++n) {
  300. const aiMaterialProperty *prop = mat->mProperties[n];
  301. const char *sz = "";
  302. if (prop->mType == aiPTI_Float) {
  303. sz = "float";
  304. } else if (prop->mType == aiPTI_Integer) {
  305. sz = "integer";
  306. } else if (prop->mType == aiPTI_String) {
  307. sz = "string";
  308. } else if (prop->mType == aiPTI_Buffer) {
  309. sz = "binary_buffer";
  310. }
  311. ioprintf(io, "\t\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%u\"",
  312. prop->mKey.data, sz,
  313. ::aiTextureTypeToString((aiTextureType)prop->mSemantic), prop->mIndex);
  314. if (prop->mType == aiPTI_Float) {
  315. ioprintf(io, " size=\"%i\">\n\t\t\t\t",
  316. static_cast<int>(prop->mDataLength / sizeof(float)));
  317. for (unsigned int pp = 0; pp < prop->mDataLength / sizeof(float); ++pp) {
  318. ioprintf(io, "%f ", *((float *)(prop->mData + pp * sizeof(float))));
  319. }
  320. } else if (prop->mType == aiPTI_Integer) {
  321. ioprintf(io, " size=\"%i\">\n\t\t\t\t",
  322. static_cast<int>(prop->mDataLength / sizeof(int)));
  323. for (unsigned int pp = 0; pp < prop->mDataLength / sizeof(int); ++pp) {
  324. ioprintf(io, "%i ", *((int *)(prop->mData + pp * sizeof(int))));
  325. }
  326. } else if (prop->mType == aiPTI_Buffer) {
  327. ioprintf(io, " size=\"%i\">\n\t\t\t\t",
  328. static_cast<int>(prop->mDataLength));
  329. for (unsigned int pp = 0; pp < prop->mDataLength; ++pp) {
  330. ioprintf(io, "%2x ", prop->mData[pp]);
  331. if (pp && 0 == pp % 30) {
  332. ioprintf(io, "\n\t\t\t\t");
  333. }
  334. }
  335. } else if (prop->mType == aiPTI_String) {
  336. ioprintf(io, ">\n\t\t\t\t\"%s\"", encodeXML(prop->mData + 4).c_str() /* skip length */);
  337. }
  338. ioprintf(io, "\n\t\t\t</MatProperty>\n");
  339. }
  340. ioprintf(io, "\t\t</MatPropertyList>\n");
  341. ioprintf(io, "\t</Material>\n");
  342. }
  343. ioprintf(io, "</MaterialList>\n");
  344. }
  345. // write animations
  346. if (scene->mNumAnimations) {
  347. ioprintf(io, "<AnimationList num=\"%u\">\n", scene->mNumAnimations);
  348. for (unsigned int i = 0; i < scene->mNumAnimations; ++i) {
  349. aiAnimation *anim = scene->mAnimations[i];
  350. // anim header
  351. ConvertName(name, anim->mName);
  352. ioprintf(io, "\t<Animation name=\"%s\" duration=\"%e\" tick_cnt=\"%e\">\n",
  353. name.data, anim->mDuration, anim->mTicksPerSecond);
  354. // write bone animation channels
  355. if (anim->mNumChannels) {
  356. ioprintf(io, "\t\t<NodeAnimList num=\"%u\">\n", anim->mNumChannels);
  357. for (unsigned int n = 0; n < anim->mNumChannels; ++n) {
  358. aiNodeAnim *nd = anim->mChannels[n];
  359. // node anim header
  360. ConvertName(name, nd->mNodeName);
  361. ioprintf(io, "\t\t\t<NodeAnim node=\"%s\">\n", name.data);
  362. if (!shortened) {
  363. // write position keys
  364. if (nd->mNumPositionKeys) {
  365. ioprintf(io, "\t\t\t\t<PositionKeyList num=\"%u\">\n", nd->mNumPositionKeys);
  366. for (unsigned int a = 0; a < nd->mNumPositionKeys; ++a) {
  367. aiVectorKey *vc = nd->mPositionKeys + a;
  368. ioprintf(io, "\t\t\t\t\t<PositionKey time=\"%e\">\n"
  369. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</PositionKey>\n",
  370. vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z);
  371. }
  372. ioprintf(io, "\t\t\t\t</PositionKeyList>\n");
  373. }
  374. // write scaling keys
  375. if (nd->mNumScalingKeys) {
  376. ioprintf(io, "\t\t\t\t<ScalingKeyList num=\"%u\">\n", nd->mNumScalingKeys);
  377. for (unsigned int a = 0; a < nd->mNumScalingKeys; ++a) {
  378. aiVectorKey *vc = nd->mScalingKeys + a;
  379. ioprintf(io, "\t\t\t\t\t<ScalingKey time=\"%e\">\n"
  380. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</ScalingKey>\n",
  381. vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z);
  382. }
  383. ioprintf(io, "\t\t\t\t</ScalingKeyList>\n");
  384. }
  385. // write rotation keys
  386. if (nd->mNumRotationKeys) {
  387. ioprintf(io, "\t\t\t\t<RotationKeyList num=\"%u\">\n", nd->mNumRotationKeys);
  388. for (unsigned int a = 0; a < nd->mNumRotationKeys; ++a) {
  389. aiQuatKey *vc = nd->mRotationKeys + a;
  390. ioprintf(io, "\t\t\t\t\t<RotationKey time=\"%e\">\n"
  391. "\t\t\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t\t\t</RotationKey>\n",
  392. vc->mTime, vc->mValue.x, vc->mValue.y, vc->mValue.z, vc->mValue.w);
  393. }
  394. ioprintf(io, "\t\t\t\t</RotationKeyList>\n");
  395. }
  396. }
  397. ioprintf(io, "\t\t\t</NodeAnim>\n");
  398. }
  399. ioprintf(io, "\t\t</NodeAnimList>\n");
  400. }
  401. ioprintf(io, "\t</Animation>\n");
  402. }
  403. ioprintf(io, "</AnimationList>\n");
  404. }
  405. // write meshes
  406. if (scene->mNumMeshes) {
  407. ioprintf(io, "<MeshList num=\"%u\">\n", scene->mNumMeshes);
  408. for (unsigned int i = 0; i < scene->mNumMeshes; ++i) {
  409. aiMesh *mesh = scene->mMeshes[i];
  410. // const unsigned int width = (unsigned int)std::log10((double)mesh->mNumVertices)+1;
  411. // mesh header
  412. ioprintf(io, "\t<Mesh types=\"%s %s %s %s\" material_index=\"%u\">\n",
  413. (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
  414. (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
  415. (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
  416. (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
  417. mesh->mMaterialIndex);
  418. // bones
  419. if (mesh->mNumBones) {
  420. ioprintf(io, "\t\t<BoneList num=\"%u\">\n", mesh->mNumBones);
  421. for (unsigned int n = 0; n < mesh->mNumBones; ++n) {
  422. aiBone *bone = mesh->mBones[n];
  423. ConvertName(name, bone->mName);
  424. // bone header
  425. ioprintf(io, "\t\t\t<Bone name=\"%s\">\n"
  426. "\t\t\t\t<Matrix4> \n"
  427. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  428. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  429. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  430. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  431. "\t\t\t\t</Matrix4> \n",
  432. name.data,
  433. bone->mOffsetMatrix.a1, bone->mOffsetMatrix.a2, bone->mOffsetMatrix.a3, bone->mOffsetMatrix.a4,
  434. bone->mOffsetMatrix.b1, bone->mOffsetMatrix.b2, bone->mOffsetMatrix.b3, bone->mOffsetMatrix.b4,
  435. bone->mOffsetMatrix.c1, bone->mOffsetMatrix.c2, bone->mOffsetMatrix.c3, bone->mOffsetMatrix.c4,
  436. bone->mOffsetMatrix.d1, bone->mOffsetMatrix.d2, bone->mOffsetMatrix.d3, bone->mOffsetMatrix.d4);
  437. if (!shortened && bone->mNumWeights) {
  438. ioprintf(io, "\t\t\t\t<WeightList num=\"%u\">\n", bone->mNumWeights);
  439. // bone weights
  440. for (unsigned int a = 0; a < bone->mNumWeights; ++a) {
  441. aiVertexWeight *wght = bone->mWeights + a;
  442. 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",
  443. wght->mVertexId, wght->mWeight);
  444. }
  445. ioprintf(io, "\t\t\t\t</WeightList>\n");
  446. }
  447. ioprintf(io, "\t\t\t</Bone>\n");
  448. }
  449. ioprintf(io, "\t\t</BoneList>\n");
  450. }
  451. // faces
  452. if (!shortened && mesh->mNumFaces) {
  453. ioprintf(io, "\t\t<FaceList num=\"%u\">\n", mesh->mNumFaces);
  454. for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
  455. aiFace &f = mesh->mFaces[n];
  456. ioprintf(io, "\t\t\t<Face num=\"%u\">\n"
  457. "\t\t\t\t",
  458. f.mNumIndices);
  459. for (unsigned int j = 0; j < f.mNumIndices; ++j)
  460. ioprintf(io, "%u ", f.mIndices[j]);
  461. ioprintf(io, "\n\t\t\t</Face>\n");
  462. }
  463. ioprintf(io, "\t\t</FaceList>\n");
  464. }
  465. // vertex positions
  466. if (mesh->HasPositions()) {
  467. ioprintf(io, "\t\t<Positions num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
  468. if (!shortened) {
  469. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  470. ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
  471. mesh->mVertices[n].x,
  472. mesh->mVertices[n].y,
  473. mesh->mVertices[n].z);
  474. }
  475. }
  476. ioprintf(io, "\t\t</Positions>\n");
  477. }
  478. // vertex normals
  479. if (mesh->HasNormals()) {
  480. ioprintf(io, "\t\t<Normals num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
  481. if (!shortened) {
  482. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  483. ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
  484. mesh->mNormals[n].x,
  485. mesh->mNormals[n].y,
  486. mesh->mNormals[n].z);
  487. }
  488. }
  489. ioprintf(io, "\t\t</Normals>\n");
  490. }
  491. // vertex tangents and bitangents
  492. if (mesh->HasTangentsAndBitangents()) {
  493. ioprintf(io, "\t\t<Tangents num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
  494. if (!shortened) {
  495. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  496. ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
  497. mesh->mTangents[n].x,
  498. mesh->mTangents[n].y,
  499. mesh->mTangents[n].z);
  500. }
  501. }
  502. ioprintf(io, "\t\t</Tangents>\n");
  503. ioprintf(io, "\t\t<Bitangents num=\"%u\" set=\"0\" num_components=\"3\"> \n", mesh->mNumVertices);
  504. if (!shortened) {
  505. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  506. ioprintf(io, "\t\t%0 8f %0 8f %0 8f\n",
  507. mesh->mBitangents[n].x,
  508. mesh->mBitangents[n].y,
  509. mesh->mBitangents[n].z);
  510. }
  511. }
  512. ioprintf(io, "\t\t</Bitangents>\n");
  513. }
  514. // texture coordinates
  515. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
  516. if (!mesh->mTextureCoords[a])
  517. break;
  518. ioprintf(io, "\t\t<TextureCoords num=\"%u\" set=\"%u\" name=\"%s\" num_components=\"%u\"> \n",
  519. mesh->mNumVertices,
  520. a,
  521. (mesh->HasTextureCoordsName(a) ? mesh->GetTextureCoordsName(a)->C_Str() : ""),
  522. 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. } else {
  532. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  533. ioprintf(io, "\t\t%0 8f %0 8f\n",
  534. mesh->mTextureCoords[a][n].x,
  535. mesh->mTextureCoords[a][n].y);
  536. }
  537. }
  538. }
  539. ioprintf(io, "\t\t</TextureCoords>\n");
  540. }
  541. // vertex colors
  542. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
  543. if (!mesh->mColors[a])
  544. break;
  545. ioprintf(io, "\t\t<Colors num=\"%u\" set=\"%u\" num_components=\"4\"> \n", mesh->mNumVertices, a);
  546. if (!shortened) {
  547. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  548. ioprintf(io, "\t\t%0 8f %0 8f %0 8f %0 8f\n",
  549. mesh->mColors[a][n].r,
  550. mesh->mColors[a][n].g,
  551. mesh->mColors[a][n].b,
  552. mesh->mColors[a][n].a);
  553. }
  554. }
  555. ioprintf(io, "\t\t</Colors>\n");
  556. }
  557. ioprintf(io, "\t</Mesh>\n");
  558. }
  559. ioprintf(io, "</MeshList>\n");
  560. }
  561. ioprintf(io, "</Scene>\n</ASSIMP>");
  562. }
  563. } // end of namespace AssxmlFileWriter
  564. void DumpSceneToAssxml(
  565. const char *pFile, const char *cmd, IOSystem *pIOSystem,
  566. const aiScene *pScene, bool shortened) {
  567. std::unique_ptr<IOStream> file(pIOSystem->Open(pFile, "wt"));
  568. if (!file) {
  569. throw std::runtime_error("Unable to open output file " + std::string(pFile) + '\n');
  570. }
  571. AssxmlFileWriter::WriteDump(pFile, cmd, pScene, file.get(), shortened);
  572. }
  573. } // end of namespace Assimp