ColladaLoader.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /** Defines the collada loader class */
  2. /*
  3. Open Asset Import Library (ASSIMP)
  4. ----------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. friend class Importer;
  68. protected:
  69. /** Constructor to be privately used by Importer */
  70. ColladaLoader();
  71. /** Destructor, private as well */
  72. ~ColladaLoader();
  73. public:
  74. /** Returns whether the class can handle the format of the given file.
  75. * See BaseImporter::CanRead() for details. */
  76. bool CanRead( const std::string& pFile, IOSystem* pIOHandler) const;
  77. protected:
  78. /** Called by Importer::GetExtensionList() for each loaded importer.
  79. * See BaseImporter::GetExtensionList() for details
  80. */
  81. void GetExtensionList( std::string& append)
  82. {
  83. append.append("*.dae");
  84. }
  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);
  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. /** Builds meshes for the given node and references them */
  92. void BuildMeshesForNode( const ColladaParser& pParser, const Collada::Node* pNode, aiNode* pTarget);
  93. /** Stores all meshes in the given scene */
  94. void StoreSceneMeshes( aiScene* pScene);
  95. /** Constructs materials from the collada material definitions */
  96. void BuildMaterials( const ColladaParser& pParser, aiScene* pScene);
  97. /** Resolves the texture name for the given effect texture entry */
  98. const aiString& FindFilenameForEffectTexture( const ColladaParser& pParser, const Collada::Effect& pEffect, const std::string& pName);
  99. /** Converts a path read from a collada file to the usual representation */
  100. void ConvertPath (aiString& ss);
  101. protected:
  102. /** Filename, for a verbose error message */
  103. std::string mFileName;
  104. /** Which mesh-material compound was stored under which mesh ID */
  105. std::map<ColladaMeshIndex, size_t> mMeshIndexByID;
  106. /** Which material was stored under which index in the scene */
  107. std::map<std::string, size_t> mMaterialIndexByName;
  108. /** Accumulated meshes for the target scene */
  109. std::vector<aiMesh*> mMeshes;
  110. };
  111. } // end of namespace Assimp
  112. #endif // AI_COLLADALOADER_H_INC