ColladaExporter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #include "AssimpPCH.h"
  34. #ifndef ASSIMP_BUILD_NO_EXPORT
  35. #ifndef ASSIMP_BUILD_NO_COLLADA_EXPORTER
  36. #include "ColladaExporter.h"
  37. #include <set>
  38. using namespace Assimp;
  39. namespace Assimp
  40. {
  41. // ------------------------------------------------------------------------------------------------
  42. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  43. void ExportSceneCollada(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  44. {
  45. // invoke the exporter
  46. ColladaExporter iDoTheExportThing( pScene);
  47. // we're still here - export successfully completed. Write result to the given IOSYstem
  48. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  49. if(outfile == NULL) {
  50. throw DeadlyExportError("could not open output .dae file: " + std::string(pFile));
  51. }
  52. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  53. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  54. }
  55. } // end of namespace Assimp
  56. // ------------------------------------------------------------------------------------------------
  57. // Constructor for a specific scene to export
  58. ColladaExporter::ColladaExporter( const aiScene* pScene)
  59. {
  60. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  61. mOutput.imbue( std::locale("C") );
  62. mScene = pScene;
  63. // set up strings
  64. endstr = "\n";
  65. // start writing
  66. WriteFile();
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. // Starts writing the contents
  70. void ColladaExporter::WriteFile()
  71. {
  72. // write the DTD
  73. mOutput << "<?xml version=\"1.0\"?>" << endstr;
  74. // COLLADA element start
  75. mOutput << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">" << endstr;
  76. PushTag();
  77. WriteHeader();
  78. WriteMaterials();
  79. WriteGeometryLibrary();
  80. WriteSceneLibrary();
  81. // useless Collada fu at the end, just in case we haven't had enough indirections, yet.
  82. mOutput << startstr << "<scene>" << endstr;
  83. PushTag();
  84. mOutput << startstr << "<instance_visual_scene url=\"#myScene\" />" << endstr;
  85. PopTag();
  86. mOutput << startstr << "</scene>" << endstr;
  87. PopTag();
  88. mOutput << "</COLLADA>" << endstr;
  89. }
  90. // ------------------------------------------------------------------------------------------------
  91. // Writes the asset header
  92. void ColladaExporter::WriteHeader()
  93. {
  94. // Dummy stuff. Nobody actually cares for it anyways
  95. mOutput << startstr << "<asset>" << endstr;
  96. PushTag();
  97. mOutput << startstr << "<contributor>" << endstr;
  98. PushTag();
  99. mOutput << startstr << "<author>Someone</author>" << endstr;
  100. mOutput << startstr << "<authoring_tool>Assimp Collada Exporter</authoring_tool>" << endstr;
  101. PopTag();
  102. mOutput << startstr << "</contributor>" << endstr;
  103. mOutput << startstr << "<created>2000-01-01T23:59:59</created>" << endstr;
  104. mOutput << startstr << "<modified>2000-01-01T23:59:59</modified>" << endstr;
  105. mOutput << startstr << "<unit name=\"centimeter\" meter=\"0.01\" />" << endstr;
  106. mOutput << startstr << "<up_axis>Y_UP</up_axis>" << endstr;
  107. PopTag();
  108. mOutput << startstr << "</asset>" << endstr;
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. // Reads a single surface entry from the given material keys
  112. void ColladaExporter::ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex)
  113. {
  114. if( pSrcMat->GetTextureCount( pTexture) > 0 )
  115. {
  116. aiString texfile;
  117. unsigned int uvChannel = 0;
  118. pSrcMat->GetTexture( pTexture, 0, &texfile, NULL, &uvChannel);
  119. poSurface.texture = texfile.C_Str();
  120. poSurface.channel = uvChannel;
  121. } else
  122. {
  123. if( pKey )
  124. pSrcMat->Get( pKey, pType, pIndex, poSurface.color);
  125. }
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // Writes an image entry for the given surface
  129. void ColladaExporter::WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd)
  130. {
  131. if( !pSurface.texture.empty() )
  132. {
  133. mOutput << startstr << "<image id=\"" << pNameAdd << "\">" << endstr;
  134. PushTag();
  135. mOutput << startstr << "<init_from>";
  136. for( std::string::const_iterator it = pSurface.texture.begin(); it != pSurface.texture.end(); ++it )
  137. {
  138. if( isalnum( *it) || *it == '_' || *it == '.' || *it == '/' || *it == '\\' )
  139. mOutput << *it;
  140. else
  141. mOutput << '%' << std::hex << size_t( (unsigned char) *it) << std::dec;
  142. }
  143. mOutput << "</init_from>" << endstr;
  144. PopTag();
  145. mOutput << startstr << "</image>" << endstr;
  146. }
  147. }
  148. // ------------------------------------------------------------------------------------------------
  149. // Writes a color-or-texture entry into an effect definition
  150. void ColladaExporter::WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName)
  151. {
  152. mOutput << startstr << "<" << pTypeName << ">" << endstr;
  153. PushTag();
  154. if( pSurface.texture.empty() )
  155. {
  156. mOutput << startstr << "<color sid=\"" << pTypeName << "\">" << pSurface.color.r << " " << pSurface.color.g << " " << pSurface.color.b << " " << pSurface.color.a << "</color>" << endstr;
  157. } else
  158. {
  159. mOutput << startstr << "<texture texture=\"" << pImageName << "\" texcoord=\"CHANNEL" << pSurface.channel << "\" />" << endstr;
  160. }
  161. PopTag();
  162. mOutput << startstr << "</" << pTypeName << ">" << endstr;
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. // Writes the two parameters necessary for referencing a texture in an effect entry
  166. void ColladaExporter::WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName)
  167. {
  168. // if surface is a texture, write out the sampler and the surface parameters necessary to reference the texture
  169. if( !pSurface.texture.empty() )
  170. {
  171. mOutput << startstr << "<newparam sid=\"" << pMatName << "-" << pTypeName << "-surface\">" << endstr;
  172. PushTag();
  173. mOutput << startstr << "<surface type=\"2D\">" << endstr;
  174. PushTag();
  175. mOutput << startstr << "<init_from>" << pMatName << "-" << pTypeName << "-image</init_from>" << endstr;
  176. PopTag();
  177. mOutput << startstr << "</surface>" << endstr;
  178. PopTag();
  179. mOutput << startstr << "</newparam>" << endstr;
  180. mOutput << startstr << "<newparam sid=\"" << pMatName << "-" << pTypeName << "-sampler\">" << endstr;
  181. PushTag();
  182. mOutput << startstr << "<sampler2D>" << endstr;
  183. PushTag();
  184. mOutput << startstr << "<source>" << pMatName << "-" << pTypeName << "-surface</source>" << endstr;
  185. PopTag();
  186. mOutput << startstr << "</sampler2D>" << endstr;
  187. PopTag();
  188. mOutput << startstr << "</newparam>" << endstr;
  189. }
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. // Writes the material setup
  193. void ColladaExporter::WriteMaterials()
  194. {
  195. materials.resize( mScene->mNumMaterials);
  196. std::set<std::string> material_names;
  197. /// collect all materials from the scene
  198. size_t numTextures = 0;
  199. for( size_t a = 0; a < mScene->mNumMaterials; ++a )
  200. {
  201. const aiMaterial* mat = mScene->mMaterials[a];
  202. aiString name;
  203. if( mat->Get( AI_MATKEY_NAME, name) != aiReturn_SUCCESS )
  204. name = "mat";
  205. if(material_names.find(name.C_Str()) != material_names.end()) {
  206. materials[a].name = std::string( "m") + boost::lexical_cast<std::string> (a) + "_" + name.C_Str();
  207. material_names.insert(materials[a].name);
  208. } else {
  209. materials[a].name = name.C_Str();
  210. }
  211. for( std::string::iterator it = materials[a].name.begin(); it != materials[a].name.end(); ++it )
  212. if( !isalnum( *it) )
  213. *it = '_';
  214. ReadMaterialSurface( materials[a].ambient, mat, aiTextureType_AMBIENT, AI_MATKEY_COLOR_AMBIENT);
  215. if( !materials[a].ambient.texture.empty() ) numTextures++;
  216. ReadMaterialSurface( materials[a].diffuse, mat, aiTextureType_DIFFUSE, AI_MATKEY_COLOR_DIFFUSE);
  217. if( !materials[a].diffuse.texture.empty() ) numTextures++;
  218. ReadMaterialSurface( materials[a].specular, mat, aiTextureType_SPECULAR, AI_MATKEY_COLOR_SPECULAR);
  219. if( !materials[a].specular.texture.empty() ) numTextures++;
  220. ReadMaterialSurface( materials[a].emissive, mat, aiTextureType_EMISSIVE, AI_MATKEY_COLOR_EMISSIVE);
  221. if( !materials[a].emissive.texture.empty() ) numTextures++;
  222. ReadMaterialSurface( materials[a].reflective, mat, aiTextureType_REFLECTION, AI_MATKEY_COLOR_REFLECTIVE);
  223. if( !materials[a].reflective.texture.empty() ) numTextures++;
  224. ReadMaterialSurface( materials[a].normal, mat, aiTextureType_NORMALS, NULL, 0, 0);
  225. if( !materials[a].normal.texture.empty() ) numTextures++;
  226. mat->Get( AI_MATKEY_SHININESS, materials[a].shininess);
  227. }
  228. // output textures if present
  229. if( numTextures > 0 )
  230. {
  231. mOutput << startstr << "<library_images>" << endstr;
  232. PushTag();
  233. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  234. {
  235. const Material& mat = *it;
  236. WriteImageEntry( mat.ambient, mat.name + "-ambient-image");
  237. WriteImageEntry( mat.diffuse, mat.name + "-diffuse-image");
  238. WriteImageEntry( mat.specular, mat.name + "-specular-image");
  239. WriteImageEntry( mat.emissive, mat.name + "-emissive-image");
  240. WriteImageEntry( mat.reflective, mat.name + "-reflective-image");
  241. WriteImageEntry( mat.normal, mat.name + "-normal-image");
  242. }
  243. PopTag();
  244. mOutput << startstr << "</library_images>" << endstr;
  245. }
  246. // output effects - those are the actual carriers of information
  247. if( !materials.empty() )
  248. {
  249. mOutput << startstr << "<library_effects>" << endstr;
  250. PushTag();
  251. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  252. {
  253. const Material& mat = *it;
  254. // this is so ridiculous it must be right
  255. mOutput << startstr << "<effect id=\"" << mat.name << "-fx\" name=\"" << mat.name << "\">" << endstr;
  256. PushTag();
  257. mOutput << startstr << "<profile_COMMON>" << endstr;
  258. PushTag();
  259. // write sampler- and surface params for the texture entries
  260. WriteTextureParamEntry( mat.emissive, "emissive", mat.name);
  261. WriteTextureParamEntry( mat.ambient, "ambient", mat.name);
  262. WriteTextureParamEntry( mat.diffuse, "diffuse", mat.name);
  263. WriteTextureParamEntry( mat.specular, "specular", mat.name);
  264. WriteTextureParamEntry( mat.reflective, "reflective", mat.name);
  265. mOutput << startstr << "<technique sid=\"standard\">" << endstr;
  266. PushTag();
  267. mOutput << startstr << "<phong>" << endstr;
  268. PushTag();
  269. WriteTextureColorEntry( mat.emissive, "emission", mat.name + "-emissive-sampler");
  270. WriteTextureColorEntry( mat.ambient, "ambient", mat.name + "-ambient-sampler");
  271. WriteTextureColorEntry( mat.diffuse, "diffuse", mat.name + "-diffuse-sampler");
  272. WriteTextureColorEntry( mat.specular, "specular", mat.name + "-specular-sampler");
  273. mOutput << startstr << "<shininess>" << endstr;
  274. PushTag();
  275. mOutput << startstr << "<float sid=\"shininess\">" << mat.shininess << "</float>" << endstr;
  276. PopTag();
  277. mOutput << startstr << "</shininess>" << endstr;
  278. WriteTextureColorEntry( mat.reflective, "reflective", mat.name + "-reflective-sampler");
  279. // deactivated because the Collada spec PHONG model does not allow other textures.
  280. // if( !mat.normal.texture.empty() )
  281. // WriteTextureColorEntry( mat.normal, "bump", mat.name + "-normal-sampler");
  282. PopTag();
  283. mOutput << startstr << "</phong>" << endstr;
  284. PopTag();
  285. mOutput << startstr << "</technique>" << endstr;
  286. PopTag();
  287. mOutput << startstr << "</profile_COMMON>" << endstr;
  288. PopTag();
  289. mOutput << startstr << "</effect>" << endstr;
  290. }
  291. PopTag();
  292. mOutput << startstr << "</library_effects>" << endstr;
  293. // write materials - they're just effect references
  294. mOutput << startstr << "<library_materials>" << endstr;
  295. PushTag();
  296. for( std::vector<Material>::const_iterator it = materials.begin(); it != materials.end(); ++it )
  297. {
  298. const Material& mat = *it;
  299. mOutput << startstr << "<material id=\"" << mat.name << "\" name=\"" << mat.name << "\">" << endstr;
  300. PushTag();
  301. mOutput << startstr << "<instance_effect url=\"#" << mat.name << "-fx\"/>" << endstr;
  302. PopTag();
  303. mOutput << startstr << "</material>" << endstr;
  304. }
  305. PopTag();
  306. mOutput << startstr << "</library_materials>" << endstr;
  307. }
  308. }
  309. // ------------------------------------------------------------------------------------------------
  310. // Writes the geometry library
  311. void ColladaExporter::WriteGeometryLibrary()
  312. {
  313. mOutput << startstr << "<library_geometries>" << endstr;
  314. PushTag();
  315. for( size_t a = 0; a < mScene->mNumMeshes; ++a)
  316. WriteGeometry( a);
  317. PopTag();
  318. mOutput << startstr << "</library_geometries>" << endstr;
  319. }
  320. // ------------------------------------------------------------------------------------------------
  321. // Writes the given mesh
  322. void ColladaExporter::WriteGeometry( size_t pIndex)
  323. {
  324. const aiMesh* mesh = mScene->mMeshes[pIndex];
  325. std::string idstr = GetMeshId( pIndex);
  326. if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 )
  327. return;
  328. // opening tag
  329. mOutput << startstr << "<geometry id=\"" << idstr << "\" name=\"" << idstr << "_name\" >" << endstr;
  330. PushTag();
  331. mOutput << startstr << "<mesh>" << endstr;
  332. PushTag();
  333. // Positions
  334. WriteFloatArray( idstr + "-positions", FloatType_Vector, (float*) mesh->mVertices, mesh->mNumVertices);
  335. // Normals, if any
  336. if( mesh->HasNormals() )
  337. WriteFloatArray( idstr + "-normals", FloatType_Vector, (float*) mesh->mNormals, mesh->mNumVertices);
  338. // texture coords
  339. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  340. {
  341. if( mesh->HasTextureCoords( a) )
  342. {
  343. WriteFloatArray( idstr + "-tex" + boost::lexical_cast<std::string> (a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2,
  344. (float*) mesh->mTextureCoords[a], mesh->mNumVertices);
  345. }
  346. }
  347. // vertex colors
  348. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  349. {
  350. if( mesh->HasVertexColors( a) )
  351. WriteFloatArray( idstr + "-color" + boost::lexical_cast<std::string> (a), FloatType_Color, (float*) mesh->mColors[a], mesh->mNumVertices);
  352. }
  353. // assemble vertex structure
  354. mOutput << startstr << "<vertices id=\"" << idstr << "-vertices" << "\">" << endstr;
  355. PushTag();
  356. mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << idstr << "-positions\" />" << endstr;
  357. if( mesh->HasNormals() )
  358. mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << idstr << "-normals\" />" << endstr;
  359. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  360. {
  361. if( mesh->HasTextureCoords( a) )
  362. mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstr << "-tex" << a << "\" " /*<< "set=\"" << a << "\"" */ << " />" << endstr;
  363. }
  364. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  365. {
  366. if( mesh->HasVertexColors( a) )
  367. mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstr << "-color" << a << "\" " /*<< set=\"" << a << "\"" */ << " />" << endstr;
  368. }
  369. PopTag();
  370. mOutput << startstr << "</vertices>" << endstr;
  371. // write face setup
  372. mOutput << startstr << "<polylist count=\"" << mesh->mNumFaces << "\" material=\"theresonlyone\">" << endstr;
  373. PushTag();
  374. mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstr << "-vertices\" />" << endstr;
  375. mOutput << startstr << "<vcount>";
  376. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  377. mOutput << mesh->mFaces[a].mNumIndices << " ";
  378. mOutput << "</vcount>" << endstr;
  379. mOutput << startstr << "<p>";
  380. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  381. {
  382. const aiFace& face = mesh->mFaces[a];
  383. for( size_t b = 0; b < face.mNumIndices; ++b )
  384. mOutput << face.mIndices[b] << " ";
  385. }
  386. mOutput << "</p>" << endstr;
  387. PopTag();
  388. mOutput << startstr << "</polylist>" << endstr;
  389. // closing tags
  390. PopTag();
  391. mOutput << startstr << "</mesh>" << endstr;
  392. PopTag();
  393. mOutput << startstr << "</geometry>" << endstr;
  394. }
  395. // ------------------------------------------------------------------------------------------------
  396. // Writes a float array of the given type
  397. void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const float* pData, size_t pElementCount)
  398. {
  399. size_t floatsPerElement = 0;
  400. switch( pType )
  401. {
  402. case FloatType_Vector: floatsPerElement = 3; break;
  403. case FloatType_TexCoord2: floatsPerElement = 2; break;
  404. case FloatType_TexCoord3: floatsPerElement = 3; break;
  405. case FloatType_Color: floatsPerElement = 3; break;
  406. default:
  407. return;
  408. }
  409. std::string arrayId = pIdString + "-array";
  410. mOutput << startstr << "<source id=\"" << pIdString << "\" name=\"" << pIdString << "\">" << endstr;
  411. PushTag();
  412. // source array
  413. mOutput << startstr << "<float_array id=\"" << arrayId << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
  414. PushTag();
  415. if( pType == FloatType_TexCoord2 )
  416. {
  417. for( size_t a = 0; a < pElementCount; ++a )
  418. {
  419. mOutput << pData[a*3+0] << " ";
  420. mOutput << pData[a*3+1] << " ";
  421. }
  422. }
  423. else if( pType == FloatType_Color )
  424. {
  425. for( size_t a = 0; a < pElementCount; ++a )
  426. {
  427. mOutput << pData[a*4+0] << " ";
  428. mOutput << pData[a*4+1] << " ";
  429. mOutput << pData[a*4+2] << " ";
  430. }
  431. }
  432. else
  433. {
  434. for( size_t a = 0; a < pElementCount * floatsPerElement; ++a )
  435. mOutput << pData[a] << " ";
  436. }
  437. mOutput << "</float_array>" << endstr;
  438. PopTag();
  439. // the usual Collada fun. Let's bloat it even more!
  440. mOutput << startstr << "<technique_common>" << endstr;
  441. PushTag();
  442. mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
  443. PushTag();
  444. switch( pType )
  445. {
  446. case FloatType_Vector:
  447. mOutput << startstr << "<param name=\"X\" type=\"float\" />" << endstr;
  448. mOutput << startstr << "<param name=\"Y\" type=\"float\" />" << endstr;
  449. mOutput << startstr << "<param name=\"Z\" type=\"float\" />" << endstr;
  450. break;
  451. case FloatType_TexCoord2:
  452. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  453. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  454. break;
  455. case FloatType_TexCoord3:
  456. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  457. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  458. mOutput << startstr << "<param name=\"P\" type=\"float\" />" << endstr;
  459. break;
  460. case FloatType_Color:
  461. mOutput << startstr << "<param name=\"R\" type=\"float\" />" << endstr;
  462. mOutput << startstr << "<param name=\"G\" type=\"float\" />" << endstr;
  463. mOutput << startstr << "<param name=\"B\" type=\"float\" />" << endstr;
  464. break;
  465. }
  466. PopTag();
  467. mOutput << startstr << "</accessor>" << endstr;
  468. PopTag();
  469. mOutput << startstr << "</technique_common>" << endstr;
  470. PopTag();
  471. mOutput << startstr << "</source>" << endstr;
  472. }
  473. // ------------------------------------------------------------------------------------------------
  474. // Writes the scene library
  475. void ColladaExporter::WriteSceneLibrary()
  476. {
  477. mOutput << startstr << "<library_visual_scenes>" << endstr;
  478. PushTag();
  479. mOutput << startstr << "<visual_scene id=\"myScene\" name=\"myScene\">" << endstr;
  480. PushTag();
  481. // start recursive write at the root node
  482. WriteNode( mScene->mRootNode);
  483. PopTag();
  484. mOutput << startstr << "</visual_scene>" << endstr;
  485. PopTag();
  486. mOutput << startstr << "</library_visual_scenes>" << endstr;
  487. }
  488. // ------------------------------------------------------------------------------------------------
  489. // Recursively writes the given node
  490. void ColladaExporter::WriteNode( const aiNode* pNode)
  491. {
  492. std::string name(pNode->mName.C_Str());
  493. std::transform(name.begin(), name.end(), name.begin(), ::tolower);
  494. if(name.compare("myscene") != 0) {
  495. mOutput << startstr << "<node id=\"" << pNode->mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr;
  496. PushTag();
  497. // write transformation - we can directly put the matrix there
  498. // TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards
  499. const aiMatrix4x4& mat = pNode->mTransformation;
  500. mOutput << startstr << "<matrix>";
  501. mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " ";
  502. mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " ";
  503. mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " ";
  504. mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4;
  505. mOutput << "</matrix>" << endstr;
  506. // instance every geometry
  507. for( size_t a = 0; a < pNode->mNumMeshes; ++a )
  508. {
  509. const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]];
  510. // do not instanciate mesh if empty. I wonder how this could happen
  511. if( mesh->mNumFaces == 0 || mesh->mNumVertices == 0 )
  512. continue;
  513. mOutput << startstr << "<instance_geometry url=\"#" << GetMeshId( pNode->mMeshes[a]) << "\">" << endstr;
  514. PushTag();
  515. mOutput << startstr << "<bind_material>" << endstr;
  516. PushTag();
  517. mOutput << startstr << "<technique_common>" << endstr;
  518. PushTag();
  519. mOutput << startstr << "<instance_material symbol=\"theresonlyone\" target=\"#" << materials[mesh->mMaterialIndex].name << "\" />" << endstr;
  520. PopTag();
  521. mOutput << startstr << "</technique_common>" << endstr;
  522. PopTag();
  523. mOutput << startstr << "</bind_material>" << endstr;
  524. PopTag();
  525. mOutput << startstr << "</instance_geometry>" << endstr;
  526. }
  527. }
  528. // recurse into subnodes
  529. for( size_t a = 0; a < pNode->mNumChildren; ++a )
  530. WriteNode( pNode->mChildren[a]);
  531. if(name.compare("myscene") != 0) {
  532. PopTag();
  533. mOutput << startstr << "</node>" << endstr;
  534. }
  535. }
  536. #endif
  537. #endif