2
0

ColladaLoader.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // Convert to Z_UP, if different orientation
  76. if( parser.mUpDirection == ColladaParser::UP_X)
  77. pScene->mRootNode->mTransformation *= aiMatrix4x4(
  78. 0, -1, 0, 0,
  79. 0, 0, -1, 0,
  80. 1, 0, 0, 0,
  81. 0, 0, 0, 1);
  82. else if( parser.mUpDirection == ColladaParser::UP_Y)
  83. pScene->mRootNode->mTransformation *= aiMatrix4x4(
  84. 1, 0, 0, 0,
  85. 0, 0, -1, 0,
  86. 0, 1, 0, 0,
  87. 0, 0, 0, 1);
  88. // store all meshes
  89. StoreSceneMeshes( pScene);
  90. // create dummy material
  91. Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
  92. aiString name( std::string( "dummy"));
  93. mat->AddProperty( &name, AI_MATKEY_NAME);
  94. int shadeMode = aiShadingMode_Phong;
  95. mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
  96. 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);
  97. mat->AddProperty( &colAmbient, 1, AI_MATKEY_COLOR_AMBIENT);
  98. mat->AddProperty( &colDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  99. mat->AddProperty( &colSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  100. float specExp = 5.0f;
  101. mat->AddProperty( &specExp, 1, AI_MATKEY_SHININESS);
  102. pScene->mNumMaterials = 1;
  103. pScene->mMaterials = new aiMaterial*[1];
  104. pScene->mMaterials[0] = mat;
  105. }
  106. // ------------------------------------------------------------------------------------------------
  107. // Recursively constructs a scene node for the given parser node and returns it.
  108. aiNode* ColladaLoader::BuildHierarchy( const ColladaParser& pParser, const Collada::Node* pNode)
  109. {
  110. // create a node for it
  111. aiNode* node = new aiNode( pNode->mName);
  112. // calculate the transformation matrix for it
  113. node->mTransformation = pParser.CalculateResultTransform( pNode->mTransforms);
  114. // add children
  115. node->mNumChildren = pNode->mChildren.size();
  116. node->mChildren = new aiNode*[node->mNumChildren];
  117. for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
  118. {
  119. node->mChildren[a] = BuildHierarchy( pParser, pNode->mChildren[a]);
  120. node->mChildren[a]->mParent = node;
  121. }
  122. // construct meshes
  123. BuildMeshesForNode( pParser, pNode, node);
  124. return node;
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. // Builds meshes for the given node and references them
  128. void ColladaLoader::BuildMeshesForNode( const ColladaParser& pParser, const Collada::Node* pNode, aiNode* pTarget)
  129. {
  130. // accumulated mesh references by this node
  131. std::vector<size_t> newMeshRefs;
  132. // for the moment we simply ignore all material tags and transfer the meshes one by one
  133. BOOST_FOREACH( const Collada::MeshInstance& mid, pNode->mMeshes)
  134. {
  135. // find the referred mesh
  136. ColladaParser::MeshLibrary::const_iterator srcMeshIt = pParser.mMeshLibrary.find( mid.mMesh);
  137. if( srcMeshIt == pParser.mMeshLibrary.end())
  138. {
  139. DefaultLogger::get()->warn( boost::str( boost::format( "Unable to find geometry for ID \"%s\". Skipping.") % mid.mMesh));
  140. continue;
  141. }
  142. // if we already have the mesh at the library, just add its index to the node's array
  143. std::map<std::string, size_t>::const_iterator dstMeshIt = mMeshIndexbyID.find( mid.mMesh);
  144. if( dstMeshIt != mMeshIndexbyID.end())
  145. {
  146. newMeshRefs.push_back( dstMeshIt->second);
  147. } else
  148. {
  149. // else we have to add the mesh to the collection and store its newly assigned index at the node
  150. aiMesh* dstMesh = new aiMesh;
  151. const Collada::Mesh* srcMesh = srcMeshIt->second;
  152. // copy positions
  153. dstMesh->mNumVertices = srcMesh->mPositions.size();
  154. dstMesh->mVertices = new aiVector3D[dstMesh->mNumVertices];
  155. std::copy( srcMesh->mPositions.begin(), srcMesh->mPositions.end(), dstMesh->mVertices);
  156. // normals, if given. HACK: (thom) Due to the fucking Collada spec we never know if we have the same
  157. // number of normals as there are positions. So we also ignore any vertex attribute if it has a different count
  158. if( srcMesh->mNormals.size() == dstMesh->mNumVertices)
  159. {
  160. dstMesh->mNormals = new aiVector3D[dstMesh->mNumVertices];
  161. std::copy( srcMesh->mNormals.begin(), srcMesh->mNormals.end(), dstMesh->mNormals);
  162. }
  163. // same for texturecoords, as many as we have
  164. for( size_t a = 0; a < AI_MAX_NUMBER_OF_TEXTURECOORDS; a++)
  165. {
  166. if( srcMesh->mTexCoords[a].size() == dstMesh->mNumVertices)
  167. {
  168. dstMesh->mTextureCoords[a] = new aiVector3D[dstMesh->mNumVertices];
  169. for( size_t b = 0; b < dstMesh->mNumVertices; ++b)
  170. dstMesh->mTextureCoords[a][b].Set( srcMesh->mTexCoords[a][b].x, srcMesh->mTexCoords[a][b].y, 0.0f);
  171. dstMesh->mNumUVComponents[a] = 2;
  172. }
  173. }
  174. // same for vertex colors, as many as we have
  175. for( size_t a = 0; a < AI_MAX_NUMBER_OF_COLOR_SETS; a++)
  176. {
  177. if( srcMesh->mColors[a].size() == dstMesh->mNumVertices)
  178. {
  179. dstMesh->mColors[a] = new aiColor4D[dstMesh->mNumVertices];
  180. std::copy( srcMesh->mColors[a].begin(), srcMesh->mColors[a].end(), dstMesh->mColors[a]);
  181. }
  182. }
  183. // create faces. Due to the fact that each face uses unique vertices, we can simply count up on each vertex
  184. size_t vertex = 0;
  185. dstMesh->mNumFaces = srcMesh->mFaceSize.size();
  186. dstMesh->mFaces = new aiFace[dstMesh->mNumFaces];
  187. for( size_t a = 0; a < dstMesh->mNumFaces; ++a)
  188. {
  189. size_t s = srcMesh->mFaceSize[a];
  190. aiFace& face = dstMesh->mFaces[a];
  191. face.mNumIndices = s;
  192. face.mIndices = new unsigned int[s];
  193. for( size_t b = 0; b < s; ++b)
  194. face.mIndices[b] = vertex++;
  195. }
  196. // store the mesh, and store its new index in the node
  197. newMeshRefs.push_back( mMeshes.size());
  198. mMeshes.push_back( dstMesh);
  199. }
  200. }
  201. // now place all mesh references we gathered in the target node
  202. pTarget->mNumMeshes = newMeshRefs.size();
  203. if( newMeshRefs.size())
  204. {
  205. pTarget->mMeshes = new unsigned int[pTarget->mNumMeshes];
  206. std::copy( newMeshRefs.begin(), newMeshRefs.end(), pTarget->mMeshes);
  207. }
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. // Stores all meshes in the given scene
  211. void ColladaLoader::StoreSceneMeshes( aiScene* pScene)
  212. {
  213. pScene->mNumMeshes = mMeshes.size();
  214. if( mMeshes.size() > 0)
  215. {
  216. pScene->mMeshes = new aiMesh*[mMeshes.size()];
  217. std::copy( mMeshes.begin(), mMeshes.end(), pScene->mMeshes);
  218. }
  219. }