ColladaLoader.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /** Implementation of the Collada loader */
  2. /*
  3. ---------------------------------------------------------------------------
  4. Open Asset Import Library (ASSIMP)
  5. ---------------------------------------------------------------------------
  6. Copyright (c) 2006-2008, ASSIMP Development Team
  7. All rights reserved.
  8. Redistribution and use of this software in source and binary forms,
  9. with or without modification, are permitted provided that the following
  10. conditions are met:
  11. * Redistributions of source code must retain the above
  12. copyright notice, this list of conditions and the
  13. following disclaimer.
  14. * Redistributions in binary form must reproduce the above
  15. copyright notice, this list of conditions and the
  16. following disclaimer in the documentation and/or other
  17. materials provided with the distribution.
  18. * Neither the name of the ASSIMP team, nor the names of its
  19. contributors may be used to endorse or promote products
  20. derived from this software without specific prior
  21. written permission of the ASSIMP Development Team.
  22. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. ---------------------------------------------------------------------------
  34. */
  35. #include "AssimpPCH.h"
  36. #include "../include/aiAnim.h"
  37. #include "ColladaLoader.h"
  38. #include "ColladaParser.h"
  39. using namespace Assimp;
  40. // ------------------------------------------------------------------------------------------------
  41. // Constructor to be privately used by Importer
  42. ColladaLoader::ColladaLoader()
  43. {
  44. }
  45. // ------------------------------------------------------------------------------------------------
  46. // Destructor, private as well
  47. ColladaLoader::~ColladaLoader()
  48. {
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // Returns whether the class can handle the format of the given file.
  52. bool ColladaLoader::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  53. {
  54. // check file extension
  55. std::string::size_type pos = pFile.find_last_of('.');
  56. // no file extension - can't read
  57. if( pos == std::string::npos)
  58. return false;
  59. std::string extension = pFile.substr( pos);
  60. for( std::string::iterator it = extension.begin(); it != extension.end(); ++it)
  61. *it = tolower( *it);
  62. if( extension == ".dae")
  63. return true;
  64. return false;
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. // Imports the given file into the given scene structure.
  68. void ColladaLoader::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  69. {
  70. mFileName = pFile;
  71. // parse the input file
  72. ColladaParser parser( pFile);
  73. // build the node hierarchy from it
  74. pScene->mRootNode = BuildHierarchy( parser, parser.mRootNode);
  75. // store all meshes
  76. StoreSceneMeshes( pScene);
  77. // create dummy material
  78. Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
  79. aiString name( std::string( "dummy"));
  80. mat->AddProperty( &name, AI_MATKEY_NAME);
  81. int shadeMode = aiShadingMode_Phong;
  82. mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
  83. aiColor4D colAmbient( 0.2f, 0.2f, 0.2f, 1.0f), colDiffuse( 0.8f, 0.8f, 0.8f, 1.0f), colSpecular( 0.5f, 0.5f, 0.5f, 0.5f);
  84. mat->AddProperty( &colAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  85. mat->AddProperty( &colDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  86. mat->AddProperty( &colSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  87. float specExp = 5.0f;
  88. mat->AddProperty( &specExp, 1, AI_MATKEY_SHININESS);
  89. pScene->mNumMaterials = 1;
  90. pScene->mMaterials = new aiMaterial*[1];
  91. pScene->mMaterials[0] = mat;
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. // Recursively constructs a scene node for the given parser node and returns it.
  95. aiNode* ColladaLoader::BuildHierarchy( const ColladaParser& pParser, const ColladaParser::Node* pNode)
  96. {
  97. // create a node for it
  98. aiNode* node = new aiNode( pNode->mName);
  99. // calculate the transformation matrix for it
  100. node->mTransformation = pParser.CalculateResultTransform( pNode->mTransforms);
  101. // add children
  102. node->mNumChildren = pNode->mChildren.size();
  103. node->mChildren = new aiNode*[node->mNumChildren];
  104. for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
  105. {
  106. node->mChildren[a] = BuildHierarchy( pParser, pNode->mChildren[a]);
  107. node->mChildren[a]->mParent = node;
  108. }
  109. // construct meshes
  110. BuildMeshesForNode( pParser, pNode, node);
  111. return node;
  112. }
  113. // ------------------------------------------------------------------------------------------------
  114. // Builds meshes for the given node and references them
  115. void ColladaLoader::BuildMeshesForNode( const ColladaParser& pParser, const ColladaParser::Node* pNode, aiNode* pTarget)
  116. {
  117. // accumulated mesh references by this node
  118. std::vector<size_t> newMeshRefs;
  119. // for the moment we simply ignore all material tags and transfer the meshes one by one
  120. BOOST_FOREACH( const std::string& mid, pNode->mMeshes)
  121. {
  122. // find the referred mesh
  123. ColladaParser::MeshLibrary::const_iterator srcMeshIt = pParser.mMeshLibrary.find( mid);
  124. if( srcMeshIt == pParser.mMeshLibrary.end())
  125. {
  126. DefaultLogger::get()->warn( boost::str( boost::format( "Unable to find geometry for ID \"%s\". Skipping.") % mid));
  127. continue;
  128. }
  129. // if we already have the mesh at the library, just add its index to the node's array
  130. std::map<std::string, size_t>::const_iterator dstMeshIt = mMeshIndexbyID.find( mid);
  131. if( dstMeshIt != mMeshIndexbyID.end())
  132. {
  133. newMeshRefs.push_back( dstMeshIt->second);
  134. } else
  135. {
  136. // else we have to add the mesh to the collection and store its newly assigned index at the node
  137. aiMesh* dstMesh = new aiMesh;
  138. const ColladaParser::Mesh* srcMesh = srcMeshIt->second;
  139. // copy positions
  140. dstMesh->mNumVertices = srcMesh->mPositions.size();
  141. dstMesh->mVertices = new aiVector3D[dstMesh->mNumVertices];
  142. std::copy( srcMesh->mPositions.begin(), srcMesh->mPositions.end(), dstMesh->mVertices);
  143. // normals, if given. HACK: (thom) Due to the fucking Collada spec we never know if we have the same
  144. // number of normals as there are positions. So we also ignore any vertex attribute if it has a different count
  145. if( srcMesh->mNormals.size() == dstMesh->mNumVertices)
  146. {
  147. dstMesh->mNormals = new aiVector3D[dstMesh->mNumVertices];
  148. std::copy( srcMesh->mNormals.begin(), srcMesh->mNormals.end(), dstMesh->mNormals);
  149. }
  150. // same for texturecoords, as many as we have
  151. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  152. {
  153. if( srcMesh->mTexCoords[a].size() == dstMesh->mNumVertices)
  154. {
  155. dstMesh->mTextureCoords[a] = new aiVector3D[dstMesh->mNumVertices];
  156. for( size_t b = 0; b < dstMesh->mNumVertices; ++b)
  157. dstMesh->mTextureCoords[a][b].Set( srcMesh->mTexCoords[a][b].x, srcMesh->mTexCoords[a][b].y, 0.0f);
  158. dstMesh->mNumUVComponents[a] = 2;
  159. }
  160. }
  161. // same for vertex colors, as many as we have
  162. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++)
  163. {
  164. if( srcMesh->mColors[a].size() == dstMesh->mNumVertices)
  165. {
  166. dstMesh->mColors[a] = new aiColor4D[dstMesh->mNumVertices];
  167. std::copy( srcMesh->mColors[a].begin(), srcMesh->mColors[a].end(), dstMesh->mColors[a]);
  168. }
  169. }
  170. // create faces. Due to the fact that each face uses unique vertices, we can simply count up on each vertex
  171. size_t vertex = 0;
  172. dstMesh->mNumFaces = srcMesh->mFaceSize.size();
  173. dstMesh->mFaces = new aiFace[dstMesh->mNumFaces];
  174. for( size_t a = 0; a < dstMesh->mNumFaces; ++a)
  175. {
  176. size_t s = srcMesh->mFaceSize[a];
  177. aiFace& face = dstMesh->mFaces[a];
  178. face.mNumIndices = s;
  179. face.mIndices = new unsigned int[s];
  180. for( size_t b = 0; b < s; ++b)
  181. face.mIndices[b] = vertex++;
  182. }
  183. // store the mesh, and store its new index in the node
  184. newMeshRefs.push_back( mMeshes.size());
  185. mMeshes.push_back( dstMesh);
  186. }
  187. }
  188. // now place all mesh references we gathered in the target node
  189. pTarget->mNumMeshes = newMeshRefs.size();
  190. if( newMeshRefs.size())
  191. {
  192. pTarget->mMeshes = new size_t[pTarget->mNumMeshes];
  193. std::copy( newMeshRefs.begin(), newMeshRefs.end(), pTarget->mMeshes);
  194. }
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. // Stores all meshes in the given scene
  198. void ColladaLoader::StoreSceneMeshes( aiScene* pScene)
  199. {
  200. pScene->mNumMeshes = mMeshes.size();
  201. if( mMeshes.size() > 0)
  202. {
  203. pScene->mMeshes = new aiMesh*[mMeshes.size()];
  204. std::copy( mMeshes.begin(), mMeshes.end(), pScene->mMeshes);
  205. }
  206. }