AssxmlExporter.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2018, 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 AssxmlExporter.cpp
  34. * ASSXML exporter main code
  35. */
  36. #include <stdarg.h>
  37. #include <assimp/version.h>
  38. #include "ProcessHelper.h"
  39. #include <assimp/IOStream.hpp>
  40. #include <assimp/IOSystem.hpp>
  41. #include <assimp/Exporter.hpp>
  42. #ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  43. # include <zlib.h>
  44. #else
  45. # include <contrib/zlib/zlib.h>
  46. #endif
  47. #include <time.h>
  48. #include <stdio.h>
  49. #ifndef ASSIMP_BUILD_NO_EXPORT
  50. #ifndef ASSIMP_BUILD_NO_ASSXML_EXPORTER
  51. using namespace Assimp;
  52. namespace Assimp {
  53. namespace AssxmlExport {
  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=\"%i\">\n%s\t",
  117. prefix,node->mNumMeshes,prefix);
  118. for (unsigned int i = 0; i < node->mNumMeshes;++i) {
  119. ioprintf(io,"%i ",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=\"%i\">\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 aiScene* scene, IOStream* io, bool shortened) {
  155. time_t tt = ::time( NULL );
  156. tm* p = ::gmtime( &tt );
  157. ai_assert( nullptr != p );
  158. // write header
  159. std::string header(
  160. "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  161. "<ASSIMP format_id=\"1\">\n\n"
  162. "<!-- XML Model dump produced by assimp dump\n"
  163. " Library version: %i.%i.%i\n"
  164. " %s\n"
  165. "-->"
  166. " \n\n"
  167. "<Scene flags=\"%d\" postprocessing=\"%i\">\n"
  168. );
  169. const unsigned int majorVersion( aiGetVersionMajor() );
  170. const unsigned int minorVersion( aiGetVersionMinor() );
  171. const unsigned int rev( aiGetVersionRevision() );
  172. const char *curtime( asctime( p ) );
  173. ioprintf( io, header.c_str(), majorVersion, minorVersion, rev, curtime, scene->mFlags, 0 );
  174. // write the node graph
  175. WriteNode(scene->mRootNode, io, 0);
  176. #if 0
  177. // write cameras
  178. for (unsigned int i = 0; i < scene->mNumCameras;++i) {
  179. aiCamera* cam = scene->mCameras[i];
  180. ConvertName(name,cam->mName);
  181. // camera header
  182. ioprintf(io,"\t<Camera parent=\"%s\">\n"
  183. "\t\t<Vector3 name=\"up\" > %0 8f %0 8f %0 8f </Vector3>\n"
  184. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n"
  185. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  186. "\t\t<Float name=\"fov\" > %f </Float>\n"
  187. "\t\t<Float name=\"aspect\" > %f </Float>\n"
  188. "\t\t<Float name=\"near_clip\" > %f </Float>\n"
  189. "\t\t<Float name=\"far_clip\" > %f </Float>\n"
  190. "\t</Camera>\n",
  191. name.data,
  192. cam->mUp.x,cam->mUp.y,cam->mUp.z,
  193. cam->mLookAt.x,cam->mLookAt.y,cam->mLookAt.z,
  194. cam->mPosition.x,cam->mPosition.y,cam->mPosition.z,
  195. cam->mHorizontalFOV,cam->mAspect,cam->mClipPlaneNear,cam->mClipPlaneFar,i);
  196. }
  197. // write lights
  198. for (unsigned int i = 0; i < scene->mNumLights;++i) {
  199. aiLight* l = scene->mLights[i];
  200. ConvertName(name,l->mName);
  201. // light header
  202. ioprintf(io,"\t<Light parent=\"%s\"> type=\"%s\"\n"
  203. "\t\t<Vector3 name=\"diffuse\" > %0 8f %0 8f %0 8f </Vector3>\n"
  204. "\t\t<Vector3 name=\"specular\" > %0 8f %0 8f %0 8f </Vector3>\n"
  205. "\t\t<Vector3 name=\"ambient\" > %0 8f %0 8f %0 8f </Vector3>\n",
  206. name.data,
  207. (l->mType == aiLightSource_DIRECTIONAL ? "directional" :
  208. (l->mType == aiLightSource_POINT ? "point" : "spot" )),
  209. l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b,
  210. l->mColorSpecular.r,l->mColorSpecular.g,l->mColorSpecular.b,
  211. l->mColorAmbient.r, l->mColorAmbient.g, l->mColorAmbient.b);
  212. if (l->mType != aiLightSource_DIRECTIONAL) {
  213. ioprintf(io,
  214. "\t\t<Vector3 name=\"pos\" > %0 8f %0 8f %0 8f </Vector3>\n"
  215. "\t\t<Float name=\"atten_cst\" > %f </Float>\n"
  216. "\t\t<Float name=\"atten_lin\" > %f </Float>\n"
  217. "\t\t<Float name=\"atten_sqr\" > %f </Float>\n",
  218. l->mPosition.x,l->mPosition.y,l->mPosition.z,
  219. l->mAttenuationConstant,l->mAttenuationLinear,l->mAttenuationQuadratic);
  220. }
  221. if (l->mType != aiLightSource_POINT) {
  222. ioprintf(io,
  223. "\t\t<Vector3 name=\"lookat\" > %0 8f %0 8f %0 8f </Vector3>\n",
  224. l->mDirection.x,l->mDirection.y,l->mDirection.z);
  225. }
  226. if (l->mType == aiLightSource_SPOT) {
  227. ioprintf(io,
  228. "\t\t<Float name=\"cone_out\" > %f </Float>\n"
  229. "\t\t<Float name=\"cone_inn\" > %f </Float>\n",
  230. l->mAngleOuterCone,l->mAngleInnerCone);
  231. }
  232. ioprintf(io,"\t</Light>\n");
  233. }
  234. #endif
  235. aiString name;
  236. // write textures
  237. if (scene->mNumTextures) {
  238. ioprintf(io,"<TextureList num=\"%i\">\n",scene->mNumTextures);
  239. for (unsigned int i = 0; i < scene->mNumTextures;++i) {
  240. aiTexture* tex = scene->mTextures[i];
  241. bool compressed = (tex->mHeight == 0);
  242. // mesh header
  243. ioprintf(io,"\t<Texture width=\"%i\" height=\"%i\" compressed=\"%s\"> \n",
  244. (compressed ? -1 : tex->mWidth),(compressed ? -1 : tex->mHeight),
  245. (compressed ? "true" : "false"));
  246. if (compressed) {
  247. ioprintf(io,"\t\t<Data length=\"%i\"> \n",tex->mWidth);
  248. if (!shortened) {
  249. for (unsigned int n = 0; n < tex->mWidth;++n) {
  250. ioprintf(io,"\t\t\t%2x",reinterpret_cast<uint8_t*>(tex->pcData)[n]);
  251. if (n && !(n % 50)) {
  252. ioprintf(io,"\n");
  253. }
  254. }
  255. }
  256. }
  257. else if (!shortened){
  258. ioprintf(io,"\t\t<Data length=\"%i\"> \n",tex->mWidth*tex->mHeight*4);
  259. // const unsigned int width = (unsigned int)std::log10((double)std::max(tex->mHeight,tex->mWidth))+1;
  260. for (unsigned int y = 0; y < tex->mHeight;++y) {
  261. for (unsigned int x = 0; x < tex->mWidth;++x) {
  262. aiTexel* tx = tex->pcData + y*tex->mWidth+x;
  263. unsigned int r = tx->r,g=tx->g,b=tx->b,a=tx->a;
  264. ioprintf(io,"\t\t\t%2x %2x %2x %2x",r,g,b,a);
  265. // group by four for readability
  266. if ( 0 == ( x + y*tex->mWidth ) % 4 ) {
  267. ioprintf( io, "\n" );
  268. }
  269. }
  270. }
  271. }
  272. ioprintf(io,"\t\t</Data>\n\t</Texture>\n");
  273. }
  274. ioprintf(io,"</TextureList>\n");
  275. }
  276. // write materials
  277. if (scene->mNumMaterials) {
  278. ioprintf(io,"<MaterialList num=\"%i\">\n",scene->mNumMaterials);
  279. for (unsigned int i = 0; i< scene->mNumMaterials; ++i) {
  280. const aiMaterial* mat = scene->mMaterials[i];
  281. ioprintf(io,"\t<Material>\n");
  282. ioprintf(io,"\t\t<MatPropertyList num=\"%i\">\n",mat->mNumProperties);
  283. for (unsigned int n = 0; n < mat->mNumProperties;++n) {
  284. const aiMaterialProperty* prop = mat->mProperties[n];
  285. const char* sz = "";
  286. if (prop->mType == aiPTI_Float) {
  287. sz = "float";
  288. }
  289. else if (prop->mType == aiPTI_Integer) {
  290. sz = "integer";
  291. }
  292. else if (prop->mType == aiPTI_String) {
  293. sz = "string";
  294. }
  295. else if (prop->mType == aiPTI_Buffer) {
  296. sz = "binary_buffer";
  297. }
  298. ioprintf(io,"\t\t\t<MatProperty key=\"%s\" \n\t\t\ttype=\"%s\" tex_usage=\"%s\" tex_index=\"%i\"",
  299. prop->mKey.data, sz,
  300. ::TextureTypeToString((aiTextureType)prop->mSemantic),prop->mIndex);
  301. if (prop->mType == aiPTI_Float) {
  302. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  303. static_cast<int>(prop->mDataLength/sizeof(float)));
  304. for (unsigned int p = 0; p < prop->mDataLength/sizeof(float);++p) {
  305. ioprintf(io,"%f ",*((float*)(prop->mData+p*sizeof(float))));
  306. }
  307. }
  308. else if (prop->mType == aiPTI_Integer) {
  309. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  310. static_cast<int>(prop->mDataLength/sizeof(int)));
  311. for (unsigned int p = 0; p < prop->mDataLength/sizeof(int);++p) {
  312. ioprintf(io,"%i ",*((int*)(prop->mData+p*sizeof(int))));
  313. }
  314. }
  315. else if (prop->mType == aiPTI_Buffer) {
  316. ioprintf(io," size=\"%i\">\n\t\t\t\t",
  317. static_cast<int>(prop->mDataLength));
  318. for (unsigned int p = 0; p < prop->mDataLength;++p) {
  319. ioprintf(io,"%2x ",prop->mData[p]);
  320. if (p && 0 == p%30) {
  321. ioprintf(io,"\n\t\t\t\t");
  322. }
  323. }
  324. }
  325. else if (prop->mType == aiPTI_String) {
  326. ioprintf(io,">\n\t\t\t\t\"%s\"",encodeXML(prop->mData+4).c_str() /* skip length */);
  327. }
  328. ioprintf(io,"\n\t\t\t</MatProperty>\n");
  329. }
  330. ioprintf(io,"\t\t</MatPropertyList>\n");
  331. ioprintf(io,"\t</Material>\n");
  332. }
  333. ioprintf(io,"</MaterialList>\n");
  334. }
  335. // write animations
  336. if (scene->mNumAnimations) {
  337. ioprintf(io,"<AnimationList num=\"%i\">\n",scene->mNumAnimations);
  338. for (unsigned int i = 0; i < scene->mNumAnimations;++i) {
  339. aiAnimation* anim = scene->mAnimations[i];
  340. // anim header
  341. ConvertName(name,anim->mName);
  342. ioprintf(io,"\t<Animation name=\"%s\" duration=\"%e\" tick_cnt=\"%e\">\n",
  343. name.data, anim->mDuration, anim->mTicksPerSecond);
  344. // write bone animation channels
  345. if (anim->mNumChannels) {
  346. ioprintf(io,"\t\t<NodeAnimList num=\"%i\">\n",anim->mNumChannels);
  347. for (unsigned int n = 0; n < anim->mNumChannels;++n) {
  348. aiNodeAnim* nd = anim->mChannels[n];
  349. // node anim header
  350. ConvertName(name,nd->mNodeName);
  351. ioprintf(io,"\t\t\t<NodeAnim node=\"%s\">\n",name.data);
  352. if (!shortened) {
  353. // write position keys
  354. if (nd->mNumPositionKeys) {
  355. ioprintf(io,"\t\t\t\t<PositionKeyList num=\"%i\">\n",nd->mNumPositionKeys);
  356. for (unsigned int a = 0; a < nd->mNumPositionKeys;++a) {
  357. aiVectorKey* vc = nd->mPositionKeys+a;
  358. ioprintf(io,"\t\t\t\t\t<PositionKey time=\"%e\">\n"
  359. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</PositionKey>\n",
  360. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  361. }
  362. ioprintf(io,"\t\t\t\t</PositionKeyList>\n");
  363. }
  364. // write scaling keys
  365. if (nd->mNumScalingKeys) {
  366. ioprintf(io,"\t\t\t\t<ScalingKeyList num=\"%i\">\n",nd->mNumScalingKeys);
  367. for (unsigned int a = 0; a < nd->mNumScalingKeys;++a) {
  368. aiVectorKey* vc = nd->mScalingKeys+a;
  369. ioprintf(io,"\t\t\t\t\t<ScalingKey time=\"%e\">\n"
  370. "\t\t\t\t\t\t%0 8f %0 8f %0 8f\n\t\t\t\t\t</ScalingKey>\n",
  371. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z);
  372. }
  373. ioprintf(io,"\t\t\t\t</ScalingKeyList>\n");
  374. }
  375. // write rotation keys
  376. if (nd->mNumRotationKeys) {
  377. ioprintf(io,"\t\t\t\t<RotationKeyList num=\"%i\">\n",nd->mNumRotationKeys);
  378. for (unsigned int a = 0; a < nd->mNumRotationKeys;++a) {
  379. aiQuatKey* vc = nd->mRotationKeys+a;
  380. ioprintf(io,"\t\t\t\t\t<RotationKey time=\"%e\">\n"
  381. "\t\t\t\t\t\t%0 8f %0 8f %0 8f %0 8f\n\t\t\t\t\t</RotationKey>\n",
  382. vc->mTime,vc->mValue.x,vc->mValue.y,vc->mValue.z,vc->mValue.w);
  383. }
  384. ioprintf(io,"\t\t\t\t</RotationKeyList>\n");
  385. }
  386. }
  387. ioprintf(io,"\t\t\t</NodeAnim>\n");
  388. }
  389. ioprintf(io,"\t\t</NodeAnimList>\n");
  390. }
  391. ioprintf(io,"\t</Animation>\n");
  392. }
  393. ioprintf(io,"</AnimationList>\n");
  394. }
  395. // write meshes
  396. if (scene->mNumMeshes) {
  397. ioprintf(io,"<MeshList num=\"%i\">\n",scene->mNumMeshes);
  398. for (unsigned int i = 0; i < scene->mNumMeshes;++i) {
  399. aiMesh* mesh = scene->mMeshes[i];
  400. // const unsigned int width = (unsigned int)std::log10((double)mesh->mNumVertices)+1;
  401. // mesh header
  402. ioprintf(io,"\t<Mesh types=\"%s %s %s %s\" material_index=\"%i\">\n",
  403. (mesh->mPrimitiveTypes & aiPrimitiveType_POINT ? "points" : ""),
  404. (mesh->mPrimitiveTypes & aiPrimitiveType_LINE ? "lines" : ""),
  405. (mesh->mPrimitiveTypes & aiPrimitiveType_TRIANGLE ? "triangles" : ""),
  406. (mesh->mPrimitiveTypes & aiPrimitiveType_POLYGON ? "polygons" : ""),
  407. mesh->mMaterialIndex);
  408. // bones
  409. if (mesh->mNumBones) {
  410. ioprintf(io,"\t\t<BoneList num=\"%i\">\n",mesh->mNumBones);
  411. for (unsigned int n = 0; n < mesh->mNumBones;++n) {
  412. aiBone* bone = mesh->mBones[n];
  413. ConvertName(name,bone->mName);
  414. // bone header
  415. ioprintf(io,"\t\t\t<Bone name=\"%s\">\n"
  416. "\t\t\t\t<Matrix4> \n"
  417. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  418. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  419. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  420. "\t\t\t\t\t%0 6f %0 6f %0 6f %0 6f\n"
  421. "\t\t\t\t</Matrix4> \n",
  422. name.data,
  423. bone->mOffsetMatrix.a1,bone->mOffsetMatrix.a2,bone->mOffsetMatrix.a3,bone->mOffsetMatrix.a4,
  424. bone->mOffsetMatrix.b1,bone->mOffsetMatrix.b2,bone->mOffsetMatrix.b3,bone->mOffsetMatrix.b4,
  425. bone->mOffsetMatrix.c1,bone->mOffsetMatrix.c2,bone->mOffsetMatrix.c3,bone->mOffsetMatrix.c4,
  426. bone->mOffsetMatrix.d1,bone->mOffsetMatrix.d2,bone->mOffsetMatrix.d3,bone->mOffsetMatrix.d4);
  427. if (!shortened && bone->mNumWeights) {
  428. ioprintf(io,"\t\t\t\t<WeightList num=\"%i\">\n",bone->mNumWeights);
  429. // bone weights
  430. for (unsigned int a = 0; a < bone->mNumWeights;++a) {
  431. aiVertexWeight* wght = bone->mWeights+a;
  432. ioprintf(io,"\t\t\t\t\t<Weight index=\"%i\">\n\t\t\t\t\t\t%f\n\t\t\t\t\t</Weight>\n",
  433. wght->mVertexId,wght->mWeight);
  434. }
  435. ioprintf(io,"\t\t\t\t</WeightList>\n");
  436. }
  437. ioprintf(io,"\t\t\t</Bone>\n");
  438. }
  439. ioprintf(io,"\t\t</BoneList>\n");
  440. }
  441. // faces
  442. if (!shortened && mesh->mNumFaces) {
  443. ioprintf(io,"\t\t<FaceList num=\"%i\">\n",mesh->mNumFaces);
  444. for (unsigned int n = 0; n < mesh->mNumFaces; ++n) {
  445. aiFace& f = mesh->mFaces[n];
  446. ioprintf(io,"\t\t\t<Face num=\"%i\">\n"
  447. "\t\t\t\t",f.mNumIndices);
  448. for (unsigned int j = 0; j < f.mNumIndices;++j)
  449. ioprintf(io,"%i ",f.mIndices[j]);
  450. ioprintf(io,"\n\t\t\t</Face>\n");
  451. }
  452. ioprintf(io,"\t\t</FaceList>\n");
  453. }
  454. // vertex positions
  455. if (mesh->HasPositions()) {
  456. ioprintf(io,"\t\t<Positions num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  457. if (!shortened) {
  458. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  459. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  460. mesh->mVertices[n].x,
  461. mesh->mVertices[n].y,
  462. mesh->mVertices[n].z);
  463. }
  464. }
  465. ioprintf(io,"\t\t</Positions>\n");
  466. }
  467. // vertex normals
  468. if (mesh->HasNormals()) {
  469. ioprintf(io,"\t\t<Normals num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  470. if (!shortened) {
  471. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  472. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  473. mesh->mNormals[n].x,
  474. mesh->mNormals[n].y,
  475. mesh->mNormals[n].z);
  476. }
  477. }
  478. else {
  479. }
  480. ioprintf(io,"\t\t</Normals>\n");
  481. }
  482. // vertex tangents and bitangents
  483. if (mesh->HasTangentsAndBitangents()) {
  484. ioprintf(io,"\t\t<Tangents num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  485. if (!shortened) {
  486. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  487. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  488. mesh->mTangents[n].x,
  489. mesh->mTangents[n].y,
  490. mesh->mTangents[n].z);
  491. }
  492. }
  493. ioprintf(io,"\t\t</Tangents>\n");
  494. ioprintf(io,"\t\t<Bitangents num=\"%i\" set=\"0\" num_components=\"3\"> \n",mesh->mNumVertices);
  495. if (!shortened) {
  496. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  497. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  498. mesh->mBitangents[n].x,
  499. mesh->mBitangents[n].y,
  500. mesh->mBitangents[n].z);
  501. }
  502. }
  503. ioprintf(io,"\t\t</Bitangents>\n");
  504. }
  505. // texture coordinates
  506. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a) {
  507. if (!mesh->mTextureCoords[a])
  508. break;
  509. ioprintf(io,"\t\t<TextureCoords num=\"%i\" set=\"%i\" num_components=\"%i\"> \n",mesh->mNumVertices,
  510. a,mesh->mNumUVComponents[a]);
  511. if (!shortened) {
  512. if (mesh->mNumUVComponents[a] == 3) {
  513. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  514. ioprintf(io,"\t\t%0 8f %0 8f %0 8f\n",
  515. mesh->mTextureCoords[a][n].x,
  516. mesh->mTextureCoords[a][n].y,
  517. mesh->mTextureCoords[a][n].z);
  518. }
  519. }
  520. else {
  521. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  522. ioprintf(io,"\t\t%0 8f %0 8f\n",
  523. mesh->mTextureCoords[a][n].x,
  524. mesh->mTextureCoords[a][n].y);
  525. }
  526. }
  527. }
  528. ioprintf(io,"\t\t</TextureCoords>\n");
  529. }
  530. // vertex colors
  531. for (unsigned int a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a) {
  532. if (!mesh->mColors[a])
  533. break;
  534. ioprintf(io,"\t\t<Colors num=\"%i\" set=\"%i\" num_components=\"4\"> \n",mesh->mNumVertices,a);
  535. if (!shortened) {
  536. for (unsigned int n = 0; n < mesh->mNumVertices; ++n) {
  537. ioprintf(io,"\t\t%0 8f %0 8f %0 8f %0 8f\n",
  538. mesh->mColors[a][n].r,
  539. mesh->mColors[a][n].g,
  540. mesh->mColors[a][n].b,
  541. mesh->mColors[a][n].a);
  542. }
  543. }
  544. ioprintf(io,"\t\t</Colors>\n");
  545. }
  546. ioprintf(io,"\t</Mesh>\n");
  547. }
  548. ioprintf(io,"</MeshList>\n");
  549. }
  550. ioprintf(io,"</Scene>\n</ASSIMP>");
  551. }
  552. } // end of namespace AssxmlExport
  553. void ExportSceneAssxml(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* /*pProperties*/)
  554. {
  555. IOStream * out = pIOSystem->Open( pFile, "wt" );
  556. if (!out) return;
  557. bool shortened = false;
  558. AssxmlExport::WriteDump( pScene, out, shortened );
  559. pIOSystem->Close( out );
  560. }
  561. } // end of namespace Assimp
  562. #endif // ASSIMP_BUILD_NO_ASSXML_EXPORTER
  563. #endif // ASSIMP_BUILD_NO_EXPORT