IRRLoader.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 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 "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;
  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), speed(ai_real(0.001)), direction(ai_real(0.0), ai_real(1.0), ai_real(0.0)), circleRadius(ai_real(1.0)), tightness(ai_real(0.5)), loop(true), timeForWay(100) {
  81. }
  82. // common parameters
  83. ai_real speed;
  84. aiVector3D direction;
  85. // FLY_CIRCLE
  86. aiVector3D circleCenter;
  87. ai_real circleRadius;
  88. // FOLLOW_SPLINE
  89. ai_real tightness;
  90. std::vector<aiVectorKey> splineKeys;
  91. // ROTATION (angles given in direction)
  92. // FLY STRAIGHT
  93. // circleCenter = start, direction = end
  94. bool loop;
  95. int timeForWay;
  96. };
  97. /** Data structure for a scene-graph node in an IRR file
  98. */
  99. struct Node {
  100. // Type of the node
  101. enum ET {
  102. LIGHT,
  103. CUBE,
  104. MESH,
  105. SKYBOX,
  106. DUMMY,
  107. CAMERA,
  108. TERRAIN,
  109. SPHERE,
  110. ANIMMESH
  111. } type;
  112. explicit Node(ET t) :
  113. type(t), scaling(1.0, 1.0, 1.0) // assume uniform scaling by default
  114. ,
  115. parent(),
  116. framesPerSecond(0.0),
  117. id(),
  118. sphereRadius(1.0),
  119. spherePolyCountX(100),
  120. spherePolyCountY(100) {
  121. // Generate a default name for the node
  122. char buffer[128];
  123. static int cnt;
  124. ai_snprintf(buffer, 128, "IrrNode_%i", cnt++);
  125. name = std::string(buffer);
  126. // reserve space for up to 5 materials
  127. materials.reserve(5);
  128. // reserve space for up to 5 children
  129. children.reserve(5);
  130. }
  131. // Transformation of the node
  132. aiVector3D position, rotation, scaling;
  133. // Name of the node
  134. std::string name;
  135. // List of all child nodes
  136. std::vector<Node *> children;
  137. // Parent node
  138. Node *parent;
  139. // Animated meshes: frames per second
  140. // 0.f if not specified
  141. ai_real framesPerSecond;
  142. // Meshes: path to the mesh to be loaded
  143. std::string meshPath;
  144. unsigned int id;
  145. // Meshes: List of materials to be assigned
  146. // along with their corresponding material flags
  147. std::vector<std::pair<aiMaterial *, unsigned int>> materials;
  148. // Spheres: radius of the sphere to be generates
  149. ai_real sphereRadius;
  150. // Spheres: Number of polygons in the x,y direction
  151. unsigned int spherePolyCountX, spherePolyCountY;
  152. // List of all animators assigned to the node
  153. std::list<Animator> animators;
  154. };
  155. /** Data structure for a vertex in an IRR skybox
  156. */
  157. struct SkyboxVertex {
  158. SkyboxVertex() = default;
  159. //! Construction from single vertex components
  160. SkyboxVertex(ai_real px, ai_real py, ai_real pz,
  161. ai_real nx, ai_real ny, ai_real nz,
  162. ai_real uvx, ai_real uvy)
  163. :
  164. position(px, py, pz), normal(nx, ny, nz), uv(uvx, uvy, 0.0) {}
  165. aiVector3D position, normal, uv;
  166. };
  167. // -------------------------------------------------------------------
  168. // Parse <node> tag from XML file and extract child node
  169. // @param node XML node
  170. // @param guessedMeshesContained number of extra guessed meshes
  171. IRRImporter::Node *ParseNode(pugi::xml_node &node, BatchLoader& batch);
  172. // -------------------------------------------------------------------
  173. // Parse <attributes> tags within <node> tags and apply to scene node
  174. // @param attributeNode XML child node
  175. // @param nd Attributed scene node
  176. void ParseNodeAttributes(pugi::xml_node &attributeNode, IRRImporter::Node *nd, BatchLoader& batch);
  177. // -------------------------------------------------------------------
  178. // Parse an <animator> node and attach an animator to a node
  179. // @param animatorNode XML animator node
  180. // @param nd Animated scene node
  181. void ParseAnimators(pugi::xml_node &animatorNode, IRRImporter::Node *nd);
  182. // -------------------------------------------------------------------
  183. /// Fill the scene-graph recursively
  184. void GenerateGraph(Node *root, aiNode *rootOut, aiScene *scene,
  185. BatchLoader &batch,
  186. std::vector<aiMesh *> &meshes,
  187. std::vector<aiNodeAnim *> &anims,
  188. std::vector<AttachmentInfo> &attach,
  189. std::vector<aiMaterial *> &materials,
  190. unsigned int &defaultMatIdx);
  191. // -------------------------------------------------------------------
  192. /// Generate a mesh that consists of just a single quad
  193. aiMesh *BuildSingleQuadMesh(const SkyboxVertex &v1,
  194. const SkyboxVertex &v2,
  195. const SkyboxVertex &v3,
  196. const SkyboxVertex &v4);
  197. // -------------------------------------------------------------------
  198. /// Build a sky-box
  199. ///
  200. /// @param meshes Receives 6 output meshes
  201. /// @param materials The last 6 materials are assigned to the newly
  202. /// created meshes. The names of the materials are adjusted.
  203. void BuildSkybox(std::vector<aiMesh *> &meshes,
  204. std::vector<aiMaterial *> materials);
  205. // -------------------------------------------------------------------
  206. /** Copy a material for a mesh to the output material list
  207. *
  208. * @param materials Receives an output material
  209. * @param inmaterials List of input materials
  210. * @param defMatIdx Default material index - UINT_MAX if not present
  211. * @param mesh Mesh to work on
  212. */
  213. void CopyMaterial(std::vector<aiMaterial *> &materials,
  214. std::vector<std::pair<aiMaterial *, unsigned int>> &inmaterials,
  215. unsigned int &defMatIdx,
  216. aiMesh *mesh);
  217. // -------------------------------------------------------------------
  218. /** Compute animations for a specific node
  219. *
  220. * @param root Node to be processed
  221. * @param anims The list of output animations
  222. */
  223. void ComputeAnimations(Node *root, aiNode *real,
  224. std::vector<aiNodeAnim *> &anims);
  225. private:
  226. /// Configuration option: desired output FPS
  227. double fps;
  228. /// Configuration option: speed flag was set?
  229. bool configSpeedFlag;
  230. std::vector<aiCamera*> cameras;
  231. std::vector<aiLight*> lights;
  232. unsigned int guessedMeshCnt;
  233. unsigned int guessedMatCnt;
  234. unsigned int guessedAnimCnt;
  235. };
  236. } // end of namespace Assimp
  237. #endif // AI_IRRIMPORTER_H_INC