ObjExporter.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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. /** @file ObjExporter.h
  34. * Declares the exporter class to write a scene to a Collada file
  35. */
  36. #ifndef AI_OBJEXPORTER_H_INC
  37. #define AI_OBJEXPORTER_H_INC
  38. #include <assimp/types.h>
  39. #include <sstream>
  40. #include <vector>
  41. #include <map>
  42. struct aiScene;
  43. struct aiNode;
  44. struct aiMesh;
  45. namespace Assimp {
  46. // ------------------------------------------------------------------------------------------------
  47. /** Helper class to export a given scene to an OBJ file. */
  48. // ------------------------------------------------------------------------------------------------
  49. class ObjExporter {
  50. public:
  51. /// Constructor for a specific scene to export
  52. ObjExporter(const char* filename, const aiScene* pScene, bool noMtl=false);
  53. ~ObjExporter();
  54. std::string GetMaterialLibName();
  55. std::string GetMaterialLibFileName();
  56. /// public string-streams to write all output into
  57. std::ostringstream mOutput, mOutputMat;
  58. private:
  59. // intermediate data structures
  60. struct FaceVertex {
  61. FaceVertex()
  62. : vp()
  63. , vn()
  64. , vt() {
  65. // empty
  66. }
  67. // one-based, 0 means: 'does not exist'
  68. unsigned int vp, vn, vt;
  69. };
  70. struct Face {
  71. char kind;
  72. std::vector<FaceVertex> indices;
  73. };
  74. struct MeshInstance {
  75. std::string name, matname;
  76. std::vector<Face> faces;
  77. };
  78. void WriteHeader(std::ostringstream& out);
  79. void WriteMaterialFile();
  80. void WriteGeometryFile(bool noMtl=false);
  81. std::string GetMaterialName(unsigned int index);
  82. void AddMesh(const aiString& name, const aiMesh* m, const aiMatrix4x4& mat);
  83. void AddNode(const aiNode* nd, const aiMatrix4x4& mParent);
  84. private:
  85. std::string filename;
  86. const aiScene* const pScene;
  87. struct vertexData {
  88. aiVector3D vp;
  89. aiColor3D vc; // OBJ does not support 4D color
  90. };
  91. std::vector<aiVector3D> vn, vt;
  92. std::vector<aiColor4D> vc;
  93. std::vector<vertexData> vp;
  94. bool useVc;
  95. struct vertexDataCompare {
  96. bool operator() ( const vertexData& a, const vertexData& b ) const {
  97. // position
  98. if (a.vp.x < b.vp.x) return true;
  99. if (a.vp.x > b.vp.x) return false;
  100. if (a.vp.y < b.vp.y) return true;
  101. if (a.vp.y > b.vp.y) return false;
  102. if (a.vp.z < b.vp.z) return true;
  103. if (a.vp.z > b.vp.z) return false;
  104. // color
  105. if (a.vc.r < b.vc.r) return true;
  106. if (a.vc.r > b.vc.r) return false;
  107. if (a.vc.g < b.vc.g) return true;
  108. if (a.vc.g > b.vc.g) return false;
  109. if (a.vc.b < b.vc.b) return true;
  110. if (a.vc.b > b.vc.b) return false;
  111. return false;
  112. }
  113. };
  114. struct aiVectorCompare {
  115. bool operator() (const aiVector3D& a, const aiVector3D& b) const {
  116. if(a.x < b.x) return true;
  117. if(a.x > b.x) return false;
  118. if(a.y < b.y) return true;
  119. if(a.y > b.y) return false;
  120. if(a.z < b.z) return true;
  121. return false;
  122. }
  123. };
  124. template <class T, class Compare = std::less<T>>
  125. class indexMap {
  126. int mNextIndex;
  127. typedef std::map<T, int, Compare> dataType;
  128. dataType vecMap;
  129. public:
  130. indexMap()
  131. : mNextIndex(1) {
  132. // empty
  133. }
  134. int getIndex(const T& key) {
  135. typename dataType::iterator vertIt = vecMap.find(key);
  136. // vertex already exists, so reference it
  137. if(vertIt != vecMap.end()){
  138. return vertIt->second;
  139. }
  140. return vecMap[key] = mNextIndex++;
  141. };
  142. void getKeys( std::vector<T>& keys ) {
  143. keys.resize(vecMap.size());
  144. for(typename dataType::iterator it = vecMap.begin(); it != vecMap.end(); ++it){
  145. keys[it->second-1] = it->first;
  146. }
  147. };
  148. };
  149. indexMap<aiVector3D, aiVectorCompare> mVnMap, mVtMap;
  150. indexMap<vertexData, vertexDataCompare> mVpMap;
  151. std::vector<MeshInstance> mMeshes;
  152. // this endl() doesn't flush() the stream
  153. const std::string endl;
  154. };
  155. }
  156. #endif