XFileExporter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. @author: Richard Steffen, 2014
  32. ----------------------------------------------------------------------
  33. */
  34. #include "AssimpPCH.h"
  35. #ifndef ASSIMP_BUILD_NO_EXPORT
  36. #ifndef ASSIMP_BUILD_NO_XFILE_EXPORTER
  37. #include "XFileExporter.h"
  38. #include "ConvertToLHProcess.h"
  39. #include "Bitmap.h"
  40. #include "fast_atof.h"
  41. #include "SceneCombiner.h"
  42. #include <ctime>
  43. #include <set>
  44. using namespace Assimp;
  45. namespace Assimp
  46. {
  47. // ------------------------------------------------------------------------------------------------
  48. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  49. void ExportSceneXFile(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  50. {
  51. std::string path = "";
  52. std::string file = pFile;
  53. // We need to test both types of folder separators because pIOSystem->getOsSeparator() is not reliable.
  54. // Moreover, the path given by some applications is not even consistent with the OS specific type of separator.
  55. const char* end_path = std::max(strrchr(pFile, '\\'), strrchr(pFile, '/'));
  56. if(end_path != NULL) {
  57. path = std::string(pFile, end_path + 1 - pFile);
  58. file = file.substr(end_path + 1 - pFile, file.npos);
  59. std::size_t pos = file.find_last_of('.');
  60. if(pos != file.npos) {
  61. file = file.substr(0, pos);
  62. }
  63. }
  64. // invoke the exporter
  65. XFileExporter iDoTheExportThing( pScene, pIOSystem, path, file);
  66. // we're still here - export successfully completed. Write result to the given IOSYstem
  67. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  68. if(outfile == NULL) {
  69. throw DeadlyExportError("could not open output .dae file: " + std::string(pFile));
  70. }
  71. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  72. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  73. }
  74. } // end of namespace Assimp
  75. // ------------------------------------------------------------------------------------------------
  76. // Constructor for a specific scene to export
  77. XFileExporter::XFileExporter(const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file) : mIOSystem(pIOSystem), mPath(path), mFile(file)
  78. {
  79. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  80. mOutput.imbue( std::locale("C") );
  81. mScene = pScene;
  82. mSceneOwned = false;
  83. // set up strings
  84. endstr = "\n";
  85. // start writing
  86. WriteFile();
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. // Destructor
  90. XFileExporter::~XFileExporter()
  91. {
  92. if(mSceneOwned) {
  93. delete mScene;
  94. }
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Starts writing the contents
  98. void XFileExporter::WriteFile()
  99. {
  100. // note, that all realnumber values must be comma separated in x files
  101. mOutput.setf(std::ios::fixed);
  102. mOutput.precision(6); // precission for float
  103. // entry of writing the file
  104. WriteHeader();
  105. mOutput << startstr << "Frame DXCC_ROOT {" << endstr;
  106. PushTag();
  107. aiMatrix4x4 I; // identity
  108. WriteFrameTransform(I);
  109. WriteNode(mScene->mRootNode);
  110. PopTag();
  111. mOutput << startstr << "}" << endstr;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Writes the asset header
  115. void XFileExporter::WriteHeader()
  116. {
  117. mOutput << startstr << "xof 0303txt 0032" << endstr;
  118. mOutput << endstr;
  119. mOutput << startstr << "template Frame {" << endstr;
  120. PushTag();
  121. mOutput << startstr << "<3d82ab46-62da-11cf-ab39-0020af71e433>" << endstr;
  122. mOutput << startstr << "[...]" << endstr;
  123. PopTag();
  124. mOutput << startstr << "}" << endstr;
  125. mOutput << endstr;
  126. mOutput << startstr << "template Matrix4x4 {" << endstr;
  127. PushTag();
  128. mOutput << startstr << "<f6f23f45-7686-11cf-8f52-0040333594a3>" << endstr;
  129. mOutput << startstr << "array FLOAT matrix[16];" << endstr;
  130. PopTag();
  131. mOutput << startstr << "}" << endstr;
  132. mOutput << endstr;
  133. mOutput << startstr << "template FrameTransformMatrix {" << endstr;
  134. PushTag();
  135. mOutput << startstr << "<f6f23f41-7686-11cf-8f52-0040333594a3>" << endstr;
  136. mOutput << startstr << "Matrix4x4 frameMatrix;" << endstr;
  137. PopTag();
  138. mOutput << startstr << "}" << endstr;
  139. mOutput << endstr;
  140. mOutput << startstr << "template Vector {" << endstr;
  141. PushTag();
  142. mOutput << startstr << "<3d82ab5e-62da-11cf-ab39-0020af71e433>" << endstr;
  143. mOutput << startstr << "FLOAT x;" << endstr;
  144. mOutput << startstr << "FLOAT y;" << endstr;
  145. mOutput << startstr << "FLOAT z;" << endstr;
  146. PopTag();
  147. mOutput << startstr << "}" << endstr;
  148. mOutput << endstr;
  149. mOutput << startstr << "template MeshFace {" << endstr;
  150. PushTag();
  151. mOutput << startstr << "<3d82ab5f-62da-11cf-ab39-0020af71e433>" << endstr;
  152. mOutput << startstr << "DWORD nFaceVertexIndices;" << endstr;
  153. mOutput << startstr << "array DWORD faceVertexIndices[nFaceVertexIndices];" << endstr;
  154. PopTag();
  155. mOutput << startstr << "}" << endstr;
  156. mOutput << endstr;
  157. mOutput << startstr << "template Mesh {" << endstr;
  158. PushTag();
  159. mOutput << startstr << "<3d82ab44-62da-11cf-ab39-0020af71e433>" << endstr;
  160. mOutput << startstr << "DWORD nVertices;" << endstr;
  161. mOutput << startstr << "array Vector vertices[nVertices];" << endstr;
  162. mOutput << startstr << "DWORD nFaces;" << endstr;
  163. mOutput << startstr << "array MeshFace faces[nFaces];" << endstr;
  164. mOutput << startstr << "[...]" << endstr;
  165. PopTag();
  166. mOutput << startstr << "}" << endstr;
  167. mOutput << endstr;
  168. mOutput << startstr << "template MeshNormals {" << endstr;
  169. PushTag();
  170. mOutput << startstr << "<f6f23f43-7686-11cf-8f52-0040333594a3>" << endstr;
  171. mOutput << startstr << "DWORD nNormals;" << endstr;
  172. mOutput << startstr << "array Vector normals[nNormals];" << endstr;
  173. mOutput << startstr << "DWORD nFaceNormals;" << endstr;
  174. mOutput << startstr << "array MeshFace faceNormals[nFaceNormals];" << endstr;
  175. PopTag();
  176. mOutput << startstr << "}" << endstr;
  177. mOutput << endstr;
  178. mOutput << startstr << "template Coords2d {" << endstr;
  179. PushTag();
  180. mOutput << startstr << "<f6f23f44-7686-11cf-8f52-0040333594a3>" << endstr;
  181. mOutput << startstr << "FLOAT u;" << endstr;
  182. mOutput << startstr << "FLOAT v;" << endstr;
  183. PopTag();
  184. mOutput << startstr << "}" << endstr;
  185. mOutput << endstr;
  186. mOutput << startstr << "template MeshTextureCoords {" << endstr;
  187. PushTag();
  188. mOutput << startstr << "<f6f23f40-7686-11cf-8f52-0040333594a3>" << endstr;
  189. mOutput << startstr << "DWORD nTextureCoords;" << endstr;
  190. mOutput << startstr << "array Coords2d textureCoords[nTextureCoords];" << endstr;
  191. PopTag();
  192. mOutput << startstr << "}" << endstr;
  193. mOutput << endstr;
  194. mOutput << startstr << "template ColorRGBA {" << endstr;
  195. PushTag();
  196. mOutput << startstr << "<35ff44e0-6c7c-11cf-8f52-0040333594a3>" << endstr;
  197. mOutput << startstr << "FLOAT red;" << endstr;
  198. mOutput << startstr << "FLOAT green;" << endstr;
  199. mOutput << startstr << "FLOAT blue;" << endstr;
  200. mOutput << startstr << "FLOAT alpha;" << endstr;
  201. PopTag();
  202. mOutput << startstr << "}" << endstr;
  203. mOutput << endstr;
  204. mOutput << startstr << "template IndexedColor {" << endstr;
  205. PushTag();
  206. mOutput << startstr << "<1630b820-7842-11cf-8f52-0040333594a3>" << endstr;
  207. mOutput << startstr << "DWORD index;" << endstr;
  208. mOutput << startstr << "ColorRGBA indexColor;" << endstr;
  209. PopTag();
  210. mOutput << startstr << "}" << endstr;
  211. mOutput << endstr;
  212. mOutput << startstr << "template MeshVertexColors {" << endstr;
  213. PushTag();
  214. mOutput << startstr << "<1630b821-7842-11cf-8f52-0040333594a3>" << endstr;
  215. mOutput << startstr << "DWORD nVertexColors;" << endstr;
  216. mOutput << startstr << "array IndexedColor vertexColors[nVertexColors];" << endstr;
  217. PopTag();
  218. mOutput << startstr << "}" << endstr;
  219. mOutput << endstr;
  220. mOutput << startstr << "template VertexElement {" << endstr;
  221. PushTag();
  222. mOutput << startstr << "<f752461c-1e23-48f6-b9f8-8350850f336f>" << endstr;
  223. mOutput << startstr << "DWORD Type;" << endstr;
  224. mOutput << startstr << "DWORD Method;" << endstr;
  225. mOutput << startstr << "DWORD Usage;" << endstr;
  226. mOutput << startstr << "DWORD UsageIndex;" << endstr;
  227. PopTag();
  228. mOutput << startstr << "}" << endstr;
  229. mOutput << endstr;
  230. mOutput << startstr << "template DeclData {" << endstr;
  231. PushTag();
  232. mOutput << startstr << "<bf22e553-292c-4781-9fea-62bd554bdd93>" << endstr;
  233. mOutput << startstr << "DWORD nElements;" << endstr;
  234. mOutput << startstr << "array VertexElement Elements[nElements];" << endstr;
  235. mOutput << startstr << "DWORD nDWords;" << endstr;
  236. mOutput << startstr << "array DWORD data[nDWords];" << endstr;
  237. PopTag();
  238. mOutput << startstr << "}" << endstr;
  239. mOutput << endstr;
  240. }
  241. // Writes the material setup
  242. void XFileExporter::WriteFrameTransform(aiMatrix4x4& m)
  243. {
  244. mOutput << startstr << "FrameTransformMatrix {" << endstr << " ";
  245. PushTag();
  246. mOutput << startstr << m.a1 << ", " << m.b1 << ", " << m.c1 << ", " << m.d1 << "," << endstr;
  247. mOutput << startstr << m.a2 << ", " << m.b2 << ", " << m.c2 << ", " << m.d2 << "," << endstr;
  248. mOutput << startstr << m.a3 << ", " << m.b3 << ", " << m.c3 << ", " << m.d3 << "," << endstr;
  249. mOutput << startstr << m.a4 << ", " << m.b4 << ", " << m.c4 << ", " << m.d4 << ";;" << endstr;
  250. PopTag();
  251. mOutput << startstr << "}" << endstr << endstr;
  252. }
  253. // ------------------------------------------------------------------------------------------------
  254. // Recursively writes the given node
  255. void XFileExporter::WriteNode( aiNode* pNode)
  256. {
  257. if (pNode->mName.length==0)
  258. {
  259. std::stringstream ss;
  260. ss << "Node_" << pNode;
  261. pNode->mName.Set(ss.str());
  262. }
  263. mOutput << startstr << "Frame " << pNode->mName.C_Str() << " {" << endstr;
  264. PushTag();
  265. aiMatrix4x4 m = pNode->mTransformation;
  266. WriteFrameTransform(m);
  267. for (size_t i = 0; i < pNode->mNumMeshes; i++)
  268. WriteMesh(mScene->mMeshes[pNode->mMeshes[i]]);
  269. // recursive call the Nodes
  270. for (size_t a = 0; a < pNode->mNumChildren; a++)
  271. WriteNode( mScene->mRootNode->mChildren[a]);
  272. PopTag();
  273. mOutput << startstr << "}" << endstr << endstr;
  274. }
  275. void XFileExporter::WriteMesh(const aiMesh* mesh)
  276. {
  277. mOutput << startstr << "Mesh " << mesh->mName.C_Str() << "_mShape" << " {" << endstr;
  278. PushTag();
  279. // write all the vertices
  280. mOutput << startstr << mesh->mNumVertices << ";" << endstr;
  281. for (size_t a = 0; a < mesh->mNumVertices; a++)
  282. {
  283. aiVector3D &v = mesh->mVertices[a];
  284. mOutput << startstr << v[0] << ";"<< v[1] << ";" << v[2] << ";";
  285. if (a < mesh->mNumVertices - 1)
  286. mOutput << "," << endstr;
  287. else
  288. mOutput << ";" << endstr;
  289. }
  290. // write all the faces
  291. mOutput << startstr << mesh->mNumFaces << ";" << endstr;
  292. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  293. {
  294. const aiFace& face = mesh->mFaces[a];
  295. mOutput << startstr << face.mNumIndices << ";";
  296. // must be counter clockwise triangle
  297. //for(int b = face.mNumIndices - 1; b >= 0 ; --b)
  298. for(size_t b = 0; b < face.mNumIndices ; ++b)
  299. {
  300. mOutput << face.mIndices[b];
  301. //if (b > 0)
  302. if (b<face.mNumIndices-1)
  303. mOutput << ",";
  304. else
  305. mOutput << ";";
  306. }
  307. if (a < mesh->mNumFaces - 1)
  308. mOutput << "," << endstr;
  309. else
  310. mOutput << ";" << endstr;
  311. }
  312. mOutput << endstr;
  313. if (mesh->HasTextureCoords(0))
  314. {
  315. const aiMaterial* mat = mScene->mMaterials[mesh->mMaterialIndex];
  316. aiString relpath;
  317. mat->Get(_AI_MATKEY_TEXTURE_BASE, aiTextureType_DIFFUSE, 0, relpath);
  318. mOutput << startstr << "MeshMaterialList {" << endstr;
  319. PushTag();
  320. mOutput << startstr << "1;" << endstr; // number of materials
  321. mOutput << startstr << mesh->mNumFaces << ";" << endstr; // number of faces
  322. mOutput << startstr;
  323. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  324. {
  325. mOutput << "0"; // the material index
  326. if (a < mesh->mNumFaces - 1)
  327. mOutput << ", ";
  328. else
  329. mOutput << ";" << endstr;
  330. }
  331. mOutput << startstr << "Material {" << endstr;
  332. PushTag();
  333. mOutput << startstr << "1.0; 1.0; 1.0; 1.000000;;" << endstr;
  334. mOutput << startstr << "1.000000;" << endstr; // power
  335. mOutput << startstr << "0.000000; 0.000000; 0.000000;;" << endstr; // specularity
  336. mOutput << startstr << "0.000000; 0.000000; 0.000000;;" << endstr; // emission
  337. mOutput << startstr << "TextureFilename { \"";
  338. writePath(relpath);
  339. mOutput << "\"; }" << endstr;
  340. PopTag();
  341. mOutput << startstr << "}" << endstr;
  342. PopTag();
  343. mOutput << startstr << "}" << endstr;
  344. }
  345. // write normals (every vertex has one)
  346. if (mesh->HasNormals())
  347. {
  348. mOutput << endstr << startstr << "MeshNormals {" << endstr;
  349. mOutput << startstr << mesh->mNumVertices << ";" << endstr;
  350. for (size_t a = 0; a < mesh->mNumVertices; a++)
  351. {
  352. aiVector3D &v = mesh->mNormals[a];
  353. // because we have a LHS and also changed wth winding, we need to invert the normals again
  354. mOutput << startstr << -v[0] << ";"<< -v[1] << ";" << -v[2] << ";";
  355. if (a < mesh->mNumVertices - 1)
  356. mOutput << "," << endstr;
  357. else
  358. mOutput << ";" << endstr;
  359. }
  360. mOutput << startstr << mesh->mNumFaces << ";" << endstr;
  361. for (size_t a = 0; a < mesh->mNumFaces; a++)
  362. {
  363. const aiFace& face = mesh->mFaces[a];
  364. mOutput << startstr << face.mNumIndices << ";";
  365. //for(int b = face.mNumIndices-1; b >= 0 ; --b)
  366. for(size_t b = 0; b < face.mNumIndices ; ++b)
  367. {
  368. mOutput << face.mIndices[b];
  369. //if (b > 0)
  370. if (b<face.mNumIndices-1)
  371. mOutput << ",";
  372. else
  373. mOutput << ";";
  374. }
  375. if (a < mesh->mNumFaces-1)
  376. mOutput << "," << endstr;
  377. else
  378. mOutput << ";" << endstr;
  379. }
  380. mOutput << startstr << "}" << endstr;
  381. }
  382. // write texture UVs if available
  383. if (mesh->HasTextureCoords(0))
  384. {
  385. mOutput << endstr << startstr << "MeshTextureCoords {" << endstr;
  386. mOutput << startstr << mesh->mNumVertices << ";" << endstr;
  387. for (size_t a = 0; a < mesh->mNumVertices; a++)
  388. //for (int a = (int)mesh->mNumVertices-1; a >=0 ; a--)
  389. {
  390. aiVector3D& uv = mesh->mTextureCoords[0][a]; // uv of first uv layer for the vertex
  391. mOutput << startstr << uv.x << ";" << uv.y;
  392. if (a < mesh->mNumVertices-1)
  393. //if (a >0 )
  394. mOutput << ";," << endstr;
  395. else
  396. mOutput << ";;" << endstr;
  397. }
  398. mOutput << startstr << "}" << endstr;
  399. }
  400. // write color channel if available
  401. if (mesh->HasVertexColors(0))
  402. {
  403. mOutput << endstr << startstr << "MeshVertexColors {" << endstr;
  404. mOutput << startstr << mesh->mNumVertices << ";" << endstr;
  405. for (size_t a = 0; a < mesh->mNumVertices; a++)
  406. {
  407. aiColor4D& mColors = mesh->mColors[0][a]; // color of first vertex color set for the vertex
  408. mOutput << startstr << a << ";" << mColors.r << ";" << mColors.g << ";" << mColors.b << ";" << mColors.a << ";;";
  409. if (a < mesh->mNumVertices-1)
  410. mOutput << "," << endstr;
  411. else
  412. mOutput << ";" << endstr;
  413. }
  414. mOutput << startstr << "}" << endstr;
  415. }
  416. /*
  417. else
  418. {
  419. mOutput << endstr << startstr << "MeshVertexColors {" << endstr;
  420. mOutput << startstr << mesh->mNumVertices << ";" << endstr;
  421. for (size_t a = 0; a < mesh->mNumVertices; a++)
  422. {
  423. aiColor4D* mColors = mesh->mColors[a];
  424. mOutput << startstr << a << ";0.500000;0.000000;0.000000;0.500000;;";
  425. if (a < mesh->mNumVertices-1)
  426. mOutput << "," << endstr;
  427. else
  428. mOutput << ";" << endstr;
  429. }
  430. mOutput << startstr << "}" << endstr;
  431. }
  432. */
  433. PopTag();
  434. mOutput << startstr << "}" << endstr << endstr;
  435. }
  436. void XFileExporter::writePath(aiString path)
  437. {
  438. std::string str = std::string(path.C_Str());
  439. BaseImporter::ConvertUTF8toISO8859_1(str);
  440. while( str.find( "\\\\") != std::string::npos)
  441. str.replace( str.find( "\\\\"), 2, "\\");
  442. while( str.find( "\\") != std::string::npos)
  443. str.replace( str.find( "\\"), 1, "/");
  444. mOutput << str;
  445. }
  446. #endif
  447. #endif