MD5Loader.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 MD5 importer class */
  35. // internal headers
  36. #include "MaterialSystem.h"
  37. #include "RemoveComments.h"
  38. #include "MD5Loader.h"
  39. #include "StringComparison.h"
  40. #include "fast_atof.h"
  41. // public headers
  42. #include "../include/DefaultLogger.h"
  43. #include "../include/IOStream.h"
  44. #include "../include/IOSystem.h"
  45. #include "../include/aiMesh.h"
  46. #include "../include/aiScene.h"
  47. #include "../include/aiAssert.h"
  48. // boost headers
  49. #include <boost/scoped_ptr.hpp>
  50. using namespace Assimp;
  51. // ------------------------------------------------------------------------------------------------
  52. // Constructor to be privately used by Importer
  53. MD5Importer::MD5Importer()
  54. {
  55. // nothing to do here
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Destructor, private as well
  59. MD5Importer::~MD5Importer()
  60. {
  61. // nothing to do here
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Returns whether the class can handle the format of the given file.
  65. bool MD5Importer::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  66. {
  67. // simple check of file extension is enough for the moment
  68. std::string::size_type pos = pFile.find_last_of('.');
  69. // no file extension - can't read
  70. if( pos == std::string::npos)
  71. return false;
  72. std::string extension = pFile.substr( pos);
  73. if (extension.length() < 4)return false;
  74. if (extension[0] != '.')return false;
  75. if (extension[1] != 'm' && extension[1] != 'M')return false;
  76. if (extension[2] != 'd' && extension[2] != 'D')return false;
  77. if (extension[3] != '5')return false;
  78. return true;
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Imports the given file into the given scene structure.
  82. void MD5Importer::InternReadFile(
  83. const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
  84. {
  85. // remove the file extension
  86. std::string::size_type pos = pFile.find_last_of('.');
  87. this->mFile = pFile.substr(0,pos+1);
  88. this->pIOHandler = pIOHandler;
  89. this->pScene = pScene;
  90. bHadMD5Mesh = bHadMD5Anim = false;
  91. // load the animation keyframes
  92. this->LoadMD5AnimFile();
  93. // load the mesh vertices and bones
  94. this->LoadMD5MeshFile();
  95. // make sure we return no incomplete data
  96. if (!bHadMD5Mesh && !bHadMD5Anim)
  97. throw new ImportErrorException("Failed to read valid data from this MD5");
  98. if (!bHadMD5Mesh)pScene->mFlags |= AI_SCENE_FLAGS_ANIM_SKELETON_ONLY;
  99. }
  100. // ------------------------------------------------------------------------------------------------
  101. void MD5Importer::LoadFileIntoMemory (IOStream* file)
  102. {
  103. ai_assert(NULL != file);
  104. this->fileSize = (unsigned int)file->FileSize();
  105. // allocate storage and copy the contents of the file to a memory buffer
  106. this->pScene = pScene;
  107. this->mBuffer = new char[this->fileSize+1];
  108. file->Read( (void*)mBuffer, 1, this->fileSize);
  109. this->iLineNumber = 1;
  110. // append a terminal 0
  111. this->mBuffer[this->fileSize] = '\0';
  112. // now remove all line comments from the file
  113. CommentRemover::RemoveLineComments("//",this->mBuffer,' ');
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. void MD5Importer::UnloadFileFromMemory ()
  117. {
  118. // delete the file buffer
  119. delete[] this->mBuffer;
  120. this->mBuffer = NULL;
  121. this->fileSize = 0;
  122. }
  123. // ------------------------------------------------------------------------------------------------
  124. void MakeDataUnique (MD5::MeshDesc& meshSrc)
  125. {
  126. std::vector<bool> abHad(meshSrc.mVertices.size(),false);
  127. // allocate enough storage to keep the output structures
  128. const unsigned int iNewNum = (unsigned int)meshSrc.mFaces.size()*3;
  129. unsigned int iNewIndex = (unsigned int)meshSrc.mVertices.size();
  130. meshSrc.mVertices.resize(iNewNum);
  131. // try to guess how much storage we'll need for new weights
  132. const float fWeightsPerVert = meshSrc.mWeights.size() / (float)iNewIndex;
  133. const unsigned int guess = (unsigned int)(fWeightsPerVert*iNewNum);
  134. meshSrc.mWeights.reserve(guess + (guess >> 3)); // + 12.5% as buffer
  135. for (FaceList::const_iterator iter = meshSrc.mFaces.begin(),iterEnd = meshSrc.mFaces.end();
  136. iter != iterEnd;++iter)
  137. {
  138. const aiFace& face = *iter;
  139. for (unsigned int i = 0; i < 3;++i)
  140. {
  141. if (abHad[face.mIndices[i]])
  142. {
  143. // generate a new vertex
  144. meshSrc.mVertices[iNewIndex] = meshSrc.mVertices[face.mIndices[i]];
  145. face.mIndices[i] = iNewIndex++;
  146. // FIX: removed this ...
  147. #if 0
  148. // the algorithm in MD5Importer::LoadMD5MeshFile() doesn't work if
  149. // a weight is referenced by more than one vertex. This shouldn't
  150. // occur in MD5 files, but we must take care that we generate new
  151. // weights now, too.
  152. vertNew.mFirstWeight = (unsigned int)meshSrc.mWeights.size();
  153. meshSrc.mWeights.resize(vertNew.mFirstWeight+vertNew.mNumWeights);
  154. for (unsigned int q = 0; q < vertNew.mNumWeights;++q)
  155. {
  156. meshSrc.mWeights[vertNew.mFirstWeight+q] = meshSrc.mWeights[vertOld.mFirstWeight+q];
  157. }
  158. #endif
  159. }
  160. else abHad[face.mIndices[i]] = true;
  161. }
  162. }
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. void AttachChilds(int iParentID,aiNode* piParent,BoneList& bones)
  166. {
  167. ai_assert(NULL != piParent && !piParent->mNumChildren);
  168. for (int i = 0; i < (int)bones.size();++i)
  169. {
  170. // (avoid infinite recursion)
  171. if (iParentID != i && bones[i].mParentIndex == iParentID)
  172. {
  173. // have it ...
  174. ++piParent->mNumChildren;
  175. }
  176. }
  177. if (piParent->mNumChildren)
  178. {
  179. piParent->mChildren = new aiNode*[piParent->mNumChildren];
  180. for (int i = 0; i < (int)bones.size();++i)
  181. {
  182. // (avoid infinite recursion)
  183. if (iParentID != i && bones[i].mParentIndex == iParentID)
  184. {
  185. aiNode* pc;
  186. *piParent->mChildren++ = pc = new aiNode();
  187. pc->mName = aiString(bones[i].mName);
  188. pc->mParent = piParent;
  189. // get the transformation matrix from rotation and translational components
  190. aiQuaternion quat = aiQuaternion ( bones[i].mRotationQuat );
  191. //quat.w *= -1.0f; // DX to OGL
  192. pc->mTransformation = aiMatrix4x4 ( quat.GetMatrix());
  193. aiMatrix4x4 mTranslate;
  194. mTranslate.a4 = bones[i].mPositionXYZ.x;
  195. mTranslate.b4 = bones[i].mPositionXYZ.y;
  196. mTranslate.c4 = bones[i].mPositionXYZ.z;
  197. pc->mTransformation = pc->mTransformation*mTranslate;
  198. // store it for later use
  199. bones[i].mTransform = bones[i].mInvTransform = pc->mTransformation;
  200. bones[i].mInvTransform.Inverse();
  201. // the transformations for each bone are absolute,
  202. // so we need to multiply them with the inverse
  203. // of the absolut matrix of the parent
  204. if (-1 != iParentID)
  205. {
  206. pc->mTransformation = bones[iParentID].mInvTransform*pc->mTransformation;
  207. }
  208. // add children to this node, too
  209. AttachChilds( i, pc, bones);
  210. }
  211. }
  212. // undo our nice shift
  213. piParent->mChildren -= piParent->mNumChildren;
  214. }
  215. }
  216. // ------------------------------------------------------------------------------------------------
  217. void MD5Importer::LoadMD5MeshFile ()
  218. {
  219. std::string pFile = this->mFile + "MD5MESH";
  220. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  221. // Check whether we can read from the file
  222. if( file.get() == NULL)
  223. {
  224. DefaultLogger::get()->warn("Failed to read MD5 mesh file: " + pFile);
  225. return;
  226. }
  227. bHadMD5Mesh = true;
  228. // now load the file into memory
  229. this->LoadFileIntoMemory(file.get());
  230. // now construct a parser and parse the file
  231. MD5::MD5Parser parser(mBuffer,fileSize);
  232. // load the mesh information from it
  233. MD5::MD5MeshParser meshParser(parser.mSections);
  234. // create the bone hierarchy - first the root node
  235. // and dummy nodes for all meshes
  236. pScene->mRootNode = new aiNode();
  237. pScene->mRootNode->mNumChildren = 2;
  238. pScene->mRootNode->mChildren = new aiNode*[2];
  239. // now create the hierarchy of animated bones
  240. aiNode* pcNode = pScene->mRootNode->mChildren[1] = new aiNode();
  241. pcNode->mName.Set("MD5Anim");
  242. pcNode->mParent = pScene->mRootNode;
  243. AttachChilds(-1,pcNode,meshParser.mJoints);
  244. pcNode = pScene->mRootNode->mChildren[0] = new aiNode();
  245. pcNode->mName.Set("MD5Mesh");
  246. pcNode->mParent = pScene->mRootNode;
  247. std::vector<MD5::MeshDesc>::const_iterator end = meshParser.mMeshes.end();
  248. // FIX: MD5 files exported from Blender can have empty meshes
  249. for (std::vector<MD5::MeshDesc>::const_iterator
  250. it = meshParser.mMeshes.begin(),
  251. end = meshParser.mMeshes.end(); it != end;++it)
  252. {
  253. if (!(*it).mFaces.empty() && !(*it).mVertices.empty())
  254. ++pScene->mNumMaterials;
  255. }
  256. // generate all meshes
  257. pScene->mNumMeshes = pScene->mNumMaterials;
  258. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  259. pScene->mMaterials = new aiMaterial*[pScene->mNumMeshes];
  260. // storage for node mesh indices
  261. pcNode->mNumMeshes = pScene->mNumMeshes;
  262. pcNode->mMeshes = new unsigned int[pcNode->mNumMeshes];
  263. for (unsigned int m = 0; m < pcNode->mNumMeshes;++m)
  264. pcNode->mMeshes[m] = m;
  265. unsigned int n = 0;
  266. for (std::vector<MD5::MeshDesc>::iterator
  267. it = meshParser.mMeshes.begin(),
  268. end = meshParser.mMeshes.end(); it != end;++it)
  269. {
  270. MD5::MeshDesc& meshSrc = *it;
  271. if (meshSrc.mFaces.empty() || meshSrc.mVertices.empty())
  272. continue;
  273. aiMesh* mesh = pScene->mMeshes[n] = new aiMesh();
  274. // generate unique vertices in our internal verbose format
  275. MakeDataUnique(meshSrc);
  276. mesh->mNumVertices = (unsigned int) meshSrc.mVertices.size();
  277. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  278. mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  279. mesh->mNumUVComponents[0] = 2;
  280. // copy texture coordinates
  281. aiVector3D* pv = mesh->mTextureCoords[0];
  282. for (MD5::VertexList::const_iterator
  283. iter = meshSrc.mVertices.begin();
  284. iter != meshSrc.mVertices.end();++iter,++pv)
  285. {
  286. pv->x = (*iter).mUV.x;
  287. pv->y = 1.0f-(*iter).mUV.y; // D3D to OpenGL
  288. pv->z = 0.0f;
  289. }
  290. // sort all bone weights - per bone
  291. unsigned int* piCount = new unsigned int[meshParser.mJoints.size()];
  292. ::memset(piCount,0,sizeof(unsigned int)*meshParser.mJoints.size());
  293. for (MD5::VertexList::const_iterator
  294. iter = meshSrc.mVertices.begin();
  295. iter != meshSrc.mVertices.end();++iter,++pv)
  296. {
  297. for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w)
  298. {
  299. MD5::WeightDesc& desc = meshSrc.mWeights[w];
  300. ++piCount[desc.mBone];
  301. }
  302. }
  303. // check how many we will need
  304. for (unsigned int p = 0; p < meshParser.mJoints.size();++p)
  305. if (piCount[p])mesh->mNumBones++;
  306. if (mesh->mNumBones) // just for safety
  307. {
  308. mesh->mBones = new aiBone*[mesh->mNumBones];
  309. for (unsigned int q = 0,h = 0; q < meshParser.mJoints.size();++q)
  310. {
  311. if (!piCount[q])continue;
  312. aiBone* p = mesh->mBones[h] = new aiBone();
  313. p->mNumWeights = piCount[q];
  314. p->mWeights = new aiVertexWeight[p->mNumWeights];
  315. p->mName = aiString(meshParser.mJoints[q].mName);
  316. // store the index for later use
  317. meshParser.mJoints[q].mMap = h++;
  318. }
  319. //unsigned int g = 0;
  320. pv = mesh->mVertices;
  321. for (MD5::VertexList::const_iterator
  322. iter = meshSrc.mVertices.begin();
  323. iter != meshSrc.mVertices.end();++iter,++pv)
  324. {
  325. // compute the final vertex position from all single weights
  326. *pv = aiVector3D();
  327. // there are models which have weights which don't sum to 1 ...
  328. // granite.md5mesh for example
  329. float fSum = 0.0f;
  330. for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w)
  331. fSum += meshSrc.mWeights[w].mWeight;
  332. if (!fSum)throw new ImportErrorException("The sum of all vertex bone weights is 0");
  333. // process bone weights
  334. for (unsigned int jub = (*iter).mFirstWeight, w = jub; w < jub + (*iter).mNumWeights;++w)
  335. {
  336. MD5::WeightDesc& desc = meshSrc.mWeights[w];
  337. float fNewWeight = desc.mWeight / fSum;
  338. // transform the local position into worldspace
  339. MD5::BoneDesc& boneSrc = meshParser.mJoints[desc.mBone];
  340. aiVector3D v = desc.vOffsetPosition;
  341. aiQuaternion quat = aiQuaternion( boneSrc.mRotationQuat );
  342. //quat.w *= -1.0f;
  343. v = quat.GetMatrix() * v;
  344. v += boneSrc.mPositionXYZ;
  345. // use the original weight to compute the vertex position
  346. // (some MD5s seem to depend on the invalid weight values ...)
  347. pv->operator +=(v * desc.mWeight);
  348. aiBone* bone = mesh->mBones[boneSrc.mMap];
  349. *bone->mWeights++ = aiVertexWeight((unsigned int)(pv-mesh->mVertices),fNewWeight);
  350. }
  351. // convert from DOOM coordinate system to OGL
  352. std::swap((float&)pv->z,(float&)pv->y);
  353. }
  354. // undo our nice offset tricks ...
  355. for (unsigned int p = 0; p < mesh->mNumBones;++p)
  356. mesh->mBones[p]->mWeights -= mesh->mBones[p]->mNumWeights;
  357. }
  358. delete[] piCount;
  359. // now setup all faces - we can directly copy the list
  360. // (however, take care that the aiFace destructor doesn't delete the mIndices array)
  361. mesh->mNumFaces = (unsigned int)meshSrc.mFaces.size();
  362. mesh->mFaces = new aiFace[mesh->mNumFaces];
  363. for (unsigned int c = 0; c < mesh->mNumFaces;++c)
  364. {
  365. mesh->mFaces[c].mNumIndices = 3;
  366. mesh->mFaces[c].mIndices = meshSrc.mFaces[c].mIndices;
  367. meshSrc.mFaces[c].mIndices = NULL;
  368. }
  369. // generate a material for the mesh
  370. MaterialHelper* mat = new MaterialHelper();
  371. pScene->mMaterials[n] = mat;
  372. mat->AddProperty(&meshSrc.mShader,AI_MATKEY_TEXTURE_DIFFUSE(0));
  373. mesh->mMaterialIndex = n++;
  374. }
  375. // delete the file again
  376. this->UnloadFileFromMemory();
  377. }
  378. // ------------------------------------------------------------------------------------------------
  379. void MD5Importer::LoadMD5AnimFile ()
  380. {
  381. std::string pFile = this->mFile + "MD5ANIM";
  382. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  383. // Check whether we can read from the file
  384. if( file.get() == NULL)
  385. {
  386. DefaultLogger::get()->warn("Failed to read MD5 anim file: " + pFile);
  387. return;
  388. }
  389. bHadMD5Anim = true;
  390. // now load the file into memory
  391. this->LoadFileIntoMemory(file.get());
  392. // now construct a parser and parse the file
  393. MD5::MD5Parser parser(mBuffer,fileSize);
  394. // load the animation information from it
  395. MD5::MD5AnimParser animParser(parser.mSections);
  396. // generate and fill the output animation
  397. if (!animParser.mAnimatedBones.empty())
  398. {
  399. pScene->mNumAnimations = 1;
  400. pScene->mAnimations = new aiAnimation*[1];
  401. aiAnimation* anim = pScene->mAnimations[0] = new aiAnimation();
  402. anim->mNumBones = (unsigned int)animParser.mAnimatedBones.size();
  403. anim->mBones = new aiBoneAnim*[anim->mNumBones];
  404. for (unsigned int i = 0; i < anim->mNumBones;++i)
  405. {
  406. aiBoneAnim* bone = anim->mBones[i] = new aiBoneAnim();
  407. bone->mBoneName = aiString( animParser.mAnimatedBones[i].mName );
  408. // allocate storage for the keyframes
  409. bone->mNumPositionKeys = bone->mNumRotationKeys = (unsigned int)animParser.mFrames.size();
  410. bone->mPositionKeys = new aiVectorKey[bone->mNumPositionKeys];
  411. bone->mRotationKeys = new aiQuatKey[bone->mNumPositionKeys];
  412. }
  413. // 1 tick == 1 frame
  414. anim->mTicksPerSecond = animParser.fFrameRate;
  415. for (FrameList::const_iterator iter = animParser.mFrames.begin(),
  416. iterEnd = animParser.mFrames.end();iter != iterEnd;++iter)
  417. {
  418. double dTime = (double)(*iter).iIndex;
  419. if (!(*iter).mValues.empty())
  420. {
  421. // now process all values in there ... read all joints
  422. aiBoneAnim** pcAnimBone = anim->mBones;
  423. MD5::BaseFrameDesc* pcBaseFrame = &animParser.mBaseFrames[0];
  424. for (AnimBoneList::const_iterator
  425. iter2 = animParser.mAnimatedBones.begin(),
  426. iterEnd2 = animParser.mAnimatedBones.end();
  427. iter2 != iterEnd2;++iter2,++pcAnimBone,++pcBaseFrame)
  428. {
  429. if((*iter2).iFirstKeyIndex >= (*iter).mValues.size())
  430. {
  431. // TODO: add proper array checks for all cases here ...
  432. DefaultLogger::get()->error("Keyframe index is out of range. ");
  433. continue;
  434. }
  435. const float* fpCur = &(*iter).mValues[(*iter2).iFirstKeyIndex];
  436. aiBoneAnim* pcCurAnimBone = *pcAnimBone;
  437. aiVectorKey* vKey = pcCurAnimBone->mPositionKeys++;
  438. // translation on the x axis
  439. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_TRANSLATE_X)
  440. vKey->mValue.x = *fpCur++;
  441. else vKey->mValue.x = pcBaseFrame->vPositionXYZ.x;
  442. // translation on the y axis
  443. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_TRANSLATE_Y)
  444. vKey->mValue.y = *fpCur++;
  445. else vKey->mValue.y = pcBaseFrame->vPositionXYZ.y;
  446. // translation on the z axis
  447. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_TRANSLATE_Z)
  448. vKey->mValue.z = *fpCur++;
  449. else vKey->mValue.z = pcBaseFrame->vPositionXYZ.z;
  450. // rotation quaternion, x component
  451. aiQuatKey* qKey = pcCurAnimBone->mRotationKeys++;
  452. aiVector3D vTemp;
  453. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_ROTQUAT_X)
  454. vTemp.x = *fpCur++;
  455. else vTemp.x = pcBaseFrame->vRotationQuat.x;
  456. // rotation quaternion, y component
  457. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_ROTQUAT_Y)
  458. vTemp.y = *fpCur++;
  459. else vTemp.y = pcBaseFrame->vRotationQuat.y;
  460. // rotation quaternion, z component
  461. if ((*iter2).iFlags & AI_MD5_ANIMATION_FLAG_ROTQUAT_Z)
  462. vTemp.z = *fpCur++;
  463. else vTemp.z = pcBaseFrame->vRotationQuat.z;
  464. // compute the w component of the quaternion - invert it (DX to OGL)
  465. qKey->mValue = aiQuaternion(vTemp);
  466. //qKey->mValue.w *= -1.0f;
  467. qKey->mTime = dTime;
  468. vKey->mTime = dTime;
  469. }
  470. }
  471. // compute the duration of the animation
  472. anim->mDuration = std::max(dTime,anim->mDuration);
  473. }
  474. // undo our offset computations
  475. for (unsigned int i = 0; i < anim->mNumBones;++i)
  476. {
  477. aiBoneAnim* bone = anim->mBones[i];
  478. bone->mPositionKeys -= bone->mNumPositionKeys;
  479. bone->mRotationKeys -= bone->mNumPositionKeys;
  480. }
  481. }
  482. // delete the file again
  483. this->UnloadFileFromMemory();
  484. }