AssxmlFileWriter.cpp 27 KB

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