Anim.cpp 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../Game_local.h"
  23. bool idAnimManager::forceExport = false;
  24. /***********************************************************************
  25. idMD5Anim
  26. ***********************************************************************/
  27. /*
  28. ====================
  29. idMD5Anim::idMD5Anim
  30. ====================
  31. */
  32. idMD5Anim::idMD5Anim() {
  33. ref_count = 0;
  34. numFrames = 0;
  35. numJoints = 0;
  36. frameRate = 24;
  37. animLength = 0;
  38. totaldelta.Zero();
  39. }
  40. /*
  41. ====================
  42. idMD5Anim::idMD5Anim
  43. ====================
  44. */
  45. idMD5Anim::~idMD5Anim() {
  46. Free();
  47. }
  48. /*
  49. ====================
  50. idMD5Anim::Free
  51. ====================
  52. */
  53. void idMD5Anim::Free( void ) {
  54. numFrames = 0;
  55. numJoints = 0;
  56. frameRate = 24;
  57. animLength = 0;
  58. name = "";
  59. totaldelta.Zero();
  60. jointInfo.Clear();
  61. bounds.Clear();
  62. componentFrames.Clear();
  63. }
  64. /*
  65. ====================
  66. idMD5Anim::NumFrames
  67. ====================
  68. */
  69. int idMD5Anim::NumFrames( void ) const {
  70. return numFrames;
  71. }
  72. /*
  73. ====================
  74. idMD5Anim::NumJoints
  75. ====================
  76. */
  77. int idMD5Anim::NumJoints( void ) const {
  78. return numJoints;
  79. }
  80. /*
  81. ====================
  82. idMD5Anim::Length
  83. ====================
  84. */
  85. int idMD5Anim::Length( void ) const {
  86. return animLength;
  87. }
  88. /*
  89. =====================
  90. idMD5Anim::TotalMovementDelta
  91. =====================
  92. */
  93. const idVec3 &idMD5Anim::TotalMovementDelta( void ) const {
  94. return totaldelta;
  95. }
  96. /*
  97. =====================
  98. idMD5Anim::TotalMovementDelta
  99. =====================
  100. */
  101. const char *idMD5Anim::Name( void ) const {
  102. return name;
  103. }
  104. /*
  105. ====================
  106. idMD5Anim::Reload
  107. ====================
  108. */
  109. bool idMD5Anim::Reload( void ) {
  110. idStr filename;
  111. filename = name;
  112. Free();
  113. return LoadAnim( filename );
  114. }
  115. /*
  116. ====================
  117. idMD5Anim::Allocated
  118. ====================
  119. */
  120. size_t idMD5Anim::Allocated( void ) const {
  121. size_t size = bounds.Allocated() + jointInfo.Allocated() + componentFrames.Allocated() + name.Allocated();
  122. return size;
  123. }
  124. /*
  125. ====================
  126. idMD5Anim::LoadAnim
  127. ====================
  128. */
  129. bool idMD5Anim::LoadAnim( const char *filename ) {
  130. int version;
  131. idLexer parser( LEXFL_ALLOWPATHNAMES | LEXFL_NOSTRINGESCAPECHARS | LEXFL_NOSTRINGCONCAT );
  132. idToken token;
  133. int i, j;
  134. int num;
  135. if ( !parser.LoadFile( filename ) ) {
  136. return false;
  137. }
  138. Free();
  139. name = filename;
  140. parser.ExpectTokenString( MD5_VERSION_STRING );
  141. version = parser.ParseInt();
  142. if ( version != MD5_VERSION ) {
  143. parser.Error( "Invalid version %d. Should be version %d\n", version, MD5_VERSION );
  144. }
  145. // skip the commandline
  146. parser.ExpectTokenString( "commandline" );
  147. parser.ReadToken( &token );
  148. // parse num frames
  149. parser.ExpectTokenString( "numFrames" );
  150. numFrames = parser.ParseInt();
  151. if ( numFrames <= 0 ) {
  152. parser.Error( "Invalid number of frames: %d", numFrames );
  153. }
  154. // parse num joints
  155. parser.ExpectTokenString( "numJoints" );
  156. numJoints = parser.ParseInt();
  157. if ( numJoints <= 0 ) {
  158. parser.Error( "Invalid number of joints: %d", numJoints );
  159. }
  160. // parse frame rate
  161. parser.ExpectTokenString( "frameRate" );
  162. frameRate = parser.ParseInt();
  163. if ( frameRate < 0 ) {
  164. parser.Error( "Invalid frame rate: %d", frameRate );
  165. }
  166. // parse number of animated components
  167. parser.ExpectTokenString( "numAnimatedComponents" );
  168. numAnimatedComponents = parser.ParseInt();
  169. if ( ( numAnimatedComponents < 0 ) || ( numAnimatedComponents > numJoints * 6 ) ) {
  170. parser.Error( "Invalid number of animated components: %d", numAnimatedComponents );
  171. }
  172. // parse the hierarchy
  173. jointInfo.SetGranularity( 1 );
  174. jointInfo.SetNum( numJoints );
  175. parser.ExpectTokenString( "hierarchy" );
  176. parser.ExpectTokenString( "{" );
  177. for( i = 0; i < numJoints; i++ ) {
  178. parser.ReadToken( &token );
  179. jointInfo[ i ].nameIndex = animationLib.JointIndex( token );
  180. // parse parent num
  181. jointInfo[ i ].parentNum = parser.ParseInt();
  182. if ( jointInfo[ i ].parentNum >= i ) {
  183. parser.Error( "Invalid parent num: %d", jointInfo[ i ].parentNum );
  184. }
  185. if ( ( i != 0 ) && ( jointInfo[ i ].parentNum < 0 ) ) {
  186. parser.Error( "Animations may have only one root joint" );
  187. }
  188. // parse anim bits
  189. jointInfo[ i ].animBits = parser.ParseInt();
  190. if ( jointInfo[ i ].animBits & ~63 ) {
  191. parser.Error( "Invalid anim bits: %d", jointInfo[ i ].animBits );
  192. }
  193. // parse first component
  194. jointInfo[ i ].firstComponent = parser.ParseInt();
  195. if ( ( numAnimatedComponents > 0 ) && ( ( jointInfo[ i ].firstComponent < 0 ) || ( jointInfo[ i ].firstComponent >= numAnimatedComponents ) ) ) {
  196. parser.Error( "Invalid first component: %d", jointInfo[ i ].firstComponent );
  197. }
  198. }
  199. parser.ExpectTokenString( "}" );
  200. // parse bounds
  201. parser.ExpectTokenString( "bounds" );
  202. parser.ExpectTokenString( "{" );
  203. bounds.SetGranularity( 1 );
  204. bounds.SetNum( numFrames );
  205. for( i = 0; i < numFrames; i++ ) {
  206. parser.Parse1DMatrix( 3, bounds[ i ][ 0 ].ToFloatPtr() );
  207. parser.Parse1DMatrix( 3, bounds[ i ][ 1 ].ToFloatPtr() );
  208. }
  209. parser.ExpectTokenString( "}" );
  210. // parse base frame
  211. baseFrame.SetGranularity( 1 );
  212. baseFrame.SetNum( numJoints );
  213. parser.ExpectTokenString( "baseframe" );
  214. parser.ExpectTokenString( "{" );
  215. for( i = 0; i < numJoints; i++ ) {
  216. idCQuat q;
  217. parser.Parse1DMatrix( 3, baseFrame[ i ].t.ToFloatPtr() );
  218. parser.Parse1DMatrix( 3, q.ToFloatPtr() );//baseFrame[ i ].q.ToFloatPtr() );
  219. baseFrame[ i ].q = q.ToQuat();//.w = baseFrame[ i ].q.CalcW();
  220. }
  221. parser.ExpectTokenString( "}" );
  222. // parse frames
  223. componentFrames.SetGranularity( 1 );
  224. componentFrames.SetNum( numAnimatedComponents * numFrames );
  225. float *componentPtr = componentFrames.Ptr();
  226. for( i = 0; i < numFrames; i++ ) {
  227. parser.ExpectTokenString( "frame" );
  228. num = parser.ParseInt();
  229. if ( num != i ) {
  230. parser.Error( "Expected frame number %d", i );
  231. }
  232. parser.ExpectTokenString( "{" );
  233. for( j = 0; j < numAnimatedComponents; j++, componentPtr++ ) {
  234. *componentPtr = parser.ParseFloat();
  235. }
  236. parser.ExpectTokenString( "}" );
  237. }
  238. // get total move delta
  239. if ( !numAnimatedComponents ) {
  240. totaldelta.Zero();
  241. } else {
  242. componentPtr = &componentFrames[ jointInfo[ 0 ].firstComponent ];
  243. if ( jointInfo[ 0 ].animBits & ANIM_TX ) {
  244. for( i = 0; i < numFrames; i++ ) {
  245. componentPtr[ numAnimatedComponents * i ] -= baseFrame[ 0 ].t.x;
  246. }
  247. totaldelta.x = componentPtr[ numAnimatedComponents * ( numFrames - 1 ) ];
  248. componentPtr++;
  249. } else {
  250. totaldelta.x = 0.0f;
  251. }
  252. if ( jointInfo[ 0 ].animBits & ANIM_TY ) {
  253. for( i = 0; i < numFrames; i++ ) {
  254. componentPtr[ numAnimatedComponents * i ] -= baseFrame[ 0 ].t.y;
  255. }
  256. totaldelta.y = componentPtr[ numAnimatedComponents * ( numFrames - 1 ) ];
  257. componentPtr++;
  258. } else {
  259. totaldelta.y = 0.0f;
  260. }
  261. if ( jointInfo[ 0 ].animBits & ANIM_TZ ) {
  262. for( i = 0; i < numFrames; i++ ) {
  263. componentPtr[ numAnimatedComponents * i ] -= baseFrame[ 0 ].t.z;
  264. }
  265. totaldelta.z = componentPtr[ numAnimatedComponents * ( numFrames - 1 ) ];
  266. } else {
  267. totaldelta.z = 0.0f;
  268. }
  269. }
  270. baseFrame[ 0 ].t.Zero();
  271. // we don't count last frame because it would cause a 1 frame pause at the end
  272. animLength = ( ( numFrames - 1 ) * 1000 + frameRate - 1 ) / frameRate;
  273. // done
  274. return true;
  275. }
  276. /*
  277. ====================
  278. idMD5Anim::IncreaseRefs
  279. ====================
  280. */
  281. void idMD5Anim::IncreaseRefs( void ) const {
  282. ref_count++;
  283. }
  284. /*
  285. ====================
  286. idMD5Anim::DecreaseRefs
  287. ====================
  288. */
  289. void idMD5Anim::DecreaseRefs( void ) const {
  290. ref_count--;
  291. }
  292. /*
  293. ====================
  294. idMD5Anim::NumRefs
  295. ====================
  296. */
  297. int idMD5Anim::NumRefs( void ) const {
  298. return ref_count;
  299. }
  300. /*
  301. ====================
  302. idMD5Anim::GetFrameBlend
  303. ====================
  304. */
  305. void idMD5Anim::GetFrameBlend( int framenum, frameBlend_t &frame ) const {
  306. frame.cycleCount = 0;
  307. frame.backlerp = 0.0f;
  308. frame.frontlerp = 1.0f;
  309. // frame 1 is first frame
  310. framenum--;
  311. if ( framenum < 0 ) {
  312. framenum = 0;
  313. } else if ( framenum >= numFrames ) {
  314. framenum = numFrames - 1;
  315. }
  316. frame.frame1 = framenum;
  317. frame.frame2 = framenum;
  318. }
  319. /*
  320. ====================
  321. idMD5Anim::ConvertTimeToFrame
  322. ====================
  323. */
  324. void idMD5Anim::ConvertTimeToFrame( int time, int cyclecount, frameBlend_t &frame ) const {
  325. int frameTime;
  326. int frameNum;
  327. if ( numFrames <= 1 ) {
  328. frame.frame1 = 0;
  329. frame.frame2 = 0;
  330. frame.backlerp = 0.0f;
  331. frame.frontlerp = 1.0f;
  332. frame.cycleCount = 0;
  333. return;
  334. }
  335. if ( time <= 0 ) {
  336. frame.frame1 = 0;
  337. frame.frame2 = 1;
  338. frame.backlerp = 0.0f;
  339. frame.frontlerp = 1.0f;
  340. frame.cycleCount = 0;
  341. return;
  342. }
  343. frameTime = time * frameRate;
  344. frameNum = frameTime / 1000;
  345. frame.cycleCount = frameNum / ( numFrames - 1 );
  346. if ( ( cyclecount > 0 ) && ( frame.cycleCount >= cyclecount ) ) {
  347. frame.cycleCount = cyclecount - 1;
  348. frame.frame1 = numFrames - 1;
  349. frame.frame2 = frame.frame1;
  350. frame.backlerp = 0.0f;
  351. frame.frontlerp = 1.0f;
  352. return;
  353. }
  354. frame.frame1 = frameNum % ( numFrames - 1 );
  355. frame.frame2 = frame.frame1 + 1;
  356. if ( frame.frame2 >= numFrames ) {
  357. frame.frame2 = 0;
  358. }
  359. frame.backlerp = ( frameTime % 1000 ) * 0.001f;
  360. frame.frontlerp = 1.0f - frame.backlerp;
  361. }
  362. /*
  363. ====================
  364. idMD5Anim::GetOrigin
  365. ====================
  366. */
  367. void idMD5Anim::GetOrigin( idVec3 &offset, int time, int cyclecount ) const {
  368. frameBlend_t frame;
  369. offset = baseFrame[ 0 ].t;
  370. if ( !( jointInfo[ 0 ].animBits & ( ANIM_TX | ANIM_TY | ANIM_TZ ) ) ) {
  371. // just use the baseframe
  372. return;
  373. }
  374. ConvertTimeToFrame( time, cyclecount, frame );
  375. const float *componentPtr1 = &componentFrames[ numAnimatedComponents * frame.frame1 + jointInfo[ 0 ].firstComponent ];
  376. const float *componentPtr2 = &componentFrames[ numAnimatedComponents * frame.frame2 + jointInfo[ 0 ].firstComponent ];
  377. if ( jointInfo[ 0 ].animBits & ANIM_TX ) {
  378. offset.x = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  379. componentPtr1++;
  380. componentPtr2++;
  381. }
  382. if ( jointInfo[ 0 ].animBits & ANIM_TY ) {
  383. offset.y = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  384. componentPtr1++;
  385. componentPtr2++;
  386. }
  387. if ( jointInfo[ 0 ].animBits & ANIM_TZ ) {
  388. offset.z = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  389. }
  390. if ( frame.cycleCount ) {
  391. offset += totaldelta * ( float )frame.cycleCount;
  392. }
  393. }
  394. /*
  395. ====================
  396. idMD5Anim::GetOriginRotation
  397. ====================
  398. */
  399. void idMD5Anim::GetOriginRotation( idQuat &rotation, int time, int cyclecount ) const {
  400. frameBlend_t frame;
  401. int animBits;
  402. animBits = jointInfo[ 0 ].animBits;
  403. if ( !( animBits & ( ANIM_QX | ANIM_QY | ANIM_QZ ) ) ) {
  404. // just use the baseframe
  405. rotation = baseFrame[ 0 ].q;
  406. return;
  407. }
  408. ConvertTimeToFrame( time, cyclecount, frame );
  409. const float *jointframe1 = &componentFrames[ numAnimatedComponents * frame.frame1 + jointInfo[ 0 ].firstComponent ];
  410. const float *jointframe2 = &componentFrames[ numAnimatedComponents * frame.frame2 + jointInfo[ 0 ].firstComponent ];
  411. if ( animBits & ANIM_TX ) {
  412. jointframe1++;
  413. jointframe2++;
  414. }
  415. if ( animBits & ANIM_TY ) {
  416. jointframe1++;
  417. jointframe2++;
  418. }
  419. if ( animBits & ANIM_TZ ) {
  420. jointframe1++;
  421. jointframe2++;
  422. }
  423. idQuat q1;
  424. idQuat q2;
  425. switch( animBits & (ANIM_QX|ANIM_QY|ANIM_QZ) ) {
  426. case ANIM_QX:
  427. q1.x = jointframe1[0];
  428. q2.x = jointframe2[0];
  429. q1.y = baseFrame[ 0 ].q.y;
  430. q2.y = q1.y;
  431. q1.z = baseFrame[ 0 ].q.z;
  432. q2.z = q1.z;
  433. q1.w = q1.CalcW();
  434. q2.w = q2.CalcW();
  435. break;
  436. case ANIM_QY:
  437. q1.y = jointframe1[0];
  438. q2.y = jointframe2[0];
  439. q1.x = baseFrame[ 0 ].q.x;
  440. q2.x = q1.x;
  441. q1.z = baseFrame[ 0 ].q.z;
  442. q2.z = q1.z;
  443. q1.w = q1.CalcW();
  444. q2.w = q2.CalcW();
  445. break;
  446. case ANIM_QZ:
  447. q1.z = jointframe1[0];
  448. q2.z = jointframe2[0];
  449. q1.x = baseFrame[ 0 ].q.x;
  450. q2.x = q1.x;
  451. q1.y = baseFrame[ 0 ].q.y;
  452. q2.y = q1.y;
  453. q1.w = q1.CalcW();
  454. q2.w = q2.CalcW();
  455. break;
  456. case ANIM_QX|ANIM_QY:
  457. q1.x = jointframe1[0];
  458. q1.y = jointframe1[1];
  459. q2.x = jointframe2[0];
  460. q2.y = jointframe2[1];
  461. q1.z = baseFrame[ 0 ].q.z;
  462. q2.z = q1.z;
  463. q1.w = q1.CalcW();
  464. q2.w = q2.CalcW();
  465. break;
  466. case ANIM_QX|ANIM_QZ:
  467. q1.x = jointframe1[0];
  468. q1.z = jointframe1[1];
  469. q2.x = jointframe2[0];
  470. q2.z = jointframe2[1];
  471. q1.y = baseFrame[ 0 ].q.y;
  472. q2.y = q1.y;
  473. q1.w = q1.CalcW();
  474. q2.w = q2.CalcW();
  475. break;
  476. case ANIM_QY|ANIM_QZ:
  477. q1.y = jointframe1[0];
  478. q1.z = jointframe1[1];
  479. q2.y = jointframe2[0];
  480. q2.z = jointframe2[1];
  481. q1.x = baseFrame[ 0 ].q.x;
  482. q2.x = q1.x;
  483. q1.w = q1.CalcW();
  484. q2.w = q2.CalcW();
  485. break;
  486. case ANIM_QX|ANIM_QY|ANIM_QZ:
  487. q1.x = jointframe1[0];
  488. q1.y = jointframe1[1];
  489. q1.z = jointframe1[2];
  490. q2.x = jointframe2[0];
  491. q2.y = jointframe2[1];
  492. q2.z = jointframe2[2];
  493. q1.w = q1.CalcW();
  494. q2.w = q2.CalcW();
  495. break;
  496. }
  497. rotation.Slerp( q1, q2, frame.backlerp );
  498. }
  499. /*
  500. ====================
  501. idMD5Anim::GetBounds
  502. ====================
  503. */
  504. void idMD5Anim::GetBounds( idBounds &bnds, int time, int cyclecount ) const {
  505. frameBlend_t frame;
  506. idVec3 offset;
  507. ConvertTimeToFrame( time, cyclecount, frame );
  508. bnds = bounds[ frame.frame1 ];
  509. bnds.AddBounds( bounds[ frame.frame2 ] );
  510. // origin position
  511. offset = baseFrame[ 0 ].t;
  512. if ( jointInfo[ 0 ].animBits & ( ANIM_TX | ANIM_TY | ANIM_TZ ) ) {
  513. const float *componentPtr1 = &componentFrames[ numAnimatedComponents * frame.frame1 + jointInfo[ 0 ].firstComponent ];
  514. const float *componentPtr2 = &componentFrames[ numAnimatedComponents * frame.frame2 + jointInfo[ 0 ].firstComponent ];
  515. if ( jointInfo[ 0 ].animBits & ANIM_TX ) {
  516. offset.x = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  517. componentPtr1++;
  518. componentPtr2++;
  519. }
  520. if ( jointInfo[ 0 ].animBits & ANIM_TY ) {
  521. offset.y = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  522. componentPtr1++;
  523. componentPtr2++;
  524. }
  525. if ( jointInfo[ 0 ].animBits & ANIM_TZ ) {
  526. offset.z = *componentPtr1 * frame.frontlerp + *componentPtr2 * frame.backlerp;
  527. }
  528. }
  529. bnds[ 0 ] -= offset;
  530. bnds[ 1 ] -= offset;
  531. }
  532. /*
  533. ====================
  534. idMD5Anim::GetInterpolatedFrame
  535. ====================
  536. */
  537. void idMD5Anim::GetInterpolatedFrame( frameBlend_t &frame, idJointQuat *joints, const int *index, int numIndexes ) const {
  538. int i, numLerpJoints;
  539. const float *frame1;
  540. const float *frame2;
  541. const float *jointframe1;
  542. const float *jointframe2;
  543. const jointAnimInfo_t *infoPtr;
  544. int animBits;
  545. idJointQuat *blendJoints;
  546. idJointQuat *jointPtr;
  547. idJointQuat *blendPtr;
  548. int *lerpIndex;
  549. // copy the baseframe
  550. SIMDProcessor->Memcpy( joints, baseFrame.Ptr(), baseFrame.Num() * sizeof( baseFrame[ 0 ] ) );
  551. if ( !numAnimatedComponents ) {
  552. // just use the base frame
  553. return;
  554. }
  555. blendJoints = (idJointQuat *)_alloca16( baseFrame.Num() * sizeof( blendPtr[ 0 ] ) );
  556. lerpIndex = (int *)_alloca16( baseFrame.Num() * sizeof( lerpIndex[ 0 ] ) );
  557. numLerpJoints = 0;
  558. frame1 = &componentFrames[ frame.frame1 * numAnimatedComponents ];
  559. frame2 = &componentFrames[ frame.frame2 * numAnimatedComponents ];
  560. for ( i = 0; i < numIndexes; i++ ) {
  561. int j = index[i];
  562. jointPtr = &joints[j];
  563. blendPtr = &blendJoints[j];
  564. infoPtr = &jointInfo[j];
  565. animBits = infoPtr->animBits;
  566. if ( animBits ) {
  567. lerpIndex[numLerpJoints++] = j;
  568. jointframe1 = frame1 + infoPtr->firstComponent;
  569. jointframe2 = frame2 + infoPtr->firstComponent;
  570. switch( animBits & (ANIM_TX|ANIM_TY|ANIM_TZ) ) {
  571. case 0:
  572. blendPtr->t = jointPtr->t;
  573. break;
  574. case ANIM_TX:
  575. jointPtr->t.x = jointframe1[0];
  576. blendPtr->t.x = jointframe2[0];
  577. blendPtr->t.y = jointPtr->t.y;
  578. blendPtr->t.z = jointPtr->t.z;
  579. jointframe1++;
  580. jointframe2++;
  581. break;
  582. case ANIM_TY:
  583. jointPtr->t.y = jointframe1[0];
  584. blendPtr->t.y = jointframe2[0];
  585. blendPtr->t.x = jointPtr->t.x;
  586. blendPtr->t.z = jointPtr->t.z;
  587. jointframe1++;
  588. jointframe2++;
  589. break;
  590. case ANIM_TZ:
  591. jointPtr->t.z = jointframe1[0];
  592. blendPtr->t.z = jointframe2[0];
  593. blendPtr->t.x = jointPtr->t.x;
  594. blendPtr->t.y = jointPtr->t.y;
  595. jointframe1++;
  596. jointframe2++;
  597. break;
  598. case ANIM_TX|ANIM_TY:
  599. jointPtr->t.x = jointframe1[0];
  600. jointPtr->t.y = jointframe1[1];
  601. blendPtr->t.x = jointframe2[0];
  602. blendPtr->t.y = jointframe2[1];
  603. blendPtr->t.z = jointPtr->t.z;
  604. jointframe1 += 2;
  605. jointframe2 += 2;
  606. break;
  607. case ANIM_TX|ANIM_TZ:
  608. jointPtr->t.x = jointframe1[0];
  609. jointPtr->t.z = jointframe1[1];
  610. blendPtr->t.x = jointframe2[0];
  611. blendPtr->t.z = jointframe2[1];
  612. blendPtr->t.y = jointPtr->t.y;
  613. jointframe1 += 2;
  614. jointframe2 += 2;
  615. break;
  616. case ANIM_TY|ANIM_TZ:
  617. jointPtr->t.y = jointframe1[0];
  618. jointPtr->t.z = jointframe1[1];
  619. blendPtr->t.y = jointframe2[0];
  620. blendPtr->t.z = jointframe2[1];
  621. blendPtr->t.x = jointPtr->t.x;
  622. jointframe1 += 2;
  623. jointframe2 += 2;
  624. break;
  625. case ANIM_TX|ANIM_TY|ANIM_TZ:
  626. jointPtr->t.x = jointframe1[0];
  627. jointPtr->t.y = jointframe1[1];
  628. jointPtr->t.z = jointframe1[2];
  629. blendPtr->t.x = jointframe2[0];
  630. blendPtr->t.y = jointframe2[1];
  631. blendPtr->t.z = jointframe2[2];
  632. jointframe1 += 3;
  633. jointframe2 += 3;
  634. break;
  635. }
  636. switch( animBits & (ANIM_QX|ANIM_QY|ANIM_QZ) ) {
  637. case 0:
  638. blendPtr->q = jointPtr->q;
  639. break;
  640. case ANIM_QX:
  641. jointPtr->q.x = jointframe1[0];
  642. blendPtr->q.x = jointframe2[0];
  643. blendPtr->q.y = jointPtr->q.y;
  644. blendPtr->q.z = jointPtr->q.z;
  645. jointPtr->q.w = jointPtr->q.CalcW();
  646. blendPtr->q.w = blendPtr->q.CalcW();
  647. break;
  648. case ANIM_QY:
  649. jointPtr->q.y = jointframe1[0];
  650. blendPtr->q.y = jointframe2[0];
  651. blendPtr->q.x = jointPtr->q.x;
  652. blendPtr->q.z = jointPtr->q.z;
  653. jointPtr->q.w = jointPtr->q.CalcW();
  654. blendPtr->q.w = blendPtr->q.CalcW();
  655. break;
  656. case ANIM_QZ:
  657. jointPtr->q.z = jointframe1[0];
  658. blendPtr->q.z = jointframe2[0];
  659. blendPtr->q.x = jointPtr->q.x;
  660. blendPtr->q.y = jointPtr->q.y;
  661. jointPtr->q.w = jointPtr->q.CalcW();
  662. blendPtr->q.w = blendPtr->q.CalcW();
  663. break;
  664. case ANIM_QX|ANIM_QY:
  665. jointPtr->q.x = jointframe1[0];
  666. jointPtr->q.y = jointframe1[1];
  667. blendPtr->q.x = jointframe2[0];
  668. blendPtr->q.y = jointframe2[1];
  669. blendPtr->q.z = jointPtr->q.z;
  670. jointPtr->q.w = jointPtr->q.CalcW();
  671. blendPtr->q.w = blendPtr->q.CalcW();
  672. break;
  673. case ANIM_QX|ANIM_QZ:
  674. jointPtr->q.x = jointframe1[0];
  675. jointPtr->q.z = jointframe1[1];
  676. blendPtr->q.x = jointframe2[0];
  677. blendPtr->q.z = jointframe2[1];
  678. blendPtr->q.y = jointPtr->q.y;
  679. jointPtr->q.w = jointPtr->q.CalcW();
  680. blendPtr->q.w = blendPtr->q.CalcW();
  681. break;
  682. case ANIM_QY|ANIM_QZ:
  683. jointPtr->q.y = jointframe1[0];
  684. jointPtr->q.z = jointframe1[1];
  685. blendPtr->q.y = jointframe2[0];
  686. blendPtr->q.z = jointframe2[1];
  687. blendPtr->q.x = jointPtr->q.x;
  688. jointPtr->q.w = jointPtr->q.CalcW();
  689. blendPtr->q.w = blendPtr->q.CalcW();
  690. break;
  691. case ANIM_QX|ANIM_QY|ANIM_QZ:
  692. jointPtr->q.x = jointframe1[0];
  693. jointPtr->q.y = jointframe1[1];
  694. jointPtr->q.z = jointframe1[2];
  695. blendPtr->q.x = jointframe2[0];
  696. blendPtr->q.y = jointframe2[1];
  697. blendPtr->q.z = jointframe2[2];
  698. jointPtr->q.w = jointPtr->q.CalcW();
  699. blendPtr->q.w = blendPtr->q.CalcW();
  700. break;
  701. }
  702. }
  703. }
  704. SIMDProcessor->BlendJoints( joints, blendJoints, frame.backlerp, lerpIndex, numLerpJoints );
  705. if ( frame.cycleCount ) {
  706. joints[ 0 ].t += totaldelta * ( float )frame.cycleCount;
  707. }
  708. }
  709. /*
  710. ====================
  711. idMD5Anim::GetSingleFrame
  712. ====================
  713. */
  714. void idMD5Anim::GetSingleFrame( int framenum, idJointQuat *joints, const int *index, int numIndexes ) const {
  715. int i;
  716. const float *frame;
  717. const float *jointframe;
  718. int animBits;
  719. idJointQuat *jointPtr;
  720. const jointAnimInfo_t *infoPtr;
  721. // copy the baseframe
  722. SIMDProcessor->Memcpy( joints, baseFrame.Ptr(), baseFrame.Num() * sizeof( baseFrame[ 0 ] ) );
  723. if ( ( framenum == 0 ) || !numAnimatedComponents ) {
  724. // just use the base frame
  725. return;
  726. }
  727. frame = &componentFrames[ framenum * numAnimatedComponents ];
  728. for ( i = 0; i < numIndexes; i++ ) {
  729. int j = index[i];
  730. jointPtr = &joints[j];
  731. infoPtr = &jointInfo[j];
  732. animBits = infoPtr->animBits;
  733. if ( animBits ) {
  734. jointframe = frame + infoPtr->firstComponent;
  735. if ( animBits & (ANIM_TX|ANIM_TY|ANIM_TZ) ) {
  736. if ( animBits & ANIM_TX ) {
  737. jointPtr->t.x = *jointframe++;
  738. }
  739. if ( animBits & ANIM_TY ) {
  740. jointPtr->t.y = *jointframe++;
  741. }
  742. if ( animBits & ANIM_TZ ) {
  743. jointPtr->t.z = *jointframe++;
  744. }
  745. }
  746. if ( animBits & (ANIM_QX|ANIM_QY|ANIM_QZ) ) {
  747. if ( animBits & ANIM_QX ) {
  748. jointPtr->q.x = *jointframe++;
  749. }
  750. if ( animBits & ANIM_QY ) {
  751. jointPtr->q.y = *jointframe++;
  752. }
  753. if ( animBits & ANIM_QZ ) {
  754. jointPtr->q.z = *jointframe;
  755. }
  756. jointPtr->q.w = jointPtr->q.CalcW();
  757. }
  758. }
  759. }
  760. }
  761. /*
  762. ====================
  763. idMD5Anim::CheckModelHierarchy
  764. ====================
  765. */
  766. void idMD5Anim::CheckModelHierarchy( const idRenderModel *model ) const {
  767. int i;
  768. int jointNum;
  769. int parent;
  770. if ( jointInfo.Num() != model->NumJoints() ) {
  771. gameLocal.Error( "Model '%s' has different # of joints than anim '%s'", model->Name(), name.c_str() );
  772. }
  773. const idMD5Joint *modelJoints = model->GetJoints();
  774. for( i = 0; i < jointInfo.Num(); i++ ) {
  775. jointNum = jointInfo[ i ].nameIndex;
  776. if ( modelJoints[ i ].name != animationLib.JointName( jointNum ) ) {
  777. gameLocal.Error( "Model '%s''s joint names don't match anim '%s''s", model->Name(), name.c_str() );
  778. }
  779. if ( modelJoints[ i ].parent ) {
  780. parent = modelJoints[ i ].parent - modelJoints;
  781. } else {
  782. parent = -1;
  783. }
  784. if ( parent != jointInfo[ i ].parentNum ) {
  785. gameLocal.Error( "Model '%s' has different joint hierarchy than anim '%s'", model->Name(), name.c_str() );
  786. }
  787. }
  788. }
  789. /***********************************************************************
  790. idAnimManager
  791. ***********************************************************************/
  792. /*
  793. ====================
  794. idAnimManager::idAnimManager
  795. ====================
  796. */
  797. idAnimManager::idAnimManager() {
  798. }
  799. /*
  800. ====================
  801. idAnimManager::~idAnimManager
  802. ====================
  803. */
  804. idAnimManager::~idAnimManager() {
  805. Shutdown();
  806. }
  807. /*
  808. ====================
  809. idAnimManager::Shutdown
  810. ====================
  811. */
  812. void idAnimManager::Shutdown( void ) {
  813. animations.DeleteContents();
  814. jointnames.Clear();
  815. jointnamesHash.Free();
  816. }
  817. /*
  818. ====================
  819. idAnimManager::GetAnim
  820. ====================
  821. */
  822. idMD5Anim *idAnimManager::GetAnim( const char *name ) {
  823. idMD5Anim **animptrptr;
  824. idMD5Anim *anim;
  825. // see if it has been asked for before
  826. animptrptr = NULL;
  827. if ( animations.Get( name, &animptrptr ) ) {
  828. anim = *animptrptr;
  829. } else {
  830. idStr extension;
  831. idStr filename = name;
  832. filename.ExtractFileExtension( extension );
  833. if ( extension != MD5_ANIM_EXT ) {
  834. return NULL;
  835. }
  836. anim = new idMD5Anim();
  837. if ( !anim->LoadAnim( filename ) ) {
  838. gameLocal.Warning( "Couldn't load anim: '%s'", filename.c_str() );
  839. delete anim;
  840. anim = NULL;
  841. }
  842. animations.Set( filename, anim );
  843. }
  844. return anim;
  845. }
  846. /*
  847. ================
  848. idAnimManager::ReloadAnims
  849. ================
  850. */
  851. void idAnimManager::ReloadAnims( void ) {
  852. int i;
  853. idMD5Anim **animptr;
  854. for( i = 0; i < animations.Num(); i++ ) {
  855. animptr = animations.GetIndex( i );
  856. if ( animptr && *animptr ) {
  857. ( *animptr )->Reload();
  858. }
  859. }
  860. }
  861. /*
  862. ================
  863. idAnimManager::JointIndex
  864. ================
  865. */
  866. int idAnimManager::JointIndex( const char *name ) {
  867. int i, hash;
  868. hash = jointnamesHash.GenerateKey( name );
  869. for ( i = jointnamesHash.First( hash ); i != -1; i = jointnamesHash.Next( i ) ) {
  870. if ( jointnames[i].Cmp( name ) == 0 ) {
  871. return i;
  872. }
  873. }
  874. i = jointnames.Append( name );
  875. jointnamesHash.Add( hash, i );
  876. return i;
  877. }
  878. /*
  879. ================
  880. idAnimManager::JointName
  881. ================
  882. */
  883. const char *idAnimManager::JointName( int index ) const {
  884. return jointnames[ index ];
  885. }
  886. /*
  887. ================
  888. idAnimManager::ListAnims
  889. ================
  890. */
  891. void idAnimManager::ListAnims( void ) const {
  892. int i;
  893. idMD5Anim **animptr;
  894. idMD5Anim *anim;
  895. size_t size;
  896. size_t s;
  897. size_t namesize;
  898. int num;
  899. num = 0;
  900. size = 0;
  901. for( i = 0; i < animations.Num(); i++ ) {
  902. animptr = animations.GetIndex( i );
  903. if ( animptr && *animptr ) {
  904. anim = *animptr;
  905. s = anim->Size();
  906. gameLocal.Printf( "%8d bytes : %2d refs : %s\n", s, anim->NumRefs(), anim->Name() );
  907. size += s;
  908. num++;
  909. }
  910. }
  911. namesize = jointnames.Size() + jointnamesHash.Size();
  912. for( i = 0; i < jointnames.Num(); i++ ) {
  913. namesize += jointnames[ i ].Size();
  914. }
  915. gameLocal.Printf( "\n%d memory used in %d anims\n", size, num );
  916. gameLocal.Printf( "%d memory used in %d joint names\n", namesize, jointnames.Num() );
  917. }
  918. /*
  919. ================
  920. idAnimManager::FlushUnusedAnims
  921. ================
  922. */
  923. void idAnimManager::FlushUnusedAnims( void ) {
  924. int i;
  925. idMD5Anim **animptr;
  926. idList<idMD5Anim *> removeAnims;
  927. for( i = 0; i < animations.Num(); i++ ) {
  928. animptr = animations.GetIndex( i );
  929. if ( animptr && *animptr ) {
  930. if ( ( *animptr )->NumRefs() <= 0 ) {
  931. removeAnims.Append( *animptr );
  932. }
  933. }
  934. }
  935. for( i = 0; i < removeAnims.Num(); i++ ) {
  936. animations.Remove( removeAnims[ i ]->Name() );
  937. delete removeAnims[ i ];
  938. }
  939. }