ColladaExporter.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2017, 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 <assimp/mesh.h>
  41. #include <assimp/light.h>
  42. #include <assimp/Exporter.hpp>
  43. #include <sstream>
  44. #include <vector>
  45. #include <map>
  46. #include "StringUtils.h"
  47. struct aiScene;
  48. struct aiNode;
  49. namespace Assimp
  50. {
  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. {
  55. public:
  56. /// Constructor for a specific scene to export
  57. ColladaExporter( const aiScene* pScene, IOSystem* pIOSystem, const std::string& path, const std::string& file);
  58. /// Destructor
  59. virtual ~ColladaExporter();
  60. protected:
  61. /// Starts writing the contents
  62. void WriteFile();
  63. /// Writes the asset header
  64. void WriteHeader();
  65. /// Writes the embedded textures
  66. void WriteTextures();
  67. /// Writes the material setup
  68. void WriteMaterials();
  69. /// Writes the cameras library
  70. void WriteCamerasLibrary();
  71. // Write a camera entry
  72. void WriteCamera(size_t pIndex);
  73. /// Writes the cameras library
  74. void WriteLightsLibrary();
  75. // Write a camera entry
  76. void WriteLight(size_t pIndex);
  77. void WritePointLight(const aiLight *const light);
  78. void WriteDirectionalLight(const aiLight *const light);
  79. void WriteSpotLight(const aiLight *const light);
  80. void WriteAmbienttLight(const aiLight *const light);
  81. /// Writes the controller library
  82. void WriteControllerLibrary();
  83. /// Writes a skin controller of the given mesh
  84. void WriteController( size_t pIndex);
  85. /// Writes the geometry library
  86. void WriteGeometryLibrary();
  87. /// Writes the given mesh
  88. void WriteGeometry( size_t pIndex);
  89. //enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color, FloatType_Mat4x4, FloatType_Weight };
  90. // customized to add animation related type
  91. enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color, FloatType_Mat4x4, FloatType_Weight, FloatType_Time };
  92. /// Writes a float array of the given type
  93. void WriteFloatArray( const std::string& pIdString, FloatDataType pType, const ai_real* pData, size_t pElementCount);
  94. /// Writes the scene library
  95. void WriteSceneLibrary();
  96. // customized, Writes the animation library
  97. void WriteAnimationsLibrary();
  98. void WriteAnimationLibrary( size_t pIndex);
  99. std::string mFoundSkeletonRootNodeID = "skeleton_root"; // will be replaced by found node id in the WriteNode call.
  100. /// Recursively writes the given node
  101. void WriteNode( const aiScene* scene, aiNode* pNode);
  102. /// Enters a new xml element, which increases the indentation
  103. void PushTag() { startstr.append( " "); }
  104. /// Leaves an element, decreasing the indentation
  105. void PopTag() {
  106. ai_assert( startstr.length() > 1);
  107. startstr.erase( startstr.length() - 2);
  108. }
  109. /// Creates a mesh ID for the given mesh
  110. std::string GetMeshId( size_t pIndex) const {
  111. return std::string( "meshId" ) + to_string(pIndex);
  112. }
  113. public:
  114. /// Stringstream to write all output into
  115. std::stringstream mOutput;
  116. protected:
  117. /// The IOSystem for output
  118. IOSystem* mIOSystem;
  119. /// Path of the directory where the scene will be exported
  120. const std::string mPath;
  121. /// Name of the file (without extension) where the scene will be exported
  122. const std::string mFile;
  123. /// The scene to be written
  124. const aiScene* mScene;
  125. bool mSceneOwned;
  126. /// current line start string, contains the current indentation for simple stream insertion
  127. std::string startstr;
  128. /// current line end string for simple stream insertion
  129. std::string endstr;
  130. // pair of color and texture - texture precedences color
  131. struct Surface
  132. {
  133. bool exist;
  134. aiColor4D color;
  135. std::string texture;
  136. size_t channel;
  137. Surface() { exist = false; channel = 0; }
  138. };
  139. struct Property
  140. {
  141. bool exist;
  142. ai_real value;
  143. Property()
  144. : exist(false)
  145. , value(0.0)
  146. {}
  147. };
  148. // summarize a material in an convinient way.
  149. struct Material
  150. {
  151. std::string name;
  152. std::string shading_model;
  153. Surface ambient, diffuse, specular, emissive, reflective, transparent, normal;
  154. Property shininess, transparency, index_refraction;
  155. Material() {}
  156. };
  157. std::vector<Material> materials;
  158. std::map<unsigned int, std::string> textures;
  159. protected:
  160. /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions
  161. /// Reads a single surface entry from the given material keys
  162. void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex);
  163. /// Writes an image entry for the given surface
  164. void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd);
  165. /// Writes the two parameters necessary for referencing a texture in an effect entry
  166. void WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName);
  167. /// Writes a color-or-texture entry into an effect definition
  168. void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName);
  169. /// Writes a scalar property
  170. void WriteFloatEntry( const Property& pProperty, const std::string& pTypeName);
  171. };
  172. }
  173. #endif // !! AI_COLLADAEXPORTER_H_INC