XFileImporter.cpp 25 KB

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