ObjExporter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_OBJ_EXPORTER
  36. #include "ObjExporter.h"
  37. #include "../include/assimp/version.h"
  38. using namespace Assimp;
  39. namespace Assimp {
  40. // ------------------------------------------------------------------------------------------------
  41. // Worker function for exporting a scene to Wavefront OBJ. Prototyped and registered in Exporter.cpp
  42. void ExportSceneObj(const char* pFile,IOSystem* pIOSystem, const aiScene* pScene)
  43. {
  44. // invoke the exporter
  45. ObjExporter exporter(pFile, pScene);
  46. // we're still here - export successfully completed. Write both the main OBJ file and the material script
  47. {
  48. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(pFile,"wt"));
  49. outfile->Write( exporter.mOutput.str().c_str(), static_cast<size_t>(exporter.mOutput.tellp()),1);
  50. }
  51. {
  52. boost::scoped_ptr<IOStream> outfile (pIOSystem->Open(exporter.GetMaterialLibFileName(),"wt"));
  53. outfile->Write( exporter.mOutputMat.str().c_str(), static_cast<size_t>(exporter.mOutputMat.tellp()),1);
  54. }
  55. }
  56. } // end of namespace Assimp
  57. // ------------------------------------------------------------------------------------------------
  58. ObjExporter :: ObjExporter(const char* _filename, const aiScene* pScene)
  59. : filename(_filename)
  60. , pScene(pScene)
  61. , endl("\n")
  62. {
  63. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  64. const std::locale& l = std::locale("C");
  65. mOutput.imbue(l);
  66. mOutputMat.imbue(l);
  67. WriteGeometryFile();
  68. WriteMaterialFile();
  69. }
  70. // ------------------------------------------------------------------------------------------------
  71. std::string ObjExporter :: GetMaterialLibName()
  72. {
  73. // within the Obj file, we use just the relative file name with the path stripped
  74. const std::string& s = GetMaterialLibFileName();
  75. std::string::size_type il = s.find_last_of("/\\");
  76. if (il != std::string::npos) {
  77. return s.substr(il + 1);
  78. }
  79. return s;
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. std::string ObjExporter :: GetMaterialLibFileName()
  83. {
  84. return filename + ".mtl";
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. void ObjExporter :: WriteHeader(std::ostringstream& out)
  88. {
  89. out << "# File produced by Open Asset Import Library (http://www.assimp.sf.net)" << endl;
  90. out << "# (assimp v" << aiGetVersionMajor() << '.' << aiGetVersionMinor() << '.' << aiGetVersionRevision() << ")" << endl << endl;
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. std::string ObjExporter :: GetMaterialName(unsigned int index)
  94. {
  95. const aiMaterial* const mat = pScene->mMaterials[index];
  96. aiString s;
  97. if(AI_SUCCESS == mat->Get(AI_MATKEY_NAME,s)) {
  98. return std::string(s.data,s.length);
  99. }
  100. char number[ sizeof(unsigned int) * 3 + 1 ];
  101. ASSIMP_itoa10(number,index);
  102. return "$Material_" + std::string(number);
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. void ObjExporter :: WriteMaterialFile()
  106. {
  107. WriteHeader(mOutputMat);
  108. for(unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
  109. const aiMaterial* const mat = pScene->mMaterials[i];
  110. int illum = 1;
  111. mOutputMat << "newmtl " << GetMaterialName(i) << endl;
  112. aiColor4D c;
  113. if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_DIFFUSE,c)) {
  114. mOutputMat << "kd " << c.r << " " << c.g << " " << c.b << endl;
  115. }
  116. if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_AMBIENT,c)) {
  117. mOutputMat << "ka " << c.r << " " << c.g << " " << c.b << endl;
  118. }
  119. if(AI_SUCCESS == mat->Get(AI_MATKEY_COLOR_SPECULAR,c)) {
  120. mOutputMat << "ks " << c.r << " " << c.g << " " << c.b << endl;
  121. }
  122. float o;
  123. if(AI_SUCCESS == mat->Get(AI_MATKEY_OPACITY,o)) {
  124. mOutputMat << "d " << o << endl;
  125. }
  126. if(AI_SUCCESS == mat->Get(AI_MATKEY_SHININESS,o) && o) {
  127. mOutputMat << "Ns " << o << endl;
  128. illum = 2;
  129. }
  130. mOutputMat << "illum " << illum << endl;
  131. aiString s;
  132. if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_DIFFUSE(0),s)) {
  133. mOutputMat << "map_kd " << s.data << endl;
  134. }
  135. if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_AMBIENT(0),s)) {
  136. mOutputMat << "map_ka " << s.data << endl;
  137. }
  138. if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_SPECULAR(0),s)) {
  139. mOutputMat << "map_ks " << s.data << endl;
  140. }
  141. if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_SHININESS(0),s)) {
  142. mOutputMat << "map_ns " << s.data << endl;
  143. }
  144. if(AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_HEIGHT(0),s) || AI_SUCCESS == mat->Get(AI_MATKEY_TEXTURE_NORMALS(0),s)) {
  145. // implementations seem to vary here, so write both variants
  146. mOutputMat << "bump " << s.data << endl;
  147. mOutputMat << "map_bump " << s.data << endl;
  148. }
  149. mOutputMat << endl;
  150. }
  151. }
  152. // ------------------------------------------------------------------------------------------------
  153. void ObjExporter :: WriteGeometryFile()
  154. {
  155. WriteHeader(mOutput);
  156. mOutput << "mtllib " << GetMaterialLibName() << endl << endl;
  157. // collect mesh geometry
  158. aiMatrix4x4 mBase;
  159. AddNode(pScene->mRootNode,mBase);
  160. // write vertex positions
  161. mOutput << "# " << vp.size() << " vertex positions" << endl;
  162. BOOST_FOREACH(const aiVector3D& v, vp) {
  163. mOutput << "v " << v.x << " " << v.y << " " << v.z << endl;
  164. }
  165. mOutput << endl;
  166. // write uv coordinates
  167. mOutput << "# " << vt.size() << " UV coordinates" << endl;
  168. BOOST_FOREACH(const aiVector3D& v, vt) {
  169. mOutput << "vt " << v.x << " " << v.y << " " << v.z << endl;
  170. }
  171. mOutput << endl;
  172. // write vertex normals
  173. mOutput << "# " << vn.size() << " vertex normals" << endl;
  174. BOOST_FOREACH(const aiVector3D& v, vn) {
  175. mOutput << "vn " << v.x << " " << v.y << " " << v.z << endl;
  176. }
  177. mOutput << endl;
  178. // now write all mesh instances
  179. BOOST_FOREACH(const MeshInstance& m, meshes) {
  180. mOutput << "# Mesh \'" << m.name << "\' with " << m.faces.size() << " faces" << endl;
  181. mOutput << "g " << m.name << endl;
  182. mOutput << "usemtl " << m.matname << endl;
  183. BOOST_FOREACH(const Face& f, m.faces) {
  184. mOutput << f.kind << ' ';
  185. BOOST_FOREACH(const FaceVertex& fv, f.indices) {
  186. mOutput << ' ' << fv.vp;
  187. if (f.kind != 'p') {
  188. if (fv.vt || f.kind == 'f') {
  189. mOutput << '/';
  190. }
  191. if (fv.vt) {
  192. mOutput << fv.vt;
  193. }
  194. if (f.kind == 'f') {
  195. mOutput << '/';
  196. if (fv.vn) {
  197. mOutput << fv.vn;
  198. }
  199. }
  200. }
  201. }
  202. mOutput << endl;
  203. }
  204. mOutput << endl;
  205. }
  206. }
  207. // ------------------------------------------------------------------------------------------------
  208. void ObjExporter :: AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat)
  209. {
  210. meshes.push_back(MeshInstance());
  211. MeshInstance& mesh = meshes.back();
  212. mesh.name = std::string(name.data,name.length) + (m->mName.length ? "_"+std::string(m->mName.data,m->mName.length) : "");
  213. mesh.matname = GetMaterialName(m->mMaterialIndex);
  214. mesh.faces.resize(m->mNumFaces);
  215. for(unsigned int i = 0; i < m->mNumFaces; ++i) {
  216. const aiFace& f = m->mFaces[i];
  217. Face& face = mesh.faces[i];
  218. switch (f.mNumIndices) {
  219. case 1:
  220. face.kind = 'p';
  221. break;
  222. case 2:
  223. face.kind = 'l';
  224. break;
  225. default:
  226. face.kind = 'f';
  227. }
  228. face.indices.resize(f.mNumIndices);
  229. for(unsigned int a = 0; a < f.mNumIndices; ++a) {
  230. const unsigned int idx = f.mIndices[a];
  231. // XXX need a way to check if this is an unique vertex or if we had it already,
  232. // in which case we should instead reference the previous occurrence.
  233. ai_assert(m->mVertices);
  234. vp.push_back( mat * m->mVertices[idx] );
  235. face.indices[a].vp = vp.size();
  236. if (m->mNormals) {
  237. vn.push_back( m->mNormals[idx] );
  238. }
  239. face.indices[a].vn = vn.size();
  240. if (m->mTextureCoords[0]) {
  241. vt.push_back( m->mTextureCoords[0][idx] );
  242. }
  243. face.indices[a].vt = vt.size();
  244. }
  245. }
  246. }
  247. // ------------------------------------------------------------------------------------------------
  248. void ObjExporter :: AddNode(const aiNode* nd, const aiMatrix4x4& mParent)
  249. {
  250. const aiMatrix4x4& mAbs = mParent * nd->mTransformation;
  251. for(unsigned int i = 0; i < nd->mNumMeshes; ++i) {
  252. AddMesh(nd->mName, pScene->mMeshes[nd->mMeshes[i]],mAbs);
  253. }
  254. for(unsigned int i = 0; i < nd->mNumChildren; ++i) {
  255. AddNode(nd->mChildren[i],mAbs);
  256. }
  257. }
  258. #endif
  259. #endif