XFileImporter.cpp 25 KB

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