ColladaExporter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #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. WriteGeometryLibrary();
  75. WriteSceneLibrary();
  76. // useless Collada bullshit at the end, just in case we haven't had enough indirections, yet.
  77. mOutput << startstr << "<scene>" << endstr;
  78. PushTag();
  79. mOutput << startstr << "<instance_visual_scene url=\"#myScene\" />" << endstr;
  80. PopTag();
  81. mOutput << startstr << "</scene>" << endstr;
  82. PopTag();
  83. mOutput << "</COLLADA>" << endstr;
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. // Writes the asset header
  87. void ColladaExporter::WriteHeader()
  88. {
  89. // Dummy stuff. Nobody actually cares for it anyways
  90. mOutput << startstr << "<asset>" << endstr;
  91. PushTag();
  92. mOutput << startstr << "<contributor>" << endstr;
  93. PushTag();
  94. mOutput << startstr << "<author>Someone</author>" << endstr;
  95. mOutput << startstr << "<authoring_tool>Assimp Collada Exporter</authoring_tool>" << endstr;
  96. PopTag();
  97. mOutput << startstr << "</contributor>" << endstr;
  98. mOutput << startstr << "<unit meter=\"1.0\" name=\"meter\" />" << endstr;
  99. mOutput << startstr << "<up_axis>Y_UP</up_axis>" << endstr;
  100. PopTag();
  101. mOutput << startstr << "</asset>" << endstr;
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. // Writes the geometry library
  105. void ColladaExporter::WriteGeometryLibrary()
  106. {
  107. mOutput << startstr << "<library_geometries>" << endstr;
  108. PushTag();
  109. for( size_t a = 0; a < mScene->mNumMeshes; ++a)
  110. WriteGeometry( a);
  111. PopTag();
  112. mOutput << startstr << "</library_geometries>" << endstr;
  113. }
  114. // ------------------------------------------------------------------------------------------------
  115. // Writes the given mesh
  116. void ColladaExporter::WriteGeometry( size_t pIndex)
  117. {
  118. const aiMesh* mesh = mScene->mMeshes[pIndex];
  119. std::string idstr = GetMeshId( pIndex);
  120. // opening tag
  121. mOutput << startstr << "<geometry id=\"" << idstr << "\" name=\"" << idstr << "_name\" >" << endstr;
  122. PushTag();
  123. mOutput << startstr << "<mesh>" << endstr;
  124. PushTag();
  125. // Positions
  126. WriteFloatArray( idstr + "-positions", FloatType_Vector, (float*) mesh->mVertices, mesh->mNumVertices);
  127. // Normals, if any
  128. if( mesh->HasNormals() )
  129. WriteFloatArray( idstr + "-normals", FloatType_Vector, (float*) mesh->mNormals, mesh->mNumVertices);
  130. // texture coords
  131. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  132. {
  133. if( mesh->HasTextureCoords( a) )
  134. {
  135. WriteFloatArray( idstr + "-tex" + boost::lexical_cast<std::string> (a), mesh->mNumUVComponents[a] == 3 ? FloatType_TexCoord3 : FloatType_TexCoord2,
  136. (float*) mesh->mTextureCoords[a], mesh->mNumVertices);
  137. }
  138. }
  139. // vertex colors
  140. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a)
  141. {
  142. if( mesh->HasVertexColors( a) )
  143. WriteFloatArray( idstr + "-color" + boost::lexical_cast<std::string> (a), FloatType_Color, (float*) mesh->mColors[a], mesh->mNumVertices);
  144. }
  145. // assemble vertex structure
  146. mOutput << startstr << "<vertices id=\"" << idstr << "-vertices" << "\">" << endstr;
  147. PushTag();
  148. mOutput << startstr << "<input semantic=\"POSITION\" source=\"#" << idstr << "-positions\" />" << endstr;
  149. if( mesh->HasNormals() )
  150. mOutput << startstr << "<input semantic=\"NORMAL\" source=\"#" << idstr << "-normals\" />" << endstr;
  151. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++a )
  152. {
  153. if( mesh->HasTextureCoords( a) )
  154. mOutput << startstr << "<input semantic=\"TEXCOORD\" source=\"#" << idstr << "-tex" << a << "\" set=\"" << a << "\" />" << endstr;
  155. }
  156. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; ++a )
  157. {
  158. if( mesh->HasVertexColors( a) )
  159. mOutput << startstr << "<input semantic=\"COLOR\" source=\"#" << idstr << "-color" << a << "\" set=\"" << a << "\" />" << endstr;
  160. }
  161. PopTag();
  162. mOutput << startstr << "</vertices>" << endstr;
  163. // write face setup
  164. mOutput << startstr << "<polylist count=\"" << mesh->mNumFaces << "\" material=\"tellme\">" << endstr;
  165. PushTag();
  166. mOutput << startstr << "<input offset=\"0\" semantic=\"VERTEX\" source=\"#" << idstr << "-vertices\" />" << endstr;
  167. mOutput << startstr << "<vcount>";
  168. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  169. mOutput << mesh->mFaces[a].mNumIndices << " ";
  170. mOutput << "</vcount>" << endstr;
  171. mOutput << startstr << "<p>";
  172. for( size_t a = 0; a < mesh->mNumFaces; ++a )
  173. {
  174. const aiFace& face = mesh->mFaces[a];
  175. for( size_t b = 0; b < face.mNumIndices; ++b )
  176. mOutput << face.mIndices[b] << " ";
  177. }
  178. mOutput << "</p>" << endstr;
  179. PopTag();
  180. mOutput << startstr << "</polylist>" << endstr;
  181. // closing tags
  182. PopTag();
  183. mOutput << startstr << "</mesh>" << endstr;
  184. PopTag();
  185. mOutput << startstr << "</geometry>" << endstr;
  186. }
  187. // ------------------------------------------------------------------------------------------------
  188. // Writes a float array of the given type
  189. void ColladaExporter::WriteFloatArray( const std::string& pIdString, FloatDataType pType, const float* pData, size_t pElementCount)
  190. {
  191. size_t floatsPerElement = 0;
  192. switch( pType )
  193. {
  194. case FloatType_Vector: floatsPerElement = 3; break;
  195. case FloatType_TexCoord2: floatsPerElement = 2; break;
  196. case FloatType_TexCoord3: floatsPerElement = 3; break;
  197. case FloatType_Color: floatsPerElement = 3; break;
  198. default:
  199. return;
  200. }
  201. std::string arrayId = pIdString + "-array";
  202. mOutput << startstr << "<source id=\"" << pIdString << "\" name=\"" << pIdString << "\">" << endstr;
  203. PushTag();
  204. // source array
  205. mOutput << startstr << "<float_array id=\"" << arrayId << "\" count=\"" << pElementCount * floatsPerElement << "\"> ";
  206. PushTag();
  207. if( pType == FloatType_TexCoord2 )
  208. {
  209. for( size_t a = 0; a < pElementCount; ++a )
  210. {
  211. mOutput << pData[a*3+0] << " ";
  212. mOutput << pData[a*3+1] << " ";
  213. }
  214. }
  215. else if( pType == FloatType_Color )
  216. {
  217. for( size_t a = 0; a < pElementCount; ++a )
  218. {
  219. mOutput << pData[a*4+0] << " ";
  220. mOutput << pData[a*4+1] << " ";
  221. mOutput << pData[a*4+2] << " ";
  222. }
  223. }
  224. else
  225. {
  226. for( size_t a = 0; a < pElementCount * floatsPerElement; ++a )
  227. mOutput << pData[a] << " ";
  228. }
  229. mOutput << "</float_array>" << endstr;
  230. PopTag();
  231. // the usual Collada bullshit. Let's bloat it even more!
  232. mOutput << startstr << "<technique_common>" << endstr;
  233. PushTag();
  234. mOutput << startstr << "<accessor count=\"" << pElementCount << "\" offset=\"0\" source=\"#" << arrayId << "\" stride=\"" << floatsPerElement << "\">" << endstr;
  235. PushTag();
  236. switch( pType )
  237. {
  238. case FloatType_Vector:
  239. mOutput << startstr << "<param name=\"X\" type=\"float\" />" << endstr;
  240. mOutput << startstr << "<param name=\"Y\" type=\"float\" />" << endstr;
  241. mOutput << startstr << "<param name=\"Z\" type=\"float\" />" << endstr;
  242. break;
  243. case FloatType_TexCoord2:
  244. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  245. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  246. break;
  247. case FloatType_TexCoord3:
  248. mOutput << startstr << "<param name=\"S\" type=\"float\" />" << endstr;
  249. mOutput << startstr << "<param name=\"T\" type=\"float\" />" << endstr;
  250. mOutput << startstr << "<param name=\"P\" type=\"float\" />" << endstr;
  251. break;
  252. case FloatType_Color:
  253. mOutput << startstr << "<param name=\"R\" type=\"float\" />" << endstr;
  254. mOutput << startstr << "<param name=\"G\" type=\"float\" />" << endstr;
  255. mOutput << startstr << "<param name=\"B\" type=\"float\" />" << endstr;
  256. break;
  257. }
  258. PopTag();
  259. mOutput << startstr << "</accessor>" << endstr;
  260. PopTag();
  261. mOutput << startstr << "</technique_common>" << endstr;
  262. PopTag();
  263. mOutput << startstr << "</source>" << endstr;
  264. }
  265. // ------------------------------------------------------------------------------------------------
  266. // Writes the scene library
  267. void ColladaExporter::WriteSceneLibrary()
  268. {
  269. mOutput << startstr << "<library_visual_scenes>" << endstr;
  270. PushTag();
  271. mOutput << startstr << "<visual_scene id=\"myScene\" name=\"myScene\">" << endstr;
  272. PushTag();
  273. // start recursive write at the root node
  274. WriteNode( mScene->mRootNode);
  275. PopTag();
  276. mOutput << startstr << "</visual_scene>" << endstr;
  277. PopTag();
  278. mOutput << startstr << "</library_visual_scenes>" << endstr;
  279. }
  280. // ------------------------------------------------------------------------------------------------
  281. // Recursively writes the given node
  282. void ColladaExporter::WriteNode( const aiNode* pNode)
  283. {
  284. mOutput << startstr << "<node id=\"" << pNode->mName.data << "\" name=\"" << pNode->mName.data << "\">" << endstr;
  285. PushTag();
  286. // write transformation - we can directly put the matrix there
  287. // TODO: (thom) decompose into scale - rot - quad to allow adressing it by animations afterwards
  288. const aiMatrix4x4& mat = pNode->mTransformation;
  289. mOutput << startstr << "<matrix>";
  290. mOutput << mat.a1 << " " << mat.a2 << " " << mat.a3 << " " << mat.a4 << " ";
  291. mOutput << mat.b1 << " " << mat.b2 << " " << mat.b3 << " " << mat.b4 << " ";
  292. mOutput << mat.c1 << " " << mat.c2 << " " << mat.c3 << " " << mat.c4 << " ";
  293. mOutput << mat.d1 << " " << mat.d2 << " " << mat.d3 << " " << mat.d4;
  294. mOutput << "</matrix>" << endstr;
  295. // instance every geometry
  296. for( size_t a = 0; a < pNode->mNumMeshes; ++a )
  297. {
  298. // const aiMesh* mesh = mScene->mMeshes[pNode->mMeshes[a]];
  299. mOutput << startstr << "<instance_geometry url=\"#" << GetMeshId( a) << "\">" << endstr;
  300. PushTag();
  301. PopTag();
  302. mOutput << startstr << "</instance_geometry>" << endstr;
  303. }
  304. // recurse into subnodes
  305. for( size_t a = 0; a < pNode->mNumChildren; ++a )
  306. WriteNode( pNode->mChildren[a]);
  307. PopTag();
  308. mOutput << startstr << "</node>" << endstr;
  309. }
  310. #endif
  311. #endif