ColladaExporter.cpp 28 KB

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