ColladaExporter.cpp 23 KB

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