ColladaExporter.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 geometry library
  82. void WriteGeometryLibrary();
  83. /// Writes the given mesh
  84. void WriteGeometry( size_t pIndex);
  85. enum FloatDataType { FloatType_Vector, FloatType_TexCoord2, FloatType_TexCoord3, FloatType_Color };
  86. /// Writes a float array of the given type
  87. void WriteFloatArray( const std::string& pIdString, FloatDataType pType, const ai_real* pData, size_t pElementCount);
  88. /// Writes the scene library
  89. void WriteSceneLibrary();
  90. /// Recursively writes the given node
  91. void WriteNode( const aiScene* scene, aiNode* pNode);
  92. /// Enters a new xml element, which increases the indentation
  93. void PushTag() { startstr.append( " "); }
  94. /// Leaves an element, decreasing the indentation
  95. void PopTag() { ai_assert( startstr.length() > 1); startstr.erase( startstr.length() - 2); }
  96. /// Creates a mesh ID for the given mesh
  97. std::string GetMeshId( size_t pIndex) const {
  98. return std::string( "meshId" ) + to_string(pIndex);
  99. }
  100. public:
  101. /// Stringstream to write all output into
  102. std::stringstream mOutput;
  103. protected:
  104. /// The IOSystem for output
  105. IOSystem* mIOSystem;
  106. /// Path of the directory where the scene will be exported
  107. const std::string mPath;
  108. /// Name of the file (without extension) where the scene will be exported
  109. const std::string mFile;
  110. /// The scene to be written
  111. const aiScene* mScene;
  112. bool mSceneOwned;
  113. /// current line start string, contains the current indentation for simple stream insertion
  114. std::string startstr;
  115. /// current line end string for simple stream insertion
  116. std::string endstr;
  117. // pair of color and texture - texture precedences color
  118. struct Surface
  119. {
  120. bool exist;
  121. aiColor4D color;
  122. std::string texture;
  123. size_t channel;
  124. Surface() { exist = false; channel = 0; }
  125. };
  126. struct Property
  127. {
  128. bool exist;
  129. ai_real value;
  130. Property()
  131. : exist(false)
  132. , value(0.0)
  133. {}
  134. };
  135. // summarize a material in an convinient way.
  136. struct Material
  137. {
  138. std::string name;
  139. std::string shading_model;
  140. Surface ambient, diffuse, specular, emissive, reflective, transparent, normal;
  141. Property shininess, transparency, index_refraction;
  142. Material() {}
  143. };
  144. std::vector<Material> materials;
  145. std::map<unsigned int, std::string> textures;
  146. protected:
  147. /// Dammit C++ - y u no compile two-pass? No I have to add all methods below the struct definitions
  148. /// Reads a single surface entry from the given material keys
  149. void ReadMaterialSurface( Surface& poSurface, const aiMaterial* pSrcMat, aiTextureType pTexture, const char* pKey, size_t pType, size_t pIndex);
  150. /// Writes an image entry for the given surface
  151. void WriteImageEntry( const Surface& pSurface, const std::string& pNameAdd);
  152. /// Writes the two parameters necessary for referencing a texture in an effect entry
  153. void WriteTextureParamEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pMatName);
  154. /// Writes a color-or-texture entry into an effect definition
  155. void WriteTextureColorEntry( const Surface& pSurface, const std::string& pTypeName, const std::string& pImageName);
  156. /// Writes a scalar property
  157. void WriteFloatEntry( const Property& pProperty, const std::string& pTypeName);
  158. };
  159. }
  160. #endif // !! AI_COLLADAEXPORTER_H_INC