ColladaLoader.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /** Defines the collada loader class */
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2020, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ----------------------------------------------------------------------
  33. */
  34. #ifndef AI_COLLADALOADER_H_INC
  35. #define AI_COLLADALOADER_H_INC
  36. #include <assimp/BaseImporter.h>
  37. #include "ColladaParser.h"
  38. struct aiNode;
  39. struct aiCamera;
  40. struct aiLight;
  41. struct aiTexture;
  42. struct aiAnimation;
  43. namespace Assimp
  44. {
  45. struct ColladaMeshIndex
  46. {
  47. std::string mMeshID;
  48. size_t mSubMesh;
  49. std::string mMaterial;
  50. ColladaMeshIndex( const std::string& pMeshID, size_t pSubMesh, const std::string& pMaterial)
  51. : mMeshID( pMeshID), mSubMesh( pSubMesh), mMaterial( pMaterial)
  52. { }
  53. bool operator < (const ColladaMeshIndex& p) const
  54. {
  55. if( mMeshID == p.mMeshID)
  56. {
  57. if( mSubMesh == p.mSubMesh)
  58. return mMaterial < p.mMaterial;
  59. else
  60. return mSubMesh < p.mSubMesh;
  61. } else
  62. {
  63. return mMeshID < p.mMeshID;
  64. }
  65. }
  66. };
  67. /** Loader class to read Collada scenes. Collada is over-engineered to death, with every new iteration bringing
  68. * more useless stuff, so I limited the data to what I think is useful for games.
  69. */
  70. class ColladaLoader : public BaseImporter
  71. {
  72. public:
  73. ColladaLoader();
  74. ~ColladaLoader();
  75. public:
  76. /** Returns whether the class can handle the format of the given file.
  77. * See BaseImporter::CanRead() for details. */
  78. bool CanRead(const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const override;
  79. protected:
  80. /** Return importer meta information.
  81. * See #BaseImporter::GetInfo for the details
  82. */
  83. const aiImporterDesc* GetInfo () const override;
  84. void SetupProperties(const Importer* pImp) override;
  85. /** Imports the given file into the given scene structure.
  86. * See BaseImporter::InternReadFile() for details
  87. */
  88. void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler) override;
  89. /** Recursively constructs a scene node for the given parser node and returns it. */
  90. aiNode* BuildHierarchy( const ColladaParser& pParser, const Collada::Node* pNode);
  91. /** Resolve node instances */
  92. void ResolveNodeInstances( const ColladaParser& pParser, const Collada::Node* pNode,
  93. std::vector<const Collada::Node*>& resolved);
  94. /** Builds meshes for the given node and references them */
  95. void BuildMeshesForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  96. aiNode* pTarget);
  97. aiMesh *findMesh(const std::string& meshid);
  98. /** Creates a mesh for the given ColladaMesh face subset and returns the newly created mesh */
  99. aiMesh* CreateMesh( const ColladaParser& pParser, const Collada::Mesh* pSrcMesh, const Collada::SubMesh& pSubMesh,
  100. const Collada::Controller* pSrcController, size_t pStartVertex, size_t pStartFace);
  101. /** Builds cameras for the given node and references them */
  102. void BuildCamerasForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  103. aiNode* pTarget);
  104. /** Builds lights for the given node and references them */
  105. void BuildLightsForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  106. aiNode* pTarget);
  107. /** Stores all meshes in the given scene */
  108. void StoreSceneMeshes( aiScene* pScene);
  109. /** Stores all materials in the given scene */
  110. void StoreSceneMaterials( aiScene* pScene);
  111. /** Stores all lights in the given scene */
  112. void StoreSceneLights( aiScene* pScene);
  113. /** Stores all cameras in the given scene */
  114. void StoreSceneCameras( aiScene* pScene);
  115. /** Stores all textures in the given scene */
  116. void StoreSceneTextures( aiScene* pScene);
  117. /** Stores all animations
  118. * @param pScene target scene to store the anims
  119. */
  120. void StoreAnimations( aiScene* pScene, const ColladaParser& pParser);
  121. /** Stores all animations for the given source anim and its nested child animations
  122. * @param pScene target scene to store the anims
  123. * @param pSrcAnim the source animation to process
  124. * @param pPrefix Prefix to the name in case of nested animations
  125. */
  126. void StoreAnimations( aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string& pPrefix);
  127. /** Constructs the animation for the given source anim */
  128. void CreateAnimation( aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string& pName);
  129. /** Constructs materials from the collada material definitions */
  130. void BuildMaterials( ColladaParser& pParser, aiScene* pScene);
  131. /** Fill materials from the collada material definitions */
  132. void FillMaterials( const ColladaParser& pParser, aiScene* pScene);
  133. /** Resolve UV channel mappings*/
  134. void ApplyVertexToEffectSemanticMapping(Collada::Sampler& sampler,
  135. const Collada::SemanticMappingTable& table);
  136. /** Add a texture and all of its sampling properties to a material*/
  137. void AddTexture ( aiMaterial& mat, const ColladaParser& pParser,
  138. const Collada::Effect& effect,
  139. const Collada::Sampler& sampler,
  140. aiTextureType type, unsigned int idx = 0);
  141. /** Resolves the texture name for the given effect texture entry */
  142. aiString FindFilenameForEffectTexture( const ColladaParser& pParser,
  143. const Collada::Effect& pEffect, const std::string& pName);
  144. /** Reads a float value from an accessor and its data array.
  145. * @param pAccessor The accessor to use for reading
  146. * @param pData The data array to read from
  147. * @param pIndex The index of the element to retrieve
  148. * @param pOffset Offset into the element, for multipart elements such as vectors or matrices
  149. * @return the specified value
  150. */
  151. ai_real ReadFloat( const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex, size_t pOffset) const;
  152. /** Reads a string value from an accessor and its data array.
  153. * @param pAccessor The accessor to use for reading
  154. * @param pData The data array to read from
  155. * @param pIndex The index of the element to retrieve
  156. * @return the specified value
  157. */
  158. const std::string& ReadString( const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex) const;
  159. /** Recursively collects all nodes into the given array */
  160. void CollectNodes( const aiNode* pNode, std::vector<const aiNode*>& poNodes) const;
  161. /** Finds a node in the collada scene by the given name */
  162. const Collada::Node* FindNode( const Collada::Node* pNode, const std::string& pName) const;
  163. /** Finds a node in the collada scene by the given SID */
  164. const Collada::Node* FindNodeBySID( const Collada::Node* pNode, const std::string& pSID) const;
  165. /** Finds a proper name for a node derived from the collada-node's properties */
  166. std::string FindNameForNode( const Collada::Node* pNode);
  167. protected:
  168. /** Filename, for a verbose error message */
  169. std::string mFileName;
  170. /** Which mesh-material compound was stored under which mesh ID */
  171. std::map<ColladaMeshIndex, size_t> mMeshIndexByID;
  172. /** Which material was stored under which index in the scene */
  173. std::map<std::string, size_t> mMaterialIndexByName;
  174. /** Accumulated meshes for the target scene */
  175. std::vector<aiMesh*> mMeshes;
  176. /** Accumulated morph target meshes */
  177. std::vector<aiMesh*> mTargetMeshes;
  178. /** Temporary material list */
  179. std::vector<std::pair<Collada::Effect*, aiMaterial*> > newMats;
  180. /** Temporary camera list */
  181. std::vector<aiCamera*> mCameras;
  182. /** Temporary light list */
  183. std::vector<aiLight*> mLights;
  184. /** Temporary texture list */
  185. std::vector<aiTexture*> mTextures;
  186. /** Accumulated animations for the target scene */
  187. std::vector<aiAnimation*> mAnims;
  188. bool noSkeletonMesh;
  189. bool ignoreUpDirection;
  190. bool useColladaName;
  191. /** Used by FindNameForNode() to generate unique node names */
  192. unsigned int mNodeNameCounter;
  193. };
  194. } // end of namespace Assimp
  195. #endif // AI_COLLADALOADER_H_INC