You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

243 lines
8.7 KiB

  1. /** Defines the collada loader class */
  2. /*
  3. Open Asset Import Library (assimp)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2012, 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 "BaseImporter.h"
  37. #include "ColladaParser.h"
  38. namespace Assimp
  39. {
  40. struct ColladaMeshIndex
  41. {
  42. std::string mMeshID;
  43. size_t mSubMesh;
  44. std::string mMaterial;
  45. ColladaMeshIndex( const std::string& pMeshID, size_t pSubMesh, const std::string& pMaterial)
  46. : mMeshID( pMeshID), mSubMesh( pSubMesh), mMaterial( pMaterial)
  47. { }
  48. bool operator < (const ColladaMeshIndex& p) const
  49. {
  50. if( mMeshID == p.mMeshID)
  51. {
  52. if( mSubMesh == p.mSubMesh)
  53. return mMaterial < p.mMaterial;
  54. else
  55. return mSubMesh < p.mSubMesh;
  56. } else
  57. {
  58. return mMeshID < p.mMeshID;
  59. }
  60. }
  61. };
  62. /** Loader class to read Collada scenes. Collada is over-engineered to death, with every new iteration bringing
  63. * more useless stuff, so I limited the data to what I think is useful for games.
  64. */
  65. class ColladaLoader : public BaseImporter
  66. {
  67. public:
  68. ColladaLoader();
  69. ~ColladaLoader();
  70. public:
  71. /** Returns whether the class can handle the format of the given file.
  72. * See BaseImporter::CanRead() for details. */
  73. bool CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const;
  74. protected:
  75. /** Return importer meta information.
  76. * See #BaseImporter::GetInfo for the details
  77. */
  78. const aiImporterDesc* GetInfo () const;
  79. void SetupProperties(const Importer* pImp);
  80. /** Imports the given file into the given scene structure.
  81. * See BaseImporter::InternReadFile() for details
  82. */
  83. void InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
  84. /** Recursively constructs a scene node for the given parser node and returns it. */
  85. aiNode* BuildHierarchy( const ColladaParser& pParser, const Collada::Node* pNode);
  86. /** Resolve node instances */
  87. void ResolveNodeInstances( const ColladaParser& pParser, const Collada::Node* pNode,
  88. std::vector<const Collada::Node*>& resolved);
  89. /** Builds meshes for the given node and references them */
  90. void BuildMeshesForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  91. aiNode* pTarget);
  92. /** Creates a mesh for the given ColladaMesh face subset and returns the newly created mesh */
  93. aiMesh* CreateMesh( const ColladaParser& pParser, const Collada::Mesh* pSrcMesh, const Collada::SubMesh& pSubMesh,
  94. const Collada::Controller* pSrcController, size_t pStartVertex, size_t pStartFace);
  95. /** Builds cameras for the given node and references them */
  96. void BuildCamerasForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  97. aiNode* pTarget);
  98. /** Builds lights for the given node and references them */
  99. void BuildLightsForNode( const ColladaParser& pParser, const Collada::Node* pNode,
  100. aiNode* pTarget);
  101. /** Stores all meshes in the given scene */
  102. void StoreSceneMeshes( aiScene* pScene);
  103. /** Stores all materials in the given scene */
  104. void StoreSceneMaterials( aiScene* pScene);
  105. /** Stores all lights in the given scene */
  106. void StoreSceneLights( aiScene* pScene);
  107. /** Stores all cameras in the given scene */
  108. void StoreSceneCameras( aiScene* pScene);
  109. /** Stores all textures in the given scene */
  110. void StoreSceneTextures( aiScene* pScene);
  111. /** Stores all animations
  112. * @param pScene target scene to store the anims
  113. */
  114. void StoreAnimations( aiScene* pScene, const ColladaParser& pParser);
  115. /** Stores all animations for the given source anim and its nested child animations
  116. * @param pScene target scene to store the anims
  117. * @param pSrcAnim the source animation to process
  118. * @param pPrefix Prefix to the name in case of nested animations
  119. */
  120. void StoreAnimations( aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string pPrefix);
  121. /** Constructs the animation for the given source anim */
  122. void CreateAnimation( aiScene* pScene, const ColladaParser& pParser, const Collada::Animation* pSrcAnim, const std::string& pName);
  123. /** Constructs materials from the collada material definitions */
  124. void BuildMaterials( ColladaParser& pParser, aiScene* pScene);
  125. /** Fill materials from the collada material definitions */
  126. void FillMaterials( const ColladaParser& pParser, aiScene* pScene);
  127. /** Resolve UV channel mappings*/
  128. void ApplyVertexToEffectSemanticMapping(Collada::Sampler& sampler,
  129. const Collada::SemanticMappingTable& table);
  130. /** Add a texture and all of its sampling properties to a material*/
  131. void AddTexture ( aiMaterial& mat, const ColladaParser& pParser,
  132. const Collada::Effect& effect,
  133. const Collada::Sampler& sampler,
  134. aiTextureType type, unsigned int idx = 0);
  135. /** Resolves the texture name for the given effect texture entry */
  136. aiString FindFilenameForEffectTexture( const ColladaParser& pParser,
  137. const Collada::Effect& pEffect, const std::string& pName);
  138. /** Converts a path read from a collada file to the usual representation */
  139. void ConvertPath( aiString& ss);
  140. /** Reads a float value from an accessor and its data array.
  141. * @param pAccessor The accessor to use for reading
  142. * @param pData The data array to read from
  143. * @param pIndex The index of the element to retrieve
  144. * @param pOffset Offset into the element, for multipart elements such as vectors or matrices
  145. * @return the specified value
  146. */
  147. float ReadFloat( const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex, size_t pOffset) const;
  148. /** Reads a string value from an accessor and its data array.
  149. * @param pAccessor The accessor to use for reading
  150. * @param pData The data array to read from
  151. * @param pIndex The index of the element to retrieve
  152. * @return the specified value
  153. */
  154. const std::string& ReadString( const Collada::Accessor& pAccessor, const Collada::Data& pData, size_t pIndex) const;
  155. /** Recursively collects all nodes into the given array */
  156. void CollectNodes( const aiNode* pNode, std::vector<const aiNode*>& poNodes) const;
  157. /** Finds a node in the collada scene by the given name */
  158. const Collada::Node* FindNode( const Collada::Node* pNode, const std::string& pName) const;
  159. /** Finds a node in the collada scene by the given SID */
  160. const Collada::Node* FindNodeBySID( const Collada::Node* pNode, const std::string& pSID) const;
  161. /** Finds a proper name for a node derived from the collada-node's properties */
  162. std::string FindNameForNode( const Collada::Node* pNode) const;
  163. protected:
  164. /** Filename, for a verbose error message */
  165. std::string mFileName;
  166. /** Which mesh-material compound was stored under which mesh ID */
  167. std::map<ColladaMeshIndex, size_t> mMeshIndexByID;
  168. /** Which material was stored under which index in the scene */
  169. std::map<std::string, size_t> mMaterialIndexByName;
  170. /** Accumulated meshes for the target scene */
  171. std::vector<aiMesh*> mMeshes;
  172. /** Temporary material list */
  173. std::vector<std::pair<Collada::Effect*, aiMaterial*> > newMats;
  174. /** Temporary camera list */
  175. std::vector<aiCamera*> mCameras;
  176. /** Temporary light list */
  177. std::vector<aiLight*> mLights;
  178. /** Temporary texture list */
  179. std::vector<aiTexture*> mTextures;
  180. /** Accumulated animations for the target scene */
  181. std::vector<aiAnimation*> mAnims;
  182. bool noSkeletonMesh;
  183. bool ignoreUpDirection;
  184. };
  185. } // end of namespace Assimp
  186. #endif // AI_COLLADALOADER_H_INC