ColladaExporter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2010, ASSIMP Development 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 Development 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. #include "ColladaExporter.h"
  36. using namespace Assimp;
  37. namespace Assimp
  38. {
  39. // ------------------------------------------------------------------------------------------------
  40. // Worker function for exporting a scene to Collada. Prototyped and registered in Exporter.cpp
  41. void ExportSceneCollada(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  42. {
  43. // invoke the exporter
  44. ColladaExporter iDoTheExportThing( pScene);
  45. // we're still here - export successfully completed. Write result to the given IOSYstem
  46. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  47. // XXX maybe use a small wrapper around IOStream that behaves like std::stringstream in order to avoid the extra copy.
  48. outfile->Write( iDoTheExportThing.mOutput.str().c_str(), static_cast<size_t>(iDoTheExportThing.mOutput.tellp()),1);
  49. }
  50. } // end of namespace Assimp
  51. // ------------------------------------------------------------------------------------------------
  52. // Constructor for a specific scene to export
  53. ColladaExporter::ColladaExporter( const aiScene* pScene)
  54. {
  55. mScene = pScene;
  56. // set up strings
  57. endstr = "\n"; // std::endl is too complicated for me to insert here.
  58. // start writing
  59. WriteFile();
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Starts writing the contents
  63. void ColladaExporter::WriteFile()
  64. {
  65. // write the DTD
  66. mOutput << "<?xml version=\"1.0\"?>" << endstr;
  67. // COLLADA element start
  68. mOutput << "<COLLADA xmlns=\"http://www.collada.org/2005/11/COLLADASchema\" version=\"1.4.1\">" << endstr;
  69. PushTag();
  70. WriteHeader();
  71. WriteGeometryLibrary();
  72. WriteSceneLibrary();
  73. // useless Collada bullshit at the end, just in case we haven't had enough indirections, yet.
  74. mOutput << startstr << "<scene>" << endstr;
  75. PushTag();
  76. mOutput << startstr << "<instance_visual_scene url=\"#myScene\" />" << endstr;
  77. PopTag();
  78. mOutput << startstr << "</scene>" << endstr;
  79. PopTag();
  80. mOutput << "</COLLADA>" << endstr;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Writes the asset header
  84. void ColladaExporter::WriteHeader()
  85. {
  86. // Dummy stuff. Nobody actually cares for it anyways
  87. mOutput << startstr << "<asset>" << endstr;
  88. PushTag();
  89. mOutput << startstr << "<contributor>" << endstr;
  90. PushTag();
  91. mOutput << startstr << "<author>Someone</author>" << endstr;
  92. mOutput << startstr << "<authoring_tool>Assimp Collada Exporter</authoring_tool>" << endstr;
  93. PopTag();
  94. mOutput << startstr << "</contributor>" << endstr;
  95. mOutput << startstr << "<unit meter=\"1.0\" name=\"meter\" />" << endstr;
  96. mOutput << startstr << "<up_axis>Y_UP</up_axis>" << endstr;
  97. PopTag();
  98. mOutput << startstr << "</asset>" << endstr;
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. // Writes the geometry library
  102. void ColladaExporter::WriteGeometryLibrary()
  103. {
  104. mOutput << startstr << "<library_geometries>" << endstr;
  105. PushTag();
  106. for( size_t a = 0; a < mScene->mNumMeshes; ++a)
  107. WriteGeometry( a);
  108. PopTag();
  109. mOutput << startstr << "</library_geometries>" << endstr;
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // Writes the given mesh
  113. void ColladaExporter::WriteGeometry( size_t pIndex)
  114. {
  115. const aiMesh* mesh = mScene->mMeshes[pIndex];
  116. std::string idstr = GetMeshId( pIndex);
  117. // opening tag
  118. mOutput << startstr << "<geometry id=\"" << idstr << "\" name=\"" << idstr << "_name\" >" << endstr;
  119. PushTag();
  120. mOutput << startstr << "<mesh>" << endstr;
  121. PushTag();
  122. // Positions
  123. WriteFloatArray( idstr + "-positions", FloatType_Vector, (float*) mesh->mVertices, mesh->mNumVertices);
  124. // Normals, if any
  125. if( mesh->HasNormals() )
  126. WriteFloatArray( idstr + "-normals", FloatType_Vector, (float*) mesh->mNormals, mesh->mNumVertices);
  127. // texture coords
  128. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  129. {
  130. if( mesh->HasTextureCoords( a) )
  131. {
  132. WriteFloatArray( idstr + "-tex" + boost::lexical_cast<std::string> (a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2,
  133. (float*) mesh->mTextureCoords[a], mesh->mNumVertices);
  134. }
  135. }
  136. // vertex colors
  137. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  138. {
  139. if( mesh->HasVertexColors( a) )
  140. WriteFloatArray( idstr + "-color" + boost::lexical_cast<std::string> (a), FloatType_Color, (float*) mesh->mColors[a], mesh->mNumVertices);
  141. }
  142. // assemble vertex structure
  143. mOutput << startstr << "<vertices id=\"" << idstr << "-vertices" << "\">" << endstr;
  144. PushTag();
  145. mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << idstr << "-positions\" />" << endstr;
  146. if( mesh->HasNormals() )
  147. mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << idstr << "-normals\" />" << endstr;
  148. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  149. {
  150. if( mesh->HasTextureCoords( a) )
  151. mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstr << "-tex" << a << "\" set=\"" << a << "\" />" << endstr;
  152. }
  153. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  154. {
  155. if( mesh->HasVertexColors( a) )
  156. mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstr << "-color" << a << "\" set=\"" << a << "\" />" << endstr;
  157. }
  158. PopTag();
  159. mOutput << startstr << "</vertices>" << endstr;
  160. // write face setup
  161. mOutput << startstr << "<polylist count=\"" << mesh->mNumFaces << "\" material=\"tellme\">" << endstr;
  162. PushTag();
  163. mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstr << "-vertices\" />" << endstr;
  164. mOutput << startstr << "<vcount>";
  165. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  166. mOutput << mesh->mFaces[a].mNumIndices << " ";
  167. mOutput << "</vcount>" << endstr;
  168. mOutput << startstr << "<p>";
  169. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  170. {
  171. const aiFace& face = mesh->mFaces[a];
  172. for( size_t b = 0; b < face.mNumIndices; ++b )
  173. mOutput << face.mIndices[b] << " ";
  174. }
  175. mOutput << "</p>" << endstr;
  176. PopTag();
  177. mOutput << startstr << "</polylist>" << endstr;
  178. // closing tags
  179. PopTag();
  180. mOutput << startstr << "</mesh>" << endstr;
  181. PopTag();
  182. mOutput << startstr << "</geometry>" << endstr;
  183. }
  184. // ------------------------------------------------------------------------------------------------
  185. // Writes a float array of the given type
  186. void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const float* pData, size_t pElementCount)
  187. {
  188. size_t floatsPerElement = 0;
  189. switch( pType )
  190. {
  191. case FloatType_Vector: floatsPerElement = 3; break;
  192. case FloatType_TexCoord2: floatsPerElement = 2; break;
  193. case FloatType_TexCoord3: floatsPerElement = 3; break;
  194. case FloatType_Color: floatsPerElement = 3; break;
  195. default:
  196. return;
  197. }
  198. std::string arrayId = pIdString + "-array";
  199. mOutput << startstr << "<source id=\"" << pIdString << "\" name=\"" << pIdString << "\">" << endstr;
  200. PushTag();
  201. // source array
  202. mOutput << startstr << "<float_array id=\"" << arrayId << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
  203. PushTag();
  204. if( pType == FloatType_TexCoord2 )
  205. {
  206. for( size_t a = 0; a < pElementCount; ++a )
  207. {
  208. mOutput << pData[a*3+0] << " ";
  209. mOutput << pData[a*3+1] << " ";
  210. }
  211. }
  212. else if( pType == FloatType_Color )
  213. {
  214. for( size_t a = 0; a < pElementCount; ++a )
  215. {
  216. mOutput << pData[a*4+0] << " ";
  217. mOutput << pData[a*4+1] << " ";
  218. mOutput << pData[a*4+2] << " ";
  219. }
  220. }
  221. else
  222. {
  223. for( size_t a = 0; a < pElementCount * floatsPerElement; ++a )
  224. mOutput << pData[a] << " ";
  225. }
  226. mOutput << "</float_array>" << endstr;
  227. PopTag();
  228. // the usual Collada bullshit. Let's bloat it even more!
  229. mOutput << startstr << "<technique_common>" << endstr;
  230. PushTag();
  231. mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
  232. PushTag();
  233. switch( pType )
  234. {
  235. case FloatType_Vector:
  236. mOutput << startstr << "<param name=\"X\" type=\"float\" />" << endstr;
  237. mOutput << startstr << "<param name=\"Y\" type=\"float\" />" << endstr;
  238. mOutput << startstr << "<param name=\"Z\" type=\"float\" />" << endstr;
  239. break;
  240. case FloatType_TexCoord2:
  241. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  242. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  243. break;
  244. case FloatType_TexCoord3:
  245. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  246. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  247. mOutput << startstr << "<param name=\"P\" type=\"float\" />" << endstr;
  248. break;
  249. case FloatType_Color:
  250. mOutput << startstr << "<param name=\"R\" type=\"float\" />" << endstr;
  251. mOutput << startstr << "<param name=\"G\" type=\"float\" />" << endstr;
  252. mOutput << startstr << "<param name=\"B\" type=\"float\" />" << endstr;
  253. break;
  254. }
  255. PopTag();
  256. mOutput << startstr << "</accessor>" << endstr;
  257. PopTag();
  258. mOutput << startstr << "</technique_common>" << endstr;
  259. PopTag();
  260. mOutput << startstr << "</source>" << endstr;
  261. }
  262. // ------------------------------------------------------------------------------------------------
  263. // Writes the scene library
  264. void ColladaExporter::WriteSceneLibrary()
  265. {
  266. mOutput << startstr << "<library_visual_scenes>" << endstr;
  267. PushTag();
  268. mOutput << startstr << "<visual_scene id=\"myScene\" name=\"myScene\">" << endstr;
  269. PushTag();
  270. // start recursive write at the root node
  271. WriteNode( mScene->mRootNode);
  272. PopTag();
  273. mOutput << startstr << "</visual_scene>" << endstr;
  274. PopTag();
  275. mOutput << startstr << "</library_visual_scenes>" << endstr;
  276. }
  277. // ------------------------------------------------------------------------------------------------
  278. // Recursively writes the given node
  279. void ColladaExporter::WriteNode( const aiNode* pNode)
  280. {
  281. mOutput << startstr << "<node id=\"" << pNode->mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr;
  282. PushTag();
  283. // write transformation - we can directly put the matrix there
  284. // TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards
  285. const aiMatrix4x4& mat = pNode->mTransformation;
  286. mOutput << startstr << "<matrix>";
  287. mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " ";
  288. mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " ";
  289. mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " ";
  290. mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4;
  291. mOutput << "</matrix>" << endstr;
  292. // instance every geometry
  293. for( size_t a = 0; a < pNode->mNumMeshes; ++a )
  294. {
  295. // const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]];
  296. mOutput << startstr << "<instance_geometry url=\"#" << GetMeshId( a) << "\">" << endstr;
  297. PushTag();
  298. PopTag();
  299. mOutput << startstr << "</instance_geometry>" << endstr;
  300. }
  301. // recurse into subnodes
  302. for( size_t a = 0; a < pNode->mNumChildren; ++a )
  303. WriteNode( pNode->mChildren[a]);
  304. PopTag();
  305. mOutput << startstr << "</node>" << endstr;
  306. }
  307. #endif