X3DExporter.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /// \file X3DExporter.hpp
  2. /// \brief X3D-format files exporter for Assimp.
  3. /// \date 2016
  4. /// \author [email protected]
  5. // Thanks to acorn89 for support.
  6. #ifndef INCLUDED_AI_X3D_EXPORTER_H
  7. #define INCLUDED_AI_X3D_EXPORTER_H
  8. // Header files, Assimp.
  9. #include <assimp/DefaultLogger.hpp>
  10. #include <assimp/Exporter.hpp>
  11. #include <assimp/material.h>
  12. #include <assimp/scene.h>
  13. // Header files, stdlib.
  14. #include <list>
  15. #include <string>
  16. namespace Assimp
  17. {
  18. /// \class X3DExporter
  19. /// Class which export aiScene to X3D file.
  20. ///
  21. /// Limitations.
  22. ///
  23. /// Pay attention that X3D is format for interactive graphic and simulations for web browsers. aiScene can not contain all features of the X3D format.
  24. /// Also, aiScene contain rasterized-like data. For example, X3D can describe circle all cylinder with one tag, but aiScene contain result of tesselation:
  25. /// vertices, faces etc. Yes, you can use algorithm for detecting figures or shapes, but thats not good idea at all.
  26. ///
  27. /// Supported nodes:
  28. /// Core component:
  29. /// "MetadataBoolean", "MetadataDouble", "MetadataFloat", "MetadataInteger", "MetadataSet", "MetadataString"
  30. /// Geometry3D component:
  31. /// "IndexedFaceSet"
  32. /// Grouping component:
  33. /// "Group", "Transform"
  34. /// Lighting component:
  35. /// "DirectionalLight", "PointLight", "SpotLight"
  36. /// Rendering component:
  37. /// "ColorRGBA", "Coordinate", "Normal"
  38. /// Shape component:
  39. /// "Shape", "Appearance", "Material"
  40. /// Texturing component:
  41. /// "ImageTexture", "TextureCoordinate", "TextureTransform"
  42. ///
  43. class X3DExporter
  44. {
  45. /***********************************************/
  46. /******************** Types ********************/
  47. /***********************************************/
  48. struct SAttribute
  49. {
  50. const std::string Name;
  51. const std::string Value;
  52. };
  53. /***********************************************/
  54. /****************** Constants ******************/
  55. /***********************************************/
  56. const aiScene* const mScene;
  57. /***********************************************/
  58. /****************** Variables ******************/
  59. /***********************************************/
  60. IOStream* mOutFile;
  61. std::map<size_t, std::string> mDEF_Map_Mesh;
  62. std::map<size_t, std::string> mDEF_Map_Material;
  63. private:
  64. std::string mIndentationString;
  65. /***********************************************/
  66. /****************** Functions ******************/
  67. /***********************************************/
  68. /// \fn void IndentationStringSet(const size_t pNewLevel)
  69. /// Set value of the indentation string.
  70. /// \param [in] pNewLevel - new level of the indentation.
  71. void IndentationStringSet(const size_t pNewLevel);
  72. /// \fn void XML_Write(const std::string& pData)
  73. /// Write data to XML-file.
  74. /// \param [in] pData - reference to string which must be written.
  75. void XML_Write(const std::string& pData);
  76. /// \fn aiMatrix4x4 Matrix_GlobalToCurrent(const aiNode& pNode) const
  77. /// Calculate transformation matrix for transformation from global coordinate system to pointed aiNode.
  78. /// \param [in] pNode - reference to local node.
  79. /// \return calculated matrix.
  80. aiMatrix4x4 Matrix_GlobalToCurrent(const aiNode& pNode) const;
  81. /// \fn void AttrHelper_CommaToPoint(std::string& pStringWithComma)
  82. /// Convert commas in string to points. Thats need because "std::to_string" result depend on locale (regional settings).
  83. /// \param [in, out] pStringWithComma - reference to string, which must be modified.
  84. void AttrHelper_CommaToPoint(std::string& pStringWithComma) { for(char& c: pStringWithComma) { if(c == ',') c = '.'; } }
  85. /// \fn void AttrHelper_FloatToString(const float pValue, std::string& pTargetString)
  86. /// Converts float to string.
  87. /// \param [in] pValue - value for converting.
  88. /// \param [out] pTargetString - reference to string where result will be placed. Will be cleared before using.
  89. void AttrHelper_FloatToString(const float pValue, std::string& pTargetString);
  90. /// \fn void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  91. /// Converts array of vectors to string.
  92. /// \param [in] pArray - pointer to array of vectors.
  93. /// \param [in] pArray_Size - count of elements in array.
  94. /// \param [out] pTargetString - reference to string where result will be placed. Will be cleared before using.
  95. void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString);
  96. /// \fn void AttrHelper_Vec2DArrToString(const aiVector2D* pArray, const size_t pArray_Size, std::string& pTargetString)
  97. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  98. void AttrHelper_Vec2DArrToString(const aiVector2D* pArray, const size_t pArray_Size, std::string& pTargetString);
  99. /// \fn void AttrHelper_Vec3DAsVec2fArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  100. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  101. /// Only x, y is used from aiVector3D.
  102. void AttrHelper_Vec3DAsVec2fArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString);
  103. /// \fn void AttrHelper_Col4DArrToString(const aiColor4D* pArray, const size_t pArray_Size, std::string& pTargetString)
  104. /// \overload void AttrHelper_Vec3DArrToString(const aiVector3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  105. /// Converts array of colors to string.
  106. void AttrHelper_Col4DArrToString(const aiColor4D* pArray, const size_t pArray_Size, std::string& pTargetString);
  107. /// \fn void AttrHelper_Col3DArrToString(const aiColor3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  108. /// \overload void AttrHelper_Col4DArrToString(const aiColor4D* pArray, const size_t pArray_Size, std::string& pTargetString)
  109. /// Converts array of colors to string.
  110. void AttrHelper_Col3DArrToString(const aiColor3D* pArray, const size_t pArray_Size, std::string& pTargetString);
  111. /// \fn void AttrHelper_FloatToAttrList(std::list<SAttribute> pList, const std::string& pName, const float pValue, const float pDefaultValue)
  112. /// \overload void AttrHelper_Col3DArrToString(const aiColor3D* pArray, const size_t pArray_Size, std::string& pTargetString)
  113. void AttrHelper_FloatToAttrList(std::list<SAttribute> pList, const std::string& pName, const float pValue, const float pDefaultValue);
  114. /// \fn void AttrHelper_Color3ToAttrList(std::list<SAttribute> pList, const std::string& pName, const aiColor3D& pValue, const aiColor3D& pDefaultValue)
  115. /// Add attribute to list if value not equal to default.
  116. /// \param [in] pList - target list of the attributes.
  117. /// \param [in] pName - name of new attribute.
  118. /// \param [in] pValue - value of the new attribute.
  119. /// \param [in] pDefaultValue - default value for checking: if pValue is equal to pDefaultValue then attribute will not be added.
  120. void AttrHelper_Color3ToAttrList(std::list<SAttribute> pList, const std::string& pName, const aiColor3D& pValue, const aiColor3D& pDefaultValue);
  121. /// \fn void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute>& pAttrList)
  122. /// Begin new XML-node element.
  123. /// \param [in] pNodeName - name of the element.
  124. /// \param [in] pTabLevel - indentation level.
  125. /// \param [in] pEmtyElement - if true then empty element will be created.
  126. /// \param [in] pAttrList - list of the attributes for element.
  127. void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute>& pAttrList);
  128. /// \fn void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement = false)
  129. /// \overload void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement, const std::list<SAttribute>& pAttrList)
  130. void NodeHelper_OpenNode(const std::string& pNodeName, const size_t pTabLevel, const bool pEmptyElement = false);
  131. /// \fn void NodeHelper_CloseNode(const std::string& pNodeName, const size_t pTabLevel)
  132. /// End XML-node element.
  133. /// \param [in] pNodeName - name of the element.
  134. /// \param [in] pTabLevel - indentation level.
  135. void NodeHelper_CloseNode(const std::string& pNodeName, const size_t pTabLevel);
  136. /// \fn void Export_Node(const aiNode* pNode, const size_t pTabLevel)
  137. /// Export data from scene to XML-file: aiNode.
  138. /// \param [in] pNode - source aiNode.
  139. /// \param [in] pTabLevel - indentation level.
  140. void Export_Node(const aiNode* pNode, const size_t pTabLevel);
  141. /// \fn void Export_Mesh(const size_t pIdxMesh, const size_t pTabLevel)
  142. /// Export data from scene to XML-file: aiMesh.
  143. /// \param [in] pMesh - index of the source aiMesh.
  144. /// \param [in] pTabLevel - indentation level.
  145. void Export_Mesh(const size_t pIdxMesh, const size_t pTabLevel);
  146. /// \fn void Export_Material(const size_t pIdxMaterial, const size_t pTabLevel)
  147. /// Export data from scene to XML-file: aiMaterial.
  148. /// \param [in] pIdxMaterial - index of the source aiMaterial.
  149. /// \param [in] pTabLevel - indentation level.
  150. void Export_Material(const size_t pIdxMaterial, const size_t pTabLevel);
  151. /// \fn void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  152. /// Export data from scene to XML-file: aiMetadata.
  153. /// \param [in] pKey - source data: value of the metadata key.
  154. /// \param [in] pValue - source data: value of the metadata value.
  155. /// \param [in] pTabLevel - indentation level.
  156. void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel);
  157. /// \fn void Export_MetadataDouble(const aiString& pKey, const double pValue, const size_t pTabLevel)
  158. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  159. void Export_MetadataDouble(const aiString& pKey, const double pValue, const size_t pTabLevel);
  160. /// \fn void Export_MetadataFloat(const aiString& pKey, const float pValue, const size_t pTabLevel)
  161. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  162. void Export_MetadataFloat(const aiString& pKey, const float pValue, const size_t pTabLevel);
  163. /// \fn void Export_MetadataInteger(const aiString& pKey, const int32_t pValue, const size_t pTabLevel)
  164. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  165. void Export_MetadataInteger(const aiString& pKey, const int32_t pValue, const size_t pTabLevel);
  166. /// \fn void Export_MetadataString(const aiString& pKey, const aiString& pValue, const size_t pTabLevel)
  167. /// \overload void Export_MetadataBoolean(const aiString& pKey, const bool pValue, const size_t pTabLevel)
  168. void Export_MetadataString(const aiString& pKey, const aiString& pValue, const size_t pTabLevel);
  169. /// \fn bool CheckAndExport_Light(const aiNode& pNode, const size_t pTabLevel)
  170. /// Check if node point to light source. If yes then export light source.
  171. /// \param [in] pNode - reference to node for checking.
  172. /// \param [in] pTabLevel - indentation level.
  173. /// \return true - if node assigned with light and it was exported, else - return false.
  174. bool CheckAndExport_Light(const aiNode& pNode, const size_t pTabLevel);
  175. /***********************************************/
  176. /************** Functions: LOG set *************/
  177. /***********************************************/
  178. /// \fn void LogError(const std::string& pMessage)
  179. /// Short variant for calling \ref DefaultLogger::get()->error()
  180. void LogError(const std::string& pMessage) { DefaultLogger::get()->error(pMessage); }
  181. public:
  182. /// \fn X3DExporter()
  183. /// Default constructor.
  184. X3DExporter(const char* pFileName, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties);
  185. /// \fn ~X3DExporter()
  186. /// Default destructor.
  187. ~X3DExporter() {}
  188. };// class X3DExporter
  189. }// namespace Assimp
  190. #endif // INCLUDED_AI_X3D_EXPORTER_H