IRRLoader.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, 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 IRRLoader.h
  34. * @brief Declaration of the .irrMesh (Irrlight Engine Mesh Format)
  35. * importer class.
  36. */
  37. #ifndef AI_IRRLOADER_H_INCLUDED
  38. #define AI_IRRLOADER_H_INCLUDED
  39. #include "AssetLib/Irr/IRRShared.h"
  40. #include "Common/Importer.h"
  41. #include <assimp/SceneCombiner.h>
  42. #include <assimp/StringUtils.h>
  43. #include <assimp/anim.h>
  44. namespace Assimp {
  45. // ---------------------------------------------------------------------------
  46. /** Irr importer class.
  47. *
  48. * Irr is the native scene file format of the Irrlight engine and its editor
  49. * irrEdit. As IrrEdit itself is capable of importing quite many file formats,
  50. * it might be a good file format for data exchange.
  51. */
  52. class IRRImporter : public BaseImporter, public IrrlichtBase {
  53. public:
  54. IRRImporter();
  55. ~IRRImporter() override = default;
  56. // -------------------------------------------------------------------
  57. /** Returns whether the class can handle the format of the given file.
  58. * See BaseImporter::CanRead() for details.
  59. */
  60. bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
  61. bool checkSig) const override;
  62. protected:
  63. const aiImporterDesc* GetInfo () const override;
  64. void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) override;
  65. void SetupProperties(const Importer* pImp) override;
  66. private:
  67. /** Data structure for a scene-graph node animator
  68. */
  69. struct Animator {
  70. // Type of the animator
  71. enum AT {
  72. UNKNOWN = 0x0,
  73. ROTATION = 0x1,
  74. FLY_CIRCLE = 0x2,
  75. FLY_STRAIGHT = 0x3,
  76. FOLLOW_SPLINE = 0x4,
  77. OTHER = 0x5
  78. } type;
  79. explicit Animator(AT t = UNKNOWN)
  80. : type (t)
  81. , speed ( ai_real( 0.001 ) )
  82. , direction ( ai_real( 0.0 ), ai_real( 1.0 ), ai_real( 0.0 ) )
  83. , circleRadius ( ai_real( 1.0) )
  84. , tightness ( ai_real( 0.5 ) )
  85. , loop (true)
  86. , timeForWay (100)
  87. {
  88. }
  89. // common parameters
  90. ai_real speed;
  91. aiVector3D direction;
  92. // FLY_CIRCLE
  93. aiVector3D circleCenter;
  94. ai_real circleRadius;
  95. // FOLLOW_SPLINE
  96. ai_real tightness;
  97. std::vector<aiVectorKey> splineKeys;
  98. // ROTATION (angles given in direction)
  99. // FLY STRAIGHT
  100. // circleCenter = start, direction = end
  101. bool loop;
  102. int timeForWay;
  103. };
  104. /** Data structure for a scene-graph node in an IRR file
  105. */
  106. struct Node
  107. {
  108. // Type of the node
  109. enum ET
  110. {
  111. LIGHT,
  112. CUBE,
  113. MESH,
  114. SKYBOX,
  115. DUMMY,
  116. CAMERA,
  117. TERRAIN,
  118. SPHERE,
  119. ANIMMESH
  120. } type;
  121. explicit Node(ET t)
  122. : type (t)
  123. , scaling (1.0,1.0,1.0) // assume uniform scaling by default
  124. , parent()
  125. , framesPerSecond (0.0)
  126. , id()
  127. , sphereRadius (1.0)
  128. , spherePolyCountX (100)
  129. , spherePolyCountY (100)
  130. {
  131. // Generate a default name for the node
  132. char buffer[128];
  133. static int cnt;
  134. ai_snprintf(buffer, 128, "IrrNode_%i",cnt++);
  135. name = std::string(buffer);
  136. // reserve space for up to 5 materials
  137. materials.reserve(5);
  138. // reserve space for up to 5 children
  139. children.reserve(5);
  140. }
  141. // Transformation of the node
  142. aiVector3D position, rotation, scaling;
  143. // Name of the node
  144. std::string name;
  145. // List of all child nodes
  146. std::vector<Node*> children;
  147. // Parent node
  148. Node* parent;
  149. // Animated meshes: frames per second
  150. // 0.f if not specified
  151. ai_real framesPerSecond;
  152. // Meshes: path to the mesh to be loaded
  153. std::string meshPath;
  154. unsigned int id;
  155. // Meshes: List of materials to be assigned
  156. // along with their corresponding material flags
  157. std::vector< std::pair<aiMaterial*, unsigned int> > materials;
  158. // Spheres: radius of the sphere to be generates
  159. ai_real sphereRadius;
  160. // Spheres: Number of polygons in the x,y direction
  161. unsigned int spherePolyCountX,spherePolyCountY;
  162. // List of all animators assigned to the node
  163. std::list<Animator> animators;
  164. };
  165. /** Data structure for a vertex in an IRR skybox
  166. */
  167. struct SkyboxVertex
  168. {
  169. SkyboxVertex() = default;
  170. //! Construction from single vertex components
  171. SkyboxVertex(ai_real px, ai_real py, ai_real pz,
  172. ai_real nx, ai_real ny, ai_real nz,
  173. ai_real uvx, ai_real uvy)
  174. : position (px,py,pz)
  175. , normal (nx,ny,nz)
  176. , uv (uvx,uvy,0.0)
  177. {}
  178. aiVector3D position, normal, uv;
  179. };
  180. // -------------------------------------------------------------------
  181. /// Fill the scene-graph recursively
  182. void GenerateGraph(Node* root,aiNode* rootOut ,aiScene* scene,
  183. BatchLoader& batch,
  184. std::vector<aiMesh*>& meshes,
  185. std::vector<aiNodeAnim*>& anims,
  186. std::vector<AttachmentInfo>& attach,
  187. std::vector<aiMaterial*>& materials,
  188. unsigned int& defaultMatIdx);
  189. // -------------------------------------------------------------------
  190. /// Generate a mesh that consists of just a single quad
  191. aiMesh* BuildSingleQuadMesh(const SkyboxVertex& v1,
  192. const SkyboxVertex& v2,
  193. const SkyboxVertex& v3,
  194. const SkyboxVertex& v4);
  195. // -------------------------------------------------------------------
  196. /// Build a sky-box
  197. ///
  198. /// @param meshes Receives 6 output meshes
  199. /// @param materials The last 6 materials are assigned to the newly
  200. /// created meshes. The names of the materials are adjusted.
  201. void BuildSkybox(std::vector<aiMesh*>& meshes,
  202. std::vector<aiMaterial*> materials);
  203. // -------------------------------------------------------------------
  204. /** Copy a material for a mesh to the output material list
  205. *
  206. * @param materials Receives an output material
  207. * @param inmaterials List of input materials
  208. * @param defMatIdx Default material index - UINT_MAX if not present
  209. * @param mesh Mesh to work on
  210. */
  211. void CopyMaterial(std::vector<aiMaterial*>& materials,
  212. std::vector< std::pair<aiMaterial*, unsigned int> >& inmaterials,
  213. unsigned int& defMatIdx,
  214. aiMesh* mesh);
  215. // -------------------------------------------------------------------
  216. /** Compute animations for a specific node
  217. *
  218. * @param root Node to be processed
  219. * @param anims The list of output animations
  220. */
  221. void ComputeAnimations(Node* root, aiNode* real,
  222. std::vector<aiNodeAnim*>& anims);
  223. private:
  224. /// Configuration option: desired output FPS
  225. double fps;
  226. /// Configuration option: speed flag was set?
  227. bool configSpeedFlag;
  228. };
  229. } // end of namespace Assimp
  230. #endif // AI_IRRIMPORTER_H_INC