Anim_Blend.cpp 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109
  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. static const char *channelNames[ ANIM_NumAnimChannels ] = {
  24. "all", "torso", "legs", "head", "eyelids"
  25. };
  26. /***********************************************************************
  27. idAnim
  28. ***********************************************************************/
  29. /*
  30. =====================
  31. idAnim::idAnim
  32. =====================
  33. */
  34. idAnim::idAnim() {
  35. modelDef = NULL;
  36. numAnims = 0;
  37. memset( anims, 0, sizeof( anims ) );
  38. memset( &flags, 0, sizeof( flags ) );
  39. }
  40. /*
  41. =====================
  42. idAnim::idAnim
  43. =====================
  44. */
  45. idAnim::idAnim( const idDeclModelDef *modelDef, const idAnim *anim ) {
  46. int i;
  47. this->modelDef = modelDef;
  48. numAnims = anim->numAnims;
  49. name = anim->name;
  50. realname = anim->realname;
  51. flags = anim->flags;
  52. memset( anims, 0, sizeof( anims ) );
  53. for( i = 0; i < numAnims; i++ ) {
  54. anims[ i ] = anim->anims[ i ];
  55. anims[ i ]->IncreaseRefs();
  56. }
  57. frameLookup.SetNum( anim->frameLookup.Num() );
  58. memcpy( frameLookup.Ptr(), anim->frameLookup.Ptr(), frameLookup.MemoryUsed() );
  59. frameCommands.SetNum( anim->frameCommands.Num() );
  60. for( i = 0; i < frameCommands.Num(); i++ ) {
  61. frameCommands[ i ] = anim->frameCommands[ i ];
  62. if ( anim->frameCommands[ i ].string ) {
  63. frameCommands[ i ].string = new idStr( *anim->frameCommands[ i ].string );
  64. }
  65. }
  66. }
  67. /*
  68. =====================
  69. idAnim::~idAnim
  70. =====================
  71. */
  72. idAnim::~idAnim() {
  73. int i;
  74. for( i = 0; i < numAnims; i++ ) {
  75. anims[ i ]->DecreaseRefs();
  76. }
  77. for( i = 0; i < frameCommands.Num(); i++ ) {
  78. delete frameCommands[ i ].string;
  79. }
  80. }
  81. /*
  82. =====================
  83. idAnim::SetAnim
  84. =====================
  85. */
  86. void idAnim::SetAnim( const idDeclModelDef *modelDef, const char *sourcename, const char *animname, int num, const idMD5Anim *md5anims[ ANIM_MaxSyncedAnims ] ) {
  87. int i;
  88. this->modelDef = modelDef;
  89. for( i = 0; i < numAnims; i++ ) {
  90. anims[ i ]->DecreaseRefs();
  91. anims[ i ] = NULL;
  92. }
  93. assert( ( num > 0 ) && ( num <= ANIM_MaxSyncedAnims ) );
  94. numAnims = num;
  95. realname = sourcename;
  96. name = animname;
  97. for( i = 0; i < num; i++ ) {
  98. anims[ i ] = md5anims[ i ];
  99. anims[ i ]->IncreaseRefs();
  100. }
  101. memset( &flags, 0, sizeof( flags ) );
  102. for( i = 0; i < frameCommands.Num(); i++ ) {
  103. delete frameCommands[ i ].string;
  104. }
  105. frameLookup.Clear();
  106. frameCommands.Clear();
  107. }
  108. /*
  109. =====================
  110. idAnim::Name
  111. =====================
  112. */
  113. const char *idAnim::Name( void ) const {
  114. return name;
  115. }
  116. /*
  117. =====================
  118. idAnim::FullName
  119. =====================
  120. */
  121. const char *idAnim::FullName( void ) const {
  122. return realname;
  123. }
  124. /*
  125. =====================
  126. idAnim::MD5Anim
  127. index 0 will never be NULL. Any anim >= NumAnims will return NULL.
  128. =====================
  129. */
  130. const idMD5Anim *idAnim::MD5Anim( int num ) const {
  131. if ( anims == NULL || anims[0] == NULL ) {
  132. return NULL;
  133. }
  134. return anims[ num ];
  135. }
  136. /*
  137. =====================
  138. idAnim::ModelDef
  139. =====================
  140. */
  141. const idDeclModelDef *idAnim::ModelDef( void ) const {
  142. return modelDef;
  143. }
  144. /*
  145. =====================
  146. idAnim::Length
  147. =====================
  148. */
  149. int idAnim::Length( void ) const {
  150. if ( !anims[ 0 ] ) {
  151. return 0;
  152. }
  153. return anims[ 0 ]->Length();
  154. }
  155. /*
  156. =====================
  157. idAnim::NumFrames
  158. =====================
  159. */
  160. int idAnim::NumFrames( void ) const {
  161. if ( !anims[ 0 ] ) {
  162. return 0;
  163. }
  164. return anims[ 0 ]->NumFrames();
  165. }
  166. /*
  167. =====================
  168. idAnim::NumAnims
  169. =====================
  170. */
  171. int idAnim::NumAnims( void ) const {
  172. return numAnims;
  173. }
  174. /*
  175. =====================
  176. idAnim::TotalMovementDelta
  177. =====================
  178. */
  179. const idVec3 &idAnim::TotalMovementDelta( void ) const {
  180. if ( !anims[ 0 ] ) {
  181. return vec3_zero;
  182. }
  183. return anims[ 0 ]->TotalMovementDelta();
  184. }
  185. /*
  186. =====================
  187. idAnim::GetOrigin
  188. =====================
  189. */
  190. bool idAnim::GetOrigin( idVec3 &offset, int animNum, int currentTime, int cyclecount ) const {
  191. if ( !anims[ animNum ] ) {
  192. offset.Zero();
  193. return false;
  194. }
  195. anims[ animNum ]->GetOrigin( offset, currentTime, cyclecount );
  196. return true;
  197. }
  198. /*
  199. =====================
  200. idAnim::GetOriginRotation
  201. =====================
  202. */
  203. bool idAnim::GetOriginRotation( idQuat &rotation, int animNum, int currentTime, int cyclecount ) const {
  204. if ( !anims[ animNum ] ) {
  205. rotation.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  206. return false;
  207. }
  208. anims[ animNum ]->GetOriginRotation( rotation, currentTime, cyclecount );
  209. return true;
  210. }
  211. /*
  212. =====================
  213. idAnim::GetBounds
  214. =====================
  215. */
  216. ID_INLINE bool idAnim::GetBounds( idBounds &bounds, int animNum, int currentTime, int cyclecount ) const {
  217. if ( !anims[ animNum ] ) {
  218. return false;
  219. }
  220. anims[ animNum ]->GetBounds( bounds, currentTime, cyclecount );
  221. return true;
  222. }
  223. /*
  224. =====================
  225. idAnim::AddFrameCommand
  226. Returns NULL if no error.
  227. =====================
  228. */
  229. const char *idAnim::AddFrameCommand( const idDeclModelDef *modelDef, int framenum, idLexer &src, const idDict *def ) {
  230. int i;
  231. int index;
  232. idStr text;
  233. idStr funcname;
  234. frameCommand_t fc;
  235. idToken token;
  236. const jointInfo_t *jointInfo;
  237. // make sure we're within bounds
  238. if ( ( framenum < 1 ) || ( framenum > anims[ 0 ]->NumFrames() ) ) {
  239. return va( "Frame %d out of range", framenum );
  240. }
  241. // frame numbers are 1 based in .def files, but 0 based internally
  242. framenum--;
  243. memset( &fc, 0, sizeof( fc ) );
  244. if( !src.ReadTokenOnLine( &token ) ) {
  245. return "Unexpected end of line";
  246. }
  247. if ( token == "call" ) {
  248. if( !src.ReadTokenOnLine( &token ) ) {
  249. return "Unexpected end of line";
  250. }
  251. fc.type = FC_SCRIPTFUNCTION;
  252. fc.function = gameLocal.program.FindFunction( token );
  253. if ( !fc.function ) {
  254. return va( "Function '%s' not found", token.c_str() );
  255. }
  256. } else if ( token == "object_call" ) {
  257. if( !src.ReadTokenOnLine( &token ) ) {
  258. return "Unexpected end of line";
  259. }
  260. fc.type = FC_SCRIPTFUNCTIONOBJECT;
  261. fc.string = new idStr( token );
  262. } else if ( token == "event" ) {
  263. if( !src.ReadTokenOnLine( &token ) ) {
  264. return "Unexpected end of line";
  265. }
  266. fc.type = FC_EVENTFUNCTION;
  267. const idEventDef *ev = idEventDef::FindEvent( token );
  268. if ( !ev ) {
  269. return va( "Event '%s' not found", token.c_str() );
  270. }
  271. if ( ev->GetNumArgs() != 0 ) {
  272. return va( "Event '%s' has arguments", token.c_str() );
  273. }
  274. fc.string = new idStr( token );
  275. } else if ( token == "sound" ) {
  276. if( !src.ReadTokenOnLine( &token ) ) {
  277. return "Unexpected end of line";
  278. }
  279. fc.type = FC_SOUND;
  280. if ( !token.Cmpn( "snd_", 4 ) ) {
  281. fc.string = new idStr( token );
  282. } else {
  283. fc.soundShader = declManager->FindSound( token );
  284. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  285. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  286. }
  287. }
  288. } else if ( token == "sound_voice" ) {
  289. if( !src.ReadTokenOnLine( &token ) ) {
  290. return "Unexpected end of line";
  291. }
  292. fc.type = FC_SOUND_VOICE;
  293. if ( !token.Cmpn( "snd_", 4 ) ) {
  294. fc.string = new idStr( token );
  295. } else {
  296. fc.soundShader = declManager->FindSound( token );
  297. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  298. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  299. }
  300. }
  301. } else if ( token == "sound_voice2" ) {
  302. if( !src.ReadTokenOnLine( &token ) ) {
  303. return "Unexpected end of line";
  304. }
  305. fc.type = FC_SOUND_VOICE2;
  306. if ( !token.Cmpn( "snd_", 4 ) ) {
  307. fc.string = new idStr( token );
  308. } else {
  309. fc.soundShader = declManager->FindSound( token );
  310. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  311. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  312. }
  313. }
  314. } else if ( token == "sound_body" ) {
  315. if( !src.ReadTokenOnLine( &token ) ) {
  316. return "Unexpected end of line";
  317. }
  318. fc.type = FC_SOUND_BODY;
  319. if ( !token.Cmpn( "snd_", 4 ) ) {
  320. fc.string = new idStr( token );
  321. } else {
  322. fc.soundShader = declManager->FindSound( token );
  323. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  324. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  325. }
  326. }
  327. } else if ( token == "sound_body2" ) {
  328. if( !src.ReadTokenOnLine( &token ) ) {
  329. return "Unexpected end of line";
  330. }
  331. fc.type = FC_SOUND_BODY2;
  332. if ( !token.Cmpn( "snd_", 4 ) ) {
  333. fc.string = new idStr( token );
  334. } else {
  335. fc.soundShader = declManager->FindSound( token );
  336. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  337. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  338. }
  339. }
  340. } else if ( token == "sound_body3" ) {
  341. if( !src.ReadTokenOnLine( &token ) ) {
  342. return "Unexpected end of line";
  343. }
  344. fc.type = FC_SOUND_BODY3;
  345. if ( !token.Cmpn( "snd_", 4 ) ) {
  346. fc.string = new idStr( token );
  347. } else {
  348. fc.soundShader = declManager->FindSound( token );
  349. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  350. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  351. }
  352. }
  353. } else if ( token == "sound_weapon" ) {
  354. if( !src.ReadTokenOnLine( &token ) ) {
  355. return "Unexpected end of line";
  356. }
  357. fc.type = FC_SOUND_WEAPON;
  358. if ( !token.Cmpn( "snd_", 4 ) ) {
  359. fc.string = new idStr( token );
  360. } else {
  361. fc.soundShader = declManager->FindSound( token );
  362. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  363. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  364. }
  365. }
  366. } else if ( token == "sound_global" ) {
  367. if( !src.ReadTokenOnLine( &token ) ) {
  368. return "Unexpected end of line";
  369. }
  370. fc.type = FC_SOUND_GLOBAL;
  371. if ( !token.Cmpn( "snd_", 4 ) ) {
  372. fc.string = new idStr( token );
  373. } else {
  374. fc.soundShader = declManager->FindSound( token );
  375. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  376. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  377. }
  378. }
  379. } else if ( token == "sound_item" ) {
  380. if( !src.ReadTokenOnLine( &token ) ) {
  381. return "Unexpected end of line";
  382. }
  383. fc.type = FC_SOUND_ITEM;
  384. if ( !token.Cmpn( "snd_", 4 ) ) {
  385. fc.string = new idStr( token );
  386. } else {
  387. fc.soundShader = declManager->FindSound( token );
  388. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  389. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  390. }
  391. }
  392. } else if ( token == "sound_chatter" ) {
  393. if( !src.ReadTokenOnLine( &token ) ) {
  394. return "Unexpected end of line";
  395. }
  396. fc.type = FC_SOUND_CHATTER;
  397. if ( !token.Cmpn( "snd_", 4 ) ) {
  398. fc.string = new idStr( token );
  399. } else {
  400. fc.soundShader = declManager->FindSound( token );
  401. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  402. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  403. }
  404. }
  405. } else if ( token == "skin" ) {
  406. if( !src.ReadTokenOnLine( &token ) ) {
  407. return "Unexpected end of line";
  408. }
  409. fc.type = FC_SKIN;
  410. if ( token == "none" ) {
  411. fc.skin = NULL;
  412. } else {
  413. fc.skin = declManager->FindSkin( token );
  414. if ( !fc.skin ) {
  415. return va( "Skin '%s' not found", token.c_str() );
  416. }
  417. }
  418. } else if ( token == "fx" ) {
  419. if( !src.ReadTokenOnLine( &token ) ) {
  420. return "Unexpected end of line";
  421. }
  422. fc.type = FC_FX;
  423. if ( !declManager->FindType( DECL_FX, token.c_str() ) ) {
  424. return va( "fx '%s' not found", token.c_str() );
  425. }
  426. fc.string = new idStr( token );
  427. } else if ( token == "trigger" ) {
  428. if( !src.ReadTokenOnLine( &token ) ) {
  429. return "Unexpected end of line";
  430. }
  431. fc.type = FC_TRIGGER;
  432. fc.string = new idStr( token );
  433. } else if ( token == "triggerSmokeParticle" ) {
  434. if( !src.ReadTokenOnLine( &token ) ) {
  435. return "Unexpected end of line";
  436. }
  437. fc.type = FC_TRIGGER_SMOKE_PARTICLE;
  438. fc.string = new idStr( token );
  439. } else if ( token == "melee" ) {
  440. if( !src.ReadTokenOnLine( &token ) ) {
  441. return "Unexpected end of line";
  442. }
  443. fc.type = FC_MELEE;
  444. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  445. return va( "Unknown entityDef '%s'", token.c_str() );
  446. }
  447. fc.string = new idStr( token );
  448. } else if ( token == "direct_damage" ) {
  449. if( !src.ReadTokenOnLine( &token ) ) {
  450. return "Unexpected end of line";
  451. }
  452. fc.type = FC_DIRECTDAMAGE;
  453. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  454. return va( "Unknown entityDef '%s'", token.c_str() );
  455. }
  456. fc.string = new idStr( token );
  457. } else if ( token == "attack_begin" ) {
  458. if( !src.ReadTokenOnLine( &token ) ) {
  459. return "Unexpected end of line";
  460. }
  461. fc.type = FC_BEGINATTACK;
  462. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  463. return va( "Unknown entityDef '%s'", token.c_str() );
  464. }
  465. fc.string = new idStr( token );
  466. } else if ( token == "attack_end" ) {
  467. fc.type = FC_ENDATTACK;
  468. } else if ( token == "muzzle_flash" ) {
  469. if( !src.ReadTokenOnLine( &token ) ) {
  470. return "Unexpected end of line";
  471. }
  472. if ( ( token != "" ) && !modelDef->FindJoint( token ) ) {
  473. return va( "Joint '%s' not found", token.c_str() );
  474. }
  475. fc.type = FC_MUZZLEFLASH;
  476. fc.string = new idStr( token );
  477. } else if ( token == "muzzle_flash" ) {
  478. fc.type = FC_MUZZLEFLASH;
  479. fc.string = new idStr( "" );
  480. } else if ( token == "create_missile" ) {
  481. if( !src.ReadTokenOnLine( &token ) ) {
  482. return "Unexpected end of line";
  483. }
  484. if ( !modelDef->FindJoint( token ) ) {
  485. return va( "Joint '%s' not found", token.c_str() );
  486. }
  487. fc.type = FC_CREATEMISSILE;
  488. fc.string = new idStr( token );
  489. } else if ( token == "launch_missile" ) {
  490. if( !src.ReadTokenOnLine( &token ) ) {
  491. return "Unexpected end of line";
  492. }
  493. if ( !modelDef->FindJoint( token ) ) {
  494. return va( "Joint '%s' not found", token.c_str() );
  495. }
  496. fc.type = FC_LAUNCHMISSILE;
  497. fc.string = new idStr( token );
  498. } else if ( token == "fire_missile_at_target" ) {
  499. if( !src.ReadTokenOnLine( &token ) ) {
  500. return "Unexpected end of line";
  501. }
  502. jointInfo = modelDef->FindJoint( token );
  503. if ( !jointInfo ) {
  504. return va( "Joint '%s' not found", token.c_str() );
  505. }
  506. if( !src.ReadTokenOnLine( &token ) ) {
  507. return "Unexpected end of line";
  508. }
  509. fc.type = FC_FIREMISSILEATTARGET;
  510. fc.string = new idStr( token );
  511. fc.index = jointInfo->num;
  512. #ifdef _D3XP
  513. } else if ( token == "launch_projectile" ) {
  514. if( !src.ReadTokenOnLine( &token ) ) {
  515. return "Unexpected end of line";
  516. }
  517. if ( !declManager->FindDeclWithoutParsing( DECL_ENTITYDEF, token, false ) ) {
  518. return "Unknown projectile def";
  519. }
  520. fc.type = FC_LAUNCH_PROJECTILE;
  521. fc.string = new idStr( token );
  522. } else if ( token == "trigger_fx" ) {
  523. if( !src.ReadTokenOnLine( &token ) ) {
  524. return "Unexpected end of line";
  525. }
  526. jointInfo = modelDef->FindJoint( token );
  527. if ( !jointInfo ) {
  528. return va( "Joint '%s' not found", token.c_str() );
  529. }
  530. if( !src.ReadTokenOnLine( &token ) ) {
  531. return "Unexpected end of line";
  532. }
  533. if ( !declManager->FindType( DECL_FX, token, false ) ) {
  534. return "Unknown FX def";
  535. }
  536. fc.type = FC_TRIGGER_FX;
  537. fc.string = new idStr( token );
  538. fc.index = jointInfo->num;
  539. } else if ( token == "start_emitter" ) {
  540. idStr str;
  541. if( !src.ReadTokenOnLine( &token ) ) {
  542. return "Unexpected end of line";
  543. }
  544. str = token + " ";
  545. if( !src.ReadTokenOnLine( &token ) ) {
  546. return "Unexpected end of line";
  547. }
  548. jointInfo = modelDef->FindJoint( token );
  549. if ( !jointInfo ) {
  550. return va( "Joint '%s' not found", token.c_str() );
  551. }
  552. if( !src.ReadTokenOnLine( &token ) ) {
  553. return "Unexpected end of line";
  554. }
  555. str += token;
  556. fc.type = FC_START_EMITTER;
  557. fc.string = new idStr( str );
  558. fc.index = jointInfo->num;
  559. } else if ( token == "stop_emitter" ) {
  560. if( !src.ReadTokenOnLine( &token ) ) {
  561. return "Unexpected end of line";
  562. }
  563. fc.type = FC_STOP_EMITTER;
  564. fc.string = new idStr( token );
  565. #endif
  566. } else if ( token == "footstep" ) {
  567. fc.type = FC_FOOTSTEP;
  568. } else if ( token == "leftfoot" ) {
  569. fc.type = FC_LEFTFOOT;
  570. } else if ( token == "rightfoot" ) {
  571. fc.type = FC_RIGHTFOOT;
  572. } else if ( token == "enableEyeFocus" ) {
  573. fc.type = FC_ENABLE_EYE_FOCUS;
  574. } else if ( token == "disableEyeFocus" ) {
  575. fc.type = FC_DISABLE_EYE_FOCUS;
  576. } else if ( token == "disableGravity" ) {
  577. fc.type = FC_DISABLE_GRAVITY;
  578. } else if ( token == "enableGravity" ) {
  579. fc.type = FC_ENABLE_GRAVITY;
  580. } else if ( token == "jump" ) {
  581. fc.type = FC_JUMP;
  582. } else if ( token == "enableClip" ) {
  583. fc.type = FC_ENABLE_CLIP;
  584. } else if ( token == "disableClip" ) {
  585. fc.type = FC_DISABLE_CLIP;
  586. } else if ( token == "enableWalkIK" ) {
  587. fc.type = FC_ENABLE_WALK_IK;
  588. } else if ( token == "disableWalkIK" ) {
  589. fc.type = FC_DISABLE_WALK_IK;
  590. } else if ( token == "enableLegIK" ) {
  591. if( !src.ReadTokenOnLine( &token ) ) {
  592. return "Unexpected end of line";
  593. }
  594. fc.type = FC_ENABLE_LEG_IK;
  595. fc.index = atoi( token );
  596. } else if ( token == "disableLegIK" ) {
  597. if( !src.ReadTokenOnLine( &token ) ) {
  598. return "Unexpected end of line";
  599. }
  600. fc.type = FC_DISABLE_LEG_IK;
  601. fc.index = atoi( token );
  602. } else if ( token == "recordDemo" ) {
  603. fc.type = FC_RECORDDEMO;
  604. if( src.ReadTokenOnLine( &token ) ) {
  605. fc.string = new idStr( token );
  606. }
  607. } else if ( token == "aviGame" ) {
  608. fc.type = FC_AVIGAME;
  609. if( src.ReadTokenOnLine( &token ) ) {
  610. fc.string = new idStr( token );
  611. }
  612. } else {
  613. return va( "Unknown command '%s'", token.c_str() );
  614. }
  615. // check if we've initialized the frame loopup table
  616. if ( !frameLookup.Num() ) {
  617. // we haven't, so allocate the table and initialize it
  618. frameLookup.SetGranularity( 1 );
  619. frameLookup.SetNum( anims[ 0 ]->NumFrames() );
  620. for( i = 0; i < frameLookup.Num(); i++ ) {
  621. frameLookup[ i ].num = 0;
  622. frameLookup[ i ].firstCommand = 0;
  623. }
  624. }
  625. // allocate space for a new command
  626. frameCommands.Alloc();
  627. // calculate the index of the new command
  628. index = frameLookup[ framenum ].firstCommand + frameLookup[ framenum ].num;
  629. // move all commands from our index onward up one to give us space for our new command
  630. for( i = frameCommands.Num() - 1; i > index; i-- ) {
  631. frameCommands[ i ] = frameCommands[ i - 1 ];
  632. }
  633. // fix the indices of any later frames to account for the inserted command
  634. for( i = framenum + 1; i < frameLookup.Num(); i++ ) {
  635. frameLookup[ i ].firstCommand++;
  636. }
  637. // store the new command
  638. frameCommands[ index ] = fc;
  639. // increase the number of commands on this frame
  640. frameLookup[ framenum ].num++;
  641. // return with no error
  642. return NULL;
  643. }
  644. /*
  645. =====================
  646. idAnim::CallFrameCommands
  647. =====================
  648. */
  649. void idAnim::CallFrameCommands( idEntity *ent, int from, int to ) const {
  650. int index;
  651. int end;
  652. int frame;
  653. int numframes;
  654. numframes = anims[ 0 ]->NumFrames();
  655. frame = from;
  656. while( frame != to ) {
  657. frame++;
  658. if ( frame >= numframes ) {
  659. frame = 0;
  660. }
  661. index = frameLookup[ frame ].firstCommand;
  662. end = index + frameLookup[ frame ].num;
  663. while( index < end ) {
  664. const frameCommand_t &command = frameCommands[ index++ ];
  665. switch( command.type ) {
  666. case FC_SCRIPTFUNCTION: {
  667. gameLocal.CallFrameCommand( ent, command.function );
  668. break;
  669. }
  670. case FC_SCRIPTFUNCTIONOBJECT: {
  671. gameLocal.CallObjectFrameCommand( ent, command.string->c_str() );
  672. break;
  673. }
  674. case FC_EVENTFUNCTION: {
  675. const idEventDef *ev = idEventDef::FindEvent( command.string->c_str() );
  676. ent->ProcessEvent( ev );
  677. break;
  678. }
  679. case FC_SOUND: {
  680. if ( !command.soundShader ) {
  681. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ANY, 0, false, NULL ) ) {
  682. gameLocal.Warning( "Framecommand 'sound' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  683. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  684. }
  685. } else {
  686. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ANY, 0, false, NULL );
  687. }
  688. break;
  689. }
  690. case FC_SOUND_VOICE: {
  691. if ( !command.soundShader ) {
  692. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE, 0, false, NULL ) ) {
  693. gameLocal.Warning( "Framecommand 'sound_voice' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  694. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  695. }
  696. } else {
  697. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE, 0, false, NULL );
  698. }
  699. break;
  700. }
  701. case FC_SOUND_VOICE2: {
  702. if ( !command.soundShader ) {
  703. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE2, 0, false, NULL ) ) {
  704. gameLocal.Warning( "Framecommand 'sound_voice2' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  705. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  706. }
  707. } else {
  708. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE2, 0, false, NULL );
  709. }
  710. break;
  711. }
  712. case FC_SOUND_BODY: {
  713. if ( !command.soundShader ) {
  714. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY, 0, false, NULL ) ) {
  715. gameLocal.Warning( "Framecommand 'sound_body' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  716. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  717. }
  718. } else {
  719. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY, 0, false, NULL );
  720. }
  721. break;
  722. }
  723. case FC_SOUND_BODY2: {
  724. if ( !command.soundShader ) {
  725. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY2, 0, false, NULL ) ) {
  726. gameLocal.Warning( "Framecommand 'sound_body2' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  727. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  728. }
  729. } else {
  730. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY2, 0, false, NULL );
  731. }
  732. break;
  733. }
  734. case FC_SOUND_BODY3: {
  735. if ( !command.soundShader ) {
  736. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY3, 0, false, NULL ) ) {
  737. gameLocal.Warning( "Framecommand 'sound_body3' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  738. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  739. }
  740. } else {
  741. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY3, 0, false, NULL );
  742. }
  743. break;
  744. }
  745. case FC_SOUND_WEAPON: {
  746. if ( !command.soundShader ) {
  747. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_WEAPON, 0, false, NULL ) ) {
  748. gameLocal.Warning( "Framecommand 'sound_weapon' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  749. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  750. }
  751. } else {
  752. ent->StartSoundShader( command.soundShader, SND_CHANNEL_WEAPON, 0, false, NULL );
  753. }
  754. break;
  755. }
  756. case FC_SOUND_GLOBAL: {
  757. if ( !command.soundShader ) {
  758. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ANY, SSF_GLOBAL, false, NULL ) ) {
  759. gameLocal.Warning( "Framecommand 'sound_global' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  760. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  761. }
  762. } else {
  763. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ANY, SSF_GLOBAL, false, NULL );
  764. }
  765. break;
  766. }
  767. case FC_SOUND_ITEM: {
  768. if ( !command.soundShader ) {
  769. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ITEM, 0, false, NULL ) ) {
  770. gameLocal.Warning( "Framecommand 'sound_item' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  771. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  772. }
  773. } else {
  774. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ITEM, 0, false, NULL );
  775. }
  776. break;
  777. }
  778. case FC_SOUND_CHATTER: {
  779. if ( ent->CanPlayChatterSounds() ) {
  780. if ( !command.soundShader ) {
  781. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE, 0, false, NULL ) ) {
  782. gameLocal.Warning( "Framecommand 'sound_chatter' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  783. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  784. }
  785. } else {
  786. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE, 0, false, NULL );
  787. }
  788. }
  789. break;
  790. }
  791. case FC_FX: {
  792. idEntityFx::StartFx( command.string->c_str(), NULL, NULL, ent, true );
  793. break;
  794. }
  795. case FC_SKIN: {
  796. ent->SetSkin( command.skin );
  797. break;
  798. }
  799. case FC_TRIGGER: {
  800. idEntity *target;
  801. target = gameLocal.FindEntity( command.string->c_str() );
  802. if ( target ) {
  803. #ifdef _D3XP
  804. SetTimeState ts(target->timeGroup);
  805. #endif
  806. target->Signal( SIG_TRIGGER );
  807. target->ProcessEvent( &EV_Activate, ent );
  808. target->TriggerGuis();
  809. } else {
  810. gameLocal.Warning( "Framecommand 'trigger' on entity '%s', anim '%s', frame %d: Could not find entity '%s'",
  811. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  812. }
  813. break;
  814. }
  815. case FC_TRIGGER_SMOKE_PARTICLE: {
  816. ent->ProcessEvent( &AI_TriggerParticles, command.string->c_str() );
  817. break;
  818. }
  819. case FC_MELEE: {
  820. ent->ProcessEvent( &AI_AttackMelee, command.string->c_str() );
  821. break;
  822. }
  823. case FC_DIRECTDAMAGE: {
  824. ent->ProcessEvent( &AI_DirectDamage, command.string->c_str() );
  825. break;
  826. }
  827. case FC_BEGINATTACK: {
  828. ent->ProcessEvent( &AI_BeginAttack, command.string->c_str() );
  829. break;
  830. }
  831. case FC_ENDATTACK: {
  832. ent->ProcessEvent( &AI_EndAttack );
  833. break;
  834. }
  835. case FC_MUZZLEFLASH: {
  836. ent->ProcessEvent( &AI_MuzzleFlash, command.string->c_str() );
  837. break;
  838. }
  839. case FC_CREATEMISSILE: {
  840. ent->ProcessEvent( &AI_CreateMissile, command.string->c_str() );
  841. break;
  842. }
  843. case FC_LAUNCHMISSILE: {
  844. ent->ProcessEvent( &AI_AttackMissile, command.string->c_str() );
  845. break;
  846. }
  847. case FC_FIREMISSILEATTARGET: {
  848. ent->ProcessEvent( &AI_FireMissileAtTarget, modelDef->GetJointName( command.index ), command.string->c_str() );
  849. break;
  850. }
  851. #ifdef _D3XP
  852. case FC_LAUNCH_PROJECTILE: {
  853. ent->ProcessEvent( &AI_LaunchProjectile, command.string->c_str() );
  854. break;
  855. }
  856. case FC_TRIGGER_FX: {
  857. ent->ProcessEvent( &AI_TriggerFX, modelDef->GetJointName( command.index ), command.string->c_str() );
  858. break;
  859. }
  860. case FC_START_EMITTER: {
  861. int index = command.string->Find(" ");
  862. if(index >= 0) {
  863. idStr name = command.string->Left(index);
  864. idStr particle = command.string->Right(command.string->Length() - index - 1);
  865. ent->ProcessEvent( &AI_StartEmitter, name.c_str(), modelDef->GetJointName( command.index ), particle.c_str() );
  866. }
  867. }
  868. case FC_STOP_EMITTER: {
  869. ent->ProcessEvent( &AI_StopEmitter, command.string->c_str() );
  870. }
  871. #endif
  872. case FC_FOOTSTEP : {
  873. ent->ProcessEvent( &EV_Footstep );
  874. break;
  875. }
  876. case FC_LEFTFOOT: {
  877. ent->ProcessEvent( &EV_FootstepLeft );
  878. break;
  879. }
  880. case FC_RIGHTFOOT: {
  881. ent->ProcessEvent( &EV_FootstepRight );
  882. break;
  883. }
  884. case FC_ENABLE_EYE_FOCUS: {
  885. ent->ProcessEvent( &AI_EnableEyeFocus );
  886. break;
  887. }
  888. case FC_DISABLE_EYE_FOCUS: {
  889. ent->ProcessEvent( &AI_DisableEyeFocus );
  890. break;
  891. }
  892. case FC_DISABLE_GRAVITY: {
  893. ent->ProcessEvent( &AI_DisableGravity );
  894. break;
  895. }
  896. case FC_ENABLE_GRAVITY: {
  897. ent->ProcessEvent( &AI_EnableGravity );
  898. break;
  899. }
  900. case FC_JUMP: {
  901. ent->ProcessEvent( &AI_JumpFrame );
  902. break;
  903. }
  904. case FC_ENABLE_CLIP: {
  905. ent->ProcessEvent( &AI_EnableClip );
  906. break;
  907. }
  908. case FC_DISABLE_CLIP: {
  909. ent->ProcessEvent( &AI_DisableClip );
  910. break;
  911. }
  912. case FC_ENABLE_WALK_IK: {
  913. ent->ProcessEvent( &EV_EnableWalkIK );
  914. break;
  915. }
  916. case FC_DISABLE_WALK_IK: {
  917. ent->ProcessEvent( &EV_DisableWalkIK );
  918. break;
  919. }
  920. case FC_ENABLE_LEG_IK: {
  921. ent->ProcessEvent( &EV_EnableLegIK, command.index );
  922. break;
  923. }
  924. case FC_DISABLE_LEG_IK: {
  925. ent->ProcessEvent( &EV_DisableLegIK, command.index );
  926. break;
  927. }
  928. case FC_RECORDDEMO: {
  929. if ( command.string ) {
  930. cmdSystem->BufferCommandText( CMD_EXEC_NOW, va( "recordDemo %s", command.string->c_str() ) );
  931. } else {
  932. cmdSystem->BufferCommandText( CMD_EXEC_NOW, "stoprecording" );
  933. }
  934. break;
  935. }
  936. case FC_AVIGAME: {
  937. if ( command.string ) {
  938. cmdSystem->BufferCommandText( CMD_EXEC_NOW, va( "aviGame %s", command.string->c_str() ) );
  939. } else {
  940. cmdSystem->BufferCommandText( CMD_EXEC_NOW, "aviGame" );
  941. }
  942. break;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. /*
  949. =====================
  950. idAnim::FindFrameForFrameCommand
  951. =====================
  952. */
  953. int idAnim::FindFrameForFrameCommand( frameCommandType_t framecommand, const frameCommand_t **command ) const {
  954. int frame;
  955. int index;
  956. int numframes;
  957. int end;
  958. if ( !frameCommands.Num() ) {
  959. return -1;
  960. }
  961. numframes = anims[ 0 ]->NumFrames();
  962. for( frame = 0; frame < numframes; frame++ ) {
  963. end = frameLookup[ frame ].firstCommand + frameLookup[ frame ].num;
  964. for( index = frameLookup[ frame ].firstCommand; index < end; index++ ) {
  965. if ( frameCommands[ index ].type == framecommand ) {
  966. if ( command ) {
  967. *command = &frameCommands[ index ];
  968. }
  969. return frame;
  970. }
  971. }
  972. }
  973. if ( command ) {
  974. *command = NULL;
  975. }
  976. return -1;
  977. }
  978. /*
  979. =====================
  980. idAnim::HasFrameCommands
  981. =====================
  982. */
  983. bool idAnim::HasFrameCommands( void ) const {
  984. if ( !frameCommands.Num() ) {
  985. return false;
  986. }
  987. return true;
  988. }
  989. /*
  990. =====================
  991. idAnim::SetAnimFlags
  992. =====================
  993. */
  994. void idAnim::SetAnimFlags( const animFlags_t &animflags ) {
  995. flags = animflags;
  996. }
  997. /*
  998. =====================
  999. idAnim::GetAnimFlags
  1000. =====================
  1001. */
  1002. const animFlags_t &idAnim::GetAnimFlags( void ) const {
  1003. return flags;
  1004. }
  1005. /***********************************************************************
  1006. idAnimBlend
  1007. ***********************************************************************/
  1008. /*
  1009. =====================
  1010. idAnimBlend::idAnimBlend
  1011. =====================
  1012. */
  1013. idAnimBlend::idAnimBlend( void ) {
  1014. Reset( NULL );
  1015. }
  1016. /*
  1017. =====================
  1018. idAnimBlend::Save
  1019. archives object for save game file
  1020. =====================
  1021. */
  1022. void idAnimBlend::Save( idSaveGame *savefile ) const {
  1023. int i;
  1024. savefile->WriteInt( starttime );
  1025. savefile->WriteInt( endtime );
  1026. savefile->WriteInt( timeOffset );
  1027. savefile->WriteFloat( rate );
  1028. savefile->WriteInt( blendStartTime );
  1029. savefile->WriteInt( blendDuration );
  1030. savefile->WriteFloat( blendStartValue );
  1031. savefile->WriteFloat( blendEndValue );
  1032. for( i = 0; i < ANIM_MaxSyncedAnims; i++ ) {
  1033. savefile->WriteFloat( animWeights[ i ] );
  1034. }
  1035. savefile->WriteShort( cycle );
  1036. savefile->WriteShort( frame );
  1037. savefile->WriteShort( animNum );
  1038. savefile->WriteBool( allowMove );
  1039. savefile->WriteBool( allowFrameCommands );
  1040. }
  1041. /*
  1042. =====================
  1043. idAnimBlend::Restore
  1044. unarchives object from save game file
  1045. =====================
  1046. */
  1047. void idAnimBlend::Restore( idRestoreGame *savefile, const idDeclModelDef *modelDef ) {
  1048. int i;
  1049. this->modelDef = modelDef;
  1050. savefile->ReadInt( starttime );
  1051. savefile->ReadInt( endtime );
  1052. savefile->ReadInt( timeOffset );
  1053. savefile->ReadFloat( rate );
  1054. savefile->ReadInt( blendStartTime );
  1055. savefile->ReadInt( blendDuration );
  1056. savefile->ReadFloat( blendStartValue );
  1057. savefile->ReadFloat( blendEndValue );
  1058. for( i = 0; i < ANIM_MaxSyncedAnims; i++ ) {
  1059. savefile->ReadFloat( animWeights[ i ] );
  1060. }
  1061. savefile->ReadShort( cycle );
  1062. savefile->ReadShort( frame );
  1063. savefile->ReadShort( animNum );
  1064. if ( !modelDef ) {
  1065. animNum = 0;
  1066. } else if ( ( animNum < 0 ) || ( animNum > modelDef->NumAnims() ) ) {
  1067. gameLocal.Warning( "Anim number %d out of range for model '%s' during save game", animNum, modelDef->GetModelName() );
  1068. animNum = 0;
  1069. }
  1070. savefile->ReadBool( allowMove );
  1071. savefile->ReadBool( allowFrameCommands );
  1072. }
  1073. /*
  1074. =====================
  1075. idAnimBlend::Reset
  1076. =====================
  1077. */
  1078. void idAnimBlend::Reset( const idDeclModelDef *_modelDef ) {
  1079. modelDef = _modelDef;
  1080. cycle = 1;
  1081. starttime = 0;
  1082. endtime = 0;
  1083. timeOffset = 0;
  1084. rate = 1.0f;
  1085. frame = 0;
  1086. allowMove = true;
  1087. allowFrameCommands = true;
  1088. animNum = 0;
  1089. memset( animWeights, 0, sizeof( animWeights ) );
  1090. blendStartValue = 0.0f;
  1091. blendEndValue = 0.0f;
  1092. blendStartTime = 0;
  1093. blendDuration = 0;
  1094. }
  1095. /*
  1096. =====================
  1097. idAnimBlend::FullName
  1098. =====================
  1099. */
  1100. const char *idAnimBlend::AnimFullName( void ) const {
  1101. const idAnim *anim = Anim();
  1102. if ( !anim ) {
  1103. return "";
  1104. }
  1105. return anim->FullName();
  1106. }
  1107. /*
  1108. =====================
  1109. idAnimBlend::AnimName
  1110. =====================
  1111. */
  1112. const char *idAnimBlend::AnimName( void ) const {
  1113. const idAnim *anim = Anim();
  1114. if ( !anim ) {
  1115. return "";
  1116. }
  1117. return anim->Name();
  1118. }
  1119. /*
  1120. =====================
  1121. idAnimBlend::NumFrames
  1122. =====================
  1123. */
  1124. int idAnimBlend::NumFrames( void ) const {
  1125. const idAnim *anim = Anim();
  1126. if ( !anim ) {
  1127. return 0;
  1128. }
  1129. return anim->NumFrames();
  1130. }
  1131. /*
  1132. =====================
  1133. idAnimBlend::Length
  1134. =====================
  1135. */
  1136. int idAnimBlend::Length( void ) const {
  1137. const idAnim *anim = Anim();
  1138. if ( !anim ) {
  1139. return 0;
  1140. }
  1141. return anim->Length();
  1142. }
  1143. /*
  1144. =====================
  1145. idAnimBlend::GetWeight
  1146. =====================
  1147. */
  1148. float idAnimBlend::GetWeight( int currentTime ) const {
  1149. int timeDelta;
  1150. float frac;
  1151. float w;
  1152. timeDelta = currentTime - blendStartTime;
  1153. if ( timeDelta <= 0 ) {
  1154. w = blendStartValue;
  1155. } else if ( timeDelta >= blendDuration ) {
  1156. w = blendEndValue;
  1157. } else {
  1158. frac = ( float )timeDelta / ( float )blendDuration;
  1159. w = blendStartValue + ( blendEndValue - blendStartValue ) * frac;
  1160. }
  1161. return w;
  1162. }
  1163. /*
  1164. =====================
  1165. idAnimBlend::GetFinalWeight
  1166. =====================
  1167. */
  1168. float idAnimBlend::GetFinalWeight( void ) const {
  1169. return blendEndValue;
  1170. }
  1171. /*
  1172. =====================
  1173. idAnimBlend::SetWeight
  1174. =====================
  1175. */
  1176. void idAnimBlend::SetWeight( float newweight, int currentTime, int blendTime ) {
  1177. blendStartValue = GetWeight( currentTime );
  1178. blendEndValue = newweight;
  1179. blendStartTime = currentTime - 1;
  1180. blendDuration = blendTime;
  1181. if ( !newweight ) {
  1182. endtime = currentTime + blendTime;
  1183. }
  1184. }
  1185. /*
  1186. =====================
  1187. idAnimBlend::NumSyncedAnims
  1188. =====================
  1189. */
  1190. int idAnimBlend::NumSyncedAnims( void ) const {
  1191. const idAnim *anim = Anim();
  1192. if ( !anim ) {
  1193. return 0;
  1194. }
  1195. return anim->NumAnims();
  1196. }
  1197. /*
  1198. =====================
  1199. idAnimBlend::SetSyncedAnimWeight
  1200. =====================
  1201. */
  1202. bool idAnimBlend::SetSyncedAnimWeight( int num, float weight ) {
  1203. const idAnim *anim = Anim();
  1204. if ( !anim ) {
  1205. return false;
  1206. }
  1207. if ( ( num < 0 ) || ( num > anim->NumAnims() ) ) {
  1208. return false;
  1209. }
  1210. animWeights[ num ] = weight;
  1211. return true;
  1212. }
  1213. /*
  1214. =====================
  1215. idAnimBlend::SetFrame
  1216. =====================
  1217. */
  1218. void idAnimBlend::SetFrame( const idDeclModelDef *modelDef, int _animNum, int _frame, int currentTime, int blendTime ) {
  1219. Reset( modelDef );
  1220. if ( !modelDef ) {
  1221. return;
  1222. }
  1223. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1224. if ( !_anim ) {
  1225. return;
  1226. }
  1227. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1228. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1229. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1230. return;
  1231. }
  1232. animNum = _animNum;
  1233. starttime = currentTime;
  1234. endtime = -1;
  1235. cycle = -1;
  1236. animWeights[ 0 ] = 1.0f;
  1237. frame = _frame;
  1238. // a frame of 0 means it's not a single frame blend, so we set it to frame + 1
  1239. if ( frame <= 0 ) {
  1240. frame = 1;
  1241. } else if ( frame > _anim->NumFrames() ) {
  1242. frame = _anim->NumFrames();
  1243. }
  1244. // set up blend
  1245. blendEndValue = 1.0f;
  1246. blendStartTime = currentTime - 1;
  1247. blendDuration = blendTime;
  1248. blendStartValue = 0.0f;
  1249. }
  1250. /*
  1251. =====================
  1252. idAnimBlend::CycleAnim
  1253. =====================
  1254. */
  1255. void idAnimBlend::CycleAnim( const idDeclModelDef *modelDef, int _animNum, int currentTime, int blendTime ) {
  1256. Reset( modelDef );
  1257. if ( !modelDef ) {
  1258. return;
  1259. }
  1260. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1261. if ( !_anim ) {
  1262. return;
  1263. }
  1264. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1265. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1266. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1267. return;
  1268. }
  1269. animNum = _animNum;
  1270. animWeights[ 0 ] = 1.0f;
  1271. endtime = -1;
  1272. cycle = -1;
  1273. if ( _anim->GetAnimFlags().random_cycle_start ) {
  1274. // start the animation at a random time so that characters don't walk in sync
  1275. starttime = currentTime - gameLocal.random.RandomFloat() * _anim->Length();
  1276. } else {
  1277. starttime = currentTime;
  1278. }
  1279. // set up blend
  1280. blendEndValue = 1.0f;
  1281. blendStartTime = currentTime - 1;
  1282. blendDuration = blendTime;
  1283. blendStartValue = 0.0f;
  1284. }
  1285. /*
  1286. =====================
  1287. idAnimBlend::PlayAnim
  1288. =====================
  1289. */
  1290. void idAnimBlend::PlayAnim( const idDeclModelDef *modelDef, int _animNum, int currentTime, int blendTime ) {
  1291. Reset( modelDef );
  1292. if ( !modelDef ) {
  1293. return;
  1294. }
  1295. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1296. if ( !_anim ) {
  1297. return;
  1298. }
  1299. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1300. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1301. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1302. return;
  1303. }
  1304. animNum = _animNum;
  1305. starttime = currentTime;
  1306. endtime = starttime + _anim->Length();
  1307. cycle = 1;
  1308. animWeights[ 0 ] = 1.0f;
  1309. // set up blend
  1310. blendEndValue = 1.0f;
  1311. blendStartTime = currentTime - 1;
  1312. blendDuration = blendTime;
  1313. blendStartValue = 0.0f;
  1314. }
  1315. /*
  1316. =====================
  1317. idAnimBlend::Clear
  1318. =====================
  1319. */
  1320. void idAnimBlend::Clear( int currentTime, int clearTime ) {
  1321. if ( !clearTime ) {
  1322. Reset( modelDef );
  1323. } else {
  1324. SetWeight( 0.0f, currentTime, clearTime );
  1325. }
  1326. }
  1327. /*
  1328. =====================
  1329. idAnimBlend::IsDone
  1330. =====================
  1331. */
  1332. bool idAnimBlend::IsDone( int currentTime ) const {
  1333. if ( !frame && ( endtime > 0 ) && ( currentTime >= endtime ) ) {
  1334. return true;
  1335. }
  1336. if ( ( blendEndValue <= 0.0f ) && ( currentTime >= ( blendStartTime + blendDuration ) ) ) {
  1337. return true;
  1338. }
  1339. return false;
  1340. }
  1341. /*
  1342. =====================
  1343. idAnimBlend::FrameHasChanged
  1344. =====================
  1345. */
  1346. bool idAnimBlend::FrameHasChanged( int currentTime ) const {
  1347. // if we don't have an anim, no change
  1348. if ( !animNum ) {
  1349. return false;
  1350. }
  1351. // if anim is done playing, no change
  1352. if ( ( endtime > 0 ) && ( currentTime > endtime ) ) {
  1353. return false;
  1354. }
  1355. // if our blend weight changes, we need to update
  1356. if ( ( currentTime < ( blendStartTime + blendDuration ) && ( blendStartValue != blendEndValue ) ) ) {
  1357. return true;
  1358. }
  1359. // if we're a single frame anim and this isn't the frame we started on, we don't need to update
  1360. if ( ( frame || ( NumFrames() == 1 ) ) && ( currentTime != starttime ) ) {
  1361. return false;
  1362. }
  1363. return true;
  1364. }
  1365. /*
  1366. =====================
  1367. idAnimBlend::GetCycleCount
  1368. =====================
  1369. */
  1370. int idAnimBlend::GetCycleCount( void ) const {
  1371. return cycle;
  1372. }
  1373. /*
  1374. =====================
  1375. idAnimBlend::SetCycleCount
  1376. =====================
  1377. */
  1378. void idAnimBlend::SetCycleCount( int count ) {
  1379. const idAnim *anim = Anim();
  1380. if ( !anim ) {
  1381. cycle = -1;
  1382. endtime = 0;
  1383. } else {
  1384. cycle = count;
  1385. if ( cycle < 0 ) {
  1386. cycle = -1;
  1387. endtime = -1;
  1388. } else if ( cycle == 0 ) {
  1389. cycle = 1;
  1390. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1391. if ( rate == 1.0f ) {
  1392. endtime = starttime - timeOffset + anim->Length();
  1393. } else if ( rate != 0.0f ) {
  1394. endtime = starttime - timeOffset + anim->Length() / rate;
  1395. } else {
  1396. endtime = -1;
  1397. }
  1398. } else {
  1399. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1400. if ( rate == 1.0f ) {
  1401. endtime = starttime - timeOffset + anim->Length() * cycle;
  1402. } else if ( rate != 0.0f ) {
  1403. endtime = starttime - timeOffset + ( anim->Length() * cycle ) / rate;
  1404. } else {
  1405. endtime = -1;
  1406. }
  1407. }
  1408. }
  1409. }
  1410. /*
  1411. =====================
  1412. idAnimBlend::SetPlaybackRate
  1413. =====================
  1414. */
  1415. void idAnimBlend::SetPlaybackRate( int currentTime, float newRate ) {
  1416. int animTime;
  1417. if ( rate == newRate ) {
  1418. return;
  1419. }
  1420. animTime = AnimTime( currentTime );
  1421. if ( newRate == 1.0f ) {
  1422. timeOffset = animTime - ( currentTime - starttime );
  1423. } else {
  1424. timeOffset = animTime - ( currentTime - starttime ) * newRate;
  1425. }
  1426. rate = newRate;
  1427. // update the anim endtime
  1428. SetCycleCount( cycle );
  1429. }
  1430. /*
  1431. =====================
  1432. idAnimBlend::GetPlaybackRate
  1433. =====================
  1434. */
  1435. float idAnimBlend::GetPlaybackRate( void ) const {
  1436. return rate;
  1437. }
  1438. /*
  1439. =====================
  1440. idAnimBlend::SetStartTime
  1441. =====================
  1442. */
  1443. void idAnimBlend::SetStartTime( int _startTime ) {
  1444. starttime = _startTime;
  1445. // update the anim endtime
  1446. SetCycleCount( cycle );
  1447. }
  1448. /*
  1449. =====================
  1450. idAnimBlend::GetStartTime
  1451. =====================
  1452. */
  1453. int idAnimBlend::GetStartTime( void ) const {
  1454. if ( !animNum ) {
  1455. return 0;
  1456. }
  1457. return starttime;
  1458. }
  1459. /*
  1460. =====================
  1461. idAnimBlend::GetEndTime
  1462. =====================
  1463. */
  1464. int idAnimBlend::GetEndTime( void ) const {
  1465. if ( !animNum ) {
  1466. return 0;
  1467. }
  1468. return endtime;
  1469. }
  1470. /*
  1471. =====================
  1472. idAnimBlend::PlayLength
  1473. =====================
  1474. */
  1475. int idAnimBlend::PlayLength( void ) const {
  1476. if ( !animNum ) {
  1477. return 0;
  1478. }
  1479. if ( endtime < 0 ) {
  1480. return -1;
  1481. }
  1482. return endtime - starttime + timeOffset;
  1483. }
  1484. /*
  1485. =====================
  1486. idAnimBlend::AllowMovement
  1487. =====================
  1488. */
  1489. void idAnimBlend::AllowMovement( bool allow ) {
  1490. allowMove = allow;
  1491. }
  1492. /*
  1493. =====================
  1494. idAnimBlend::AllowFrameCommands
  1495. =====================
  1496. */
  1497. void idAnimBlend::AllowFrameCommands( bool allow ) {
  1498. allowFrameCommands = allow;
  1499. }
  1500. /*
  1501. =====================
  1502. idAnimBlend::Anim
  1503. =====================
  1504. */
  1505. const idAnim *idAnimBlend::Anim( void ) const {
  1506. if ( !modelDef ) {
  1507. return NULL;
  1508. }
  1509. const idAnim *anim = modelDef->GetAnim( animNum );
  1510. return anim;
  1511. }
  1512. /*
  1513. =====================
  1514. idAnimBlend::AnimNum
  1515. =====================
  1516. */
  1517. int idAnimBlend::AnimNum( void ) const {
  1518. return animNum;
  1519. }
  1520. /*
  1521. =====================
  1522. idAnimBlend::AnimTime
  1523. =====================
  1524. */
  1525. int idAnimBlend::AnimTime( int currentTime ) const {
  1526. int time;
  1527. int length;
  1528. const idAnim *anim = Anim();
  1529. if ( anim ) {
  1530. if ( frame ) {
  1531. return FRAME2MS( frame - 1 );
  1532. }
  1533. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1534. if ( rate == 1.0f ) {
  1535. time = currentTime - starttime + timeOffset;
  1536. } else {
  1537. time = static_cast<int>( ( currentTime - starttime ) * rate ) + timeOffset;
  1538. }
  1539. // given enough time, we can easily wrap time around in our frame calculations, so
  1540. // keep cycling animations' time within the length of the anim.
  1541. length = anim->Length();
  1542. if ( ( cycle < 0 ) && ( length > 0 ) ) {
  1543. time %= length;
  1544. // time will wrap after 24 days (oh no!), resulting in negative results for the %.
  1545. // adding the length gives us the proper result.
  1546. if ( time < 0 ) {
  1547. time += length;
  1548. }
  1549. }
  1550. return time;
  1551. } else {
  1552. return 0;
  1553. }
  1554. }
  1555. /*
  1556. =====================
  1557. idAnimBlend::GetFrameNumber
  1558. =====================
  1559. */
  1560. int idAnimBlend::GetFrameNumber( int currentTime ) const {
  1561. const idMD5Anim *md5anim;
  1562. frameBlend_t frameinfo;
  1563. int animTime;
  1564. const idAnim *anim = Anim();
  1565. if ( !anim ) {
  1566. return 1;
  1567. }
  1568. if ( frame ) {
  1569. return frame;
  1570. }
  1571. md5anim = anim->MD5Anim( 0 );
  1572. animTime = AnimTime( currentTime );
  1573. md5anim->ConvertTimeToFrame( animTime, cycle, frameinfo );
  1574. return frameinfo.frame1 + 1;
  1575. }
  1576. /*
  1577. =====================
  1578. idAnimBlend::CallFrameCommands
  1579. =====================
  1580. */
  1581. void idAnimBlend::CallFrameCommands( idEntity *ent, int fromtime, int totime ) const {
  1582. const idMD5Anim *md5anim;
  1583. frameBlend_t frame1;
  1584. frameBlend_t frame2;
  1585. int fromFrameTime;
  1586. int toFrameTime;
  1587. if ( !allowFrameCommands || !ent || frame || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1588. return;
  1589. }
  1590. const idAnim *anim = Anim();
  1591. if ( !anim || !anim->HasFrameCommands() ) {
  1592. return;
  1593. }
  1594. if ( totime <= starttime ) {
  1595. // don't play until next frame or we'll play commands twice.
  1596. // this happens on the player sometimes.
  1597. return;
  1598. }
  1599. fromFrameTime = AnimTime( fromtime );
  1600. toFrameTime = AnimTime( totime );
  1601. if ( toFrameTime < fromFrameTime ) {
  1602. toFrameTime += anim->Length();
  1603. }
  1604. md5anim = anim->MD5Anim( 0 );
  1605. md5anim->ConvertTimeToFrame( fromFrameTime, cycle, frame1 );
  1606. md5anim->ConvertTimeToFrame( toFrameTime, cycle, frame2 );
  1607. if ( fromFrameTime <= 0 ) {
  1608. // make sure first frame is called
  1609. anim->CallFrameCommands( ent, -1, frame2.frame1 );
  1610. } else {
  1611. anim->CallFrameCommands( ent, frame1.frame1, frame2.frame1 );
  1612. }
  1613. }
  1614. /*
  1615. =====================
  1616. idAnimBlend::BlendAnim
  1617. =====================
  1618. */
  1619. bool idAnimBlend::BlendAnim( int currentTime, int channel, int numJoints, idJointQuat *blendFrame, float &blendWeight, bool removeOriginOffset, bool overrideBlend, bool printInfo ) const {
  1620. int i;
  1621. float lerp;
  1622. float mixWeight;
  1623. const idMD5Anim *md5anim;
  1624. idJointQuat *ptr;
  1625. frameBlend_t frametime;
  1626. idJointQuat *jointFrame;
  1627. idJointQuat *mixFrame;
  1628. int numAnims;
  1629. int time;
  1630. const idAnim *anim = Anim();
  1631. if ( !anim ) {
  1632. return false;
  1633. }
  1634. float weight = GetWeight( currentTime );
  1635. if ( blendWeight > 0.0f ) {
  1636. if ( ( endtime >= 0 ) && ( currentTime >= endtime ) ) {
  1637. return false;
  1638. }
  1639. if ( !weight ) {
  1640. return false;
  1641. }
  1642. if ( overrideBlend ) {
  1643. blendWeight = 1.0f - weight;
  1644. }
  1645. }
  1646. if ( ( channel == ANIMCHANNEL_ALL ) && !blendWeight ) {
  1647. // we don't need a temporary buffer, so just store it directly in the blend frame
  1648. jointFrame = blendFrame;
  1649. } else {
  1650. // allocate a temporary buffer to copy the joints from
  1651. jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  1652. }
  1653. time = AnimTime( currentTime );
  1654. numAnims = anim->NumAnims();
  1655. if ( numAnims == 1 ) {
  1656. md5anim = anim->MD5Anim( 0 );
  1657. if ( frame ) {
  1658. md5anim->GetSingleFrame( frame - 1, jointFrame, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1659. } else {
  1660. md5anim->ConvertTimeToFrame( time, cycle, frametime );
  1661. md5anim->GetInterpolatedFrame( frametime, jointFrame, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1662. }
  1663. } else {
  1664. //
  1665. // need to mix the multipoint anim together first
  1666. //
  1667. // allocate a temporary buffer to copy the joints to
  1668. mixFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  1669. if ( !frame ) {
  1670. anim->MD5Anim( 0 )->ConvertTimeToFrame( time, cycle, frametime );
  1671. }
  1672. ptr = jointFrame;
  1673. mixWeight = 0.0f;
  1674. for( i = 0; i < numAnims; i++ ) {
  1675. if ( animWeights[ i ] > 0.0f ) {
  1676. mixWeight += animWeights[ i ];
  1677. lerp = animWeights[ i ] / mixWeight;
  1678. md5anim = anim->MD5Anim( i );
  1679. if ( frame ) {
  1680. md5anim->GetSingleFrame( frame - 1, ptr, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1681. } else {
  1682. md5anim->GetInterpolatedFrame( frametime, ptr, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1683. }
  1684. // only blend after the first anim is mixed in
  1685. if ( ptr != jointFrame ) {
  1686. SIMDProcessor->BlendJoints( jointFrame, ptr, lerp, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1687. }
  1688. ptr = mixFrame;
  1689. }
  1690. }
  1691. if ( !mixWeight ) {
  1692. return false;
  1693. }
  1694. }
  1695. if ( removeOriginOffset ) {
  1696. if ( allowMove ) {
  1697. #ifdef VELOCITY_MOVE
  1698. jointFrame[ 0 ].t.x = 0.0f;
  1699. #else
  1700. jointFrame[ 0 ].t.Zero();
  1701. #endif
  1702. }
  1703. if ( anim->GetAnimFlags().anim_turn ) {
  1704. jointFrame[ 0 ].q.Set( -0.70710677f, 0.0f, 0.0f, 0.70710677f );
  1705. }
  1706. }
  1707. if ( !blendWeight ) {
  1708. blendWeight = weight;
  1709. if ( channel != ANIMCHANNEL_ALL ) {
  1710. const int *index = modelDef->GetChannelJoints( channel );
  1711. const int num = modelDef->NumJointsOnChannel( channel );
  1712. for( i = 0; i < num; i++ ) {
  1713. int j = index[i];
  1714. blendFrame[j].t = jointFrame[j].t;
  1715. blendFrame[j].q = jointFrame[j].q;
  1716. }
  1717. }
  1718. } else {
  1719. blendWeight += weight;
  1720. lerp = weight / blendWeight;
  1721. SIMDProcessor->BlendJoints( blendFrame, jointFrame, lerp, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1722. }
  1723. if ( printInfo ) {
  1724. if ( frame ) {
  1725. gameLocal.Printf( " %s: '%s', %d, %.2f%%\n", channelNames[ channel ], anim->FullName(), frame, weight * 100.0f );
  1726. } else {
  1727. gameLocal.Printf( " %s: '%s', %.3f, %.2f%%\n", channelNames[ channel ], anim->FullName(), ( float )frametime.frame1 + frametime.backlerp, weight * 100.0f );
  1728. }
  1729. }
  1730. return true;
  1731. }
  1732. /*
  1733. =====================
  1734. idAnimBlend::BlendOrigin
  1735. =====================
  1736. */
  1737. void idAnimBlend::BlendOrigin( int currentTime, idVec3 &blendPos, float &blendWeight, bool removeOriginOffset ) const {
  1738. float lerp;
  1739. idVec3 animpos;
  1740. idVec3 pos;
  1741. int time;
  1742. int num;
  1743. int i;
  1744. if ( frame || ( ( endtime > 0 ) && ( currentTime > endtime ) ) ) {
  1745. return;
  1746. }
  1747. const idAnim *anim = Anim();
  1748. if ( !anim ) {
  1749. return;
  1750. }
  1751. if ( allowMove && removeOriginOffset ) {
  1752. return;
  1753. }
  1754. float weight = GetWeight( currentTime );
  1755. if ( !weight ) {
  1756. return;
  1757. }
  1758. time = AnimTime( currentTime );
  1759. pos.Zero();
  1760. num = anim->NumAnims();
  1761. for( i = 0; i < num; i++ ) {
  1762. anim->GetOrigin( animpos, i, time, cycle );
  1763. pos += animpos * animWeights[ i ];
  1764. }
  1765. if ( !blendWeight ) {
  1766. blendPos = pos;
  1767. blendWeight = weight;
  1768. } else {
  1769. lerp = weight / ( blendWeight + weight );
  1770. blendPos += lerp * ( pos - blendPos );
  1771. blendWeight += weight;
  1772. }
  1773. }
  1774. /*
  1775. =====================
  1776. idAnimBlend::BlendDelta
  1777. =====================
  1778. */
  1779. void idAnimBlend::BlendDelta( int fromtime, int totime, idVec3 &blendDelta, float &blendWeight ) const {
  1780. idVec3 pos1;
  1781. idVec3 pos2;
  1782. idVec3 animpos;
  1783. idVec3 delta;
  1784. int time1;
  1785. int time2;
  1786. float lerp;
  1787. int num;
  1788. int i;
  1789. if ( frame || !allowMove || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1790. return;
  1791. }
  1792. const idAnim *anim = Anim();
  1793. if ( !anim ) {
  1794. return;
  1795. }
  1796. float weight = GetWeight( totime );
  1797. if ( !weight ) {
  1798. return;
  1799. }
  1800. time1 = AnimTime( fromtime );
  1801. time2 = AnimTime( totime );
  1802. if ( time2 < time1 ) {
  1803. time2 += anim->Length();
  1804. }
  1805. num = anim->NumAnims();
  1806. pos1.Zero();
  1807. pos2.Zero();
  1808. for( i = 0; i < num; i++ ) {
  1809. anim->GetOrigin( animpos, i, time1, cycle );
  1810. pos1 += animpos * animWeights[ i ];
  1811. anim->GetOrigin( animpos, i, time2, cycle );
  1812. pos2 += animpos * animWeights[ i ];
  1813. }
  1814. delta = pos2 - pos1;
  1815. if ( !blendWeight ) {
  1816. blendDelta = delta;
  1817. blendWeight = weight;
  1818. } else {
  1819. lerp = weight / ( blendWeight + weight );
  1820. blendDelta += lerp * ( delta - blendDelta );
  1821. blendWeight += weight;
  1822. }
  1823. }
  1824. /*
  1825. =====================
  1826. idAnimBlend::BlendDeltaRotation
  1827. =====================
  1828. */
  1829. void idAnimBlend::BlendDeltaRotation( int fromtime, int totime, idQuat &blendDelta, float &blendWeight ) const {
  1830. idQuat q1;
  1831. idQuat q2;
  1832. idQuat q3;
  1833. int time1;
  1834. int time2;
  1835. float lerp;
  1836. float mixWeight;
  1837. int num;
  1838. int i;
  1839. if ( frame || !allowMove || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1840. return;
  1841. }
  1842. const idAnim *anim = Anim();
  1843. if ( !anim || !anim->GetAnimFlags().anim_turn ) {
  1844. return;
  1845. }
  1846. float weight = GetWeight( totime );
  1847. if ( !weight ) {
  1848. return;
  1849. }
  1850. time1 = AnimTime( fromtime );
  1851. time2 = AnimTime( totime );
  1852. if ( time2 < time1 ) {
  1853. time2 += anim->Length();
  1854. }
  1855. q1.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  1856. q2.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  1857. mixWeight = 0.0f;
  1858. num = anim->NumAnims();
  1859. for( i = 0; i < num; i++ ) {
  1860. if ( animWeights[ i ] > 0.0f ) {
  1861. mixWeight += animWeights[ i ];
  1862. if ( animWeights[ i ] == mixWeight ) {
  1863. anim->GetOriginRotation( q1, i, time1, cycle );
  1864. anim->GetOriginRotation( q2, i, time2, cycle );
  1865. } else {
  1866. lerp = animWeights[ i ] / mixWeight;
  1867. anim->GetOriginRotation( q3, i, time1, cycle );
  1868. q1.Slerp( q1, q3, lerp );
  1869. anim->GetOriginRotation( q3, i, time2, cycle );
  1870. q2.Slerp( q1, q3, lerp );
  1871. }
  1872. }
  1873. }
  1874. q3 = q1.Inverse() * q2;
  1875. if ( !blendWeight ) {
  1876. blendDelta = q3;
  1877. blendWeight = weight;
  1878. } else {
  1879. lerp = weight / ( blendWeight + weight );
  1880. blendDelta.Slerp( blendDelta, q3, lerp );
  1881. blendWeight += weight;
  1882. }
  1883. }
  1884. /*
  1885. =====================
  1886. idAnimBlend::AddBounds
  1887. =====================
  1888. */
  1889. bool idAnimBlend::AddBounds( int currentTime, idBounds &bounds, bool removeOriginOffset ) const {
  1890. int i;
  1891. int num;
  1892. idBounds b;
  1893. int time;
  1894. idVec3 pos;
  1895. bool addorigin;
  1896. if ( ( endtime > 0 ) && ( currentTime > endtime ) ) {
  1897. return false;
  1898. }
  1899. const idAnim *anim = Anim();
  1900. if ( !anim ) {
  1901. return false;
  1902. }
  1903. float weight = GetWeight( currentTime );
  1904. if ( !weight ) {
  1905. return false;
  1906. }
  1907. time = AnimTime( currentTime );
  1908. num = anim->NumAnims();
  1909. addorigin = !allowMove || !removeOriginOffset;
  1910. for( i = 0; i < num; i++ ) {
  1911. if ( anim->GetBounds( b, i, time, cycle ) ) {
  1912. if ( addorigin ) {
  1913. anim->GetOrigin( pos, i, time, cycle );
  1914. b.TranslateSelf( pos );
  1915. }
  1916. bounds.AddBounds( b );
  1917. }
  1918. }
  1919. return true;
  1920. }
  1921. /***********************************************************************
  1922. idDeclModelDef
  1923. ***********************************************************************/
  1924. /*
  1925. =====================
  1926. idDeclModelDef::idDeclModelDef
  1927. =====================
  1928. */
  1929. idDeclModelDef::idDeclModelDef() {
  1930. modelHandle = NULL;
  1931. skin = NULL;
  1932. offset.Zero();
  1933. for ( int i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1934. channelJoints[i].Clear();
  1935. }
  1936. }
  1937. /*
  1938. =====================
  1939. idDeclModelDef::~idDeclModelDef
  1940. =====================
  1941. */
  1942. idDeclModelDef::~idDeclModelDef() {
  1943. FreeData();
  1944. }
  1945. /*
  1946. =================
  1947. idDeclModelDef::Size
  1948. =================
  1949. */
  1950. size_t idDeclModelDef::Size( void ) const {
  1951. return sizeof( idDeclModelDef );
  1952. }
  1953. /*
  1954. =====================
  1955. idDeclModelDef::CopyDecl
  1956. =====================
  1957. */
  1958. void idDeclModelDef::CopyDecl( const idDeclModelDef *decl ) {
  1959. int i;
  1960. FreeData();
  1961. offset = decl->offset;
  1962. modelHandle = decl->modelHandle;
  1963. skin = decl->skin;
  1964. anims.SetNum( decl->anims.Num() );
  1965. for( i = 0; i < anims.Num(); i++ ) {
  1966. anims[ i ] = new idAnim( this, decl->anims[ i ] );
  1967. }
  1968. joints.SetNum( decl->joints.Num() );
  1969. memcpy( joints.Ptr(), decl->joints.Ptr(), decl->joints.Num() * sizeof( joints[0] ) );
  1970. jointParents.SetNum( decl->jointParents.Num() );
  1971. memcpy( jointParents.Ptr(), decl->jointParents.Ptr(), decl->jointParents.Num() * sizeof( jointParents[0] ) );
  1972. for ( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1973. channelJoints[i] = decl->channelJoints[i];
  1974. }
  1975. }
  1976. /*
  1977. =====================
  1978. idDeclModelDef::FreeData
  1979. =====================
  1980. */
  1981. void idDeclModelDef::FreeData( void ) {
  1982. anims.DeleteContents( true );
  1983. joints.Clear();
  1984. jointParents.Clear();
  1985. modelHandle = NULL;
  1986. skin = NULL;
  1987. offset.Zero();
  1988. for ( int i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1989. channelJoints[i].Clear();
  1990. }
  1991. }
  1992. /*
  1993. ================
  1994. idDeclModelDef::DefaultDefinition
  1995. ================
  1996. */
  1997. const char *idDeclModelDef::DefaultDefinition( void ) const {
  1998. return "{ }";
  1999. }
  2000. /*
  2001. ====================
  2002. idDeclModelDef::FindJoint
  2003. ====================
  2004. */
  2005. const jointInfo_t *idDeclModelDef::FindJoint( const char *name ) const {
  2006. int i;
  2007. const idMD5Joint *joint;
  2008. if ( !modelHandle ) {
  2009. return NULL;
  2010. }
  2011. joint = modelHandle->GetJoints();
  2012. for( i = 0; i < joints.Num(); i++, joint++ ) {
  2013. if ( !joint->name.Icmp( name ) ) {
  2014. return &joints[ i ];
  2015. }
  2016. }
  2017. return NULL;
  2018. }
  2019. /*
  2020. =====================
  2021. idDeclModelDef::ModelHandle
  2022. =====================
  2023. */
  2024. idRenderModel *idDeclModelDef::ModelHandle( void ) const {
  2025. return ( idRenderModel * )modelHandle;
  2026. }
  2027. /*
  2028. =====================
  2029. idDeclModelDef::GetJointList
  2030. =====================
  2031. */
  2032. void idDeclModelDef::GetJointList( const char *jointnames, idList<jointHandle_t> &jointList ) const {
  2033. const char *pos;
  2034. idStr jointname;
  2035. const jointInfo_t *joint;
  2036. const jointInfo_t *child;
  2037. int i;
  2038. int num;
  2039. bool getChildren;
  2040. bool subtract;
  2041. if ( !modelHandle ) {
  2042. return;
  2043. }
  2044. jointList.Clear();
  2045. num = modelHandle->NumJoints();
  2046. // scan through list of joints and add each to the joint list
  2047. pos = jointnames;
  2048. while( *pos ) {
  2049. // skip over whitespace
  2050. while( ( *pos != 0 ) && isspace( *pos ) ) {
  2051. pos++;
  2052. }
  2053. if ( !*pos ) {
  2054. // no more names
  2055. break;
  2056. }
  2057. // copy joint name
  2058. jointname = "";
  2059. if ( *pos == '-' ) {
  2060. subtract = true;
  2061. pos++;
  2062. } else {
  2063. subtract = false;
  2064. }
  2065. if ( *pos == '*' ) {
  2066. getChildren = true;
  2067. pos++;
  2068. } else {
  2069. getChildren = false;
  2070. }
  2071. while( ( *pos != 0 ) && !isspace( *pos ) ) {
  2072. jointname += *pos;
  2073. pos++;
  2074. }
  2075. joint = FindJoint( jointname );
  2076. if ( !joint ) {
  2077. gameLocal.Warning( "Unknown joint '%s' in '%s' for model '%s'", jointname.c_str(), jointnames, GetName() );
  2078. continue;
  2079. }
  2080. if ( !subtract ) {
  2081. jointList.AddUnique( joint->num );
  2082. } else {
  2083. jointList.Remove( joint->num );
  2084. }
  2085. if ( getChildren ) {
  2086. // include all joint's children
  2087. child = joint + 1;
  2088. for( i = joint->num + 1; i < num; i++, child++ ) {
  2089. // all children of the joint should follow it in the list.
  2090. // once we reach a joint without a parent or with a parent
  2091. // who is earlier in the list than the specified joint, then
  2092. // we've gone through all it's children.
  2093. if ( child->parentNum < joint->num ) {
  2094. break;
  2095. }
  2096. if ( !subtract ) {
  2097. jointList.AddUnique( child->num );
  2098. } else {
  2099. jointList.Remove( child->num );
  2100. }
  2101. }
  2102. }
  2103. }
  2104. }
  2105. /*
  2106. =====================
  2107. idDeclModelDef::Touch
  2108. =====================
  2109. */
  2110. void idDeclModelDef::Touch( void ) const {
  2111. if ( modelHandle ) {
  2112. renderModelManager->FindModel( modelHandle->Name() );
  2113. }
  2114. }
  2115. /*
  2116. =====================
  2117. idDeclModelDef::GetDefaultSkin
  2118. =====================
  2119. */
  2120. const idDeclSkin *idDeclModelDef::GetDefaultSkin( void ) const {
  2121. return skin;
  2122. }
  2123. /*
  2124. =====================
  2125. idDeclModelDef::GetDefaultPose
  2126. =====================
  2127. */
  2128. const idJointQuat *idDeclModelDef::GetDefaultPose( void ) const {
  2129. return modelHandle->GetDefaultPose();
  2130. }
  2131. /*
  2132. =====================
  2133. idDeclModelDef::SetupJoints
  2134. =====================
  2135. */
  2136. void idDeclModelDef::SetupJoints( int *numJoints, idJointMat **jointList, idBounds &frameBounds, bool removeOriginOffset ) const {
  2137. int num;
  2138. const idJointQuat *pose;
  2139. idJointMat *list;
  2140. if ( !modelHandle || modelHandle->IsDefaultModel() ) {
  2141. Mem_Free16( (*jointList) );
  2142. (*jointList) = NULL;
  2143. frameBounds.Clear();
  2144. return;
  2145. }
  2146. // get the number of joints
  2147. num = modelHandle->NumJoints();
  2148. if ( !num ) {
  2149. gameLocal.Error( "model '%s' has no joints", modelHandle->Name() );
  2150. }
  2151. // set up initial pose for model (with no pose, model is just a jumbled mess)
  2152. list = (idJointMat *) Mem_Alloc16( num * sizeof( list[0] ) );
  2153. pose = GetDefaultPose();
  2154. // convert the joint quaternions to joint matrices
  2155. SIMDProcessor->ConvertJointQuatsToJointMats( list, pose, joints.Num() );
  2156. // check if we offset the model by the origin joint
  2157. if ( removeOriginOffset ) {
  2158. #ifdef VELOCITY_MOVE
  2159. list[ 0 ].SetTranslation( idVec3( offset.x, offset.y + pose[0].t.y, offset.z + pose[0].t.z ) );
  2160. #else
  2161. list[ 0 ].SetTranslation( offset );
  2162. #endif
  2163. } else {
  2164. list[ 0 ].SetTranslation( pose[0].t + offset );
  2165. }
  2166. // transform the joint hierarchy
  2167. SIMDProcessor->TransformJoints( list, jointParents.Ptr(), 1, joints.Num() - 1 );
  2168. *numJoints = num;
  2169. *jointList = list;
  2170. // get the bounds of the default pose
  2171. frameBounds = modelHandle->Bounds( NULL );
  2172. }
  2173. /*
  2174. =====================
  2175. idDeclModelDef::ParseAnim
  2176. =====================
  2177. */
  2178. bool idDeclModelDef::ParseAnim( idLexer &src, int numDefaultAnims ) {
  2179. int i;
  2180. int len;
  2181. idAnim *anim;
  2182. const idMD5Anim *md5anims[ ANIM_MaxSyncedAnims ];
  2183. const idMD5Anim *md5anim;
  2184. idStr alias;
  2185. idToken realname;
  2186. idToken token;
  2187. int numAnims;
  2188. animFlags_t flags;
  2189. numAnims = 0;
  2190. memset( md5anims, 0, sizeof( md5anims ) );
  2191. if( !src.ReadToken( &realname ) ) {
  2192. src.Warning( "Unexpected end of file" );
  2193. MakeDefault();
  2194. return false;
  2195. }
  2196. alias = realname;
  2197. for( i = 0; i < anims.Num(); i++ ) {
  2198. if ( !strcmp( anims[ i ]->FullName(), realname ) ) {
  2199. break;
  2200. }
  2201. }
  2202. if ( ( i < anims.Num() ) && ( i >= numDefaultAnims ) ) {
  2203. src.Warning( "Duplicate anim '%s'", realname.c_str() );
  2204. MakeDefault();
  2205. return false;
  2206. }
  2207. if ( i < numDefaultAnims ) {
  2208. anim = anims[ i ];
  2209. } else {
  2210. // create the alias associated with this animation
  2211. anim = new idAnim();
  2212. anims.Append( anim );
  2213. }
  2214. // random anims end with a number. find the numeric suffix of the animation.
  2215. len = alias.Length();
  2216. for( i = len - 1; i > 0; i-- ) {
  2217. if ( !isdigit( alias[ i ] ) ) {
  2218. break;
  2219. }
  2220. }
  2221. // check for zero length name, or a purely numeric name
  2222. if ( i <= 0 ) {
  2223. src.Warning( "Invalid animation name '%s'", alias.c_str() );
  2224. MakeDefault();
  2225. return false;
  2226. }
  2227. // remove the numeric suffix
  2228. alias.CapLength( i + 1 );
  2229. // parse the anims from the string
  2230. do {
  2231. if( !src.ReadToken( &token ) ) {
  2232. src.Warning( "Unexpected end of file" );
  2233. MakeDefault();
  2234. return false;
  2235. }
  2236. // lookup the animation
  2237. md5anim = animationLib.GetAnim( token );
  2238. if ( !md5anim ) {
  2239. src.Warning( "Couldn't load anim '%s'", token.c_str() );
  2240. MakeDefault();
  2241. return false;
  2242. }
  2243. md5anim->CheckModelHierarchy( modelHandle );
  2244. if ( numAnims > 0 ) {
  2245. // make sure it's the same length as the other anims
  2246. if ( md5anim->Length() != md5anims[ 0 ]->Length() ) {
  2247. src.Warning( "Anim '%s' does not match length of anim '%s'", md5anim->Name(), md5anims[ 0 ]->Name() );
  2248. MakeDefault();
  2249. return false;
  2250. }
  2251. }
  2252. if ( numAnims >= ANIM_MaxSyncedAnims ) {
  2253. src.Warning( "Exceeded max synced anims (%d)", ANIM_MaxSyncedAnims );
  2254. MakeDefault();
  2255. return false;
  2256. }
  2257. // add it to our list
  2258. md5anims[ numAnims ] = md5anim;
  2259. numAnims++;
  2260. } while ( src.CheckTokenString( "," ) );
  2261. if ( !numAnims ) {
  2262. src.Warning( "No animation specified" );
  2263. MakeDefault();
  2264. return false;
  2265. }
  2266. anim->SetAnim( this, realname, alias, numAnims, md5anims );
  2267. memset( &flags, 0, sizeof( flags ) );
  2268. // parse any frame commands or animflags
  2269. if ( src.CheckTokenString( "{" ) ) {
  2270. while( 1 ) {
  2271. if( !src.ReadToken( &token ) ) {
  2272. src.Warning( "Unexpected end of file" );
  2273. MakeDefault();
  2274. return false;
  2275. }
  2276. if ( token == "}" ) {
  2277. break;
  2278. }else if ( token == "prevent_idle_override" ) {
  2279. flags.prevent_idle_override = true;
  2280. } else if ( token == "random_cycle_start" ) {
  2281. flags.random_cycle_start = true;
  2282. } else if ( token == "ai_no_turn" ) {
  2283. flags.ai_no_turn = true;
  2284. } else if ( token == "anim_turn" ) {
  2285. flags.anim_turn = true;
  2286. } else if ( token == "frame" ) {
  2287. // create a frame command
  2288. int framenum;
  2289. const char *err;
  2290. // make sure we don't have any line breaks while reading the frame command so the error line # will be correct
  2291. if ( !src.ReadTokenOnLine( &token ) ) {
  2292. src.Warning( "Missing frame # after 'frame'" );
  2293. MakeDefault();
  2294. return false;
  2295. }
  2296. if ( token.type == TT_PUNCTUATION && token == "-" ) {
  2297. src.Warning( "Invalid frame # after 'frame'" );
  2298. MakeDefault();
  2299. return false;
  2300. } else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  2301. src.Error( "expected integer value, found '%s'", token.c_str() );
  2302. }
  2303. // get the frame number
  2304. framenum = token.GetIntValue();
  2305. // put the command on the specified frame of the animation
  2306. err = anim->AddFrameCommand( this, framenum, src, NULL );
  2307. if ( err ) {
  2308. src.Warning( "%s", err );
  2309. MakeDefault();
  2310. return false;
  2311. }
  2312. } else {
  2313. src.Warning( "Unknown command '%s'", token.c_str() );
  2314. MakeDefault();
  2315. return false;
  2316. }
  2317. }
  2318. }
  2319. // set the flags
  2320. anim->SetAnimFlags( flags );
  2321. return true;
  2322. }
  2323. /*
  2324. ================
  2325. idDeclModelDef::Parse
  2326. ================
  2327. */
  2328. bool idDeclModelDef::Parse( const char *text, const int textLength ) {
  2329. int i;
  2330. int num;
  2331. idStr filename;
  2332. idStr extension;
  2333. const idMD5Joint *md5joint;
  2334. const idMD5Joint *md5joints;
  2335. idLexer src;
  2336. idToken token;
  2337. idToken token2;
  2338. idStr jointnames;
  2339. int channel;
  2340. jointHandle_t jointnum;
  2341. idList<jointHandle_t> jointList;
  2342. int numDefaultAnims;
  2343. src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
  2344. src.SetFlags( DECL_LEXER_FLAGS );
  2345. src.SkipUntilString( "{" );
  2346. numDefaultAnims = 0;
  2347. while( 1 ) {
  2348. if ( !src.ReadToken( &token ) ) {
  2349. break;
  2350. }
  2351. if ( !token.Icmp( "}" ) ) {
  2352. break;
  2353. }
  2354. if ( token == "inherit" ) {
  2355. if( !src.ReadToken( &token2 ) ) {
  2356. src.Warning( "Unexpected end of file" );
  2357. MakeDefault();
  2358. return false;
  2359. }
  2360. const idDeclModelDef *copy = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, token2, false ) );
  2361. if ( !copy ) {
  2362. common->Warning( "Unknown model definition '%s'", token2.c_str() );
  2363. } else if ( copy->GetState() == DS_DEFAULTED ) {
  2364. common->Warning( "inherited model definition '%s' defaulted", token2.c_str() );
  2365. MakeDefault();
  2366. return false;
  2367. } else {
  2368. CopyDecl( copy );
  2369. numDefaultAnims = anims.Num();
  2370. }
  2371. } else if ( token == "skin" ) {
  2372. if( !src.ReadToken( &token2 ) ) {
  2373. src.Warning( "Unexpected end of file" );
  2374. MakeDefault();
  2375. return false;
  2376. }
  2377. skin = declManager->FindSkin( token2 );
  2378. if ( !skin ) {
  2379. src.Warning( "Skin '%s' not found", token2.c_str() );
  2380. MakeDefault();
  2381. return false;
  2382. }
  2383. } else if ( token == "mesh" ) {
  2384. if( !src.ReadToken( &token2 ) ) {
  2385. src.Warning( "Unexpected end of file" );
  2386. MakeDefault();
  2387. return false;
  2388. }
  2389. filename = token2;
  2390. filename.ExtractFileExtension( extension );
  2391. if ( extension != MD5_MESH_EXT ) {
  2392. src.Warning( "Invalid model for MD5 mesh" );
  2393. MakeDefault();
  2394. return false;
  2395. }
  2396. modelHandle = renderModelManager->FindModel( filename );
  2397. if ( !modelHandle ) {
  2398. src.Warning( "Model '%s' not found", filename.c_str() );
  2399. MakeDefault();
  2400. return false;
  2401. }
  2402. if ( modelHandle->IsDefaultModel() ) {
  2403. src.Warning( "Model '%s' defaulted", filename.c_str() );
  2404. MakeDefault();
  2405. return false;
  2406. }
  2407. // get the number of joints
  2408. num = modelHandle->NumJoints();
  2409. if ( !num ) {
  2410. src.Warning( "Model '%s' has no joints", filename.c_str() );
  2411. }
  2412. // set up the joint hierarchy
  2413. joints.SetGranularity( 1 );
  2414. joints.SetNum( num );
  2415. jointParents.SetNum( num );
  2416. channelJoints[0].SetNum( num );
  2417. md5joints = modelHandle->GetJoints();
  2418. md5joint = md5joints;
  2419. for( i = 0; i < num; i++, md5joint++ ) {
  2420. joints[i].channel = ANIMCHANNEL_ALL;
  2421. joints[i].num = static_cast<jointHandle_t>( i );
  2422. if ( md5joint->parent ) {
  2423. joints[i].parentNum = static_cast<jointHandle_t>( md5joint->parent - md5joints );
  2424. } else {
  2425. joints[i].parentNum = INVALID_JOINT;
  2426. }
  2427. jointParents[i] = joints[i].parentNum;
  2428. channelJoints[0][i] = i;
  2429. }
  2430. } else if ( token == "remove" ) {
  2431. // removes any anims whos name matches
  2432. if( !src.ReadToken( &token2 ) ) {
  2433. src.Warning( "Unexpected end of file" );
  2434. MakeDefault();
  2435. return false;
  2436. }
  2437. num = 0;
  2438. for( i = 0; i < anims.Num(); i++ ) {
  2439. if ( ( token2 == anims[ i ]->Name() ) || ( token2 == anims[ i ]->FullName() ) ) {
  2440. delete anims[ i ];
  2441. anims.RemoveIndex( i );
  2442. if ( i >= numDefaultAnims ) {
  2443. src.Warning( "Anim '%s' was not inherited. Anim should be removed from the model def.", token2.c_str() );
  2444. MakeDefault();
  2445. return false;
  2446. }
  2447. i--;
  2448. numDefaultAnims--;
  2449. num++;
  2450. continue;
  2451. }
  2452. }
  2453. if ( !num ) {
  2454. src.Warning( "Couldn't find anim '%s' to remove", token2.c_str() );
  2455. MakeDefault();
  2456. return false;
  2457. }
  2458. } else if ( token == "anim" ) {
  2459. if ( !modelHandle ) {
  2460. src.Warning( "Must specify mesh before defining anims" );
  2461. MakeDefault();
  2462. return false;
  2463. }
  2464. if ( !ParseAnim( src, numDefaultAnims ) ) {
  2465. MakeDefault();
  2466. return false;
  2467. }
  2468. } else if ( token == "offset" ) {
  2469. if ( !src.Parse1DMatrix( 3, offset.ToFloatPtr() ) ) {
  2470. src.Warning( "Expected vector following 'offset'" );
  2471. MakeDefault();
  2472. return false;
  2473. }
  2474. } else if ( token == "channel" ) {
  2475. if ( !modelHandle ) {
  2476. src.Warning( "Must specify mesh before defining channels" );
  2477. MakeDefault();
  2478. return false;
  2479. }
  2480. // set the channel for a group of joints
  2481. if( !src.ReadToken( &token2 ) ) {
  2482. src.Warning( "Unexpected end of file" );
  2483. MakeDefault();
  2484. return false;
  2485. }
  2486. if ( !src.CheckTokenString( "(" ) ) {
  2487. src.Warning( "Expected { after '%s'\n", token2.c_str() );
  2488. MakeDefault();
  2489. return false;
  2490. }
  2491. for( i = ANIMCHANNEL_ALL + 1; i < ANIM_NumAnimChannels; i++ ) {
  2492. if ( !idStr::Icmp( channelNames[ i ], token2 ) ) {
  2493. break;
  2494. }
  2495. }
  2496. if ( i >= ANIM_NumAnimChannels ) {
  2497. src.Warning( "Unknown channel '%s'", token2.c_str() );
  2498. MakeDefault();
  2499. return false;
  2500. }
  2501. channel = i;
  2502. jointnames = "";
  2503. while( !src.CheckTokenString( ")" ) ) {
  2504. if( !src.ReadToken( &token2 ) ) {
  2505. src.Warning( "Unexpected end of file" );
  2506. MakeDefault();
  2507. return false;
  2508. }
  2509. jointnames += token2;
  2510. if ( ( token2 != "*" ) && ( token2 != "-" ) ) {
  2511. jointnames += " ";
  2512. }
  2513. }
  2514. GetJointList( jointnames, jointList );
  2515. channelJoints[ channel ].SetNum( jointList.Num() );
  2516. for( num = i = 0; i < jointList.Num(); i++ ) {
  2517. jointnum = jointList[ i ];
  2518. if ( joints[ jointnum ].channel != ANIMCHANNEL_ALL ) {
  2519. src.Warning( "Joint '%s' assigned to multiple channels", modelHandle->GetJointName( jointnum ) );
  2520. continue;
  2521. }
  2522. joints[ jointnum ].channel = channel;
  2523. channelJoints[ channel ][ num++ ] = jointnum;
  2524. }
  2525. channelJoints[ channel ].SetNum( num );
  2526. } else {
  2527. src.Warning( "unknown token '%s'", token.c_str() );
  2528. MakeDefault();
  2529. return false;
  2530. }
  2531. }
  2532. // shrink the anim list down to save space
  2533. anims.SetGranularity( 1 );
  2534. anims.SetNum( anims.Num() );
  2535. return true;
  2536. }
  2537. /*
  2538. =====================
  2539. idDeclModelDef::HasAnim
  2540. =====================
  2541. */
  2542. bool idDeclModelDef::HasAnim( const char *name ) const {
  2543. int i;
  2544. // find any animations with same name
  2545. for( i = 0; i < anims.Num(); i++ ) {
  2546. if ( !strcmp( anims[ i ]->Name(), name ) ) {
  2547. return true;
  2548. }
  2549. }
  2550. return false;
  2551. }
  2552. /*
  2553. =====================
  2554. idDeclModelDef::NumAnims
  2555. =====================
  2556. */
  2557. int idDeclModelDef::NumAnims( void ) const {
  2558. return anims.Num() + 1;
  2559. }
  2560. /*
  2561. =====================
  2562. idDeclModelDef::GetSpecificAnim
  2563. Gets the exact anim for the name, without randomization.
  2564. =====================
  2565. */
  2566. int idDeclModelDef::GetSpecificAnim( const char *name ) const {
  2567. int i;
  2568. // find a specific animation
  2569. for( i = 0; i < anims.Num(); i++ ) {
  2570. if ( !strcmp( anims[ i ]->FullName(), name ) ) {
  2571. return i + 1;
  2572. }
  2573. }
  2574. // didn't find it
  2575. return 0;
  2576. }
  2577. /*
  2578. =====================
  2579. idDeclModelDef::GetAnim
  2580. =====================
  2581. */
  2582. const idAnim *idDeclModelDef::GetAnim( int index ) const {
  2583. if ( ( index < 1 ) || ( index > anims.Num() ) ) {
  2584. return NULL;
  2585. }
  2586. return anims[ index - 1 ];
  2587. }
  2588. /*
  2589. =====================
  2590. idDeclModelDef::GetAnim
  2591. =====================
  2592. */
  2593. int idDeclModelDef::GetAnim( const char *name ) const {
  2594. int i;
  2595. int which;
  2596. const int MAX_ANIMS = 64;
  2597. int animList[ MAX_ANIMS ];
  2598. int numAnims;
  2599. int len;
  2600. len = strlen( name );
  2601. if ( len && idStr::CharIsNumeric( name[ len - 1 ] ) ) {
  2602. // find a specific animation
  2603. return GetSpecificAnim( name );
  2604. }
  2605. // find all animations with same name
  2606. numAnims = 0;
  2607. for( i = 0; i < anims.Num(); i++ ) {
  2608. if ( !strcmp( anims[ i ]->Name(), name ) ) {
  2609. animList[ numAnims++ ] = i;
  2610. if ( numAnims >= MAX_ANIMS ) {
  2611. break;
  2612. }
  2613. }
  2614. }
  2615. if ( !numAnims ) {
  2616. return 0;
  2617. }
  2618. // get a random anim
  2619. //FIXME: don't access gameLocal here?
  2620. which = gameLocal.random.RandomInt( numAnims );
  2621. return animList[ which ] + 1;
  2622. }
  2623. /*
  2624. =====================
  2625. idDeclModelDef::GetSkin
  2626. =====================
  2627. */
  2628. const idDeclSkin *idDeclModelDef::GetSkin( void ) const {
  2629. return skin;
  2630. }
  2631. /*
  2632. =====================
  2633. idDeclModelDef::GetModelName
  2634. =====================
  2635. */
  2636. const char *idDeclModelDef::GetModelName( void ) const {
  2637. if ( modelHandle ) {
  2638. return modelHandle->Name();
  2639. } else {
  2640. return "";
  2641. }
  2642. }
  2643. /*
  2644. =====================
  2645. idDeclModelDef::Joints
  2646. =====================
  2647. */
  2648. const idList<jointInfo_t> &idDeclModelDef::Joints( void ) const {
  2649. return joints;
  2650. }
  2651. /*
  2652. =====================
  2653. idDeclModelDef::JointParents
  2654. =====================
  2655. */
  2656. const int * idDeclModelDef::JointParents( void ) const {
  2657. return jointParents.Ptr();
  2658. }
  2659. /*
  2660. =====================
  2661. idDeclModelDef::NumJoints
  2662. =====================
  2663. */
  2664. int idDeclModelDef::NumJoints( void ) const {
  2665. return joints.Num();
  2666. }
  2667. /*
  2668. =====================
  2669. idDeclModelDef::GetJoint
  2670. =====================
  2671. */
  2672. const jointInfo_t *idDeclModelDef::GetJoint( int jointHandle ) const {
  2673. if ( ( jointHandle < 0 ) || ( jointHandle > joints.Num() ) ) {
  2674. gameLocal.Error( "idDeclModelDef::GetJoint : joint handle out of range" );
  2675. }
  2676. return &joints[ jointHandle ];
  2677. }
  2678. /*
  2679. ====================
  2680. idDeclModelDef::GetJointName
  2681. ====================
  2682. */
  2683. const char *idDeclModelDef::GetJointName( int jointHandle ) const {
  2684. const idMD5Joint *joint;
  2685. if ( !modelHandle ) {
  2686. return NULL;
  2687. }
  2688. if ( ( jointHandle < 0 ) || ( jointHandle > joints.Num() ) ) {
  2689. gameLocal.Error( "idDeclModelDef::GetJointName : joint handle out of range" );
  2690. }
  2691. joint = modelHandle->GetJoints();
  2692. return joint[ jointHandle ].name.c_str();
  2693. }
  2694. /*
  2695. =====================
  2696. idDeclModelDef::NumJointsOnChannel
  2697. =====================
  2698. */
  2699. int idDeclModelDef::NumJointsOnChannel( int channel ) const {
  2700. if ( ( channel < 0 ) || ( channel >= ANIM_NumAnimChannels ) ) {
  2701. gameLocal.Error( "idDeclModelDef::NumJointsOnChannel : channel out of range" );
  2702. }
  2703. return channelJoints[ channel ].Num();
  2704. }
  2705. /*
  2706. =====================
  2707. idDeclModelDef::GetChannelJoints
  2708. =====================
  2709. */
  2710. const int * idDeclModelDef::GetChannelJoints( int channel ) const {
  2711. if ( ( channel < 0 ) || ( channel >= ANIM_NumAnimChannels ) ) {
  2712. gameLocal.Error( "idDeclModelDef::GetChannelJoints : channel out of range" );
  2713. }
  2714. return channelJoints[ channel ].Ptr();
  2715. }
  2716. /*
  2717. =====================
  2718. idDeclModelDef::GetVisualOffset
  2719. =====================
  2720. */
  2721. const idVec3 &idDeclModelDef::GetVisualOffset( void ) const {
  2722. return offset;
  2723. }
  2724. /***********************************************************************
  2725. idAnimator
  2726. ***********************************************************************/
  2727. /*
  2728. =====================
  2729. idAnimator::idAnimator
  2730. =====================
  2731. */
  2732. idAnimator::idAnimator() {
  2733. int i, j;
  2734. modelDef = NULL;
  2735. entity = NULL;
  2736. numJoints = 0;
  2737. joints = NULL;
  2738. lastTransformTime = -1;
  2739. stoppedAnimatingUpdate = false;
  2740. removeOriginOffset = false;
  2741. forceUpdate = false;
  2742. frameBounds.Clear();
  2743. AFPoseJoints.SetGranularity( 1 );
  2744. AFPoseJointMods.SetGranularity( 1 );
  2745. AFPoseJointFrame.SetGranularity( 1 );
  2746. ClearAFPose();
  2747. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2748. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2749. channels[ i ][ j ].Reset( NULL );
  2750. }
  2751. }
  2752. }
  2753. /*
  2754. =====================
  2755. idAnimator::~idAnimator
  2756. =====================
  2757. */
  2758. idAnimator::~idAnimator() {
  2759. FreeData();
  2760. }
  2761. /*
  2762. =====================
  2763. idAnimator::Allocated
  2764. =====================
  2765. */
  2766. size_t idAnimator::Allocated( void ) const {
  2767. size_t size;
  2768. size = jointMods.Allocated() + numJoints * sizeof( joints[0] ) + jointMods.Num() * sizeof( jointMods[ 0 ] ) + AFPoseJointMods.Allocated() + AFPoseJointFrame.Allocated() + AFPoseJoints.Allocated();
  2769. return size;
  2770. }
  2771. /*
  2772. =====================
  2773. idAnimator::Save
  2774. archives object for save game file
  2775. =====================
  2776. */
  2777. void idAnimator::Save( idSaveGame *savefile ) const {
  2778. int i;
  2779. int j;
  2780. savefile->WriteModelDef( modelDef );
  2781. savefile->WriteObject( entity );
  2782. savefile->WriteInt( jointMods.Num() );
  2783. for( i = 0; i < jointMods.Num(); i++ ) {
  2784. savefile->WriteInt( jointMods[ i ]->jointnum );
  2785. savefile->WriteMat3( jointMods[ i ]->mat );
  2786. savefile->WriteVec3( jointMods[ i ]->pos );
  2787. savefile->WriteInt( (int&)jointMods[ i ]->transform_pos );
  2788. savefile->WriteInt( (int&)jointMods[ i ]->transform_axis );
  2789. }
  2790. savefile->WriteInt( numJoints );
  2791. for ( i = 0; i < numJoints; i++ ) {
  2792. float *data = joints[i].ToFloatPtr();
  2793. for ( j = 0; j < 12; j++ ) {
  2794. savefile->WriteFloat( data[j] );
  2795. }
  2796. }
  2797. savefile->WriteInt( lastTransformTime );
  2798. savefile->WriteBool( stoppedAnimatingUpdate );
  2799. savefile->WriteBool( forceUpdate );
  2800. savefile->WriteBounds( frameBounds );
  2801. savefile->WriteFloat( AFPoseBlendWeight );
  2802. savefile->WriteInt( AFPoseJoints.Num() );
  2803. for ( i = 0; i < AFPoseJoints.Num(); i++ ) {
  2804. savefile->WriteInt( AFPoseJoints[i] );
  2805. }
  2806. savefile->WriteInt( AFPoseJointMods.Num() );
  2807. for ( i = 0; i < AFPoseJointMods.Num(); i++ ) {
  2808. savefile->WriteInt( (int&)AFPoseJointMods[i].mod );
  2809. savefile->WriteMat3( AFPoseJointMods[i].axis );
  2810. savefile->WriteVec3( AFPoseJointMods[i].origin );
  2811. }
  2812. savefile->WriteInt( AFPoseJointFrame.Num() );
  2813. for ( i = 0; i < AFPoseJointFrame.Num(); i++ ) {
  2814. savefile->WriteFloat( AFPoseJointFrame[i].q.x );
  2815. savefile->WriteFloat( AFPoseJointFrame[i].q.y );
  2816. savefile->WriteFloat( AFPoseJointFrame[i].q.z );
  2817. savefile->WriteFloat( AFPoseJointFrame[i].q.w );
  2818. savefile->WriteVec3( AFPoseJointFrame[i].t );
  2819. }
  2820. savefile->WriteBounds( AFPoseBounds );
  2821. savefile->WriteInt( AFPoseTime );
  2822. savefile->WriteBool( removeOriginOffset );
  2823. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2824. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2825. channels[ i ][ j ].Save( savefile );
  2826. }
  2827. }
  2828. }
  2829. /*
  2830. =====================
  2831. idAnimator::Restore
  2832. unarchives object from save game file
  2833. =====================
  2834. */
  2835. void idAnimator::Restore( idRestoreGame *savefile ) {
  2836. int i;
  2837. int j;
  2838. int num;
  2839. savefile->ReadModelDef( modelDef );
  2840. savefile->ReadObject( reinterpret_cast<idClass *&>( entity ) );
  2841. savefile->ReadInt( num );
  2842. jointMods.SetNum( num );
  2843. for( i = 0; i < num; i++ ) {
  2844. jointMods[ i ] = new jointMod_t;
  2845. savefile->ReadInt( (int&)jointMods[ i ]->jointnum );
  2846. savefile->ReadMat3( jointMods[ i ]->mat );
  2847. savefile->ReadVec3( jointMods[ i ]->pos );
  2848. savefile->ReadInt( (int&)jointMods[ i ]->transform_pos );
  2849. savefile->ReadInt( (int&)jointMods[ i ]->transform_axis );
  2850. }
  2851. savefile->ReadInt( numJoints );
  2852. joints = (idJointMat *) Mem_Alloc16( numJoints * sizeof( joints[0] ) );
  2853. for ( i = 0; i < numJoints; i++ ) {
  2854. float *data = joints[i].ToFloatPtr();
  2855. for ( j = 0; j < 12; j++ ) {
  2856. savefile->ReadFloat( data[j] );
  2857. }
  2858. }
  2859. savefile->ReadInt( lastTransformTime );
  2860. savefile->ReadBool( stoppedAnimatingUpdate );
  2861. savefile->ReadBool( forceUpdate );
  2862. savefile->ReadBounds( frameBounds );
  2863. savefile->ReadFloat( AFPoseBlendWeight );
  2864. savefile->ReadInt( num );
  2865. AFPoseJoints.SetGranularity( 1 );
  2866. AFPoseJoints.SetNum( num );
  2867. for ( i = 0; i < AFPoseJoints.Num(); i++ ) {
  2868. savefile->ReadInt( AFPoseJoints[i] );
  2869. }
  2870. savefile->ReadInt( num );
  2871. AFPoseJointMods.SetGranularity( 1 );
  2872. AFPoseJointMods.SetNum( num );
  2873. for ( i = 0; i < AFPoseJointMods.Num(); i++ ) {
  2874. savefile->ReadInt( (int&)AFPoseJointMods[i].mod );
  2875. savefile->ReadMat3( AFPoseJointMods[i].axis );
  2876. savefile->ReadVec3( AFPoseJointMods[i].origin );
  2877. }
  2878. savefile->ReadInt( num );
  2879. AFPoseJointFrame.SetGranularity( 1 );
  2880. AFPoseJointFrame.SetNum( num );
  2881. for ( i = 0; i < AFPoseJointFrame.Num(); i++ ) {
  2882. savefile->ReadFloat( AFPoseJointFrame[i].q.x );
  2883. savefile->ReadFloat( AFPoseJointFrame[i].q.y );
  2884. savefile->ReadFloat( AFPoseJointFrame[i].q.z );
  2885. savefile->ReadFloat( AFPoseJointFrame[i].q.w );
  2886. savefile->ReadVec3( AFPoseJointFrame[i].t );
  2887. }
  2888. savefile->ReadBounds( AFPoseBounds );
  2889. savefile->ReadInt( AFPoseTime );
  2890. savefile->ReadBool( removeOriginOffset );
  2891. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2892. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2893. channels[ i ][ j ].Restore( savefile, modelDef );
  2894. }
  2895. }
  2896. }
  2897. /*
  2898. =====================
  2899. idAnimator::FreeData
  2900. =====================
  2901. */
  2902. void idAnimator::FreeData( void ) {
  2903. int i, j;
  2904. if ( entity ) {
  2905. entity->BecomeInactive( TH_ANIMATE );
  2906. }
  2907. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2908. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2909. channels[ i ][ j ].Reset( NULL );
  2910. }
  2911. }
  2912. jointMods.DeleteContents( true );
  2913. Mem_Free16( joints );
  2914. joints = NULL;
  2915. numJoints = 0;
  2916. modelDef = NULL;
  2917. ForceUpdate();
  2918. }
  2919. /*
  2920. =====================
  2921. idAnimator::PushAnims
  2922. =====================
  2923. */
  2924. void idAnimator::PushAnims( int channelNum, int currentTime, int blendTime ) {
  2925. int i;
  2926. idAnimBlend *channel;
  2927. channel = channels[ channelNum ];
  2928. if ( !channel[ 0 ].GetWeight( currentTime ) || ( channel[ 0 ].starttime == currentTime ) ) {
  2929. return;
  2930. }
  2931. for( i = ANIM_MaxAnimsPerChannel - 1; i > 0; i-- ) {
  2932. channel[ i ] = channel[ i - 1 ];
  2933. }
  2934. channel[ 0 ].Reset( modelDef );
  2935. channel[ 1 ].Clear( currentTime, blendTime );
  2936. ForceUpdate();
  2937. }
  2938. /*
  2939. =====================
  2940. idAnimator::SetModel
  2941. =====================
  2942. */
  2943. idRenderModel *idAnimator::SetModel( const char *modelname ) {
  2944. int i, j;
  2945. FreeData();
  2946. // check if we're just clearing the model
  2947. if ( !modelname || !*modelname ) {
  2948. return NULL;
  2949. }
  2950. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  2951. if ( !modelDef ) {
  2952. return NULL;
  2953. }
  2954. idRenderModel *renderModel = modelDef->ModelHandle();
  2955. if ( !renderModel ) {
  2956. modelDef = NULL;
  2957. return NULL;
  2958. }
  2959. // make sure model hasn't been purged
  2960. modelDef->Touch();
  2961. modelDef->SetupJoints( &numJoints, &joints, frameBounds, removeOriginOffset );
  2962. modelDef->ModelHandle()->Reset();
  2963. // set the modelDef on all channels
  2964. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2965. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2966. channels[ i ][ j ].Reset( modelDef );
  2967. }
  2968. }
  2969. return modelDef->ModelHandle();
  2970. }
  2971. /*
  2972. =====================
  2973. idAnimator::Size
  2974. =====================
  2975. */
  2976. size_t idAnimator::Size( void ) const {
  2977. return sizeof( *this ) + Allocated();
  2978. }
  2979. /*
  2980. =====================
  2981. idAnimator::SetEntity
  2982. =====================
  2983. */
  2984. void idAnimator::SetEntity( idEntity *ent ) {
  2985. entity = ent;
  2986. }
  2987. /*
  2988. =====================
  2989. idAnimator::GetEntity
  2990. =====================
  2991. */
  2992. idEntity *idAnimator::GetEntity( void ) const {
  2993. return entity;
  2994. }
  2995. /*
  2996. =====================
  2997. idAnimator::RemoveOriginOffset
  2998. =====================
  2999. */
  3000. void idAnimator::RemoveOriginOffset( bool remove ) {
  3001. removeOriginOffset = remove;
  3002. }
  3003. /*
  3004. =====================
  3005. idAnimator::RemoveOrigin
  3006. =====================
  3007. */
  3008. bool idAnimator::RemoveOrigin( void ) const {
  3009. return removeOriginOffset;
  3010. }
  3011. /*
  3012. =====================
  3013. idAnimator::GetJointList
  3014. =====================
  3015. */
  3016. void idAnimator::GetJointList( const char *jointnames, idList<jointHandle_t> &jointList ) const {
  3017. if ( modelDef ) {
  3018. modelDef->GetJointList( jointnames, jointList );
  3019. }
  3020. }
  3021. /*
  3022. =====================
  3023. idAnimator::NumAnims
  3024. =====================
  3025. */
  3026. int idAnimator::NumAnims( void ) const {
  3027. if ( !modelDef ) {
  3028. return 0;
  3029. }
  3030. return modelDef->NumAnims();
  3031. }
  3032. /*
  3033. =====================
  3034. idAnimator::GetAnim
  3035. =====================
  3036. */
  3037. const idAnim *idAnimator::GetAnim( int index ) const {
  3038. if ( !modelDef ) {
  3039. return NULL;
  3040. }
  3041. return modelDef->GetAnim( index );
  3042. }
  3043. /*
  3044. =====================
  3045. idAnimator::GetAnim
  3046. =====================
  3047. */
  3048. int idAnimator::GetAnim( const char *name ) const {
  3049. if ( !modelDef ) {
  3050. return 0;
  3051. }
  3052. return modelDef->GetAnim( name );
  3053. }
  3054. /*
  3055. =====================
  3056. idAnimator::HasAnim
  3057. =====================
  3058. */
  3059. bool idAnimator::HasAnim( const char *name ) const {
  3060. if ( !modelDef ) {
  3061. return false;
  3062. }
  3063. return modelDef->HasAnim( name );
  3064. }
  3065. /*
  3066. =====================
  3067. idAnimator::NumJoints
  3068. =====================
  3069. */
  3070. int idAnimator::NumJoints( void ) const {
  3071. return numJoints;
  3072. }
  3073. /*
  3074. =====================
  3075. idAnimator::ModelHandle
  3076. =====================
  3077. */
  3078. idRenderModel *idAnimator::ModelHandle( void ) const {
  3079. if ( !modelDef ) {
  3080. return NULL;
  3081. }
  3082. return modelDef->ModelHandle();
  3083. }
  3084. /*
  3085. =====================
  3086. idAnimator::ModelDef
  3087. =====================
  3088. */
  3089. const idDeclModelDef *idAnimator::ModelDef( void ) const {
  3090. return modelDef;
  3091. }
  3092. /*
  3093. =====================
  3094. idAnimator::CurrentAnim
  3095. =====================
  3096. */
  3097. idAnimBlend *idAnimator::CurrentAnim( int channelNum ) {
  3098. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3099. gameLocal.Error( "idAnimator::CurrentAnim : channel out of range" );
  3100. }
  3101. return &channels[ channelNum ][ 0 ];
  3102. }
  3103. /*
  3104. =====================
  3105. idAnimator::Clear
  3106. =====================
  3107. */
  3108. void idAnimator::Clear( int channelNum, int currentTime, int cleartime ) {
  3109. int i;
  3110. idAnimBlend *blend;
  3111. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3112. gameLocal.Error( "idAnimator::Clear : channel out of range" );
  3113. }
  3114. blend = channels[ channelNum ];
  3115. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3116. blend->Clear( currentTime, cleartime );
  3117. }
  3118. ForceUpdate();
  3119. }
  3120. /*
  3121. =====================
  3122. idAnimator::SetFrame
  3123. =====================
  3124. */
  3125. void idAnimator::SetFrame( int channelNum, int animNum, int frame, int currentTime, int blendTime ) {
  3126. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3127. gameLocal.Error( "idAnimator::SetFrame : channel out of range" );
  3128. }
  3129. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3130. return;
  3131. }
  3132. PushAnims( channelNum, currentTime, blendTime );
  3133. channels[ channelNum ][ 0 ].SetFrame( modelDef, animNum, frame, currentTime, blendTime );
  3134. if ( entity ) {
  3135. entity->BecomeActive( TH_ANIMATE );
  3136. }
  3137. }
  3138. /*
  3139. =====================
  3140. idAnimator::CycleAnim
  3141. =====================
  3142. */
  3143. void idAnimator::CycleAnim( int channelNum, int animNum, int currentTime, int blendTime ) {
  3144. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3145. gameLocal.Error( "idAnimator::CycleAnim : channel out of range" );
  3146. }
  3147. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3148. return;
  3149. }
  3150. PushAnims( channelNum, currentTime, blendTime );
  3151. channels[ channelNum ][ 0 ].CycleAnim( modelDef, animNum, currentTime, blendTime );
  3152. if ( entity ) {
  3153. entity->BecomeActive( TH_ANIMATE );
  3154. }
  3155. }
  3156. /*
  3157. =====================
  3158. idAnimator::PlayAnim
  3159. =====================
  3160. */
  3161. void idAnimator::PlayAnim( int channelNum, int animNum, int currentTime, int blendTime ) {
  3162. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3163. gameLocal.Error( "idAnimator::PlayAnim : channel out of range" );
  3164. }
  3165. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3166. return;
  3167. }
  3168. PushAnims( channelNum, currentTime, blendTime );
  3169. channels[ channelNum ][ 0 ].PlayAnim( modelDef, animNum, currentTime, blendTime );
  3170. if ( entity ) {
  3171. entity->BecomeActive( TH_ANIMATE );
  3172. }
  3173. }
  3174. /*
  3175. =====================
  3176. idAnimator::SyncAnimChannels
  3177. =====================
  3178. */
  3179. void idAnimator::SyncAnimChannels( int channelNum, int fromChannelNum, int currentTime, int blendTime ) {
  3180. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) || ( fromChannelNum < 0 ) || ( fromChannelNum >= ANIM_NumAnimChannels ) ) {
  3181. gameLocal.Error( "idAnimator::SyncToChannel : channel out of range" );
  3182. }
  3183. idAnimBlend &fromBlend = channels[ fromChannelNum ][ 0 ];
  3184. idAnimBlend &toBlend = channels[ channelNum ][ 0 ];
  3185. float weight = fromBlend.blendEndValue;
  3186. if ( ( fromBlend.Anim() != toBlend.Anim() ) || ( fromBlend.GetStartTime() != toBlend.GetStartTime() ) || ( fromBlend.GetEndTime() != toBlend.GetEndTime() ) ) {
  3187. PushAnims( channelNum, currentTime, blendTime );
  3188. toBlend = fromBlend;
  3189. toBlend.blendStartValue = 0.0f;
  3190. toBlend.blendEndValue = 0.0f;
  3191. }
  3192. toBlend.SetWeight( weight, currentTime - 1, blendTime );
  3193. // disable framecommands on the current channel so that commands aren't called twice
  3194. toBlend.AllowFrameCommands( false );
  3195. if ( entity ) {
  3196. entity->BecomeActive( TH_ANIMATE );
  3197. }
  3198. }
  3199. /*
  3200. =====================
  3201. idAnimator::SetJointPos
  3202. =====================
  3203. */
  3204. void idAnimator::SetJointPos( jointHandle_t jointnum, jointModTransform_t transform_type, const idVec3 &pos ) {
  3205. int i;
  3206. jointMod_t *jointMod;
  3207. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3208. return;
  3209. }
  3210. jointMod = NULL;
  3211. for( i = 0; i < jointMods.Num(); i++ ) {
  3212. if ( jointMods[ i ]->jointnum == jointnum ) {
  3213. jointMod = jointMods[ i ];
  3214. break;
  3215. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3216. break;
  3217. }
  3218. }
  3219. if ( !jointMod ) {
  3220. jointMod = new jointMod_t;
  3221. jointMod->jointnum = jointnum;
  3222. jointMod->mat.Identity();
  3223. jointMod->transform_axis = JOINTMOD_NONE;
  3224. jointMods.Insert( jointMod, i );
  3225. }
  3226. jointMod->pos = pos;
  3227. jointMod->transform_pos = transform_type;
  3228. if ( entity ) {
  3229. entity->BecomeActive( TH_ANIMATE );
  3230. }
  3231. ForceUpdate();
  3232. }
  3233. /*
  3234. =====================
  3235. idAnimator::SetJointAxis
  3236. =====================
  3237. */
  3238. void idAnimator::SetJointAxis( jointHandle_t jointnum, jointModTransform_t transform_type, const idMat3 &mat ) {
  3239. int i;
  3240. jointMod_t *jointMod;
  3241. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3242. return;
  3243. }
  3244. jointMod = NULL;
  3245. for( i = 0; i < jointMods.Num(); i++ ) {
  3246. if ( jointMods[ i ]->jointnum == jointnum ) {
  3247. jointMod = jointMods[ i ];
  3248. break;
  3249. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3250. break;
  3251. }
  3252. }
  3253. if ( !jointMod ) {
  3254. jointMod = new jointMod_t;
  3255. jointMod->jointnum = jointnum;
  3256. jointMod->pos.Zero();
  3257. jointMod->transform_pos = JOINTMOD_NONE;
  3258. jointMods.Insert( jointMod, i );
  3259. }
  3260. jointMod->mat = mat;
  3261. jointMod->transform_axis = transform_type;
  3262. if ( entity ) {
  3263. entity->BecomeActive( TH_ANIMATE );
  3264. }
  3265. ForceUpdate();
  3266. }
  3267. /*
  3268. =====================
  3269. idAnimator::ClearJoint
  3270. =====================
  3271. */
  3272. void idAnimator::ClearJoint( jointHandle_t jointnum ) {
  3273. int i;
  3274. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3275. return;
  3276. }
  3277. for( i = 0; i < jointMods.Num(); i++ ) {
  3278. if ( jointMods[ i ]->jointnum == jointnum ) {
  3279. delete jointMods[ i ];
  3280. jointMods.RemoveIndex( i );
  3281. ForceUpdate();
  3282. break;
  3283. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3284. break;
  3285. }
  3286. }
  3287. }
  3288. /*
  3289. =====================
  3290. idAnimator::ClearAllJoints
  3291. =====================
  3292. */
  3293. void idAnimator::ClearAllJoints( void ) {
  3294. if ( jointMods.Num() ) {
  3295. ForceUpdate();
  3296. }
  3297. jointMods.DeleteContents( true );
  3298. }
  3299. /*
  3300. =====================
  3301. idAnimator::ClearAllAnims
  3302. =====================
  3303. */
  3304. void idAnimator::ClearAllAnims( int currentTime, int cleartime ) {
  3305. int i;
  3306. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3307. Clear( i, currentTime, cleartime );
  3308. }
  3309. ClearAFPose();
  3310. ForceUpdate();
  3311. }
  3312. /*
  3313. ====================
  3314. idAnimator::GetDelta
  3315. ====================
  3316. */
  3317. void idAnimator::GetDelta( int fromtime, int totime, idVec3 &delta ) const {
  3318. int i;
  3319. const idAnimBlend *blend;
  3320. float blendWeight;
  3321. if ( !modelDef || !modelDef->ModelHandle() || ( fromtime == totime ) ) {
  3322. delta.Zero();
  3323. return;
  3324. }
  3325. delta.Zero();
  3326. blendWeight = 0.0f;
  3327. blend = channels[ ANIMCHANNEL_ALL ];
  3328. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3329. blend->BlendDelta( fromtime, totime, delta, blendWeight );
  3330. }
  3331. if ( modelDef->Joints()[ 0 ].channel ) {
  3332. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3333. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3334. blend->BlendDelta( fromtime, totime, delta, blendWeight );
  3335. }
  3336. }
  3337. }
  3338. /*
  3339. ====================
  3340. idAnimator::GetDeltaRotation
  3341. ====================
  3342. */
  3343. bool idAnimator::GetDeltaRotation( int fromtime, int totime, idMat3 &delta ) const {
  3344. int i;
  3345. const idAnimBlend *blend;
  3346. float blendWeight;
  3347. idQuat q;
  3348. if ( !modelDef || !modelDef->ModelHandle() || ( fromtime == totime ) ) {
  3349. delta.Identity();
  3350. return false;
  3351. }
  3352. q.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  3353. blendWeight = 0.0f;
  3354. blend = channels[ ANIMCHANNEL_ALL ];
  3355. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3356. blend->BlendDeltaRotation( fromtime, totime, q, blendWeight );
  3357. }
  3358. if ( modelDef->Joints()[ 0 ].channel ) {
  3359. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3360. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3361. blend->BlendDeltaRotation( fromtime, totime, q, blendWeight );
  3362. }
  3363. }
  3364. if ( blendWeight > 0.0f ) {
  3365. delta = q.ToMat3();
  3366. return true;
  3367. } else {
  3368. delta.Identity();
  3369. return false;
  3370. }
  3371. }
  3372. /*
  3373. ====================
  3374. idAnimator::GetOrigin
  3375. ====================
  3376. */
  3377. void idAnimator::GetOrigin( int currentTime, idVec3 &pos ) const {
  3378. int i;
  3379. const idAnimBlend *blend;
  3380. float blendWeight;
  3381. if ( !modelDef || !modelDef->ModelHandle() ) {
  3382. pos.Zero();
  3383. return;
  3384. }
  3385. pos.Zero();
  3386. blendWeight = 0.0f;
  3387. blend = channels[ ANIMCHANNEL_ALL ];
  3388. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3389. blend->BlendOrigin( currentTime, pos, blendWeight, removeOriginOffset );
  3390. }
  3391. if ( modelDef->Joints()[ 0 ].channel ) {
  3392. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3393. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3394. blend->BlendOrigin( currentTime, pos, blendWeight, removeOriginOffset );
  3395. }
  3396. }
  3397. pos += modelDef->GetVisualOffset();
  3398. }
  3399. /*
  3400. ====================
  3401. idAnimator::GetBounds
  3402. ====================
  3403. */
  3404. bool idAnimator::GetBounds( int currentTime, idBounds &bounds ) {
  3405. int i, j;
  3406. const idAnimBlend *blend;
  3407. int count;
  3408. if ( !modelDef || !modelDef->ModelHandle() ) {
  3409. return false;
  3410. }
  3411. if ( AFPoseJoints.Num() ) {
  3412. bounds = AFPoseBounds;
  3413. count = 1;
  3414. } else {
  3415. bounds.Clear();
  3416. count = 0;
  3417. }
  3418. blend = channels[ 0 ];
  3419. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  3420. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3421. if ( blend->AddBounds( currentTime, bounds, removeOriginOffset ) ) {
  3422. count++;
  3423. }
  3424. }
  3425. }
  3426. if ( !count ) {
  3427. if ( !frameBounds.IsCleared() ) {
  3428. bounds = frameBounds;
  3429. return true;
  3430. } else {
  3431. bounds.Zero();
  3432. return false;
  3433. }
  3434. }
  3435. bounds.TranslateSelf( modelDef->GetVisualOffset() );
  3436. if ( g_debugBounds.GetBool() ) {
  3437. if ( bounds[1][0] - bounds[0][0] > 2048 || bounds[1][1] - bounds[0][1] > 2048 ) {
  3438. if ( entity ) {
  3439. gameLocal.Warning( "big frameBounds on entity '%s' with model '%s': %f,%f", entity->name.c_str(), modelDef->ModelHandle()->Name(), bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1] );
  3440. } else {
  3441. gameLocal.Warning( "big frameBounds on model '%s': %f,%f", modelDef->ModelHandle()->Name(), bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1] );
  3442. }
  3443. }
  3444. }
  3445. frameBounds = bounds;
  3446. return true;
  3447. }
  3448. /*
  3449. =====================
  3450. idAnimator::InitAFPose
  3451. =====================
  3452. */
  3453. void idAnimator::InitAFPose( void ) {
  3454. if ( !modelDef ) {
  3455. return;
  3456. }
  3457. AFPoseJoints.SetNum( modelDef->Joints().Num(), false );
  3458. AFPoseJoints.SetNum( 0, false );
  3459. AFPoseJointMods.SetNum( modelDef->Joints().Num(), false );
  3460. AFPoseJointFrame.SetNum( modelDef->Joints().Num(), false );
  3461. }
  3462. /*
  3463. =====================
  3464. idAnimator::SetAFPoseJointMod
  3465. =====================
  3466. */
  3467. void idAnimator::SetAFPoseJointMod( const jointHandle_t jointNum, const AFJointModType_t mod, const idMat3 &axis, const idVec3 &origin ) {
  3468. AFPoseJointMods[jointNum].mod = mod;
  3469. AFPoseJointMods[jointNum].axis = axis;
  3470. AFPoseJointMods[jointNum].origin = origin;
  3471. int index = idBinSearch_GreaterEqual<int>( AFPoseJoints.Ptr(), AFPoseJoints.Num(), jointNum );
  3472. if ( index >= AFPoseJoints.Num() || jointNum != AFPoseJoints[index] ) {
  3473. AFPoseJoints.Insert( jointNum, index );
  3474. }
  3475. }
  3476. /*
  3477. =====================
  3478. idAnimator::FinishAFPose
  3479. =====================
  3480. */
  3481. void idAnimator::FinishAFPose( int animNum, const idBounds &bounds, const int time ) {
  3482. int i, j;
  3483. int numJoints;
  3484. int parentNum;
  3485. int jointMod;
  3486. int jointNum;
  3487. const int * jointParent;
  3488. if ( !modelDef ) {
  3489. return;
  3490. }
  3491. const idAnim *anim = modelDef->GetAnim( animNum );
  3492. if ( !anim ) {
  3493. return;
  3494. }
  3495. numJoints = modelDef->Joints().Num();
  3496. if ( !numJoints ) {
  3497. return;
  3498. }
  3499. idRenderModel *md5 = modelDef->ModelHandle();
  3500. const idMD5Anim *md5anim = anim->MD5Anim( 0 );
  3501. if ( numJoints != md5anim->NumJoints() ) {
  3502. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", md5->Name(), md5anim->Name() );
  3503. return;
  3504. }
  3505. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  3506. md5anim->GetSingleFrame( 0, jointFrame, modelDef->GetChannelJoints( ANIMCHANNEL_ALL ), modelDef->NumJointsOnChannel( ANIMCHANNEL_ALL ) );
  3507. if ( removeOriginOffset ) {
  3508. #ifdef VELOCITY_MOVE
  3509. jointFrame[ 0 ].t.x = 0.0f;
  3510. #else
  3511. jointFrame[ 0 ].t.Zero();
  3512. #endif
  3513. }
  3514. idJointMat *joints = ( idJointMat * )_alloca16( numJoints * sizeof( *joints ) );
  3515. // convert the joint quaternions to joint matrices
  3516. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  3517. // first joint is always root of entire hierarchy
  3518. if ( AFPoseJoints.Num() && AFPoseJoints[0] == 0 ) {
  3519. switch( AFPoseJointMods[0].mod ) {
  3520. case AF_JOINTMOD_AXIS: {
  3521. joints[0].SetRotation( AFPoseJointMods[0].axis );
  3522. break;
  3523. }
  3524. case AF_JOINTMOD_ORIGIN: {
  3525. joints[0].SetTranslation( AFPoseJointMods[0].origin );
  3526. break;
  3527. }
  3528. case AF_JOINTMOD_BOTH: {
  3529. joints[0].SetRotation( AFPoseJointMods[0].axis );
  3530. joints[0].SetTranslation( AFPoseJointMods[0].origin );
  3531. break;
  3532. }
  3533. }
  3534. j = 1;
  3535. } else {
  3536. j = 0;
  3537. }
  3538. // pointer to joint info
  3539. jointParent = modelDef->JointParents();
  3540. // transform the child joints
  3541. for( i = 1; j < AFPoseJoints.Num(); j++, i++ ) {
  3542. jointMod = AFPoseJoints[j];
  3543. // transform any joints preceding the joint modifier
  3544. SIMDProcessor->TransformJoints( joints, jointParent, i, jointMod - 1 );
  3545. i = jointMod;
  3546. parentNum = jointParent[i];
  3547. switch( AFPoseJointMods[jointMod].mod ) {
  3548. case AF_JOINTMOD_AXIS: {
  3549. joints[i].SetRotation( AFPoseJointMods[jointMod].axis );
  3550. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() );
  3551. break;
  3552. }
  3553. case AF_JOINTMOD_ORIGIN: {
  3554. joints[i].SetRotation( joints[i].ToMat3() * joints[parentNum].ToMat3() );
  3555. joints[i].SetTranslation( AFPoseJointMods[jointMod].origin );
  3556. break;
  3557. }
  3558. case AF_JOINTMOD_BOTH: {
  3559. joints[i].SetRotation( AFPoseJointMods[jointMod].axis );
  3560. joints[i].SetTranslation( AFPoseJointMods[jointMod].origin );
  3561. break;
  3562. }
  3563. }
  3564. }
  3565. // transform the rest of the hierarchy
  3566. SIMDProcessor->TransformJoints( joints, jointParent, i, numJoints - 1 );
  3567. // untransform hierarchy
  3568. SIMDProcessor->UntransformJoints( joints, jointParent, 1, numJoints - 1 );
  3569. // convert joint matrices back to joint quaternions
  3570. SIMDProcessor->ConvertJointMatsToJointQuats( AFPoseJointFrame.Ptr(), joints, numJoints );
  3571. // find all modified joints and their parents
  3572. bool *blendJoints = (bool *) _alloca16( numJoints * sizeof( bool ) );
  3573. memset( blendJoints, 0, numJoints * sizeof( bool ) );
  3574. // mark all modified joints and their parents
  3575. for( i = 0; i < AFPoseJoints.Num(); i++ ) {
  3576. for( jointNum = AFPoseJoints[i]; jointNum != INVALID_JOINT; jointNum = jointParent[jointNum] ) {
  3577. blendJoints[jointNum] = true;
  3578. }
  3579. }
  3580. // lock all parents of modified joints
  3581. AFPoseJoints.SetNum( 0, false );
  3582. for ( i = 0; i < numJoints; i++ ) {
  3583. if ( blendJoints[i] ) {
  3584. AFPoseJoints.Append( i );
  3585. }
  3586. }
  3587. AFPoseBounds = bounds;
  3588. AFPoseTime = time;
  3589. ForceUpdate();
  3590. }
  3591. /*
  3592. =====================
  3593. idAnimator::SetAFPoseBlendWeight
  3594. =====================
  3595. */
  3596. void idAnimator::SetAFPoseBlendWeight( float blendWeight ) {
  3597. AFPoseBlendWeight = blendWeight;
  3598. }
  3599. /*
  3600. =====================
  3601. idAnimator::BlendAFPose
  3602. =====================
  3603. */
  3604. bool idAnimator::BlendAFPose( idJointQuat *blendFrame ) const {
  3605. if ( !AFPoseJoints.Num() ) {
  3606. return false;
  3607. }
  3608. SIMDProcessor->BlendJoints( blendFrame, AFPoseJointFrame.Ptr(), AFPoseBlendWeight, AFPoseJoints.Ptr(), AFPoseJoints.Num() );
  3609. return true;
  3610. }
  3611. /*
  3612. =====================
  3613. idAnimator::ClearAFPose
  3614. =====================
  3615. */
  3616. void idAnimator::ClearAFPose( void ) {
  3617. if ( AFPoseJoints.Num() ) {
  3618. ForceUpdate();
  3619. }
  3620. AFPoseBlendWeight = 1.0f;
  3621. AFPoseJoints.SetNum( 0, false );
  3622. AFPoseBounds.Clear();
  3623. AFPoseTime = 0;
  3624. }
  3625. /*
  3626. =====================
  3627. idAnimator::ServiceAnims
  3628. =====================
  3629. */
  3630. void idAnimator::ServiceAnims( int fromtime, int totime ) {
  3631. int i, j;
  3632. idAnimBlend *blend;
  3633. if ( !modelDef ) {
  3634. return;
  3635. }
  3636. if ( modelDef->ModelHandle() ) {
  3637. blend = channels[ 0 ];
  3638. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3639. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3640. blend->CallFrameCommands( entity, fromtime, totime );
  3641. }
  3642. }
  3643. }
  3644. if ( !IsAnimating( totime ) ) {
  3645. stoppedAnimatingUpdate = true;
  3646. if ( entity ) {
  3647. entity->BecomeInactive( TH_ANIMATE );
  3648. // present one more time with stopped animations so the renderer can properly recreate interactions
  3649. entity->BecomeActive( TH_UPDATEVISUALS );
  3650. }
  3651. }
  3652. }
  3653. /*
  3654. =====================
  3655. idAnimator::IsAnimating
  3656. =====================
  3657. */
  3658. bool idAnimator::IsAnimating( int currentTime ) const {
  3659. int i, j;
  3660. const idAnimBlend *blend;
  3661. if ( !modelDef || !modelDef->ModelHandle() ) {
  3662. return false;
  3663. }
  3664. // if animating with an articulated figure
  3665. if ( AFPoseJoints.Num() && currentTime <= AFPoseTime ) {
  3666. return true;
  3667. }
  3668. blend = channels[ 0 ];
  3669. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3670. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3671. if ( !blend->IsDone( currentTime ) ) {
  3672. return true;
  3673. }
  3674. }
  3675. }
  3676. return false;
  3677. }
  3678. /*
  3679. =====================
  3680. idAnimator::FrameHasChanged
  3681. =====================
  3682. */
  3683. bool idAnimator::FrameHasChanged( int currentTime ) const {
  3684. int i, j;
  3685. const idAnimBlend *blend;
  3686. if ( !modelDef || !modelDef->ModelHandle() ) {
  3687. return false;
  3688. }
  3689. // if animating with an articulated figure
  3690. if ( AFPoseJoints.Num() && currentTime <= AFPoseTime ) {
  3691. return true;
  3692. }
  3693. blend = channels[ 0 ];
  3694. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3695. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3696. if ( blend->FrameHasChanged( currentTime ) ) {
  3697. return true;
  3698. }
  3699. }
  3700. }
  3701. if ( forceUpdate && IsAnimating( currentTime ) ) {
  3702. return true;
  3703. }
  3704. return false;
  3705. }
  3706. /*
  3707. =====================
  3708. idAnimator::CreateFrame
  3709. =====================
  3710. */
  3711. bool idAnimator::CreateFrame( int currentTime, bool force ) {
  3712. int i, j;
  3713. int numJoints;
  3714. int parentNum;
  3715. bool hasAnim;
  3716. bool debugInfo;
  3717. float baseBlend;
  3718. float blendWeight;
  3719. const idAnimBlend * blend;
  3720. const int * jointParent;
  3721. const jointMod_t * jointMod;
  3722. const idJointQuat * defaultPose;
  3723. static idCVar r_showSkel( "r_showSkel", "0", CVAR_RENDERER | CVAR_INTEGER, "", 0, 2, idCmdSystem::ArgCompletion_Integer<0,2> );
  3724. if ( gameLocal.inCinematic && gameLocal.skipCinematic ) {
  3725. return false;
  3726. }
  3727. if ( !modelDef || !modelDef->ModelHandle() ) {
  3728. return false;
  3729. }
  3730. if ( !force && !r_showSkel.GetInteger() ) {
  3731. if ( lastTransformTime == currentTime ) {
  3732. return false;
  3733. }
  3734. if ( lastTransformTime != -1 && !stoppedAnimatingUpdate && !IsAnimating( currentTime ) ) {
  3735. return false;
  3736. }
  3737. }
  3738. lastTransformTime = currentTime;
  3739. stoppedAnimatingUpdate = false;
  3740. if ( entity && ( ( g_debugAnim.GetInteger() == entity->entityNumber ) || ( g_debugAnim.GetInteger() == -2 ) ) ) {
  3741. debugInfo = true;
  3742. gameLocal.Printf( "---------------\n%d: entity '%s':\n", gameLocal.time, entity->GetName() );
  3743. gameLocal.Printf( "model '%s':\n", modelDef->GetModelName() );
  3744. } else {
  3745. debugInfo = false;
  3746. }
  3747. // init the joint buffer
  3748. if ( AFPoseJoints.Num() ) {
  3749. // initialize with AF pose anim for the case where there are no other animations and no AF pose joint modifications
  3750. defaultPose = AFPoseJointFrame.Ptr();
  3751. } else {
  3752. defaultPose = modelDef->GetDefaultPose();
  3753. }
  3754. if ( !defaultPose ) {
  3755. //gameLocal.Warning( "idAnimator::CreateFrame: no defaultPose on '%s'", modelDef->Name() );
  3756. return false;
  3757. }
  3758. numJoints = modelDef->Joints().Num();
  3759. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( jointFrame[0] ) );
  3760. SIMDProcessor->Memcpy( jointFrame, defaultPose, numJoints * sizeof( jointFrame[0] ) );
  3761. hasAnim = false;
  3762. // blend the all channel
  3763. baseBlend = 0.0f;
  3764. blend = channels[ ANIMCHANNEL_ALL ];
  3765. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3766. if ( blend->BlendAnim( currentTime, ANIMCHANNEL_ALL, numJoints, jointFrame, baseBlend, removeOriginOffset, false, debugInfo ) ) {
  3767. hasAnim = true;
  3768. if ( baseBlend >= 1.0f ) {
  3769. break;
  3770. }
  3771. }
  3772. }
  3773. // only blend other channels if there's enough space to blend into
  3774. if ( baseBlend < 1.0f ) {
  3775. for( i = ANIMCHANNEL_ALL + 1; i < ANIM_NumAnimChannels; i++ ) {
  3776. if ( !modelDef->NumJointsOnChannel( i ) ) {
  3777. continue;
  3778. }
  3779. if ( i == ANIMCHANNEL_EYELIDS ) {
  3780. // eyelids blend over any previous anims, so skip it and blend it later
  3781. continue;
  3782. }
  3783. blendWeight = baseBlend;
  3784. blend = channels[ i ];
  3785. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3786. if ( blend->BlendAnim( currentTime, i, numJoints, jointFrame, blendWeight, removeOriginOffset, false, debugInfo ) ) {
  3787. hasAnim = true;
  3788. if ( blendWeight >= 1.0f ) {
  3789. // fully blended
  3790. break;
  3791. }
  3792. }
  3793. }
  3794. if ( debugInfo && !AFPoseJoints.Num() && !blendWeight ) {
  3795. gameLocal.Printf( "%d: %s using default pose in model '%s'\n", gameLocal.time, channelNames[ i ], modelDef->GetModelName() );
  3796. }
  3797. }
  3798. }
  3799. // blend in the eyelids
  3800. if ( modelDef->NumJointsOnChannel( ANIMCHANNEL_EYELIDS ) ) {
  3801. blend = channels[ ANIMCHANNEL_EYELIDS ];
  3802. blendWeight = baseBlend;
  3803. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3804. if ( blend->BlendAnim( currentTime, ANIMCHANNEL_EYELIDS, numJoints, jointFrame, blendWeight, removeOriginOffset, true, debugInfo ) ) {
  3805. hasAnim = true;
  3806. if ( blendWeight >= 1.0f ) {
  3807. // fully blended
  3808. break;
  3809. }
  3810. }
  3811. }
  3812. }
  3813. // blend the articulated figure pose
  3814. if ( BlendAFPose( jointFrame ) ) {
  3815. hasAnim = true;
  3816. }
  3817. if ( !hasAnim && !jointMods.Num() ) {
  3818. // no animations were updated
  3819. return false;
  3820. }
  3821. // convert the joint quaternions to rotation matrices
  3822. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  3823. // check if we need to modify the origin
  3824. if ( jointMods.Num() && ( jointMods[0]->jointnum == 0 ) ) {
  3825. jointMod = jointMods[0];
  3826. switch( jointMod->transform_axis ) {
  3827. case JOINTMOD_NONE:
  3828. break;
  3829. case JOINTMOD_LOCAL:
  3830. joints[0].SetRotation( jointMod->mat * joints[0].ToMat3() );
  3831. break;
  3832. case JOINTMOD_WORLD:
  3833. joints[0].SetRotation( joints[0].ToMat3() * jointMod->mat );
  3834. break;
  3835. case JOINTMOD_LOCAL_OVERRIDE:
  3836. case JOINTMOD_WORLD_OVERRIDE:
  3837. joints[0].SetRotation( jointMod->mat );
  3838. break;
  3839. }
  3840. switch( jointMod->transform_pos ) {
  3841. case JOINTMOD_NONE:
  3842. break;
  3843. case JOINTMOD_LOCAL:
  3844. joints[0].SetTranslation( joints[0].ToVec3() + jointMod->pos );
  3845. break;
  3846. case JOINTMOD_LOCAL_OVERRIDE:
  3847. case JOINTMOD_WORLD:
  3848. case JOINTMOD_WORLD_OVERRIDE:
  3849. joints[0].SetTranslation( jointMod->pos );
  3850. break;
  3851. }
  3852. j = 1;
  3853. } else {
  3854. j = 0;
  3855. }
  3856. // add in the model offset
  3857. joints[0].SetTranslation( joints[0].ToVec3() + modelDef->GetVisualOffset() );
  3858. // pointer to joint info
  3859. jointParent = modelDef->JointParents();
  3860. // add in any joint modifications
  3861. for( i = 1; j < jointMods.Num(); j++, i++ ) {
  3862. jointMod = jointMods[j];
  3863. // transform any joints preceding the joint modifier
  3864. SIMDProcessor->TransformJoints( joints, jointParent, i, jointMod->jointnum - 1 );
  3865. i = jointMod->jointnum;
  3866. parentNum = jointParent[i];
  3867. // modify the axis
  3868. switch( jointMod->transform_axis ) {
  3869. case JOINTMOD_NONE:
  3870. joints[i].SetRotation( joints[i].ToMat3() * joints[ parentNum ].ToMat3() );
  3871. break;
  3872. case JOINTMOD_LOCAL:
  3873. joints[i].SetRotation( jointMod->mat * ( joints[i].ToMat3() * joints[parentNum].ToMat3() ) );
  3874. break;
  3875. case JOINTMOD_LOCAL_OVERRIDE:
  3876. joints[i].SetRotation( jointMod->mat * joints[parentNum].ToMat3() );
  3877. break;
  3878. case JOINTMOD_WORLD:
  3879. joints[i].SetRotation( ( joints[i].ToMat3() * joints[parentNum].ToMat3() ) * jointMod->mat );
  3880. break;
  3881. case JOINTMOD_WORLD_OVERRIDE:
  3882. joints[i].SetRotation( jointMod->mat );
  3883. break;
  3884. }
  3885. // modify the position
  3886. switch( jointMod->transform_pos ) {
  3887. case JOINTMOD_NONE:
  3888. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() );
  3889. break;
  3890. case JOINTMOD_LOCAL:
  3891. joints[i].SetTranslation( joints[parentNum].ToVec3() + ( joints[i].ToVec3() + jointMod->pos ) * joints[parentNum].ToMat3() );
  3892. break;
  3893. case JOINTMOD_LOCAL_OVERRIDE:
  3894. joints[i].SetTranslation( joints[parentNum].ToVec3() + jointMod->pos * joints[parentNum].ToMat3() );
  3895. break;
  3896. case JOINTMOD_WORLD:
  3897. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() + jointMod->pos );
  3898. break;
  3899. case JOINTMOD_WORLD_OVERRIDE:
  3900. joints[i].SetTranslation( jointMod->pos );
  3901. break;
  3902. }
  3903. }
  3904. // transform the rest of the hierarchy
  3905. SIMDProcessor->TransformJoints( joints, jointParent, i, numJoints - 1 );
  3906. return true;
  3907. }
  3908. /*
  3909. =====================
  3910. idAnimator::ForceUpdate
  3911. =====================
  3912. */
  3913. void idAnimator::ForceUpdate( void ) {
  3914. lastTransformTime = -1;
  3915. forceUpdate = true;
  3916. }
  3917. /*
  3918. =====================
  3919. idAnimator::ClearForceUpdate
  3920. =====================
  3921. */
  3922. void idAnimator::ClearForceUpdate( void ) {
  3923. forceUpdate = false;
  3924. }
  3925. /*
  3926. =====================
  3927. idAnimator::GetJointTransform> gamex86.dll!idAnimator::ForceUpdate() Line 4268 C++
  3928. =====================
  3929. */
  3930. bool idAnimator::GetJointTransform( jointHandle_t jointHandle, int currentTime, idVec3 &offset, idMat3 &axis ) {
  3931. if ( !modelDef || ( jointHandle < 0 ) || ( jointHandle >= modelDef->NumJoints() ) ) {
  3932. return false;
  3933. }
  3934. CreateFrame( currentTime, false );
  3935. offset = joints[ jointHandle ].ToVec3();
  3936. axis = joints[ jointHandle ].ToMat3();
  3937. return true;
  3938. }
  3939. /*
  3940. =====================
  3941. idAnimator::GetJointLocalTransform
  3942. =====================
  3943. */
  3944. bool idAnimator::GetJointLocalTransform( jointHandle_t jointHandle, int currentTime, idVec3 &offset, idMat3 &axis ) {
  3945. if ( !modelDef ) {
  3946. return false;
  3947. }
  3948. const idList<jointInfo_t> &modelJoints = modelDef->Joints();
  3949. if ( ( jointHandle < 0 ) || ( jointHandle >= modelJoints.Num() ) ) {
  3950. return false;
  3951. }
  3952. // FIXME: overkill
  3953. CreateFrame( currentTime, false );
  3954. if ( jointHandle > 0 ) {
  3955. idJointMat m = joints[ jointHandle ];
  3956. m /= joints[ modelJoints[ jointHandle ].parentNum ];
  3957. offset = m.ToVec3();
  3958. axis = m.ToMat3();
  3959. } else {
  3960. offset = joints[ jointHandle ].ToVec3();
  3961. axis = joints[ jointHandle ].ToMat3();
  3962. }
  3963. return true;
  3964. }
  3965. /*
  3966. =====================
  3967. idAnimator::GetJointHandle
  3968. =====================
  3969. */
  3970. jointHandle_t idAnimator::GetJointHandle( const char *name ) const {
  3971. if ( !modelDef || !modelDef->ModelHandle() ) {
  3972. return INVALID_JOINT;
  3973. }
  3974. return modelDef->ModelHandle()->GetJointHandle( name );
  3975. }
  3976. /*
  3977. =====================
  3978. idAnimator::GetJointName
  3979. =====================
  3980. */
  3981. const char *idAnimator::GetJointName( jointHandle_t handle ) const {
  3982. if ( !modelDef || !modelDef->ModelHandle() ) {
  3983. return "";
  3984. }
  3985. return modelDef->ModelHandle()->GetJointName( handle );
  3986. }
  3987. /*
  3988. =====================
  3989. idAnimator::GetChannelForJoint
  3990. =====================
  3991. */
  3992. int idAnimator::GetChannelForJoint( jointHandle_t joint ) const {
  3993. if ( !modelDef ) {
  3994. gameLocal.Error( "idAnimator::GetChannelForJoint: NULL model" );
  3995. }
  3996. if ( ( joint < 0 ) || ( joint >= numJoints ) ) {
  3997. gameLocal.Error( "idAnimator::GetChannelForJoint: invalid joint num (%d)", joint );
  3998. }
  3999. return modelDef->GetJoint( joint )->channel;
  4000. }
  4001. /*
  4002. =====================
  4003. idAnimator::GetFirstChild
  4004. =====================
  4005. */
  4006. jointHandle_t idAnimator::GetFirstChild( const char *name ) const {
  4007. return GetFirstChild( GetJointHandle( name ) );
  4008. }
  4009. /*
  4010. =====================
  4011. idAnimator::GetFirstChild
  4012. =====================
  4013. */
  4014. jointHandle_t idAnimator::GetFirstChild( jointHandle_t jointnum ) const {
  4015. int i;
  4016. int num;
  4017. const jointInfo_t *joint;
  4018. if ( !modelDef ) {
  4019. return INVALID_JOINT;
  4020. }
  4021. num = modelDef->NumJoints();
  4022. if ( !num ) {
  4023. return jointnum;
  4024. }
  4025. joint = modelDef->GetJoint( 0 );
  4026. for( i = 0; i < num; i++, joint++ ) {
  4027. if ( joint->parentNum == jointnum ) {
  4028. return ( jointHandle_t )joint->num;
  4029. }
  4030. }
  4031. return jointnum;
  4032. }
  4033. /*
  4034. =====================
  4035. idAnimator::GetJoints
  4036. =====================
  4037. */
  4038. void idAnimator::GetJoints( int *numJoints, idJointMat **jointsPtr ) {
  4039. *numJoints = this->numJoints;
  4040. *jointsPtr = this->joints;
  4041. }
  4042. /*
  4043. =====================
  4044. idAnimator::GetAnimFlags
  4045. =====================
  4046. */
  4047. const animFlags_t idAnimator::GetAnimFlags( int animNum ) const {
  4048. animFlags_t result;
  4049. const idAnim *anim = GetAnim( animNum );
  4050. if ( anim ) {
  4051. return anim->GetAnimFlags();
  4052. }
  4053. memset( &result, 0, sizeof( result ) );
  4054. return result;
  4055. }
  4056. /*
  4057. =====================
  4058. idAnimator::NumFrames
  4059. =====================
  4060. */
  4061. int idAnimator::NumFrames( int animNum ) const {
  4062. const idAnim *anim = GetAnim( animNum );
  4063. if ( anim ) {
  4064. return anim->NumFrames();
  4065. } else {
  4066. return 0;
  4067. }
  4068. }
  4069. /*
  4070. =====================
  4071. idAnimator::NumSyncedAnims
  4072. =====================
  4073. */
  4074. int idAnimator::NumSyncedAnims( int animNum ) const {
  4075. const idAnim *anim = GetAnim( animNum );
  4076. if ( anim ) {
  4077. return anim->NumAnims();
  4078. } else {
  4079. return 0;
  4080. }
  4081. }
  4082. /*
  4083. =====================
  4084. idAnimator::AnimName
  4085. =====================
  4086. */
  4087. const char *idAnimator::AnimName( int animNum ) const {
  4088. const idAnim *anim = GetAnim( animNum );
  4089. if ( anim ) {
  4090. return anim->Name();
  4091. } else {
  4092. return "";
  4093. }
  4094. }
  4095. /*
  4096. =====================
  4097. idAnimator::AnimFullName
  4098. =====================
  4099. */
  4100. const char *idAnimator::AnimFullName( int animNum ) const {
  4101. const idAnim *anim = GetAnim( animNum );
  4102. if ( anim ) {
  4103. return anim->FullName();
  4104. } else {
  4105. return "";
  4106. }
  4107. }
  4108. /*
  4109. =====================
  4110. idAnimator::AnimLength
  4111. =====================
  4112. */
  4113. int idAnimator::AnimLength( int animNum ) const {
  4114. const idAnim *anim = GetAnim( animNum );
  4115. if ( anim ) {
  4116. return anim->Length();
  4117. } else {
  4118. return 0;
  4119. }
  4120. }
  4121. /*
  4122. =====================
  4123. idAnimator::TotalMovementDelta
  4124. =====================
  4125. */
  4126. const idVec3 &idAnimator::TotalMovementDelta( int animNum ) const {
  4127. const idAnim *anim = GetAnim( animNum );
  4128. if ( anim ) {
  4129. return anim->TotalMovementDelta();
  4130. } else {
  4131. return vec3_origin;
  4132. }
  4133. }
  4134. /***********************************************************************
  4135. Util functions
  4136. ***********************************************************************/
  4137. /*
  4138. =====================
  4139. ANIM_GetModelDefFromEntityDef
  4140. =====================
  4141. */
  4142. const idDeclModelDef *ANIM_GetModelDefFromEntityDef( const idDict *args ) {
  4143. const idDeclModelDef *modelDef;
  4144. idStr name = args->GetString( "model" );
  4145. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, name, false ) );
  4146. if ( modelDef && modelDef->ModelHandle() ) {
  4147. return modelDef;
  4148. }
  4149. return NULL;
  4150. }
  4151. /*
  4152. =====================
  4153. idGameEdit::ANIM_GetModelFromEntityDef
  4154. =====================
  4155. */
  4156. idRenderModel *idGameEdit::ANIM_GetModelFromEntityDef( const idDict *args ) {
  4157. idRenderModel *model;
  4158. const idDeclModelDef *modelDef;
  4159. model = NULL;
  4160. idStr name = args->GetString( "model" );
  4161. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, name, false ) );
  4162. if ( modelDef ) {
  4163. model = modelDef->ModelHandle();
  4164. }
  4165. if ( !model ) {
  4166. model = renderModelManager->FindModel( name );
  4167. }
  4168. if ( model && model->IsDefaultModel() ) {
  4169. return NULL;
  4170. }
  4171. return model;
  4172. }
  4173. /*
  4174. =====================
  4175. idGameEdit::ANIM_GetModelFromEntityDef
  4176. =====================
  4177. */
  4178. idRenderModel *idGameEdit::ANIM_GetModelFromEntityDef( const char *classname ) {
  4179. const idDict *args;
  4180. args = gameLocal.FindEntityDefDict( classname, false );
  4181. if ( !args ) {
  4182. return NULL;
  4183. }
  4184. return ANIM_GetModelFromEntityDef( args );
  4185. }
  4186. /*
  4187. =====================
  4188. idGameEdit::ANIM_GetModelOffsetFromEntityDef
  4189. =====================
  4190. */
  4191. const idVec3 &idGameEdit::ANIM_GetModelOffsetFromEntityDef( const char *classname ) {
  4192. const idDict *args;
  4193. const idDeclModelDef *modelDef;
  4194. args = gameLocal.FindEntityDefDict( classname, false );
  4195. if ( !args ) {
  4196. return vec3_origin;
  4197. }
  4198. modelDef = ANIM_GetModelDefFromEntityDef( args );
  4199. if ( !modelDef ) {
  4200. return vec3_origin;
  4201. }
  4202. return modelDef->GetVisualOffset();
  4203. }
  4204. /*
  4205. =====================
  4206. idGameEdit::ANIM_GetModelFromName
  4207. =====================
  4208. */
  4209. idRenderModel *idGameEdit::ANIM_GetModelFromName( const char *modelName ) {
  4210. const idDeclModelDef *modelDef;
  4211. idRenderModel *model;
  4212. model = NULL;
  4213. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelName, false ) );
  4214. if ( modelDef ) {
  4215. model = modelDef->ModelHandle();
  4216. }
  4217. if ( !model ) {
  4218. model = renderModelManager->FindModel( modelName );
  4219. }
  4220. return model;
  4221. }
  4222. /*
  4223. =====================
  4224. idGameEdit::ANIM_GetAnimFromEntityDef
  4225. =====================
  4226. */
  4227. const idMD5Anim *idGameEdit::ANIM_GetAnimFromEntityDef( const char *classname, const char *animname ) {
  4228. const idDict *args;
  4229. const idMD5Anim *md5anim;
  4230. const idAnim *anim;
  4231. int animNum;
  4232. const char *modelname;
  4233. const idDeclModelDef *modelDef;
  4234. args = gameLocal.FindEntityDefDict( classname, false );
  4235. if ( !args ) {
  4236. return NULL;
  4237. }
  4238. md5anim = NULL;
  4239. modelname = args->GetString( "model" );
  4240. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4241. if ( modelDef ) {
  4242. animNum = modelDef->GetAnim( animname );
  4243. if ( animNum ) {
  4244. anim = modelDef->GetAnim( animNum );
  4245. if ( anim ) {
  4246. md5anim = anim->MD5Anim( 0 );
  4247. }
  4248. }
  4249. }
  4250. return md5anim;
  4251. }
  4252. /*
  4253. =====================
  4254. idGameEdit::ANIM_GetNumAnimsFromEntityDef
  4255. =====================
  4256. */
  4257. int idGameEdit::ANIM_GetNumAnimsFromEntityDef( const idDict *args ) {
  4258. const char *modelname;
  4259. const idDeclModelDef *modelDef;
  4260. modelname = args->GetString( "model" );
  4261. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4262. if ( modelDef ) {
  4263. return modelDef->NumAnims();
  4264. }
  4265. return 0;
  4266. }
  4267. /*
  4268. =====================
  4269. idGameEdit::ANIM_GetAnimNameFromEntityDef
  4270. =====================
  4271. */
  4272. const char *idGameEdit::ANIM_GetAnimNameFromEntityDef( const idDict *args, int animNum ) {
  4273. const char *modelname;
  4274. const idDeclModelDef *modelDef;
  4275. modelname = args->GetString( "model" );
  4276. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4277. if ( modelDef ) {
  4278. const idAnim* anim = modelDef->GetAnim( animNum );
  4279. if ( anim ) {
  4280. return anim->FullName();
  4281. }
  4282. }
  4283. return "";
  4284. }
  4285. /*
  4286. =====================
  4287. idGameEdit::ANIM_GetAnim
  4288. =====================
  4289. */
  4290. const idMD5Anim *idGameEdit::ANIM_GetAnim( const char *fileName ) {
  4291. return animationLib.GetAnim( fileName );
  4292. }
  4293. /*
  4294. =====================
  4295. idGameEdit::ANIM_GetLength
  4296. =====================
  4297. */
  4298. int idGameEdit::ANIM_GetLength( const idMD5Anim *anim ) {
  4299. if ( !anim ) {
  4300. return 0;
  4301. }
  4302. return anim->Length();
  4303. }
  4304. /*
  4305. =====================
  4306. idGameEdit::ANIM_GetNumFrames
  4307. =====================
  4308. */
  4309. int idGameEdit::ANIM_GetNumFrames( const idMD5Anim *anim ) {
  4310. if ( !anim ) {
  4311. return 0;
  4312. }
  4313. return anim->NumFrames();
  4314. }
  4315. /*
  4316. =====================
  4317. idGameEdit::ANIM_CreateAnimFrame
  4318. =====================
  4319. */
  4320. void idGameEdit::ANIM_CreateAnimFrame( const idRenderModel *model, const idMD5Anim *anim, int numJoints, idJointMat *joints, int time, const idVec3 &offset, bool remove_origin_offset ) {
  4321. int i;
  4322. frameBlend_t frame;
  4323. const idMD5Joint *md5joints;
  4324. int *index;
  4325. if ( !model || model->IsDefaultModel() || !anim ) {
  4326. return;
  4327. }
  4328. if ( numJoints != model->NumJoints() ) {
  4329. gameLocal.Error( "ANIM_CreateAnimFrame: different # of joints in renderEntity_t than in model (%s)", model->Name() );
  4330. }
  4331. if ( !model->NumJoints() ) {
  4332. // FIXME: Print out a warning?
  4333. return;
  4334. }
  4335. if ( !joints ) {
  4336. gameLocal.Error( "ANIM_CreateAnimFrame: NULL joint frame pointer on model (%s)", model->Name() );
  4337. }
  4338. if ( numJoints != anim->NumJoints() ) {
  4339. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", model->Name(), anim->Name() );
  4340. for( i = 0; i < numJoints; i++ ) {
  4341. joints[i].SetRotation( mat3_identity );
  4342. joints[i].SetTranslation( offset );
  4343. }
  4344. return;
  4345. }
  4346. // create index for all joints
  4347. index = ( int * )_alloca16( numJoints * sizeof( int ) );
  4348. for ( i = 0; i < numJoints; i++ ) {
  4349. index[i] = i;
  4350. }
  4351. // create the frame
  4352. anim->ConvertTimeToFrame( time, 1, frame );
  4353. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  4354. anim->GetInterpolatedFrame( frame, jointFrame, index, numJoints );
  4355. // convert joint quaternions to joint matrices
  4356. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  4357. // first joint is always root of entire hierarchy
  4358. if ( remove_origin_offset ) {
  4359. joints[0].SetTranslation( offset );
  4360. } else {
  4361. joints[0].SetTranslation( joints[0].ToVec3() + offset );
  4362. }
  4363. // transform the children
  4364. md5joints = model->GetJoints();
  4365. for( i = 1; i < numJoints; i++ ) {
  4366. joints[i] *= joints[ md5joints[i].parent - md5joints ];
  4367. }
  4368. }
  4369. /*
  4370. =====================
  4371. idGameEdit::ANIM_CreateMeshForAnim
  4372. =====================
  4373. */
  4374. idRenderModel *idGameEdit::ANIM_CreateMeshForAnim( idRenderModel *model, const char *classname, const char *animname, int frame, bool remove_origin_offset ) {
  4375. renderEntity_t ent;
  4376. const idDict *args;
  4377. const char *temp;
  4378. idRenderModel *newmodel;
  4379. const idMD5Anim *md5anim;
  4380. idStr filename;
  4381. idStr extension;
  4382. const idAnim *anim;
  4383. int animNum;
  4384. idVec3 offset;
  4385. const idDeclModelDef *modelDef;
  4386. if ( !model || model->IsDefaultModel() ) {
  4387. return NULL;
  4388. }
  4389. args = gameLocal.FindEntityDefDict( classname, false );
  4390. if ( !args ) {
  4391. return NULL;
  4392. }
  4393. memset( &ent, 0, sizeof( ent ) );
  4394. ent.bounds.Clear();
  4395. ent.suppressSurfaceInViewID = 0;
  4396. modelDef = ANIM_GetModelDefFromEntityDef( args );
  4397. if ( modelDef ) {
  4398. animNum = modelDef->GetAnim( animname );
  4399. if ( !animNum ) {
  4400. return NULL;
  4401. }
  4402. anim = modelDef->GetAnim( animNum );
  4403. if ( !anim ) {
  4404. return NULL;
  4405. }
  4406. md5anim = anim->MD5Anim( 0 );
  4407. ent.customSkin = modelDef->GetDefaultSkin();
  4408. offset = modelDef->GetVisualOffset();
  4409. } else {
  4410. filename = animname;
  4411. filename.ExtractFileExtension( extension );
  4412. if ( !extension.Length() ) {
  4413. animname = args->GetString( va( "anim %s", animname ) );
  4414. }
  4415. md5anim = animationLib.GetAnim( animname );
  4416. offset.Zero();
  4417. }
  4418. if ( !md5anim ) {
  4419. return NULL;
  4420. }
  4421. temp = args->GetString( "skin", "" );
  4422. if ( temp[ 0 ] ) {
  4423. ent.customSkin = declManager->FindSkin( temp );
  4424. }
  4425. ent.numJoints = model->NumJoints();
  4426. ent.joints = ( idJointMat * )Mem_Alloc16( ent.numJoints * sizeof( *ent.joints ) );
  4427. ANIM_CreateAnimFrame( model, md5anim, ent.numJoints, ent.joints, FRAME2MS( frame ), offset, remove_origin_offset );
  4428. newmodel = model->InstantiateDynamicModel( &ent, NULL, NULL );
  4429. Mem_Free16( ent.joints );
  4430. ent.joints = NULL;
  4431. return newmodel;
  4432. }