XFileImporter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2010, 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 following
  9. 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. /** @file XFileImporter.cpp
  35. * @brief Implementation of the XFile importer class
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_X_IMPORTER
  39. #include "XFileImporter.h"
  40. #include "XFileParser.h"
  41. #include "ConvertToLHProcess.h"
  42. using namespace Assimp;
  43. // ------------------------------------------------------------------------------------------------
  44. // Constructor to be privately used by Importer
  45. XFileImporter::XFileImporter()
  46. {}
  47. // ------------------------------------------------------------------------------------------------
  48. // Destructor, private as well
  49. XFileImporter::~XFileImporter()
  50. {}
  51. // ------------------------------------------------------------------------------------------------
  52. // Returns whether the class can handle the format of the given file.
  53. bool XFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  54. {
  55. std::string extension = GetExtension(pFile);
  56. if(extension == "x") {
  57. return true;
  58. }
  59. if (!extension.length() || checkSig) {
  60. uint32_t token[1];
  61. token[0] = AI_MAKE_MAGIC("xof ");
  62. return CheckMagicToken(pIOHandler,pFile,token,1,0);
  63. }
  64. return false;
  65. }
  66. // ------------------------------------------------------------------------------------------------
  67. void XFileImporter::GetExtensionList(std::set<std::string>& extensions)
  68. {
  69. extensions.insert("x");
  70. }
  71. // ------------------------------------------------------------------------------------------------
  72. // Imports the given file into the given scene structure.
  73. void XFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  74. {
  75. // read file into memory
  76. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  77. if( file.get() == NULL)
  78. throw DeadlyImportError( "Failed to open file " + pFile + ".");
  79. size_t fileSize = file->FileSize();
  80. if( fileSize < 16)
  81. throw DeadlyImportError( "XFile is too small.");
  82. // need to clear members - this method might be called multiple
  83. // times on a single XFileImporter instance.
  84. mImportedMats.clear();
  85. // in the hope that binary files will never start with a BOM ...
  86. mBuffer.resize( fileSize + 1);
  87. file->Read( &mBuffer.front(), 1, fileSize);
  88. ConvertToUTF8(mBuffer);
  89. // parse the file into a temporary representation
  90. XFileParser parser( mBuffer);
  91. // and create the proper return structures out of it
  92. CreateDataRepresentationFromImport( pScene, parser.GetImportedData());
  93. // if nothing came from it, report it as error
  94. if( !pScene->mRootNode)
  95. throw DeadlyImportError( "XFile is ill-formatted - no content imported.");
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // Constructs the return data structure out of the imported data.
  99. void XFileImporter::CreateDataRepresentationFromImport( aiScene* pScene, const XFile::Scene* pData)
  100. {
  101. // Read the global materials first so that meshes referring to them can find them later
  102. ConvertMaterials( pScene, pData->mGlobalMaterials);
  103. // copy nodes, extracting meshes and materials on the way
  104. pScene->mRootNode = CreateNodes( pScene, NULL, pData->mRootNode);
  105. // extract animations
  106. CreateAnimations( pScene, pData);
  107. // read the global meshes that were stored outside of any node
  108. if( pData->mGlobalMeshes.size() > 0)
  109. {
  110. // create a root node to hold them if there isn't any, yet
  111. if( pScene->mRootNode == NULL)
  112. {
  113. pScene->mRootNode = new aiNode;
  114. pScene->mRootNode->mName.Set( "$dummy_node");
  115. }
  116. // convert all global meshes and store them in the root node.
  117. // If there was one before, the global meshes now suddenly have its transformation matrix...
  118. // Don't know what to do there, I don't want to insert another node under the present root node
  119. // just to avoid this.
  120. CreateMeshes( pScene, pScene->mRootNode, pData->mGlobalMeshes);
  121. }
  122. // Convert everything to OpenGL space... it's the same operation as the conversion back, so we can reuse the step directly
  123. MakeLeftHandedProcess convertProcess;
  124. convertProcess.Execute( pScene);
  125. FlipWindingOrderProcess flipper;
  126. flipper.Execute(pScene);
  127. // finally: create a dummy material if not material was imported
  128. if( pScene->mNumMaterials == 0)
  129. {
  130. pScene->mNumMaterials = 1;
  131. // create the Material
  132. Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
  133. int shadeMode = (int) aiShadingMode_Gouraud;
  134. mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
  135. // material colours
  136. int specExp = 1;
  137. aiColor3D clr = aiColor3D( 0, 0, 0);
  138. mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_EMISSIVE);
  139. mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_SPECULAR);
  140. clr = aiColor3D( 0.5f, 0.5f, 0.5f);
  141. mat->AddProperty( &clr, 1, AI_MATKEY_COLOR_DIFFUSE);
  142. mat->AddProperty( &specExp, 1, AI_MATKEY_SHININESS);
  143. pScene->mMaterials = new aiMaterial*[1];
  144. pScene->mMaterials[0] = mat;
  145. }
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. // Recursively creates scene nodes from the imported hierarchy.
  149. aiNode* XFileImporter::CreateNodes( aiScene* pScene, aiNode* pParent, const XFile::Node* pNode)
  150. {
  151. if( !pNode)
  152. return NULL;
  153. // create node
  154. aiNode* node = new aiNode;
  155. node->mName.length = pNode->mName.length();
  156. node->mParent = pParent;
  157. memcpy( node->mName.data, pNode->mName.c_str(), pNode->mName.length());
  158. node->mName.data[node->mName.length] = 0;
  159. node->mTransformation = pNode->mTrafoMatrix;
  160. // convert meshes from the source node
  161. CreateMeshes( pScene, node, pNode->mMeshes);
  162. // handle childs
  163. if( pNode->mChildren.size() > 0)
  164. {
  165. node->mNumChildren = (unsigned int)pNode->mChildren.size();
  166. node->mChildren = new aiNode* [node->mNumChildren];
  167. for( unsigned int a = 0; a < pNode->mChildren.size(); a++)
  168. node->mChildren[a] = CreateNodes( pScene, node, pNode->mChildren[a]);
  169. }
  170. return node;
  171. }
  172. // ------------------------------------------------------------------------------------------------
  173. // Creates the meshes for the given node.
  174. void XFileImporter::CreateMeshes( aiScene* pScene, aiNode* pNode, const std::vector<XFile::Mesh*>& pMeshes)
  175. {
  176. if( pMeshes.size() == 0)
  177. return;
  178. // create a mesh for each mesh-material combination in the source node
  179. std::vector<aiMesh*> meshes;
  180. for( unsigned int a = 0; a < pMeshes.size(); a++)
  181. {
  182. const XFile::Mesh* sourceMesh = pMeshes[a];
  183. // first convert its materials so that we can find them when searching by name afterwards
  184. ConvertMaterials( pScene, sourceMesh->mMaterials);
  185. unsigned int numMaterials = std::max( (unsigned int)sourceMesh->mMaterials.size(), 1u);
  186. for( unsigned int b = 0; b < numMaterials; b++)
  187. {
  188. // collect the faces belonging to this material
  189. std::vector<unsigned int> faces;
  190. unsigned int numVertices = 0;
  191. if( sourceMesh->mFaceMaterials.size() > 0)
  192. {
  193. // if there is a per-face material defined, select the faces with the corresponding material
  194. for( unsigned int c = 0; c < sourceMesh->mFaceMaterials.size(); c++)
  195. {
  196. if( sourceMesh->mFaceMaterials[c] == b)
  197. {
  198. faces.push_back( c);
  199. numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
  200. }
  201. }
  202. } else
  203. {
  204. // if there is no per-face material, place everything into one mesh
  205. for( unsigned int c = 0; c < sourceMesh->mPosFaces.size(); c++)
  206. {
  207. faces.push_back( c);
  208. numVertices += (unsigned int)sourceMesh->mPosFaces[c].mIndices.size();
  209. }
  210. }
  211. // no faces/vertices using this material? strange...
  212. if( numVertices == 0)
  213. continue;
  214. // create a submesh using this material
  215. aiMesh* mesh = new aiMesh;
  216. meshes.push_back( mesh);
  217. // find the material by name in the scene's material list. Either own material
  218. // or referenced material, it should already be found there
  219. if( sourceMesh->mFaceMaterials.size() > 0)
  220. {
  221. std::map<std::string, unsigned int>::const_iterator matIt = mImportedMats.find( sourceMesh->mMaterials[b].mName);
  222. if( matIt == mImportedMats.end())
  223. mesh->mMaterialIndex = 0;
  224. else
  225. mesh->mMaterialIndex = matIt->second;
  226. } else
  227. {
  228. mesh->mMaterialIndex = 0;
  229. }
  230. // Create properly sized data arrays in the mesh. We store unique vertices per face,
  231. // as specified
  232. mesh->mNumVertices = numVertices;
  233. mesh->mVertices = new aiVector3D[numVertices];
  234. mesh->mNumFaces = (unsigned int)faces.size();
  235. mesh->mFaces = new aiFace[mesh->mNumFaces];
  236. // normals?
  237. if( sourceMesh->mNormals.size() > 0)
  238. mesh->mNormals = new aiVector3D[numVertices];
  239. // texture coords
  240. for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_TEXTURECOORDS; c++)
  241. {
  242. if( sourceMesh->mTexCoords[c].size() > 0)
  243. mesh->mTextureCoords[c] = new aiVector3D[numVertices];
  244. }
  245. // vertex colors
  246. for( unsigned int c = 0; c < AI_MAX_NUMBER_OF_COLOR_SETS; c++)
  247. {
  248. if( sourceMesh->mColors[c].size() > 0)
  249. mesh->mColors[c] = new aiColor4D[numVertices];
  250. }
  251. // now collect the vertex data of all data streams present in the imported mesh
  252. unsigned int newIndex = 0;
  253. std::vector<unsigned int> orgPoints; // from which original point each new vertex stems
  254. orgPoints.resize( numVertices, 0);
  255. for( unsigned int c = 0; c < faces.size(); c++)
  256. {
  257. unsigned int f = faces[c]; // index of the source face
  258. const XFile::Face& pf = sourceMesh->mPosFaces[f]; // position source face
  259. // create face. either triangle or triangle fan depending on the index count
  260. aiFace& df = mesh->mFaces[c]; // destination face
  261. df.mNumIndices = (unsigned int)pf.mIndices.size();
  262. df.mIndices = new unsigned int[ df.mNumIndices];
  263. // collect vertex data for indices of this face
  264. for( unsigned int d = 0; d < df.mNumIndices; d++)
  265. {
  266. df.mIndices[d] = newIndex;
  267. orgPoints[newIndex] = pf.mIndices[d];
  268. // Position
  269. mesh->mVertices[newIndex] = sourceMesh->mPositions[pf.mIndices[d]];
  270. // Normal, if present
  271. if( mesh->HasNormals())
  272. mesh->mNormals[newIndex] = sourceMesh->mNormals[sourceMesh->mNormFaces[f].mIndices[d]];
  273. // texture coord sets
  274. for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_TEXTURECOORDS; e++)
  275. {
  276. if( mesh->HasTextureCoords( e))
  277. {
  278. aiVector2D tex = sourceMesh->mTexCoords[e][pf.mIndices[d]];
  279. mesh->mTextureCoords[e][newIndex] = aiVector3D( tex.x, 1.0f - tex.y, 0.0f);
  280. }
  281. }
  282. // vertex color sets
  283. for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_COLOR_SETS; e++)
  284. if( mesh->HasVertexColors( e))
  285. mesh->mColors[e][newIndex] = sourceMesh->mColors[e][pf.mIndices[d]];
  286. newIndex++;
  287. }
  288. }
  289. // there should be as much new vertices as we calculated before
  290. assert( newIndex == numVertices);
  291. // convert all bones of the source mesh which influence vertices in this newly created mesh
  292. const std::vector<XFile::Bone>& bones = sourceMesh->mBones;
  293. std::vector<aiBone*> newBones;
  294. for( unsigned int c = 0; c < bones.size(); c++)
  295. {
  296. const XFile::Bone& obone = bones[c];
  297. // set up a vertex-linear array of the weights for quick searching if a bone influences a vertex
  298. std::vector<float> oldWeights( sourceMesh->mPositions.size(), 0.0f);
  299. for( unsigned int d = 0; d < obone.mWeights.size(); d++)
  300. oldWeights[obone.mWeights[d].mVertex] = obone.mWeights[d].mWeight;
  301. // collect all vertex weights that influence a vertex in the new mesh
  302. std::vector<aiVertexWeight> newWeights;
  303. newWeights.reserve( numVertices);
  304. for( unsigned int d = 0; d < orgPoints.size(); d++)
  305. {
  306. // does the new vertex stem from an old vertex which was influenced by this bone?
  307. float w = oldWeights[orgPoints[d]];
  308. if( w > 0.0f)
  309. newWeights.push_back( aiVertexWeight( d, w));
  310. }
  311. // if the bone has no weights in the newly created mesh, ignore it
  312. if( newWeights.size() == 0)
  313. continue;
  314. // create
  315. aiBone* nbone = new aiBone;
  316. newBones.push_back( nbone);
  317. // copy name and matrix
  318. nbone->mName.Set( obone.mName);
  319. nbone->mOffsetMatrix = obone.mOffsetMatrix;
  320. nbone->mNumWeights = (unsigned int)newWeights.size();
  321. nbone->mWeights = new aiVertexWeight[nbone->mNumWeights];
  322. for( unsigned int d = 0; d < newWeights.size(); d++)
  323. nbone->mWeights[d] = newWeights[d];
  324. }
  325. // store the bones in the mesh
  326. mesh->mNumBones = (unsigned int)newBones.size();
  327. if( newBones.size() > 0)
  328. {
  329. mesh->mBones = new aiBone*[mesh->mNumBones];
  330. std::copy( newBones.begin(), newBones.end(), mesh->mBones);
  331. }
  332. }
  333. }
  334. // reallocate scene mesh array to be large enough
  335. aiMesh** prevArray = pScene->mMeshes;
  336. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes + meshes.size()];
  337. if( prevArray)
  338. {
  339. memcpy( pScene->mMeshes, prevArray, pScene->mNumMeshes * sizeof( aiMesh*));
  340. delete [] prevArray;
  341. }
  342. // allocate mesh index array in the node
  343. pNode->mNumMeshes = (unsigned int)meshes.size();
  344. pNode->mMeshes = new unsigned int[pNode->mNumMeshes];
  345. // store all meshes in the mesh library of the scene and store their indices in the node
  346. for( unsigned int a = 0; a < meshes.size(); a++)
  347. {
  348. pScene->mMeshes[pScene->mNumMeshes] = meshes[a];
  349. pNode->mMeshes[a] = pScene->mNumMeshes;
  350. pScene->mNumMeshes++;
  351. }
  352. }
  353. // ------------------------------------------------------------------------------------------------
  354. // Converts the animations from the given imported data and creates them in the scene.
  355. void XFileImporter::CreateAnimations( aiScene* pScene, const XFile::Scene* pData)
  356. {
  357. std::vector<aiAnimation*> newAnims;
  358. for( unsigned int a = 0; a < pData->mAnims.size(); a++)
  359. {
  360. const XFile::Animation* anim = pData->mAnims[a];
  361. // some exporters mock me with empty animation tags.
  362. if( anim->mAnims.size() == 0)
  363. continue;
  364. // create a new animation to hold the data
  365. aiAnimation* nanim = new aiAnimation;
  366. newAnims.push_back( nanim);
  367. nanim->mName.Set( anim->mName);
  368. // duration will be determined by the maximum length
  369. nanim->mDuration = 0;
  370. nanim->mTicksPerSecond = pData->mAnimTicksPerSecond;
  371. nanim->mNumChannels = (unsigned int)anim->mAnims.size();
  372. nanim->mChannels = new aiNodeAnim*[nanim->mNumChannels];
  373. for( unsigned int b = 0; b < anim->mAnims.size(); b++)
  374. {
  375. const XFile::AnimBone* bone = anim->mAnims[b];
  376. aiNodeAnim* nbone = new aiNodeAnim;
  377. nbone->mNodeName.Set( bone->mBoneName);
  378. nanim->mChannels[b] = nbone;
  379. // keyframes are given as combined transformation matrix keys
  380. if( bone->mTrafoKeys.size() > 0)
  381. {
  382. nbone->mNumPositionKeys = (unsigned int)bone->mTrafoKeys.size();
  383. nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
  384. nbone->mNumRotationKeys = (unsigned int)bone->mTrafoKeys.size();
  385. nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
  386. nbone->mNumScalingKeys = (unsigned int)bone->mTrafoKeys.size();
  387. nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
  388. for( unsigned int c = 0; c < bone->mTrafoKeys.size(); c++)
  389. {
  390. // deconstruct each matrix into separate position, rotation and scaling
  391. double time = bone->mTrafoKeys[c].mTime;
  392. aiMatrix4x4 trafo = bone->mTrafoKeys[c].mMatrix;
  393. // extract position
  394. aiVector3D pos( trafo.a4, trafo.b4, trafo.c4);
  395. nbone->mPositionKeys[c].mTime = time;
  396. nbone->mPositionKeys[c].mValue = pos;
  397. // extract scaling
  398. aiVector3D scale;
  399. scale.x = aiVector3D( trafo.a1, trafo.b1, trafo.c1).Length();
  400. scale.y = aiVector3D( trafo.a2, trafo.b2, trafo.c2).Length();
  401. scale.z = aiVector3D( trafo.a3, trafo.b3, trafo.c3).Length();
  402. nbone->mScalingKeys[c].mTime = time;
  403. nbone->mScalingKeys[c].mValue = scale;
  404. // reconstruct rotation matrix without scaling
  405. aiMatrix3x3 rotmat(
  406. trafo.a1 / scale.x, trafo.a2 / scale.y, trafo.a3 / scale.z,
  407. trafo.b1 / scale.x, trafo.b2 / scale.y, trafo.b3 / scale.z,
  408. trafo.c1 / scale.x, trafo.c2 / scale.y, trafo.c3 / scale.z);
  409. // and convert it into a quaternion
  410. nbone->mRotationKeys[c].mTime = time;
  411. nbone->mRotationKeys[c].mValue = aiQuaternion( rotmat);
  412. }
  413. // longest lasting key sequence determines duration
  414. nanim->mDuration = std::max( nanim->mDuration, bone->mTrafoKeys.back().mTime);
  415. } else
  416. {
  417. // separate key sequences for position, rotation, scaling
  418. nbone->mNumPositionKeys = (unsigned int)bone->mPosKeys.size();
  419. nbone->mPositionKeys = new aiVectorKey[nbone->mNumPositionKeys];
  420. for( unsigned int c = 0; c < nbone->mNumPositionKeys; c++)
  421. {
  422. aiVector3D pos = bone->mPosKeys[c].mValue;
  423. nbone->mPositionKeys[c].mTime = bone->mPosKeys[c].mTime;
  424. nbone->mPositionKeys[c].mValue = pos;
  425. }
  426. // rotation
  427. nbone->mNumRotationKeys = (unsigned int)bone->mRotKeys.size();
  428. nbone->mRotationKeys = new aiQuatKey[nbone->mNumRotationKeys];
  429. for( unsigned int c = 0; c < nbone->mNumRotationKeys; c++)
  430. {
  431. aiMatrix3x3 rotmat = bone->mRotKeys[c].mValue.GetMatrix();
  432. nbone->mRotationKeys[c].mTime = bone->mRotKeys[c].mTime;
  433. nbone->mRotationKeys[c].mValue = aiQuaternion( rotmat);
  434. nbone->mRotationKeys[c].mValue.w *= -1.0f; // needs quat inversion
  435. }
  436. // scaling
  437. nbone->mNumScalingKeys = (unsigned int)bone->mScaleKeys.size();
  438. nbone->mScalingKeys = new aiVectorKey[nbone->mNumScalingKeys];
  439. for( unsigned int c = 0; c < nbone->mNumScalingKeys; c++)
  440. nbone->mScalingKeys[c] = bone->mScaleKeys[c];
  441. // longest lasting key sequence determines duration
  442. if( bone->mPosKeys.size() > 0)
  443. nanim->mDuration = std::max( nanim->mDuration, bone->mPosKeys.back().mTime);
  444. if( bone->mRotKeys.size() > 0)
  445. nanim->mDuration = std::max( nanim->mDuration, bone->mRotKeys.back().mTime);
  446. if( bone->mScaleKeys.size() > 0)
  447. nanim->mDuration = std::max( nanim->mDuration, bone->mScaleKeys.back().mTime);
  448. }
  449. }
  450. }
  451. // store all converted animations in the scene
  452. if( newAnims.size() > 0)
  453. {
  454. pScene->mNumAnimations = (unsigned int)newAnims.size();
  455. pScene->mAnimations = new aiAnimation* [pScene->mNumAnimations];
  456. for( unsigned int a = 0; a < newAnims.size(); a++)
  457. pScene->mAnimations[a] = newAnims[a];
  458. }
  459. }
  460. // ------------------------------------------------------------------------------------------------
  461. // Converts all materials in the given array and stores them in the scene's material list.
  462. void XFileImporter::ConvertMaterials( aiScene* pScene, const std::vector<XFile::Material>& pMaterials)
  463. {
  464. // count the non-referrer materials in the array
  465. unsigned int numMaterials = 0;
  466. for( unsigned int a = 0; a < pMaterials.size(); a++)
  467. if( !pMaterials[a].mIsReference)
  468. numMaterials++;
  469. if( numMaterials == 0)
  470. return;
  471. // resize the scene's material list to offer enough space for the new materials
  472. aiMaterial** prevMats = pScene->mMaterials;
  473. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials + numMaterials];
  474. if( prevMats)
  475. {
  476. memcpy( pScene->mMaterials, prevMats, pScene->mNumMaterials * sizeof( aiMaterial*));
  477. delete [] prevMats;
  478. }
  479. // convert all the materials given in the array
  480. for( unsigned int a = 0; a < pMaterials.size(); a++)
  481. {
  482. const XFile::Material& oldMat = pMaterials[a];
  483. if( oldMat.mIsReference)
  484. continue;
  485. Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
  486. aiString name;
  487. name.Set( oldMat.mName);
  488. mat->AddProperty( &name, AI_MATKEY_NAME);
  489. // Shading model: hardcoded to PHONG, there is no such information in an XFile
  490. // FIX (aramis): If the specular exponent is 0, use gouraud shading. This is a bugfix
  491. // for some models in the SDK (e.g. good old tiny.x)
  492. int shadeMode = (int)oldMat.mSpecularExponent == 0.0f
  493. ? aiShadingMode_Gouraud : aiShadingMode_Phong;
  494. mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL);
  495. // material colours
  496. // FIX: Setup this as ambient not as emissive color
  497. mat->AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_AMBIENT);
  498. mat->AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
  499. mat->AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR);
  500. mat->AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS);
  501. // texture, if there is one
  502. if (1 == oldMat.mTextures.size())
  503. {
  504. const XFile::TexEntry& otex = oldMat.mTextures.back();
  505. if (otex.mName.length())
  506. {
  507. // if there is only one texture assume it contains the diffuse color
  508. aiString tex( otex.mName);
  509. if( otex.mIsNormalMap)
  510. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_NORMALS(0));
  511. else
  512. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(0));
  513. }
  514. }
  515. else
  516. {
  517. // Otherwise ... try to search for typical strings in the
  518. // texture's file name like 'bump' or 'diffuse'
  519. unsigned int iHM = 0,iNM = 0,iDM = 0,iSM = 0,iAM = 0,iEM = 0;
  520. for( unsigned int b = 0; b < oldMat.mTextures.size(); b++)
  521. {
  522. const XFile::TexEntry& otex = oldMat.mTextures[b];
  523. std::string sz = otex.mName;
  524. if (!sz.length())continue;
  525. // find the file name
  526. //const size_t iLen = sz.length();
  527. std::string::size_type s = sz.find_last_of("\\/");
  528. if (std::string::npos == s)
  529. s = 0;
  530. // cut off the file extension
  531. std::string::size_type sExt = sz.find_last_of('.');
  532. if (std::string::npos != sExt){
  533. sz[sExt] = '\0';
  534. }
  535. // convert to lower case for easier comparision
  536. for( unsigned int c = 0; c < sz.length(); c++)
  537. if( isalpha( sz[c]))
  538. sz[c] = tolower( sz[c]);
  539. // Place texture filename property under the corresponding name
  540. aiString tex( oldMat.mTextures[b].mName);
  541. // bump map
  542. if (std::string::npos != sz.find("bump", s) || std::string::npos != sz.find("height", s))
  543. {
  544. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_HEIGHT(iHM++));
  545. } else
  546. if (otex.mIsNormalMap || std::string::npos != sz.find( "normal", s) || std::string::npos != sz.find("nm", s))
  547. {
  548. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_NORMALS(iNM++));
  549. } else
  550. if (std::string::npos != sz.find( "spec", s) || std::string::npos != sz.find( "glanz", s))
  551. {
  552. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_SPECULAR(iSM++));
  553. } else
  554. if (std::string::npos != sz.find( "ambi", s) || std::string::npos != sz.find( "env", s))
  555. {
  556. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_AMBIENT(iAM++));
  557. } else
  558. if (std::string::npos != sz.find( "emissive", s) || std::string::npos != sz.find( "self", s))
  559. {
  560. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_EMISSIVE(iEM++));
  561. } else
  562. {
  563. // Assume it is a diffuse texture
  564. mat->AddProperty( &tex, AI_MATKEY_TEXTURE_DIFFUSE(iDM++));
  565. }
  566. }
  567. }
  568. pScene->mMaterials[pScene->mNumMaterials] = mat;
  569. mImportedMats[oldMat.mName] = pScene->mNumMaterials;
  570. pScene->mNumMaterials++;
  571. }
  572. }
  573. #endif // !! ASSIMP_BUILD_NO_X_IMPORTER