Anim_Blend.cpp 121 KB

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