ColladaExporter.cpp 42 KB

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