MD5Loader.cpp 19 KB

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