ColladaExporter.cpp 23 KB

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