ColladaExporter.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  35. #include "ColladaExporter.h"
  36. #include "Bitmap.h"
  37. #include "fast_atof.h"
  38. #include "SceneCombiner.h"
  39. #include "DefaultIOSystem.h"
  40. #include "XMLTools.h"
  41. #include <assimp/IOSystem.hpp>
  42. #include <assimp/Exporter.hpp>
  43. #include <assimp/scene.h>
  44. #include "Exceptional.h"
  45. #include <memory>
  46. #include <ctime>
  47. #include <set>
  48. using namespace Assimp;
  49. namespace Assimp
  50. {
  51. // ------------------------------------------------------------------------------------------------
  52. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  53. void ExportSceneCollada(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  54. {
  55. std::string path = DefaultIOSystem::absolutePath(std::string(pFile));
  56. std::string file = DefaultIOSystem::completeBaseName(std::string(pFile));
  57. // invoke the exporter
  58. ColladaExporter iDoTheExportThing( pScene, pIOSystem, path, file);
  59. // we're still here - export successfully completed. Write result to the given IOSYstem
  60. std::unique_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  61. if(outfile == NULL) {
  62. throw DeadlyExportError("could not open output .dae file: " + std::string(pFile));
  63. }
  64. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  65. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  66. }
  67. } // end of namespace Assimp
  68. // ------------------------------------------------------------------------------------------------
  69. // Constructor for a specific scene to export
  70. ColladaExporter::ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file) : mIOSystem(pIOSystem), mPath(path), mFile(file)
  71. {
  72. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  73. mOutput.imbue( std::locale("C") );
  74. mOutput.precision(16);
  75. mScene = pScene;
  76. mSceneOwned = false;
  77. // set up strings
  78. endstr = "\n";
  79. // start writing
  80. WriteFile();
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Destructor
  84. ColladaExporter::~ColladaExporter()
  85. {
  86. if(mSceneOwned) {
  87. delete mScene;
  88. }
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // Starts writing the contents
  92. void ColladaExporter::WriteFile()
  93. {
  94. // write the DTD
  95. mOutput << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>" << endstr;
  96. // COLLADA element start
  97. mOutput << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">" << endstr;
  98. PushTag();
  99. WriteTextures();
  100. WriteHeader();
  101. WriteCamerasLibrary();
  102. WriteLightsLibrary();
  103. WriteMaterials();
  104. WriteGeometryLibrary();
  105. WriteSceneLibrary();
  106. // useless Collada fu at the end, just in case we haven't had enough indirections, yet.
  107. mOutput << startstr << "<scene>" << endstr;
  108. PushTag();
  109. mOutput << startstr << "<instance_visual_scene url=\"#" + XMLEscape(mScene->mRootNode->mName.C_Str()) + "\" />" << endstr;
  110. PopTag();
  111. mOutput << startstr << "</scene>" << endstr;
  112. PopTag();
  113. mOutput << "</COLLADA>" << endstr;
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Writes the asset header
  117. void ColladaExporter::WriteHeader()
  118. {
  119. static const ai_real epsilon = 0.00001;
  120. static const aiQuaternion x_rot(aiMatrix3x3(
  121. 0, -1, 0,
  122. 1, 0, 0,
  123. 0, 0, 1));
  124. static const aiQuaternion y_rot(aiMatrix3x3(
  125. 1, 0, 0,
  126. 0, 1, 0,
  127. 0, 0, 1));
  128. static const aiQuaternion z_rot(aiMatrix3x3(
  129. 1, 0, 0,
  130. 0, 0, 1,
  131. 0, -1, 0));
  132. static const unsigned int date_nb_chars = 20;
  133. char date_str[date_nb_chars];
  134. std::time_t date = std::time(NULL);
  135. std::strftime(date_str, date_nb_chars, "%Y-%m-%dT%H:%M:%S", std::localtime(&date));
  136. aiVector3D scaling;
  137. aiQuaternion rotation;
  138. aiVector3D position;
  139. mScene->mRootNode->mTransformation.Decompose(scaling, rotation, position);
  140. rotation.Normalize();
  141. bool add_root_node = false;
  142. ai_real scale = 1.0;
  143. if(std::abs(scaling.x - scaling.y) <= epsilon && std::abs(scaling.x - scaling.z) <= epsilon && std::abs(scaling.y - scaling.z) <= epsilon) {
  144. scale = (ai_real) ((((double) scaling.x) + ((double) scaling.y) + ((double) scaling.z)) / 3.0);
  145. } else {
  146. add_root_node = true;
  147. }
  148. std::string up_axis = "Y_UP";
  149. if(rotation.Equal(x_rot, epsilon)) {
  150. up_axis = "X_UP";
  151. } else if(rotation.Equal(y_rot, epsilon)) {
  152. up_axis = "Y_UP";
  153. } else if(rotation.Equal(z_rot, epsilon)) {
  154. up_axis = "Z_UP";
  155. } else {
  156. add_root_node = true;
  157. }
  158. if(! position.Equal(aiVector3D(0, 0, 0))) {
  159. add_root_node = true;
  160. }
  161. if(mScene->mRootNode->mNumChildren == 0) {
  162. add_root_node = true;
  163. }
  164. if(add_root_node) {
  165. aiScene* scene;
  166. SceneCombiner::CopyScene(&scene, mScene);
  167. aiNode* root = new aiNode("Scene");
  168. root->mNumChildren = 1;
  169. root->mChildren = new aiNode*[root->mNumChildren];
  170. root->mChildren[0] = scene->mRootNode;
  171. scene->mRootNode->mParent = root;
  172. scene->mRootNode = root;
  173. mScene = scene;
  174. mSceneOwned = true;
  175. up_axis = "Y_UP";
  176. scale = 1.0;
  177. }
  178. mOutput << startstr << "<asset>" << endstr;
  179. PushTag();
  180. mOutput << startstr << "<contributor>" << endstr;
  181. PushTag();
  182. aiMetadata* meta = mScene->mRootNode->mMetaData;
  183. aiString value;
  184. if (!meta || !meta->Get("Author", value))
  185. mOutput << startstr << "<author>" << "Assimp" << "</author>" << endstr;
  186. else
  187. mOutput << startstr << "<author>" << XMLEscape(value.C_Str()) << "</author>" << endstr;
  188. if (!meta || !meta->Get("AuthoringTool", value))
  189. mOutput << startstr << "<authoring_tool>" << "Assimp Exporter" << "</authoring_tool>" << endstr;
  190. else
  191. mOutput << startstr << "<authoring_tool>" << XMLEscape(value.C_Str()) << "</authoring_tool>" << endstr;
  192. //mOutput << startstr << "<author>" << mScene->author.C_Str() << "</author>" << endstr;
  193. //mOutput << startstr << "<authoring_tool>" << mScene->authoringTool.C_Str() << "</authoring_tool>" << endstr;
  194. PopTag();
  195. mOutput << startstr << "</contributor>" << endstr;
  196. mOutput << startstr << "<created>" << date_str << "</created>" << endstr;
  197. mOutput << startstr << "<modified>" << date_str << "</modified>" << endstr;
  198. mOutput << startstr << "<unit name=\"meter\" meter=\"" << scale << "\" />" << endstr;
  199. mOutput << startstr << "<up_axis>" << up_axis << "</up_axis>" << endstr;
  200. PopTag();
  201. mOutput << startstr << "</asset>" << endstr;
  202. }
  203. // ------------------------------------------------------------------------------------------------
  204. // Write the embedded textures
  205. void ColladaExporter::WriteTextures() {
  206. static const unsigned int buffer_size = 1024;
  207. char str[buffer_size];
  208. if(mScene->HasTextures()) {
  209. for(unsigned int i = 0; i < mScene->mNumTextures; i++) {
  210. // It would be great to be able to create a directory in portable standard C++, but it's not the case,
  211. // so we just write the textures in the current directory.
  212. aiTexture* texture = mScene->mTextures[i];
  213. ASSIMP_itoa10(str, buffer_size, i + 1);
  214. std::string name = mFile + "_texture_" + (i < 1000 ? "0" : "") + (i < 100 ? "0" : "") + (i < 10 ? "0" : "") + str + "." + ((const char*) texture->achFormatHint);
  215. std::unique_ptr<IOStream> outfile(mIOSystem->Open(mPath + name, "wb"));
  216. if(outfile == NULL) {
  217. throw DeadlyExportError("could not open output texture file: " + mPath + name);
  218. }
  219. if(texture->mHeight == 0) {
  220. outfile->Write((void*) texture->pcData, texture->mWidth, 1);
  221. } else {
  222. Bitmap::Save(texture, outfile.get());
  223. }
  224. outfile->Flush();
  225. textures.insert(std::make_pair(i, name));
  226. }
  227. }
  228. }
  229. // ------------------------------------------------------------------------------------------------
  230. // Write the embedded textures
  231. void ColladaExporter::WriteCamerasLibrary() {
  232. if(mScene->HasCameras()) {
  233. mOutput << startstr << "<library_cameras>" << endstr;
  234. PushTag();
  235. for( size_t a = 0; a < mScene->mNumCameras; ++a)
  236. WriteCamera( a);
  237. PopTag();
  238. mOutput << startstr << "</library_cameras>" << endstr;
  239. }
  240. }
  241. void ColladaExporter::WriteCamera(size_t pIndex){
  242. const aiCamera *cam = mScene->mCameras[pIndex];
  243. const std::string idstrEscaped = XMLEscape(cam->mName.C_Str());
  244. mOutput << startstr << "<camera id=\"" << idstrEscaped << "-camera\" name=\"" << idstrEscaped << "_name\" >" << endstr;
  245. PushTag();
  246. mOutput << startstr << "<optics>" << endstr;
  247. PushTag();
  248. mOutput << startstr << "<technique_common>" << endstr;
  249. PushTag();
  250. //assimp doesn't support the import of orthographic cameras! se we write
  251. //always perspective
  252. mOutput << startstr << "<perspective>" << endstr;
  253. PushTag();
  254. mOutput << startstr << "<xfov sid=\"xfov\">"<<
  255. AI_RAD_TO_DEG(cam->mHorizontalFOV)
  256. <<"</xfov>" << endstr;
  257. mOutput << startstr << "<aspect_ratio>"
  258. << cam->mAspect
  259. << "</aspect_ratio>" << endstr;
  260. mOutput << startstr << "<znear sid=\"znear\">"
  261. << cam->mClipPlaneNear
  262. << "</znear>" << endstr;
  263. mOutput << startstr << "<zfar sid=\"zfar\">"
  264. << cam->mClipPlaneFar
  265. << "</zfar>" << endstr;
  266. PopTag();
  267. mOutput << startstr << "</perspective>" << endstr;
  268. PopTag();
  269. mOutput << startstr << "</technique_common>" << endstr;
  270. PopTag();
  271. mOutput << startstr << "</optics>" << endstr;
  272. PopTag();
  273. mOutput << startstr << "</camera>" << endstr;
  274. }
  275. // ------------------------------------------------------------------------------------------------
  276. // Write the embedded textures
  277. void ColladaExporter::WriteLightsLibrary() {
  278. if(mScene->HasLights()) {
  279. mOutput << startstr << "<library_lights>" << endstr;
  280. PushTag();
  281. for( size_t a = 0; a < mScene->mNumLights; ++a)
  282. WriteLight( a);
  283. PopTag();
  284. mOutput << startstr << "</library_lights>" << endstr;
  285. }
  286. }
  287. void ColladaExporter::WriteLight(size_t pIndex){
  288. const aiLight *light = mScene->mLights[pIndex];
  289. const std::string idstrEscaped = XMLEscape(light->mName.C_Str());
  290. mOutput << startstr << "<light id=\"" << idstrEscaped << "-light\" name=\""
  291. << idstrEscaped << "_name\" >" << endstr;
  292. PushTag();
  293. mOutput << startstr << "<technique_common>" << endstr;
  294. PushTag();
  295. switch(light->mType){
  296. case aiLightSource_AMBIENT:
  297. WriteAmbienttLight(light);
  298. break;
  299. case aiLightSource_DIRECTIONAL:
  300. WriteDirectionalLight(light);
  301. break;
  302. case aiLightSource_POINT:
  303. WritePointLight(light);
  304. break;
  305. case aiLightSource_SPOT:
  306. WriteSpotLight(light);
  307. break;
  308. case aiLightSource_UNDEFINED:
  309. case _aiLightSource_Force32Bit:
  310. break;
  311. }
  312. PopTag();
  313. mOutput << startstr << "</technique_common>" << endstr;
  314. PopTag();
  315. mOutput << startstr << "</light>" << endstr;
  316. }
  317. void ColladaExporter::WritePointLight(const aiLight *const light){
  318. const aiColor3D &color= light->mColorDiffuse;
  319. mOutput << startstr << "<point>" << endstr;
  320. PushTag();
  321. mOutput << startstr << "<color sid=\"color\">"
  322. << color.r<<" "<<color.g<<" "<<color.b
  323. <<"</color>" << endstr;
  324. mOutput << startstr << "<constant_attenuation>"
  325. << light->mAttenuationConstant
  326. <<"</constant_attenuation>" << endstr;
  327. mOutput << startstr << "<linear_attenuation>"
  328. << light->mAttenuationLinear
  329. <<"</linear_attenuation>" << endstr;
  330. mOutput << startstr << "<quadratic_attenuation>"
  331. << light->mAttenuationQuadratic
  332. <<"</quadratic_attenuation>" << endstr;
  333. PopTag();
  334. mOutput << startstr << "</point>" << endstr;
  335. }
  336. void ColladaExporter::WriteDirectionalLight(const aiLight *const light){
  337. const aiColor3D &color= light->mColorDiffuse;
  338. mOutput << startstr << "<directional>" << endstr;
  339. PushTag();
  340. mOutput << startstr << "<color sid=\"color\">"
  341. << color.r<<" "<<color.g<<" "<<color.b
  342. <<"</color>" << endstr;
  343. PopTag();
  344. mOutput << startstr << "</directional>" << endstr;
  345. }
  346. void ColladaExporter::WriteSpotLight(const aiLight *const light){
  347. const aiColor3D &color= light->mColorDiffuse;
  348. mOutput << startstr << "<spot>" << endstr;
  349. PushTag();
  350. mOutput << startstr << "<color sid=\"color\">"
  351. << color.r<<" "<<color.g<<" "<<color.b
  352. <<"</color>" << endstr;
  353. mOutput << startstr << "<constant_attenuation>"
  354. << light->mAttenuationConstant
  355. <<"</constant_attenuation>" << endstr;
  356. mOutput << startstr << "<linear_attenuation>"
  357. << light->mAttenuationLinear
  358. <<"</linear_attenuation>" << endstr;
  359. mOutput << startstr << "<quadratic_attenuation>"
  360. << light->mAttenuationQuadratic
  361. <<"</quadratic_attenuation>" << endstr;
  362. /*
  363. out->mAngleOuterCone = AI_DEG_TO_RAD (std::acos(std::pow(0.1f,1.f/srcLight->mFalloffExponent))+
  364. srcLight->mFalloffAngle);
  365. */
  366. const ai_real fallOffAngle = AI_RAD_TO_DEG(light->mAngleInnerCone);
  367. mOutput << startstr <<"<falloff_angle sid=\"fall_off_angle\">"
  368. << fallOffAngle
  369. <<"</falloff_angle>" << endstr;
  370. double temp = light->mAngleOuterCone-light->mAngleInnerCone;
  371. temp = std::cos(temp);
  372. temp = std::log(temp)/std::log(0.1);
  373. temp = 1/temp;
  374. mOutput << startstr << "<falloff_exponent sid=\"fall_off_exponent\">"
  375. << temp
  376. <<"</falloff_exponent>" << endstr;
  377. PopTag();
  378. mOutput << startstr << "</spot>" << endstr;
  379. }
  380. void ColladaExporter::WriteAmbienttLight(const aiLight *const light){
  381. const aiColor3D &color= light->mColorAmbient;
  382. mOutput << startstr << "<ambient>" << endstr;
  383. PushTag();
  384. mOutput << startstr << "<color sid=\"color\">"
  385. << color.r<<" "<<color.g<<" "<<color.b
  386. <<"</color>" << endstr;
  387. PopTag();
  388. mOutput << startstr << "</ambient>" << endstr;
  389. }
  390. // ------------------------------------------------------------------------------------------------
  391. // Reads a single surface entry from the given material keys
  392. void ColladaExporter::ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex)
  393. {
  394. if( pSrcMat->GetTextureCount( pTexture) > 0 )
  395. {
  396. aiString texfile;
  397. unsigned int uvChannel = 0;
  398. pSrcMat->GetTexture( pTexture, 0, &texfile, NULL, &uvChannel);
  399. std::string index_str(texfile.C_Str());
  400. if(index_str.size() != 0 && index_str[0] == '*')
  401. {
  402. unsigned int index;
  403. index_str = index_str.substr(1, std::string::npos);
  404. try {
  405. index = (unsigned int) strtoul10_64(index_str.c_str());
  406. } catch(std::exception& error) {
  407. throw DeadlyExportError(error.what());
  408. }
  409. std::map<unsigned int, std::string>::const_iterator name = textures.find(index);
  410. if(name != textures.end()) {
  411. poSurface.texture = name->second;
  412. } else {
  413. throw DeadlyExportError("could not find embedded texture at index " + index_str);
  414. }
  415. } else
  416. {
  417. poSurface.texture = texfile.C_Str();
  418. }
  419. poSurface.channel = uvChannel;
  420. poSurface.exist = true;
  421. } else
  422. {
  423. if( pKey )
  424. poSurface.exist = pSrcMat->Get( pKey, pType, pIndex, poSurface.color) == aiReturn_SUCCESS;
  425. }
  426. }
  427. // ------------------------------------------------------------------------------------------------
  428. // Reimplementation of isalnum(,C locale), because AppVeyor does not see standard version.
  429. static bool isalnum_C(char c)
  430. {
  431. return ( nullptr != strchr("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",c) );
  432. }
  433. // ------------------------------------------------------------------------------------------------
  434. // Writes an image entry for the given surface
  435. void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd)
  436. {
  437. if( !pSurface.texture.empty() )
  438. {
  439. mOutput << startstr << "<image id=\"" << XMLEscape(pNameAdd) << "\">" << endstr;
  440. PushTag();
  441. mOutput << startstr << "<init_from>";
  442. // URL encode image file name first, then XML encode on top
  443. std::stringstream imageUrlEncoded;
  444. for( std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it )
  445. {
  446. if( isalnum_C( (unsigned char) *it) || *it == ':' || *it == '_' || *it == '.' || *it == '/' || *it == '\\' )
  447. imageUrlEncoded << *it;
  448. else
  449. imageUrlEncoded << '%' << std::hex << size_t( (unsigned char) *it) << std::dec;
  450. }
  451. mOutput << XMLEscape(imageUrlEncoded.str());
  452. mOutput << "</init_from>" << endstr;
  453. PopTag();
  454. mOutput << startstr << "</image>" << endstr;
  455. }
  456. }
  457. // ------------------------------------------------------------------------------------------------
  458. // Writes a color-or-texture entry into an effect definition
  459. void ColladaExporter::WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName)
  460. {
  461. if(pSurface.exist) {
  462. mOutput << startstr << "<" << pTypeName << ">" << endstr;
  463. PushTag();
  464. if( pSurface.texture.empty() )
  465. {
  466. mOutput << startstr << "<color sid=\"" << pTypeName << "\">" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "</color>" << endstr;
  467. }
  468. else
  469. {
  470. mOutput << startstr << "<texture texture=\"" << XMLEscape(pImageName) << "\" texcoord=\"CHANNEL" << pSurface.channel << "\" />" << endstr;
  471. }
  472. PopTag();
  473. mOutput << startstr << "</" << pTypeName << ">" << endstr;
  474. }
  475. }
  476. // ------------------------------------------------------------------------------------------------
  477. // Writes the two parameters necessary for referencing a texture in an effect entry
  478. void ColladaExporter::WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName)
  479. {
  480. // if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture
  481. if( !pSurface.texture.empty() )
  482. {
  483. mOutput << startstr << "<newparam sid=\"" << XMLEscape(pMatName) << "-" << pTypeName << "-surface\">" << endstr;
  484. PushTag();
  485. mOutput << startstr << "<surface type=\"2D\">" << endstr;
  486. PushTag();
  487. mOutput << startstr << "<init_from>" << XMLEscape(pMatName) << "-" << pTypeName << "-image</init_from>" << endstr;
  488. PopTag();
  489. mOutput << startstr << "</surface>" << endstr;
  490. PopTag();
  491. mOutput << startstr << "</newparam>" << endstr;
  492. mOutput << startstr << "<newparam sid=\"" << XMLEscape(pMatName) << "-" << pTypeName << "-sampler\">" << endstr;
  493. PushTag();
  494. mOutput << startstr << "<sampler2D>" << endstr;
  495. PushTag();
  496. mOutput << startstr << "<source>" << XMLEscape(pMatName) << "-" << pTypeName << "-surface</source>" << endstr;
  497. PopTag();
  498. mOutput << startstr << "</sampler2D>" << endstr;
  499. PopTag();
  500. mOutput << startstr << "</newparam>" << endstr;
  501. }
  502. }
  503. // ------------------------------------------------------------------------------------------------
  504. // Writes a scalar property
  505. void ColladaExporter::WriteFloatEntry( const Property& pProperty, const std::string& pTypeName)
  506. {
  507. if(pProperty.exist) {
  508. mOutput << startstr << "<" << pTypeName << ">" << endstr;
  509. PushTag();
  510. mOutput << startstr << "<float sid=\"" << pTypeName << "\">" << pProperty.value << "</float>" << endstr;
  511. PopTag();
  512. mOutput << startstr << "</" << pTypeName << ">" << endstr;
  513. }
  514. }
  515. // ------------------------------------------------------------------------------------------------
  516. // Writes the material setup
  517. void ColladaExporter::WriteMaterials()
  518. {
  519. materials.resize( mScene->mNumMaterials);
  520. /// collect all materials from the scene
  521. size_t numTextures = 0;
  522. for( size_t a = 0; a < mScene->mNumMaterials; ++a )
  523. {
  524. const aiMaterial* mat = mScene->mMaterials[a];
  525. aiString name;
  526. if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS )
  527. name = "mat";
  528. materials[a].name = std::string( "m") + std::to_string(a) + name.C_Str();
  529. for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it ) {
  530. if( !isalnum_C( *it ) ) {
  531. *it = '_';
  532. }
  533. }
  534. aiShadingMode shading = aiShadingMode_Flat;
  535. materials[a].shading_model = "phong";
  536. if(mat->Get( AI_MATKEY_SHADING_MODEL, shading) == aiReturn_SUCCESS) {
  537. if(shading == aiShadingMode_Phong) {
  538. materials[a].shading_model = "phong";
  539. } else if(shading == aiShadingMode_Blinn) {
  540. materials[a].shading_model = "blinn";
  541. } else if(shading == aiShadingMode_NoShading) {
  542. materials[a].shading_model = "constant";
  543. } else if(shading == aiShadingMode_Gouraud) {
  544. materials[a].shading_model = "lambert";
  545. }
  546. }
  547. ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
  548. if( !materials[a].ambient.texture.empty() ) numTextures++;
  549. ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
  550. if( !materials[a].diffuse.texture.empty() ) numTextures++;
  551. ReadMaterialSurface( materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR);
  552. if( !materials[a].specular.texture.empty() ) numTextures++;
  553. ReadMaterialSurface( materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE);
  554. if( !materials[a].emissive.texture.empty() ) numTextures++;
  555. ReadMaterialSurface( materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE);
  556. if( !materials[a].reflective.texture.empty() ) numTextures++;
  557. ReadMaterialSurface( materials[a].transparent, mat, aiTextureType_OPACITY, AI_MATKEY_COLOR_TRANSPARENT);
  558. if( !materials[a].transparent.texture.empty() ) numTextures++;
  559. ReadMaterialSurface( materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0);
  560. if( !materials[a].normal.texture.empty() ) numTextures++;
  561. materials[a].shininess.exist = mat->Get( AI_MATKEY_SHININESS, materials[a].shininess.value) == aiReturn_SUCCESS;
  562. materials[a].transparency.exist = mat->Get( AI_MATKEY_OPACITY, materials[a].transparency.value) == aiReturn_SUCCESS;
  563. materials[a].transparency.value = 1 - materials[a].transparency.value;
  564. materials[a].index_refraction.exist = mat->Get( AI_MATKEY_REFRACTI, materials[a].index_refraction.value) == aiReturn_SUCCESS;
  565. }
  566. // output textures if present
  567. if( numTextures > 0 )
  568. {
  569. mOutput << startstr << "<library_images>" << endstr;
  570. PushTag();
  571. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  572. {
  573. const Material& mat = *it;
  574. WriteImageEntry( mat.ambient, mat.name + "-ambient-image");
  575. WriteImageEntry( mat.diffuse, mat.name + "-diffuse-image");
  576. WriteImageEntry( mat.specular, mat.name + "-specular-image");
  577. WriteImageEntry( mat.emissive, mat.name + "-emission-image");
  578. WriteImageEntry( mat.reflective, mat.name + "-reflective-image");
  579. WriteImageEntry( mat.transparent, mat.name + "-transparent-image");
  580. WriteImageEntry( mat.normal, mat.name + "-normal-image");
  581. }
  582. PopTag();
  583. mOutput << startstr << "</library_images>" << endstr;
  584. }
  585. // output effects - those are the actual carriers of information
  586. if( !materials.empty() )
  587. {
  588. mOutput << startstr << "<library_effects>" << endstr;
  589. PushTag();
  590. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  591. {
  592. const Material& mat = *it;
  593. // this is so ridiculous it must be right
  594. mOutput << startstr << "<effect id=\"" << XMLEscape(mat.name) << "-fx\" name=\"" << XMLEscape(mat.name) << "\">" << endstr;
  595. PushTag();
  596. mOutput << startstr << "<profile_COMMON>" << endstr;
  597. PushTag();
  598. // write sampler- and surface params for the texture entries
  599. WriteTextureParamEntry( mat.emissive, "emission", mat.name);
  600. WriteTextureParamEntry( mat.ambient, "ambient", mat.name);
  601. WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name);
  602. WriteTextureParamEntry( mat.specular, "specular", mat.name);
  603. WriteTextureParamEntry( mat.reflective, "reflective", mat.name);
  604. WriteTextureParamEntry( mat.transparent, "transparent", mat.name);
  605. WriteTextureParamEntry( mat.normal, "normal", mat.name);
  606. mOutput << startstr << "<technique sid=\"standard\">" << endstr;
  607. PushTag();
  608. mOutput << startstr << "<" << mat.shading_model << ">" << endstr;
  609. PushTag();
  610. WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emission-sampler");
  611. WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler");
  612. WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler");
  613. WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler");
  614. WriteFloatEntry(mat.shininess, "shininess");
  615. WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler");
  616. WriteTextureColorEntry( mat.transparent, "transparent", mat.name + "-transparent-sampler");
  617. WriteFloatEntry(mat.transparency, "transparency");
  618. WriteFloatEntry(mat.index_refraction, "index_of_refraction");
  619. if(! mat.normal.texture.empty()) {
  620. WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler");
  621. }
  622. PopTag();
  623. mOutput << startstr << "</" << mat.shading_model << ">" << endstr;
  624. PopTag();
  625. mOutput << startstr << "</technique>" << endstr;
  626. PopTag();
  627. mOutput << startstr << "</profile_COMMON>" << endstr;
  628. PopTag();
  629. mOutput << startstr << "</effect>" << endstr;
  630. }
  631. PopTag();
  632. mOutput << startstr << "</library_effects>" << endstr;
  633. // write materials - they're just effect references
  634. mOutput << startstr << "<library_materials>" << endstr;
  635. PushTag();
  636. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  637. {
  638. const Material& mat = *it;
  639. mOutput << startstr << "<material id=\"" << XMLEscape(mat.name) << "\" name=\"" << mat.name << "\">" << endstr;
  640. PushTag();
  641. mOutput << startstr << "<instance_effect url=\"#" << XMLEscape(mat.name) << "-fx\"/>" << endstr;
  642. PopTag();
  643. mOutput << startstr << "</material>" << endstr;
  644. }
  645. PopTag();
  646. mOutput << startstr << "</library_materials>" << endstr;
  647. }
  648. }
  649. // ------------------------------------------------------------------------------------------------
  650. // Writes the geometry library
  651. void ColladaExporter::WriteGeometryLibrary()
  652. {
  653. mOutput << startstr << "<library_geometries>" << endstr;
  654. PushTag();
  655. for( size_t a = 0; a < mScene->mNumMeshes; ++a)
  656. WriteGeometry( a);
  657. PopTag();
  658. mOutput << startstr << "</library_geometries>" << endstr;
  659. }
  660. // ------------------------------------------------------------------------------------------------
  661. // Writes the given mesh
  662. void ColladaExporter::WriteGeometry( size_t pIndex)
  663. {
  664. const aiMesh* mesh = mScene->mMeshes[pIndex];
  665. const std::string idstr = GetMeshId( pIndex);
  666. const std::string idstrEscaped = XMLEscape(idstr);
  667. if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 )
  668. return;
  669. // opening tag
  670. mOutput << startstr << "<geometry id=\"" << idstrEscaped << "\" name=\"" << idstrEscaped << "_name\" >" << endstr;
  671. PushTag();
  672. mOutput << startstr << "<mesh>" << endstr;
  673. PushTag();
  674. // Positions
  675. WriteFloatArray( idstr + "-positions", FloatType_Vector, (ai_real*) mesh->mVertices, mesh->mNumVertices);
  676. // Normals, if any
  677. if( mesh->HasNormals() )
  678. WriteFloatArray( idstr + "-normals", FloatType_Vector, (ai_real*) mesh->mNormals, mesh->mNumVertices);
  679. // texture coords
  680. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  681. {
  682. if( mesh->HasTextureCoords( a) )
  683. {
  684. WriteFloatArray( idstr + "-tex" + std::to_string(a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2,
  685. (ai_real*) mesh->mTextureCoords[a], mesh->mNumVertices);
  686. }
  687. }
  688. // vertex colors
  689. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  690. {
  691. if( mesh->HasVertexColors( a) )
  692. WriteFloatArray( idstr + "-color" + std::to_string(a), FloatType_Color, (ai_real*) mesh->mColors[a], mesh->mNumVertices);
  693. }
  694. // assemble vertex structure
  695. mOutput << startstr << "<vertices id=\"" << idstrEscaped << "-vertices" << "\">" << endstr;
  696. PushTag();
  697. mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << idstrEscaped << "-positions\" />" << endstr;
  698. if( mesh->HasNormals() )
  699. mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << idstrEscaped << "-normals\" />" << endstr;
  700. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  701. {
  702. if( mesh->HasTextureCoords( a) )
  703. mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstrEscaped << "-tex" << a << "\" " /*<< "set=\"" << a << "\"" */ << " />" << endstr;
  704. }
  705. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  706. {
  707. if( mesh->HasVertexColors( a) )
  708. mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstrEscaped << "-color" << a << "\" " /*<< set=\"" << a << "\"" */ << " />" << endstr;
  709. }
  710. PopTag();
  711. mOutput << startstr << "</vertices>" << endstr;
  712. // count the number of lines, triangles and polygon meshes
  713. int countLines = 0;
  714. int countPoly = 0;
  715. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  716. {
  717. if (mesh->mFaces[a].mNumIndices == 2) countLines++;
  718. else if (mesh->mFaces[a].mNumIndices >= 3) countPoly++;
  719. }
  720. // lines
  721. if (countLines)
  722. {
  723. mOutput << startstr << "<lines count=\"" << countLines << "\" material=\"defaultMaterial\">" << endstr;
  724. PushTag();
  725. mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstrEscaped << "-vertices\" />" << endstr;
  726. mOutput << startstr << "<p>";
  727. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  728. {
  729. const aiFace& face = mesh->mFaces[a];
  730. if (face.mNumIndices != 2) continue;
  731. for( size_t b = 0; b < face.mNumIndices; ++b )
  732. mOutput << face.mIndices[b] << " ";
  733. }
  734. mOutput << "</p>" << endstr;
  735. PopTag();
  736. mOutput << startstr << "</lines>" << endstr;
  737. }
  738. // triangle - don't use it, because compatibility problems
  739. // polygons
  740. if (countPoly)
  741. {
  742. mOutput << startstr << "<polylist count=\"" << countPoly << "\" material=\"defaultMaterial\">" << endstr;
  743. PushTag();
  744. mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstrEscaped << "-vertices\" />" << endstr;
  745. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  746. {
  747. if( mesh->HasTextureCoords( a) )
  748. mOutput << startstr << "<input offset=\"0\" semantic=\"TEXCOORD\" source=\"#" << idstrEscaped << "-tex" << a << "\" set=\"" << a << "\" />" << endstr;
  749. }
  750. mOutput << startstr << "<vcount>";
  751. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  752. {
  753. if (mesh->mFaces[a].mNumIndices < 3) continue;
  754. mOutput << mesh->mFaces[a].mNumIndices << " ";
  755. }
  756. mOutput << "</vcount>" << endstr;
  757. mOutput << startstr << "<p>";
  758. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  759. {
  760. const aiFace& face = mesh->mFaces[a];
  761. if (face.mNumIndices < 3) continue;
  762. for( size_t b = 0; b < face.mNumIndices; ++b )
  763. mOutput << face.mIndices[b] << " ";
  764. }
  765. mOutput << "</p>" << endstr;
  766. PopTag();
  767. mOutput << startstr << "</polylist>" << endstr;
  768. }
  769. // closing tags
  770. PopTag();
  771. mOutput << startstr << "</mesh>" << endstr;
  772. PopTag();
  773. mOutput << startstr << "</geometry>" << endstr;
  774. }
  775. // ------------------------------------------------------------------------------------------------
  776. // Writes a float array of the given type
  777. void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const ai_real* pData, size_t pElementCount)
  778. {
  779. size_t floatsPerElement = 0;
  780. switch( pType )
  781. {
  782. case FloatType_Vector: floatsPerElement = 3; break;
  783. case FloatType_TexCoord2: floatsPerElement = 2; break;
  784. case FloatType_TexCoord3: floatsPerElement = 3; break;
  785. case FloatType_Color: floatsPerElement = 3; break;
  786. default:
  787. return;
  788. }
  789. std::string arrayId = pIdString + "-array";
  790. mOutput << startstr << "<source id=\"" << XMLEscape(pIdString) << "\" name=\"" << XMLEscape(pIdString) << "\">" << endstr;
  791. PushTag();
  792. // source array
  793. mOutput << startstr << "<float_array id=\"" << XMLEscape(arrayId) << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
  794. PushTag();
  795. if( pType == FloatType_TexCoord2 )
  796. {
  797. for( size_t a = 0; a < pElementCount; ++a )
  798. {
  799. mOutput << pData[a*3+0] << " ";
  800. mOutput << pData[a*3+1] << " ";
  801. }
  802. }
  803. else if( pType == FloatType_Color )
  804. {
  805. for( size_t a = 0; a < pElementCount; ++a )
  806. {
  807. mOutput << pData[a*4+0] << " ";
  808. mOutput << pData[a*4+1] << " ";
  809. mOutput << pData[a*4+2] << " ";
  810. }
  811. }
  812. else
  813. {
  814. for( size_t a = 0; a < pElementCount * floatsPerElement; ++a )
  815. mOutput << pData[a] << " ";
  816. }
  817. mOutput << "</float_array>" << endstr;
  818. PopTag();
  819. // the usual Collada fun. Let's bloat it even more!
  820. mOutput << startstr << "<technique_common>" << endstr;
  821. PushTag();
  822. mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
  823. PushTag();
  824. switch( pType )
  825. {
  826. case FloatType_Vector:
  827. mOutput << startstr << "<param name=\"X\" type=\"float\" />" << endstr;
  828. mOutput << startstr << "<param name=\"Y\" type=\"float\" />" << endstr;
  829. mOutput << startstr << "<param name=\"Z\" type=\"float\" />" << endstr;
  830. break;
  831. case FloatType_TexCoord2:
  832. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  833. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  834. break;
  835. case FloatType_TexCoord3:
  836. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  837. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  838. mOutput << startstr << "<param name=\"P\" type=\"float\" />" << endstr;
  839. break;
  840. case FloatType_Color:
  841. mOutput << startstr << "<param name=\"R\" type=\"float\" />" << endstr;
  842. mOutput << startstr << "<param name=\"G\" type=\"float\" />" << endstr;
  843. mOutput << startstr << "<param name=\"B\" type=\"float\" />" << endstr;
  844. break;
  845. }
  846. PopTag();
  847. mOutput << startstr << "</accessor>" << endstr;
  848. PopTag();
  849. mOutput << startstr << "</technique_common>" << endstr;
  850. PopTag();
  851. mOutput << startstr << "</source>" << endstr;
  852. }
  853. // ------------------------------------------------------------------------------------------------
  854. // Writes the scene library
  855. void ColladaExporter::WriteSceneLibrary()
  856. {
  857. const std::string scene_name_escaped = XMLEscape(mScene->mRootNode->mName.C_Str());
  858. mOutput << startstr << "<library_visual_scenes>" << endstr;
  859. PushTag();
  860. mOutput << startstr << "<visual_scene id=\"" + scene_name_escaped + "\" name=\"" + scene_name_escaped + "\">" << endstr;
  861. PushTag();
  862. // start recursive write at the root node
  863. for( size_t a = 0; a < mScene->mRootNode->mNumChildren; ++a )
  864. WriteNode( mScene, mScene->mRootNode->mChildren[a]);
  865. PopTag();
  866. mOutput << startstr << "</visual_scene>" << endstr;
  867. PopTag();
  868. mOutput << startstr << "</library_visual_scenes>" << endstr;
  869. }
  870. // ------------------------------------------------------------------------------------------------
  871. // Helper to find a bone by name in the scene
  872. aiBone* findBone( const aiScene* scene, const char * name) {
  873. for (size_t m=0; m<scene->mNumMeshes; m++) {
  874. aiMesh * mesh = scene->mMeshes[m];
  875. for (size_t b=0; b<mesh->mNumBones; b++) {
  876. aiBone * bone = mesh->mBones[b];
  877. if (0 == strcmp(name, bone->mName.C_Str())) {
  878. return bone;
  879. }
  880. }
  881. }
  882. return NULL;
  883. }
  884. // ------------------------------------------------------------------------------------------------
  885. // Recursively writes the given node
  886. void ColladaExporter::WriteNode( const aiScene* pScene, aiNode* pNode)
  887. {
  888. // the node must have a name
  889. if (pNode->mName.length == 0)
  890. {
  891. std::stringstream ss;
  892. ss << "Node_" << pNode;
  893. pNode->mName.Set(ss.str());
  894. }
  895. // If the node is associated with a bone, it is a joint node (JOINT)
  896. // otherwise it is a normal node (NODE)
  897. const char * node_type;
  898. if (NULL == findBone(pScene, pNode->mName.C_Str())) {
  899. node_type = "NODE";
  900. } else {
  901. node_type = "JOINT";
  902. }
  903. const std::string node_name_escaped = XMLEscape(pNode->mName.data);
  904. mOutput << startstr
  905. << "<node id=\"" << node_name_escaped
  906. << "\" name=\"" << node_name_escaped
  907. << "\" type=\"" << node_type
  908. << "\">" << endstr;
  909. PushTag();
  910. // write transformation - we can directly put the matrix there
  911. // TODO: (thom) decompose into scale - rot - quad to allow addressing it by animations afterwards
  912. const aiMatrix4x4& mat = pNode->mTransformation;
  913. mOutput << startstr << "<matrix>";
  914. mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " ";
  915. mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " ";
  916. mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " ";
  917. mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4;
  918. mOutput << "</matrix>" << endstr;
  919. if(pNode->mNumMeshes==0){
  920. //check if it is a camera node
  921. for(size_t i=0; i<mScene->mNumCameras; i++){
  922. if(mScene->mCameras[i]->mName == pNode->mName){
  923. mOutput << startstr <<"<instance_camera url=\"#" << node_name_escaped << "-camera\"/>" << endstr;
  924. break;
  925. }
  926. }
  927. //check if it is a light node
  928. for(size_t i=0; i<mScene->mNumLights; i++){
  929. if(mScene->mLights[i]->mName == pNode->mName){
  930. mOutput << startstr <<"<instance_light url=\"#" << node_name_escaped << "-light\"/>" << endstr;
  931. break;
  932. }
  933. }
  934. }else
  935. // instance every geometry
  936. for( size_t a = 0; a < pNode->mNumMeshes; ++a )
  937. {
  938. const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]];
  939. // do not instanciate mesh if empty. I wonder how this could happen
  940. if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 )
  941. continue;
  942. mOutput << startstr << "<instance_geometry url=\"#" << XMLEscape(GetMeshId( pNode->mMeshes[a])) << "\">" << endstr;
  943. PushTag();
  944. mOutput << startstr << "<bind_material>" << endstr;
  945. PushTag();
  946. mOutput << startstr << "<technique_common>" << endstr;
  947. PushTag();
  948. mOutput << startstr << "<instance_material symbol=\"defaultMaterial\" target=\"#" << XMLEscape(materials[mesh->mMaterialIndex].name) << "\">" << endstr;
  949. PushTag();
  950. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  951. {
  952. if( mesh->HasTextureCoords( a) )
  953. // semantic as in <texture texcoord=...>
  954. // input_semantic as in <input semantic=...>
  955. // input_set as in <input set=...>
  956. mOutput << startstr << "<bind_vertex_input semantic=\"CHANNEL" << a << "\" input_semantic=\"TEXCOORD\" input_set=\"" << a << "\"/>" << endstr;
  957. }
  958. PopTag();
  959. mOutput << startstr << "</instance_material>" << endstr;
  960. PopTag();
  961. mOutput << startstr << "</technique_common>" << endstr;
  962. PopTag();
  963. mOutput << startstr << "</bind_material>" << endstr;
  964. PopTag();
  965. mOutput << startstr << "</instance_geometry>" << endstr;
  966. }
  967. // recurse into subnodes
  968. for( size_t a = 0; a < pNode->mNumChildren; ++a )
  969. WriteNode( pScene, pNode->mChildren[a]);
  970. PopTag();
  971. mOutput << startstr << "</node>" << endstr;
  972. }
  973. #endif
  974. #endif