ColladaExporter.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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 ColladaExporter.h
  34. * Declares the exporter class to write a scene to a Collada file
  35. */
  36. #ifndef AI_COLLADAEXPORTER_H_INC
  37. #define AI_COLLADAEXPORTER_H_INC
  38. #include <assimp/ai_assert.h>
  39. #include <assimp/material.h>
  40. #include <array>
  41. #include <map>
  42. #include <sstream>
  43. #include <unordered_set>
  44. #include <vector>
  45. struct aiScene;
  46. struct aiNode;
  47. struct aiLight;
  48. struct aiBone;
  49. namespace Assimp {
  50. class IOSystem;
  51. /// Helper class to export a given scene to a Collada file. Just for my personal
  52. /// comfort when implementing it.
  53. class ColladaExporter {
  54. public:
  55. /// Constructor for a specific scene to export
  56. ColladaExporter(const aiScene *pScene, IOSystem *pIOSystem, const std::string &path, const std::string &file);
  57. /// Destructor
  58. virtual ~ColladaExporter() = default;
  59. protected:
  60. /// Starts writing the contents
  61. void WriteFile();
  62. /// Writes the asset header
  63. void WriteHeader();
  64. /// Writes the embedded textures
  65. void WriteTextures();
  66. /// Writes the material setup
  67. void WriteMaterials();
  68. /// Writes the cameras library
  69. void WriteCamerasLibrary();
  70. // Write a camera entry
  71. void WriteCamera(size_t pIndex);
  72. /// Writes the cameras library
  73. void WriteLightsLibrary();
  74. // Write a camera entry
  75. void WriteLight(size_t pIndex);
  76. void WritePointLight(const aiLight *const light);
  77. void WriteDirectionalLight(const aiLight *const light);
  78. void WriteSpotLight(const aiLight *const light);
  79. void WriteAmbientLight(const aiLight *const light);
  80. /// Writes the controller library
  81. void WriteControllerLibrary();
  82. /// Writes a skin controller of the given mesh
  83. void WriteController(size_t pIndex);
  84. /// Writes the geometry library
  85. void WriteGeometryLibrary();
  86. /// Writes the given mesh
  87. void WriteGeometry(size_t pIndex);
  88. //enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color, FloatType_Mat4x4, FloatType_Weight };
  89. // customized to add animation related type
  90. enum FloatDataType { FloatType_Vector,
  91. FloatType_TexCoord2,
  92. FloatType_TexCoord3,
  93. FloatType_Color,
  94. FloatType_Mat4x4,
  95. FloatType_Weight,
  96. FloatType_Time };
  97. /// Writes a float array of the given type
  98. void WriteFloatArray(const std::string &pIdString, FloatDataType pType, const ai_real *pData, size_t pElementCount);
  99. /// Writes the scene library
  100. void WriteSceneLibrary();
  101. // customized, Writes the animation library
  102. void WriteAnimationsLibrary();
  103. void WriteAnimationLibrary(size_t pIndex);
  104. std::string mFoundSkeletonRootNodeID = "skeleton_root"; // will be replaced by found node id in the WriteNode call.
  105. /// Recursively writes the given node
  106. void WriteNode(const aiNode *pNode);
  107. /// Enters a new xml element, which increases the indentation
  108. void PushTag() { startstr.append(" "); }
  109. /// Leaves an element, decreasing the indentation
  110. void PopTag() {
  111. ai_assert(startstr.length() > 1);
  112. startstr.erase(startstr.length() - 2);
  113. }
  114. void CreateNodeIds(const aiNode *node);
  115. /// Get or Create a unique Node ID string for the given Node
  116. std::string GetNodeUniqueId(const aiNode *node);
  117. std::string GetNodeName(const aiNode *node);
  118. std::string GetBoneUniqueId(const aiBone *bone);
  119. enum class AiObjectType {
  120. Mesh,
  121. Material,
  122. Animation,
  123. Light,
  124. Camera,
  125. Count,
  126. };
  127. /// Get or Create a unique ID string for the given scene object index
  128. std::string GetObjectUniqueId(AiObjectType type, size_t pIndex);
  129. /// Get or Create a name string for the given scene object index
  130. std::string GetObjectName(AiObjectType type, size_t pIndex);
  131. typedef std::map<size_t, std::string> IndexIdMap;
  132. typedef std::pair<std::string, std::string> NameIdPair;
  133. NameIdPair AddObjectIndexToMaps(AiObjectType type, size_t pIndex);
  134. // Helpers
  135. inline IndexIdMap &GetObjectIdMap(AiObjectType type) { return mObjectIdMap[static_cast<size_t>(type)]; }
  136. inline IndexIdMap &GetObjectNameMap(AiObjectType type) { return mObjectNameMap[static_cast<size_t>(type)]; }
  137. private:
  138. std::unordered_set<std::string> mUniqueIds; // Cache of used unique ids
  139. std::map<const void *, std::string> mNodeIdMap; // Cache of encoded node and bone ids
  140. std::array<IndexIdMap, static_cast<size_t>(AiObjectType::Count)> mObjectIdMap; // Cache of encoded unique IDs
  141. std::array<IndexIdMap, static_cast<size_t>(AiObjectType::Count)> mObjectNameMap; // Cache of encoded names
  142. public:
  143. /// Stringstream to write all output into
  144. std::stringstream mOutput;
  145. /// The IOSystem for output
  146. IOSystem *mIOSystem;
  147. /// Path of the directory where the scene will be exported
  148. const std::string mPath;
  149. /// Name of the file (without extension) where the scene will be exported
  150. const std::string mFile;
  151. /// The scene to be written
  152. const aiScene *const mScene;
  153. std::string mSceneId;
  154. bool mAdd_root_node = false;
  155. /// current line start string, contains the current indentation for simple stream insertion
  156. std::string startstr;
  157. /// current line end string for simple stream insertion
  158. const std::string endstr;
  159. // pair of color and texture - texture precedences color
  160. struct Surface {
  161. bool exist;
  162. aiColor4D color;
  163. std::string texture;
  164. size_t channel;
  165. Surface() {
  166. exist = false;
  167. channel = 0;
  168. }
  169. };
  170. struct Property {
  171. bool exist;
  172. ai_real value;
  173. Property() :
  174. exist(false),
  175. value(0.0) {}
  176. };
  177. // summarize a material in an convenient way.
  178. struct Material {
  179. std::string id;
  180. std::string name;
  181. std::string shading_model;
  182. Surface ambient, diffuse, specular, emissive, reflective, transparent, normal;
  183. Property shininess, transparency, index_refraction;
  184. Material() = default;
  185. };
  186. std::map<unsigned int, std::string> textures;
  187. public:
  188. /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions
  189. /// Reads a single surface entry from the given material keys
  190. bool ReadMaterialSurface(Surface &poSurface, const aiMaterial &pSrcMat, aiTextureType pTexture, const char *pKey, size_t pType, size_t pIndex);
  191. /// Writes an image entry for the given surface
  192. void WriteImageEntry(const Surface &pSurface, const std::string &imageId);
  193. /// Writes the two parameters necessary for referencing a texture in an effect entry
  194. void WriteTextureParamEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &materialId);
  195. /// Writes a color-or-texture entry into an effect definition
  196. void WriteTextureColorEntry(const Surface &pSurface, const std::string &pTypeName, const std::string &imageId);
  197. /// Writes a scalar property
  198. void WriteFloatEntry(const Property &pProperty, const std::string &pTypeName);
  199. };
  200. } // namespace Assimp
  201. #endif // !! AI_COLLADAEXPORTER_H_INC