Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PSKModel.cpp 60KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Freyja
  5. * Author : Terry 'Mongoose' Hendrix II
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : PSKModel
  9. * License : No use w/o permission (C) 2003 Mongoose
  10. * Comments: Unreal Tournament skeletal model
  11. *
  12. *
  13. * This file was generated using Mongoose's C++
  14. * template generator script. <stu7440@westga.edu>
  15. *
  16. *-- History -------------------------------------------------
  17. *
  18. * 2003.07.12:
  19. * Mongoose - UT Package prototype code moved into 'UTPackage'
  20. * API changes to structs to clearify meaning
  21. *
  22. * 2003.06.12:
  23. * Mongoose - PSA keyframes fixed, special thanks to:
  24. * Steven Fuller and Forest Hale for help picking
  25. * apart those tough little 8 bytes =)
  26. *
  27. * 2003.06.11:
  28. * Mongoose - Broke up rendering and model loading into seperate
  29. * classes ( Closely coupled )
  30. *
  31. * PSA experimental loader
  32. *
  33. * 2003.06.10:
  34. * Mongoose - Skeletal format fixed, should load perfectly
  35. *
  36. * 2003.01.20:
  37. * Mongoose - Fixed triangle rendering thanks to debugging by
  38. * Steven Fuller, who found the tris -> UV -> vert
  39. * connection
  40. *
  41. * Finished up basic rendering, lots of
  42. * small fixes/features
  43. *
  44. * 2003.01.06:
  45. * Mongoose - Created
  46. =================================================================*/
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <stdarg.h>
  51. #include <math.h>
  52. #ifdef USING_OPENGL
  53. # include <GL/gl.h>
  54. # include <GL/glu.h>
  55. #endif
  56. #include "UTPackage.h"
  57. #include "PSKModel.h"
  58. #define INTERACTIVE_BONE_RENDER
  59. void multiply_matrix(float *a, float *b, float *result)
  60. {
  61. /* Column order */
  62. result[ 0] = a[ 0] * b[ 0] + a[ 4] * b[ 1] + a[ 8] * b[ 2] + a[12] * b[ 3];
  63. result[ 1] = a[ 0] * b[ 4] + a[ 4] * b[ 5] + a[ 8] * b[ 6] + a[12] * b[ 7];
  64. result[ 2] = a[ 0] * b[ 8] + a[ 4] * b[ 9] + a[ 8] * b[10] + a[12] * b[11];
  65. result[ 3] = a[ 0] * b[12] + a[ 4] * b[13] + a[ 8] * b[14] + a[12] * b[15];
  66. result[ 4] = a[ 1] * b[ 0] + a[ 5] * b[ 1] + a[ 9] * b[ 2] + a[13] * b[ 3];
  67. result[ 5] = a[ 1] * b[ 4] + a[ 5] * b[ 5] + a[ 9] * b[ 6] + a[13] * b[ 7];
  68. result[ 6] = a[ 1] * b[ 8] + a[ 5] * b[ 9] + a[ 9] * b[10] + a[13] * b[11];
  69. result[ 7] = a[ 1] * b[12] + a[ 5] * b[13] + a[ 9] * b[14] + a[13] * b[15];
  70. result[ 8] = a[ 2] * b[ 0] + a[ 6] * b[ 1] + a[10] * b[ 2] + a[14] * b[ 3];
  71. result[ 9] = a[ 2] * b[ 4] + a[ 6] * b[ 5] + a[10] * b[ 6] + a[14] * b[ 7];
  72. result[10] = a[ 2] * b[ 8] + a[ 6] * b[ 9] + a[10] * b[10] + a[14] * b[11];
  73. result[11] = a[ 2] * b[12] + a[ 6] * b[13] + a[10] * b[14] + a[14] * b[15];
  74. result[12] = a[ 3] * b[ 0] + a[ 7] * b[ 1] + a[11] * b[ 2] + a[15] * b[ 3];
  75. result[13] = a[ 3] * b[ 4] + a[ 7] * b[ 5] + a[11] * b[ 6] + a[15] * b[ 7];
  76. result[14] = a[ 3] * b[ 8] + a[ 7] * b[ 9] + a[11] * b[10] + a[15] * b[11];
  77. result[15] = a[ 3] * b[12] + a[ 7] * b[13] + a[11] * b[14] + a[15] * b[15];
  78. }
  79. void copy_matrix(float *src, float *dest)
  80. {
  81. unsigned int i;
  82. for (i = 0; i < 16; ++i)
  83. dest[i] = src[i];
  84. }
  85. void translate_matrix(float tx, float ty, float tz, float *m)
  86. {
  87. unsigned int i;
  88. float tmat[16];
  89. float tmp[16];
  90. tmat[ 0]=1; tmat[ 1]=0; tmat[ 2]=0; tmat[ 3]=0;
  91. tmat[ 4]=0; tmat[ 5]=1; tmat[ 6]=0; tmat[ 7]=0;
  92. tmat[ 8]=0; tmat[ 9]=0; tmat[10]=1; tmat[11]=0;
  93. tmat[12]=tx; tmat[13]=ty; tmat[14]=tz; tmat[15]=1;
  94. //copy_matrix(m, tmp);
  95. for (i = 0; i < 16; ++i)
  96. tmp[i] = m[i];
  97. multiply_matrix(tmp, tmat, m);
  98. }
  99. /* <M> is input matrix
  100. <Out> is output matrix ( M inverted )
  101. both are float[16] column major matrices */
  102. bool invert_matrix(float *m, float *out)
  103. {
  104. /* Mongoose: This code was from a Jeff Lander tutorial which was based
  105. on MESA GL's InvertMatrix */
  106. /* NB. OpenGL Matrices are COLUMN major. */
  107. #define SWAP_ROWS(a, b) { float *_tmp = a; (a)=(b); (b)=_tmp; }
  108. #define MAT(m,r,c) (m)[(c)*4+(r)]
  109. float wtmp[4][8];
  110. float m0, m1, m2, m3, s;
  111. float *r0, *r1, *r2, *r3;
  112. r0 = wtmp[0], r1 = wtmp[1], r2 = wtmp[2], r3 = wtmp[3];
  113. r0[0] = MAT(m,0,0), r0[1] = MAT(m,0,1),
  114. r0[2] = MAT(m,0,2), r0[3] = MAT(m,0,3),
  115. r0[4] = 1.0, r0[5] = r0[6] = r0[7] = 0.0,
  116. r1[0] = MAT(m,1,0), r1[1] = MAT(m,1,1),
  117. r1[2] = MAT(m,1,2), r1[3] = MAT(m,1,3),
  118. r1[5] = 1.0, r1[4] = r1[6] = r1[7] = 0.0,
  119. r2[0] = MAT(m,2,0), r2[1] = MAT(m,2,1),
  120. r2[2] = MAT(m,2,2), r2[3] = MAT(m,2,3),
  121. r2[6] = 1.0, r2[4] = r2[5] = r2[7] = 0.0,
  122. r3[0] = MAT(m,3,0), r3[1] = MAT(m,3,1),
  123. r3[2] = MAT(m,3,2), r3[3] = MAT(m,3,3),
  124. r3[7] = 1.0, r3[4] = r3[5] = r3[6] = 0.0;
  125. /* choose pivot - or die */
  126. if (fabs(r3[0])>fabs(r2[0])) SWAP_ROWS(r3, r2);
  127. if (fabs(r2[0])>fabs(r1[0])) SWAP_ROWS(r2, r1);
  128. if (fabs(r1[0])>fabs(r0[0])) SWAP_ROWS(r1, r0);
  129. if (0.0 == r0[0]) return false;
  130. /* eliminate first variable */
  131. m1 = r1[0]/r0[0]; m2 = r2[0]/r0[0]; m3 = r3[0]/r0[0];
  132. s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
  133. s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
  134. s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
  135. s = r0[4];
  136. if (s != 0.0) { r1[4] -= m1 * s; r2[4] -= m2 * s; r3[4] -= m3 * s; }
  137. s = r0[5];
  138. if (s != 0.0) { r1[5] -= m1 * s; r2[5] -= m2 * s; r3[5] -= m3 * s; }
  139. s = r0[6];
  140. if (s != 0.0) { r1[6] -= m1 * s; r2[6] -= m2 * s; r3[6] -= m3 * s; }
  141. s = r0[7];
  142. if (s != 0.0) { r1[7] -= m1 * s; r2[7] -= m2 * s; r3[7] -= m3 * s; }
  143. /* choose pivot - or die */
  144. if (fabs(r3[1])>fabs(r2[1])) SWAP_ROWS(r3, r2);
  145. if (fabs(r2[1])>fabs(r1[1])) SWAP_ROWS(r2, r1);
  146. if (0.0 == r1[1]) return false;
  147. /* eliminate second variable */
  148. m2 = r2[1]/r1[1]; m3 = r3[1]/r1[1];
  149. r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
  150. r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
  151. s = r1[4]; if (0.0 != s) { r2[4] -= m2 * s; r3[4] -= m3 * s; }
  152. s = r1[5]; if (0.0 != s) { r2[5] -= m2 * s; r3[5] -= m3 * s; }
  153. s = r1[6]; if (0.0 != s) { r2[6] -= m2 * s; r3[6] -= m3 * s; }
  154. s = r1[7]; if (0.0 != s) { r2[7] -= m2 * s; r3[7] -= m3 * s; }
  155. /* choose pivot - or die */
  156. if (fabs(r3[2])>fabs(r2[2])) SWAP_ROWS(r3, r2);
  157. if (0.0 == r2[2]) return false;
  158. /* eliminate third variable */
  159. m3 = r3[2]/r2[2];
  160. r3[3] -= m3 * r2[3], r3[4] -= m3 * r2[4],
  161. r3[5] -= m3 * r2[5], r3[6] -= m3 * r2[6],
  162. r3[7] -= m3 * r2[7];
  163. /* last check */
  164. if (0.0 == r3[3]) return false;
  165. s = 1.0/r3[3]; /* now back substitute row 3 */
  166. r3[4] *= s; r3[5] *= s; r3[6] *= s; r3[7] *= s;
  167. m2 = r2[3]; /* now back substitute row 2 */
  168. s = 1.0/r2[2];
  169. r2[4] = s * (r2[4] - r3[4] * m2), r2[5] = s * (r2[5] - r3[5] * m2),
  170. r2[6] = s * (r2[6] - r3[6] * m2), r2[7] = s * (r2[7] - r3[7] * m2);
  171. m1 = r1[3];
  172. r1[4] -= r3[4] * m1, r1[5] -= r3[5] * m1,
  173. r1[6] -= r3[6] * m1, r1[7] -= r3[7] * m1;
  174. m0 = r0[3];
  175. r0[4] -= r3[4] * m0, r0[5] -= r3[5] * m0,
  176. r0[6] -= r3[6] * m0, r0[7] -= r3[7] * m0;
  177. m1 = r1[2]; /* now back substitute row 1 */
  178. s = 1.0/r1[1];
  179. r1[4] = s * (r1[4] - r2[4] * m1), r1[5] = s * (r1[5] - r2[5] * m1),
  180. r1[6] = s * (r1[6] - r2[6] * m1), r1[7] = s * (r1[7] - r2[7] * m1);
  181. m0 = r0[2];
  182. r0[4] -= r2[4] * m0, r0[5] -= r2[5] * m0,
  183. r0[6] -= r2[6] * m0, r0[7] -= r2[7] * m0;
  184. m0 = r0[1]; /* now back substitute row 0 */
  185. s = 1.0/r0[0];
  186. r0[4] = s * (r0[4] - r1[4] * m0), r0[5] = s * (r0[5] - r1[5] * m0),
  187. r0[6] = s * (r0[6] - r1[6] * m0), r0[7] = s * (r0[7] - r1[7] * m0);
  188. MAT(out,0,0) = r0[4];
  189. MAT(out,0,1) = r0[5], MAT(out,0,2) = r0[6];
  190. MAT(out,0,3) = r0[7], MAT(out,1,0) = r1[4];
  191. MAT(out,1,1) = r1[5], MAT(out,1,2) = r1[6];
  192. MAT(out,1,3) = r1[7], MAT(out,2,0) = r2[4];
  193. MAT(out,2,1) = r2[5], MAT(out,2,2) = r2[6];
  194. MAT(out,2,3) = r2[7], MAT(out,3,0) = r3[4];
  195. MAT(out,3,1) = r3[5], MAT(out,3,2) = r3[6];
  196. MAT(out,3,3) = r3[7];
  197. return true;
  198. #undef MAT
  199. #undef SWAP_ROWS
  200. }
  201. void quaternion_to_matrix(float mW, float mX, float mY, float mZ, float *m)
  202. {
  203. m[ 0] = 1.0f - 2.0f * (mY*mY + mZ*mZ);
  204. m[ 1] = 2.0f * (mX*mY - mW*mZ);
  205. m[ 2] = 2.0f * (mX*mZ + mW*mY);
  206. m[ 3] = 0.0f;
  207. m[ 4] = 2.0f * (mX*mY + mW*mZ);
  208. m[ 5] = 1.0f - 2.0f * (mX*mX + mZ*mZ);
  209. m[ 6] = 2.0f * (mY*mZ - mW*mX);
  210. m[ 7] = 0.0f;
  211. m[ 8] = 2.0f * (mX*mZ - mW*mY);
  212. m[ 9] = 2.0f * (mY*mZ + mW*mX);
  213. m[10] = 1.0 - 2.0f * (mX*mX + mY*mY);
  214. m[11] = 0.0f;
  215. m[12] = 0.0f;
  216. m[13] = 0.0f;
  217. m[14] = 0.0f;
  218. m[15] = 1.0f;
  219. }
  220. void quaternion_to_axis_angles(float qw, float qx, float qy, float qz,
  221. float *theta, float *x, float *y, float *z)
  222. {
  223. *theta = (float)acos(qw) * 2.0;
  224. double scale = (
  225. //sqrt(qx*qx + qy*qy + qz*qz);
  226. (float)sin(*theta / 2.0));
  227. *x = qx / scale;
  228. *y = qy / scale;
  229. *z = qz / scale;
  230. }
  231. void quaternion_to_euler_angles(float qw, float qx, float qy, float qz,
  232. float *x, float *y, float *z)
  233. {
  234. double qw2 = qw*qw;
  235. double qx2 = qx*qx;
  236. double qy2 = qy*qy;
  237. double qz2 = qz*qz;
  238. *x = atan(2.0 * (qx*qy+qz*qw)/(qx2 - qy2 + qz2 + qw2)); // heading
  239. *y = atan(2.0 * (qy*qz+qx*qw)/(-qx2 - qy2 + qz2 + qw2)); // bank
  240. *z = asin(-2.0 * (qx*qz-qy*qw)); // attitude
  241. }
  242. ////////////////////////////////////////////////////////////
  243. // Constructors
  244. ////////////////////////////////////////////////////////////
  245. PSKModel::PSKModel()
  246. {
  247. mFlags = 0;
  248. mNumFrames = 0;
  249. mNumVertices = 0;
  250. mNumFaces = 0;
  251. mNumVTXWs = 0;
  252. mNumMaterials = 0;
  253. mNumBones = 0;
  254. mNumWeights = 0;
  255. mVertices = 0x0;
  256. mWeights = 0x0;
  257. mVTXWs = 0x0;
  258. mFaces = 0x0;
  259. mMaterials = 0x0;
  260. mBones = 0x0;
  261. }
  262. PSKModel::~PSKModel()
  263. {
  264. if (mVertices)
  265. delete [] mVertices;
  266. if (mWeights)
  267. delete [] mWeights;
  268. if (mVTXWs)
  269. delete [] mVTXWs;
  270. if (mFaces)
  271. delete [] mFaces;
  272. if (mMaterials)
  273. delete [] mMaterials;
  274. if (mBones)
  275. delete [] mBones;
  276. }
  277. ////////////////////////////////////////////////////////////
  278. // Public Accessors
  279. ////////////////////////////////////////////////////////////
  280. void PSKModel::printSkeletion(bool printNames)
  281. {
  282. unsigned int i, j;
  283. printf("Skeletion layout...\n");
  284. for (i = 0; i < mNumBones; ++i)
  285. {
  286. printf("%3i. ", i);
  287. j = i;
  288. while (j > 0)
  289. {
  290. if (printNames)
  291. {
  292. printf(" -> %s ", mBones[j].name);
  293. }
  294. else
  295. {
  296. printf(" -> %u ", j);
  297. }
  298. j = mBones[j].parentIndex;
  299. }
  300. printf("\n");
  301. }
  302. printf("\n");
  303. }
  304. ////////////////////////////////////////////////////////////
  305. // Public Mutators
  306. ////////////////////////////////////////////////////////////
  307. int PSKModel::load(char *filename)
  308. {
  309. FILE *f;
  310. char buffer[128];
  311. unsigned int i, frame;
  312. float r;
  313. f = fopen(filename, "rb");
  314. if (!f)
  315. {
  316. perror("PSKModel::load> ");
  317. return -1;
  318. }
  319. /* ACTRHEAD... */
  320. fread(buffer, 1, 28, f);
  321. buffer[8] = 0;
  322. if (strncmp("ACTRHEAD", buffer, 8) != 0)
  323. {
  324. printf("Looking for 'ACTRHEAD' found '%s'\n", buffer);
  325. printf("This isn't a valid or known PSK model.\n");
  326. return -2;
  327. }
  328. else
  329. {
  330. printf("Unreal Tourament (c) EpicGames Skeletal Model Viewer\n");
  331. printf("Pre ALPHA release\n");
  332. printf("Give feedback on correctness and bugs\n");
  333. printf("by mongoose@icculus.org\n");
  334. }
  335. fread(&mNumFrames, 1, 4, f);
  336. printf(" NumFrames =\t %u\n", mNumFrames); // used to expand NumVerts?
  337. printf(" FORCING FRAMES TO 1 FOR TESTING NOW.\n");
  338. mNumFrames = 1;
  339. for (frame = 0; frame < mNumFrames; ++frame)
  340. {
  341. printf(" ===========================\n");
  342. printf(" Reading Frame[%03u]\n", frame);
  343. printf(" ===========================\n");
  344. /* PNTS0000... */
  345. fread(buffer, 1, 28, f);
  346. buffer[8] = 0;
  347. if (frame == 0 && strncmp("PNTS0000", buffer, 8) != 0)
  348. {
  349. printf("Expected PNTS0000, found '%s'\n", buffer);
  350. printf("Test model Vertex offset 0x003c, at 0x%lx?\n", ftell(f));
  351. printf("This isn't a known PNTS0000\n");
  352. return -2;
  353. }
  354. fread(&mNumVertices, 1, 4, f);
  355. printf(" NumVertices =\t %u\n", mNumVertices);
  356. mVertices = new float[mNumVertices*3];
  357. for (i = 0; i < mNumVertices*3; i+= 3)
  358. {
  359. fread(&r, 1, 4, f);
  360. mVertices[i] = r;
  361. fread(&r, 1, 4, f);
  362. mVertices[i+1] = r;
  363. fread(&r, 1, 4, f);
  364. mVertices[i+2] = r;
  365. if (mFlags & fDebugPointLoad)
  366. {
  367. printf("\t%5u (%.3f, %.3f, %.3f)\n",
  368. i/3, mVertices[i], mVertices[i+1], mVertices[i+2]);
  369. }
  370. }
  371. /* VTXW0000... */
  372. fread(buffer, 1, 28, f);
  373. buffer[8] = 0;
  374. if (frame == 0 && strncmp("VTXW0000", buffer, 8) != 0)
  375. {
  376. printf("Expected VTXW0000, found '%s'\n", buffer);
  377. printf("Test model UV offset 0x2554, at 0x%lx?\n", ftell(f));
  378. printf("This isn't a known VTXW0000\n");
  379. return -2;
  380. }
  381. fread(&mNumVTXWs, 1, 4, f);
  382. printf(" NumVTXWs =\t %u\n", mNumVTXWs);
  383. mVTXWs = new psk_vtxw_t[mNumVTXWs];
  384. // <relnev> (uint vertex in lowest 16bits and something in upper 16, float u, float v, uint flags?)
  385. for (i = 0; i < mNumVTXWs; ++i)
  386. {
  387. // Vertex
  388. fread(&mVTXWs[i].vertex, 2, 1, f);
  389. // ??
  390. fread(&mVTXWs[i].unknown1, 2, 1, f);
  391. // UV
  392. fread(mVTXWs[i].uv+0, 4, 1, f);
  393. fread(mVTXWs[i].uv+1, 4, 1, f);
  394. // Used to compute weight index?
  395. fread(&mVTXWs[i].unknown2, 4, 1, f);
  396. if (mFlags & fDebugUVLoad)
  397. {
  398. printf("\t%5u vertex = %u, unknown1 = 0x%x, UV = (%.3f, %.3f), unknown2 = 0x%x\n",
  399. i,
  400. mVTXWs[i].vertex, mVTXWs[i].unknown1,
  401. mVTXWs[i].uv[0], mVTXWs[i].uv[1],
  402. mVTXWs[i].unknown2);
  403. }
  404. }
  405. /* FACE0000... */
  406. fread(buffer, 1, 28, f);
  407. buffer[8] = 0;
  408. if (frame == 0 && strncmp("FACE0000", buffer, 8) != 0)
  409. {
  410. printf("Expected FACE0000, found '%s'\n", buffer);
  411. printf("Test model FACE offset 0x7074, at 0x%lx?\n", ftell(f));
  412. printf("This isn't a known value for FACE\n");
  413. return -2;
  414. }
  415. fread(&mNumFaces, 1, 4, f);
  416. printf(" NumFaces =\t %u\n", mNumFaces);
  417. mFaces = new psk_face_t[mNumFaces];
  418. for (i = 0; i < mNumFaces; ++i)
  419. {
  420. // Vertex indices
  421. fread(&mFaces[i].x, 1, 2, f);
  422. fread(&mFaces[i].y, 1, 2, f);
  423. fread(&mFaces[i].z, 1, 2, f);
  424. // Material
  425. fread(&mFaces[i].material, 1, 1, f);
  426. // Bitflags
  427. fread(&mFaces[i].flags, 1, 4, f);
  428. // ??
  429. fread(&mFaces[i].unknown, 1, 1, f);
  430. if (mFlags & fDebugFaceLoad)
  431. {
  432. printf("%5u (%u, %u, %u), material = %u, flags = %u, unknown = 0x%x\n",
  433. i,
  434. mFaces[i].x, mFaces[i].y, mFaces[i].z,
  435. mFaces[i].material, mFaces[i].flags, mFaces[i].unknown);
  436. }
  437. }
  438. /* MATT0000... */
  439. fread(buffer, 1, 28, f);
  440. buffer[8] = 0;
  441. if (frame == 0 && strncmp("MATT0000", buffer, 8) != 0)
  442. {
  443. printf("Expected MATT0000, found '%s'\n", buffer);
  444. printf("Test model Material offset 0xb750, at 0x%lx?\n", ftell(f));
  445. printf("This isn't a known value for MATT0000\n");
  446. return -2;
  447. }
  448. fread(&mNumMaterials, 1, 4, f);
  449. printf(" NumMaterials =\t %u\n", mNumMaterials);
  450. mMaterials = new psk_material_t[mNumMaterials];
  451. for (i = 0; i < mNumMaterials; ++i)
  452. {
  453. fread(&mMaterials[i].name, 1, 64, f);
  454. fread(&mMaterials[i].unknown1, 1, 4, f);
  455. fread(&mMaterials[i].unknown2, 1, 4, f);
  456. fread(&mMaterials[i].unknown3, 1, 4, f);
  457. fread(&mMaterials[i].unknown4, 1, 4, f);
  458. fread(&mMaterials[i].unknown5, 1, 4, f);
  459. fread(&mMaterials[i].unknown6, 1, 4, f);
  460. if (mFlags & fDebugMattLoad)
  461. {
  462. printf("\n# File offset 0x%lx\n", ftell(f) - 88);
  463. printf("mMaterial[%u].name = '%s'\n", i, mMaterials[i].name);
  464. printf("mMaterial[%u].unknown1 = 0x%x\n",
  465. i, mMaterials[i].unknown1);
  466. printf("mMaterial[%u].unknown2 = 0x%x\n",
  467. i, mMaterials[i].unknown2);
  468. printf("mMaterial[%u].unknown3 = 0x%x\n",
  469. i, mMaterials[i].unknown3);
  470. printf("mMaterial[%u].unknown4 = 0x%x\n",
  471. i, mMaterials[i].unknown4);
  472. printf("mMaterial[%u].unknown5 = 0x%x\n",
  473. i, mMaterials[i].unknown5);
  474. printf("mMaterial[%u].unknown6 = 0x%x\n",
  475. i, mMaterials[i].unknown6);
  476. printf("UVWs counting??\n");
  477. }
  478. }
  479. }
  480. /* REFSKELT... */
  481. fread(buffer, 1, 28, f);
  482. buffer[8] = 0;
  483. if (strncmp("REFSKELT", buffer, 8) != 0)
  484. {
  485. printf("Expected REFSKELT, found '%s'\n", buffer);
  486. printf("Test model Material offset 0xB820, at 0x%lx?\n", ftell(f));
  487. printf("This isn't a known value for REFSKELT\n");
  488. return -2;
  489. }
  490. fread(&mNumBones, 1, 4, f);
  491. printf(" NumBones =\t %u\n", mNumBones);
  492. mBones = new psk_bone_t[mNumBones];
  493. for (i = 0; i < mNumBones; ++i)
  494. {
  495. fread(buffer, 1, 64, f);
  496. strncpy(mBones[i].name, buffer, 64);
  497. fread(&mBones[i].flags, 1, 4, f);
  498. fread(&mBones[i].numChildren, 1, 4, f);
  499. fread(&mBones[i].parentIndex, 1, 4, f);
  500. fread(&r, 1, 4, f);
  501. mBones[i].restDir[0] = r;
  502. fread(&r, 1, 4, f);
  503. mBones[i].restDir[1] = r;
  504. fread(&r, 1, 4, f);
  505. mBones[i].restDir[2] = r;
  506. fread(&r, 1, 4, f);
  507. mBones[i].restDir[3] = r;
  508. fread(&r, 1, 4, f);
  509. mBones[i].restLoc[0] = r;
  510. fread(&r, 1, 4, f);
  511. mBones[i].restLoc[1] = r;
  512. fread(&r, 1, 4, f);
  513. mBones[i].restLoc[2] = r;
  514. fread(mBones[i].unknown+0, 1, 4, f);
  515. fread(mBones[i].unknown+1, 1, 4, f);
  516. fread(mBones[i].unknown+2, 1, 4, f);
  517. fread(mBones[i].unknown+3, 1, 4, f);
  518. if (mFlags & fDebugBoneLoad)
  519. {
  520. unsigned int offset = ftell(f) - 110;
  521. printf("\n# File offset 0x%x + 110bytes\n", offset);
  522. printf("Bone[%03u] = { '%s'\n", i, buffer);
  523. printf("\tParent: %u, Children: %u, flags: 0x%80x\n",
  524. mBones[i].parentIndex, mBones[i].numChildren,
  525. mBones[i].flags);
  526. printf("\tRest Loc: (%.3f, %.3f, %.3f)\n",
  527. mBones[i].restLoc[0], mBones[i].restLoc[1],
  528. mBones[i].restLoc[2]);
  529. printf("\tRest Dir XYZ: (%.3f, %.3f, %.3f), W: %.3f?\n",
  530. mBones[i].restDir[0], mBones[i].restDir[1],
  531. mBones[i].restDir[2], mBones[i].restDir[3]);
  532. printf("\tUnknown: 0x%x, 0x%x, 0x%x, Flags? 0x%x\n",
  533. mBones[i].unknown[0], mBones[i].unknown[1],
  534. mBones[i].unknown[2], mBones[i].unknown[3]);
  535. printf("}\n");
  536. }
  537. }
  538. if (mFlags & fDebugBoneLoad)
  539. {
  540. printSkeletion(false);
  541. }
  542. /* RAWWEIGHTS... */
  543. fread(buffer, 1, 28, f);
  544. buffer[11] = 0;
  545. if (strncmp("RAWWEIGHTS", buffer, 10) != 0)
  546. {
  547. printf("Expected RAWWEIGHTS, found '%s'\n", buffer);
  548. printf("Test model Weight offset 0xcb00, at 0x%lx?\n", ftell(f));
  549. printf("This isn't a known value for RAWWEIGHTS\n");
  550. return -2;
  551. }
  552. fread(&mNumWeights, 1, 4, f);
  553. printf(" NumWeights =\t %u\n", mNumWeights);
  554. mWeights = new psk_weight_t[mNumWeights];
  555. for (i = 0; i < mNumWeights; ++i)
  556. {
  557. fread(&mWeights[i].weight, 4, 1, f);
  558. fread(&mWeights[i].vertexIndex, 4, 1, f);
  559. fread(&mWeights[i].boneIndex, 4, 1, f);
  560. if (mFlags & fDebugWeightLoad)
  561. {
  562. printf("\tWeight[%u] { w = %f, vertexIndex = %u, boneIndex = %u }\n", i,
  563. mWeights[i].weight,
  564. mWeights[i].vertexIndex, mWeights[i].boneIndex);
  565. }
  566. }
  567. fclose(f);
  568. return 0;
  569. }
  570. ////////////////////////////////////////////////////////////
  571. // Private Accessors
  572. ////////////////////////////////////////////////////////////
  573. ////////////////////////////////////////////////////////////
  574. // Private Mutators
  575. ////////////////////////////////////////////////////////////
  576. //////////////////////////////////////////////////////////////////////
  577. // PSAAnimation Class
  578. //////////////////////////////////////////////////////////////////////
  579. PSAAnimation::PSAAnimation()
  580. {
  581. mNumFrames = 0;
  582. mNumBones = 0;
  583. mNumAnimInfos = 0;
  584. mNumKeyFrames = 0;
  585. mBones = 0x0;
  586. mAnimInfos = 0x0;
  587. mKeyFrames = 0x0;
  588. }
  589. PSAAnimation::~PSAAnimation()
  590. {
  591. if (mBones)
  592. delete [] mBones;
  593. if (mAnimInfos)
  594. delete [] mAnimInfos;
  595. if (mKeyFrames)
  596. delete [] mKeyFrames;
  597. }
  598. int PSAAnimation::load(char *filename)
  599. {
  600. FILE *f = fopen(filename, "rb");
  601. char s[64];
  602. unsigned int i, u;
  603. if (!f)
  604. {
  605. printf("PSAAnimation::load() Error: \n");
  606. perror(filename);
  607. return -1;
  608. }
  609. /* Is this a PSA file? */
  610. fread(s, 28, 1, f);
  611. s[8] = 0;
  612. if (strncmp("ANIMHEAD", s, 8) != 0)
  613. {
  614. printf("Expected ANIMHEAD, found '%s'\n", s);
  615. printf("File offset: 0x%lx?\n", ftell(f));
  616. printf("This isn't a known value.\n");
  617. return -2;
  618. }
  619. fread(&mNumFrames, 4, 1, f);
  620. printf("numFrames = %u\n", mNumFrames);
  621. /* Vertex blending frames? */
  622. /* Check for new section */
  623. fread(s, 28, 1, f);
  624. s[9] = 0;
  625. if (strncmp("BONENAMES", s, 9) != 0)
  626. {
  627. printf("Expected BONENAMES, found '%s'\n", s);
  628. printf("File offset: 0x%lx?\n", ftell(f));
  629. printf("This isn't a known value.\n");
  630. return -2;
  631. }
  632. fread(&mNumBones, 4, 1, f);
  633. printf("numBones = %u\n", mNumBones);
  634. mBones = new psk_bone_t[mNumBones];
  635. for (i = 0; i < mNumBones; ++i)
  636. {
  637. fread(mBones[i].name, 1, 64, f);
  638. fread(&mBones[i].flags, 1, 4, f);
  639. fread(&mBones[i].numChildren, 1, 4, f);
  640. fread(&mBones[i].parentIndex, 1, 4, f);
  641. fread(&mBones[i].restDir[0], 1, 4, f);
  642. fread(&mBones[i].restDir[1], 1, 4, f);
  643. fread(&mBones[i].restDir[2], 1, 4, f);
  644. fread(&mBones[i].restDir[3], 1, 4, f);
  645. fread(&mBones[i].restLoc[0], 1, 4, f);
  646. fread(&mBones[i].restLoc[1], 1, 4, f);
  647. fread(&mBones[i].restLoc[2], 1, 4, f);
  648. fread(mBones[i].unknown+0, 1, 4, f);
  649. fread(mBones[i].unknown+1, 1, 4, f);
  650. fread(mBones[i].unknown+2, 1, 4, f);
  651. fread(mBones[i].unknown+3, 1, 4, f);
  652. if (mFlags &fDebugBones)
  653. {
  654. printf("Bone[%03u] = { '%s'\n", i, mBones[i].name);
  655. printf("\tParent: %u, Children: %u, Flags: 0x%04x\n",
  656. mBones[i].parentIndex, mBones[i].numChildren,
  657. mBones[i].flags);
  658. printf("\tRest Loc: (%.3f, %.3f, %.3f)\n",
  659. mBones[i].restLoc[0], mBones[i].restLoc[1],
  660. mBones[i].restLoc[2]);
  661. printf("\tRest Dir XYZ: (%.3f, %.3f, %.3f), W: %.3f\n",
  662. mBones[i].restDir[0], mBones[i].restDir[1],
  663. mBones[i].restDir[2], mBones[i].restDir[3]);
  664. printf("\?: 0x%x, 0x%x, 0x%x, Flags? 0x%08x\n",
  665. mBones[i].unknown[0], mBones[i].unknown[1],
  666. mBones[i].unknown[2], mBones[i].unknown[3]);
  667. printf("? %f\n", *((float *)&mBones[i].unknown[3]));
  668. printf("}\n");
  669. }
  670. }
  671. /* Check for new section */
  672. fread(s, 28, 1, f);
  673. s[8] = 0;
  674. if (strncmp("ANIMINFO", s, 8) != 0)
  675. {
  676. printf("Expected ANIMINFO, found '%s'\n", s);
  677. printf("File offset: 0x%lx?\n", ftell(f));
  678. printf("This isn't a known value.\n");
  679. return -2;
  680. }
  681. fread(&mNumAnimInfos, 4, 1, f);
  682. printf("numAnimInfos = %u\n", mNumAnimInfos);
  683. mAnimInfos = new psa_anim_info_t[mNumAnimInfos];
  684. for (i = 0; i < mNumAnimInfos; ++i)
  685. {
  686. fread(mAnimInfos[i].name, 64, 1, f);
  687. fread(mAnimInfos[i].name2, 64, 1, f);
  688. fread(&mAnimInfos[i].numBones, 4, 1, f);
  689. fread(&mAnimInfos[i].rootId, 4, 1, f);
  690. fread(&mAnimInfos[i].key[0], 4, 1, f);
  691. fread(&mAnimInfos[i].key[1], 4, 1, f);
  692. fread(&mAnimInfos[i].keyf, 4, 1, f);
  693. fread(&mAnimInfos[i].time[0], 4, 1, f);
  694. fread(&mAnimInfos[i].time[1], 4, 1, f);
  695. fread(&mAnimInfos[i].unknown[0], 4, 1, f);
  696. fread(&mAnimInfos[i].unknown[1], 4, 1, f);
  697. fread(&mAnimInfos[i].unknown[2], 4, 1, f);
  698. if (mFlags &fDebugAnimInfos)
  699. {
  700. printf("\n");
  701. printf("AnimInfo[%u] = { name: '%s', name2: '%s'\n", i,
  702. mAnimInfos[i].name, mAnimInfos[i].name2);
  703. printf(" numBones = %u, rootId = %u\n",
  704. mAnimInfos[i].numBones, mAnimInfos[i].rootId);
  705. printf(" key: %u %u %f\n",
  706. mAnimInfos[i].key[0], mAnimInfos[i].key[1],
  707. mAnimInfos[i].keyf);
  708. printf(" time: %f %f\n",
  709. mAnimInfos[i].time[0], mAnimInfos[i].time[1]);
  710. printf(" unknowns: %f %f %f\n",
  711. mAnimInfos[i].unknown[0], mAnimInfos[i].unknown[1],
  712. mAnimInfos[i].unknown[2]);
  713. printf("}\n");
  714. }
  715. }
  716. printf("COMPUTE: psa.numKeyframes / info.numBones = frames per bone\n");
  717. printf("\n");
  718. /* Check for new section */
  719. fread(s, 28, 1, f);
  720. s[8] = 0;
  721. if (strncmp("ANIMKEYS", s, 8) != 0)
  722. {
  723. printf("Expected ANIMKEYS, found '%s'\n", s);
  724. printf("File offset: 0x%lx?\n", ftell(f));
  725. printf("This isn't a known value.\n");
  726. return -2;
  727. }
  728. fread(&mNumKeyFrames, 4, 1, f);
  729. printf("numKeyFrames = %u\n", mNumKeyFrames);
  730. mKeyFrames = new psa_key_frame_t[mNumKeyFrames];
  731. for (i = 0; i < mNumKeyFrames; ++i)
  732. {
  733. // tx ty tz qx qy qz qw s
  734. fread(&mKeyFrames[i].trans[0], 4, 1, f);
  735. fread(&mKeyFrames[i].trans[1], 4, 1, f);
  736. fread(&mKeyFrames[i].trans[2], 4, 1, f);
  737. fread(&mKeyFrames[i].dir[0], 4, 1, f);
  738. fread(&mKeyFrames[i].dir[1], 4, 1, f);
  739. fread(&mKeyFrames[i].dir[2], 4, 1, f);
  740. fread(&mKeyFrames[i].dir[3], 4, 1, f);
  741. fread(&mKeyFrames[i].scale, 4, 1, f);
  742. if (mFlags &fDebugKeyFrames)
  743. {
  744. printf("keyFrame[%u] = {\n", i);
  745. printf("Trans XYZ: ( %f %f %f )\n",
  746. mKeyFrames[i].trans[0], mKeyFrames[i].trans[1],
  747. mKeyFrames[i].trans[2]);
  748. printf("Dir XYZ: ( %f %f %f ) %f\n",
  749. mKeyFrames[i].dir[0], mKeyFrames[i].dir[1],
  750. mKeyFrames[i].dir[2], mKeyFrames[i].dir[3]);
  751. printf("scale = %f\n", mKeyFrames[i].scale);
  752. printf("}\n\n");
  753. }
  754. }
  755. /* Check for partial read */
  756. u = ftell(f);
  757. fseek(f, 0, SEEK_END);
  758. i = ftell(f);
  759. if (i - u > 0)
  760. {
  761. printf("ERROR: Didn't read all of file!\n");
  762. printf("Skipping %u bytes at end of file\n", i - u);
  763. }
  764. fclose(f);
  765. return 0;
  766. }
  767. //////////////////////////////////////////////////////////////////////
  768. // PSKModelRenderer Class
  769. //////////////////////////////////////////////////////////////////////
  770. PSKModelRenderer::PSKModelRenderer()
  771. {
  772. mBoneRotationCache = 0x0;
  773. mVertexTransformCache = 0x0;
  774. mInvertedMatrices = 0x0;
  775. mWorldMatrices = 0x0;
  776. mModel = 0x0;
  777. mAnimation = 0x0;
  778. mNumMatrices = 0;
  779. }
  780. PSKModelRenderer::~PSKModelRenderer()
  781. {
  782. unsigned int i;
  783. if (mBoneRotationCache)
  784. delete [] mBoneRotationCache;
  785. if (mVertexTransformCache)
  786. delete [] mVertexTransformCache;
  787. if (mInvertedMatrices)
  788. {
  789. for (i = 0; i < mNumMatrices; ++i)
  790. delete [] mInvertedMatrices[i];
  791. delete [] mInvertedMatrices;
  792. }
  793. if (mWorldMatrices)
  794. {
  795. for (i = 0; i < mNumMatrices; ++i)
  796. delete [] mWorldMatrices[i];
  797. delete [] mWorldMatrices;
  798. }
  799. }
  800. ////////////////////////////////////////////////////////////
  801. // Public Accessors
  802. ////////////////////////////////////////////////////////////
  803. void PSKModelRenderer::render()
  804. {
  805. #ifdef USING_OPENGL
  806. unsigned int i;
  807. glDisable(GL_TEXTURE_2D);
  808. glColor3f(1.0f, 1.0f, 1.0f);
  809. glPushMatrix();
  810. glRotatef(-90.0f, 1, 0, 0);
  811. //glTranslatef(0, 0, 2);
  812. if (mFlags & fRenderBones)
  813. {
  814. renderBone(0);
  815. }
  816. if (mFlags & fRenderPoints)
  817. {
  818. glBegin(GL_POINTS);
  819. for (i = 0; i < mNumVertices*3; i+= 3)
  820. {
  821. glVertex3fv(mVertexTransformCache+i);
  822. }
  823. glEnd();
  824. }
  825. if (mFlags & fRenderTexture)
  826. {
  827. glEnable(GL_TEXTURE_2D);
  828. }
  829. if (mFlags & fRenderFaces)
  830. {
  831. unsigned int index[3];
  832. int lastTexture = 999999;
  833. for (i = 0; i < mNumFaces; ++i)
  834. {
  835. if (mFlags & fRenderTexture)
  836. {
  837. if (lastTexture != mFaces[i].material)
  838. {
  839. glBindTexture(GL_TEXTURE_2D, mTextures[mFaces[i].material]);
  840. lastTexture = mFaces[i].material;
  841. }
  842. }
  843. glBegin(GL_TRIANGLES);
  844. index[0] = mVTXWs[mFaces[i].x].vertex*3;
  845. index[1] = mVTXWs[mFaces[i].y].vertex*3;
  846. index[2] = mVTXWs[mFaces[i].z].vertex*3;
  847. if (mFlags & fDebugFaceRender)
  848. {
  849. printf("face[%u] = \n", i);
  850. printf("\t (%u = (%.3f..), %u = (%.3f...), %u = (%.3f..))\n",
  851. index[0], mVertexTransformCache[index[0]],
  852. index[1], mVertexTransformCache[index[1]],
  853. index[2], mVertexTransformCache[index[2]]);
  854. printf("material = %u\n", mFaces[i].material);
  855. printf("UVs (%.3f, %.3f)\n",
  856. mVTXWs[mFaces[i].z].uv[0], mVTXWs[mFaces[i].z].uv[1]);
  857. }
  858. if (mFlags & fRenderTexture)
  859. {
  860. glTexCoord2fv(mVTXWs[mFaces[i].x].uv);
  861. glVertex3fv(mVertexTransformCache+index[0]);
  862. glTexCoord2fv(mVTXWs[mFaces[i].y].uv);
  863. glVertex3fv(mVertexTransformCache+index[1]);
  864. glTexCoord2fv(mVTXWs[mFaces[i].z].uv);
  865. glVertex3fv(mVertexTransformCache+index[2]);
  866. }
  867. else
  868. {
  869. ((i % 2) ?
  870. glColor3f(0.0f, 0.0f, (((float)i)/(float)mNumFaces)*.5+.25) :
  871. glColor3f(0.0f, (((float)i)/(float)mNumFaces)*.5+.25, 0.0f));
  872. glVertex3fv(mVertexTransformCache+index[0]);
  873. glVertex3fv(mVertexTransformCache+index[1]);
  874. glVertex3fv(mVertexTransformCache+index[2]);
  875. }
  876. glEnd();
  877. }
  878. if (mFlags & fDebugFaceRender)
  879. {
  880. printf("Turning fDebugFaceRender off\n");
  881. mFlags ^= fDebugFaceRender;
  882. }
  883. }
  884. glPopMatrix();
  885. #endif
  886. }
  887. #ifdef INTERACTIVE_BONE_RENDER
  888. /* HACK */
  889. float gBoneRotHackX = 0;
  890. float gBoneRotHackY = 0;
  891. float gBoneRotHackZ = 0;
  892. unsigned int gBoneId = 0;
  893. #endif
  894. void PSKModelRenderer::renderBone(unsigned int id)
  895. {
  896. #ifdef USING_OPENGL
  897. unsigned int i;
  898. float pos[3] = {0, 0, 0};
  899. float posB[3] = {0, 0, 0};
  900. unsigned int f;
  901. #ifdef INTERACTIVE_BONE_RENDER
  902. /* Hack */
  903. glPushMatrix();
  904. if (gBoneId == id)
  905. {
  906. glRotatef(gBoneRotHackZ, 0, 0, 1);
  907. glRotatef(gBoneRotHackY, 0, 1, 0);
  908. glRotatef(gBoneRotHackX, 1, 0, 0);
  909. }
  910. #endif
  911. glPushMatrix();
  912. if (id == 0)
  913. {
  914. glPointSize(8.0);
  915. #ifdef FUDGE_BONE_RENDER
  916. glRotatef(180, 0, 0, 1);
  917. glPushMatrix();
  918. #endif
  919. }
  920. /* Handle PSA animation use */
  921. if (mAnimationFrame > 0 &&
  922. mAnimation && mAnimation->mBones == mBones)
  923. {
  924. f = id + (mAnimationFrame*mAnimation->mNumBones);
  925. posB[0] = -mAnimation->mKeyFrames[f].trans[0];
  926. posB[1] = -mAnimation->mKeyFrames[f].trans[1];
  927. posB[2] = -mAnimation->mKeyFrames[f].trans[2];
  928. glTranslatef(mAnimation->mKeyFrames[f].trans[0],
  929. mAnimation->mKeyFrames[f].trans[1],
  930. mAnimation->mKeyFrames[f].trans[2]);
  931. }
  932. else
  933. {
  934. posB[0] = -mBones[id].restLoc[0];
  935. posB[1] = -mBones[id].restLoc[1];
  936. posB[2] = -mBones[id].restLoc[2];
  937. glTranslatef(mBones[id].restLoc[0],
  938. mBones[id].restLoc[1],
  939. mBones[id].restLoc[2]);
  940. }
  941. /* Draw a visible 'bone',
  942. Gradient light (parent) to dark (child) */
  943. glLineWidth(3.5);
  944. glBegin(GL_LINES);
  945. glColor3f(0.33, 0.33, 0.33);
  946. glVertex3fv(pos);
  947. glColor3f(1.0, 1.0, 1.0);
  948. glVertex3fv(posB);
  949. glEnd();
  950. if (mFlags & fConvertEuler)
  951. {
  952. // euler angles
  953. glRotatef(mBoneRotationCache[id*4], 1, 0, 0);
  954. glRotatef(mBoneRotationCache[id*4+1], 0, 1, 0);
  955. glRotatef(mBoneRotationCache[id*4+2], 0, 0, 1);
  956. }
  957. else
  958. {
  959. // axis angles
  960. glRotatef(mBoneRotationCache[id*4],
  961. mBoneRotationCache[id*4+1],
  962. mBoneRotationCache[id*4+2],
  963. mBoneRotationCache[id*4+3]);
  964. }
  965. glLineWidth(1.5);
  966. /* Draw axis list to show bone orientation */
  967. glBegin(GL_LINES);
  968. /* X axis */
  969. glColor3f(1.0f, 0.0f, 0.0f);
  970. glVertex3f(-8.0f, 0.0f, 0.0f);
  971. glVertex3f(8.0f, 0.0f, 0.0f);
  972. /* X direction */
  973. glVertex3f(8.0f, 0.0f, 0.0f);
  974. glVertex3f(7.0f, 1.0f, 0.0f);
  975. glVertex3f(8.0f, 0.0f, 0.0f);
  976. glVertex3f(7.0f, -1.0f, 0.0f);
  977. /* Y axis */
  978. glColor3f(0.0f, 1.0f, 0.0f);
  979. glVertex3f(0.0f, -8.0f, 0.0f);
  980. glVertex3f(0.0f, 8.0f, 0.0f);
  981. /* Y direction */
  982. glVertex3f(0.0f, 8.0f, 0.0f);
  983. glVertex3f(0.0f, 7.0f, 1.0f);
  984. glVertex3f(0.0f, 8.0f, 0.0f);
  985. glVertex3f(0.0f, 7.0f, -1.0f);
  986. /* Z axis */
  987. glColor3f(0.0f, 0.0f, 1.0f);
  988. glVertex3f(0.0f, 0.0f, -8.0f);
  989. glVertex3f(0.0f, 0.0f, 8.0f);
  990. /* Z direction */
  991. glVertex3f(0.0f, 0.0f, 8.0f);
  992. glVertex3f(0.0f, 1.0f, 7.0f);
  993. glVertex3f(0.0f, 0.0f, 8.0f);
  994. glVertex3f(0.0f, -1.0f, 7.0f);
  995. glEnd();
  996. /* Draw childless bone pivots darker */
  997. if (mBones[id].numChildren > 0)
  998. {
  999. glColor3f(1.0, 1.0, 1.0);
  1000. }
  1001. else
  1002. {
  1003. glColor3f(0.5, 0.5, 0.5);
  1004. }
  1005. glBegin(GL_POINTS);
  1006. glVertex3fv(pos);
  1007. glEnd();
  1008. if (mBones[id].numChildren > 0)
  1009. {
  1010. for (i = 0; i < mNumBones; ++i)
  1011. {
  1012. if (i == mBones[i].parentIndex)
  1013. continue;
  1014. if (mBones[i].parentIndex == id)
  1015. {
  1016. renderBone(i);
  1017. }
  1018. }
  1019. }
  1020. glPopMatrix();
  1021. #ifdef INTERACTIVE_BONE_RENDER
  1022. glPopMatrix();
  1023. #endif
  1024. if (id == 0)
  1025. {
  1026. #ifdef FUDGE_BONE_RENDER
  1027. glPopMatrix();
  1028. #endif
  1029. glColor3f(1.0, 1.0, 1.0);
  1030. glLineWidth(1.0);
  1031. glPointSize(1.0);
  1032. }
  1033. #endif
  1034. }
  1035. ////////////////////////////////////////////////////////////
  1036. // Public Mutators
  1037. ////////////////////////////////////////////////////////////
  1038. void PSKModelRenderer::convertBoneAngles()
  1039. {
  1040. unsigned int i;
  1041. if (!mBoneRotationCache || !mModel)
  1042. {
  1043. printf("convertBoneAngles()> Error: No PSKModel loaded?\n");
  1044. return;
  1045. }
  1046. /* Use PSK bones */
  1047. mBones = mModel->mBones;
  1048. printf("Converting quaternions to [%s]\n",
  1049. (mFlags & fConvertEuler) ? "Eular angles" : "Axis angles");
  1050. for (i = 0; i < mNumBones; ++i)
  1051. {
  1052. if (mFlags & fConvertEuler)
  1053. {
  1054. // eular angles
  1055. quaternion_to_euler_angles(mBones[i].restDir[0],
  1056. mBones[i].restDir[1],
  1057. mBones[i].restDir[2],
  1058. mBones[i].restDir[3],
  1059. &mBoneRotationCache[i*4], // x
  1060. &mBoneRotationCache[i*4+1], // y
  1061. &mBoneRotationCache[i*4+2]); // z
  1062. // Convert radians to degrees
  1063. mBoneRotationCache[i*4] *= 57.295779513082323;
  1064. mBoneRotationCache[i*4+1] *= 57.295779513082323;
  1065. mBoneRotationCache[i*4+2] *= 57.295779513082323;
  1066. }
  1067. else
  1068. {
  1069. // axis angles
  1070. quaternion_to_axis_angles(mBones[i].restDir[3], // qw is dir[3]?
  1071. mBones[i].restDir[0], // qx
  1072. mBones[i].restDir[1], // qy
  1073. mBones[i].restDir[2], // qz
  1074. &mBoneRotationCache[i*4], // theta
  1075. &mBoneRotationCache[i*4+1], // x
  1076. &mBoneRotationCache[i*4+2], // y
  1077. &mBoneRotationCache[i*4+3]); // z
  1078. // Convert radians to degrees, negated to account for UT coords
  1079. mBoneRotationCache[i*4] *= -57.295779513082323;
  1080. }
  1081. }
  1082. }
  1083. void PSKModelRenderer::convertBoneAnglesPSA(unsigned int frame)
  1084. {
  1085. unsigned int i, b, start, end;
  1086. float w, x, y, z, w2, x2, y2, z2;
  1087. float qw, qx, qy, qz;
  1088. if (!mBoneRotationCache || !mAnimation)
  1089. {
  1090. printf("convertBoneAnglesPSA()> Error: No PSA Animation loaded?\n");
  1091. return;
  1092. }
  1093. /* Use PSA bones */
  1094. mBones = mAnimation->mBones;
  1095. /* Check here for frame out of bounds */
  1096. if (frame > mAnimation->mNumKeyFrames / mAnimation->mNumBones)
  1097. {
  1098. printf("convertBoneAnglesPSA()> Error: Frame out of bounds\n");
  1099. return;
  1100. }
  1101. printf("Converting quaternions to [%s] for PSA frame [%u]\n",
  1102. (mFlags & fConvertEuler) ? "Eular angles" : "Axis angles", frame);
  1103. /* Update frame tracker */
  1104. mAnimationFrame = frame;
  1105. /* Compute start and end bones from list */
  1106. start = (mAnimationFrame*mAnimation->mNumBones);
  1107. end = start + mAnimation->mNumBones;
  1108. for (i = start, b = 0; i < end; ++i, ++b)
  1109. {
  1110. w = mBones[b].restDir[3];
  1111. x = mBones[b].restDir[0];
  1112. y = mBones[b].restDir[1];
  1113. z = mBones[b].restDir[2];
  1114. if (mAnimationFrame > 0)
  1115. {
  1116. w2 = mAnimation->mKeyFrames[i].dir[3];
  1117. x2 = mAnimation->mKeyFrames[i].dir[0];
  1118. y2 = mAnimation->mKeyFrames[i].dir[1];
  1119. z2 = mAnimation->mKeyFrames[i].dir[2];
  1120. //qw = w * w2 - x * x2 - y * y2 - z * z2;
  1121. //qx = w * x2 + x * w2 + y * z2 - z * y2;
  1122. //qy = w * y2 + y * w2 + z * x2 - x * z2;
  1123. //qz = w * z2 + z * w2 + x * y2 - y * x2;
  1124. qw = w2; qx = x2; qy = y2; qz = z2;
  1125. }
  1126. else
  1127. {
  1128. qw = w; qx = x; qy = y; qz = z;
  1129. }
  1130. if (mFlags & fConvertEuler)
  1131. {
  1132. /* FIXME: No support for frames */
  1133. // eular angles
  1134. quaternion_to_euler_angles(qw, qx, qy, qz,
  1135. &mBoneRotationCache[b*4], // x
  1136. &mBoneRotationCache[b*4+1], // y
  1137. &mBoneRotationCache[b*4+2]); // z
  1138. // Convert radians to degrees
  1139. mBoneRotationCache[b*4] *= 57.295779513082323;
  1140. mBoneRotationCache[b*4+1] *= 57.295779513082323;
  1141. mBoneRotationCache[b*4+2] *= 57.295779513082323;
  1142. }
  1143. else
  1144. {
  1145. // axis angles
  1146. quaternion_to_axis_angles(qw, qx, qy, qz,
  1147. &mBoneRotationCache[b*4], // theta
  1148. &mBoneRotationCache[b*4+1], // x
  1149. &mBoneRotationCache[b*4+2], // y
  1150. &mBoneRotationCache[b*4+3]); // z
  1151. // Convert radians to degrees, negated to account for UT coords
  1152. mBoneRotationCache[b*4] *= -57.295779513082323;
  1153. }
  1154. }
  1155. /* Setup matrices for mesh deformation use */
  1156. setupWorldMatrices(frame);
  1157. /* Reset vertices and then deform them */
  1158. copyVertices();
  1159. transformVertices(); // FIXME: pass frame time later
  1160. }
  1161. void PSKModelRenderer::copyVertices()
  1162. {
  1163. unsigned int i;
  1164. if (!mVertexTransformCache || !mModel)
  1165. {
  1166. printf("copyVertices()> Error: No PSKModel loaded?\n");
  1167. return;
  1168. }
  1169. for (i = 0; i < mModel->mNumVertices; ++i)
  1170. {
  1171. mVertexTransformCache[i*3] = mModel->mVertices[i*3]; // 0
  1172. mVertexTransformCache[i*3+1] = mModel->mVertices[i*3+1]; // 2
  1173. mVertexTransformCache[i*3+2] = mModel->mVertices[i*3+2]; // 1
  1174. }
  1175. }
  1176. void PSKModelRenderer::setAnimation(PSAAnimation *anim)
  1177. {
  1178. if (!anim || !mModel || anim->mNumBones != mModel->mNumBones)
  1179. {
  1180. printf("Error: Unable to load PSA Animation, [%s]\n",
  1181. (!mModel) ? "No PSK model loaded" :
  1182. (!anim) ? "Null PSA given" :
  1183. "PSA animation doesn't match loaded PSK model");
  1184. return;
  1185. }
  1186. mAnimation = anim;
  1187. /* Bones are forced to same size to match model now,
  1188. and there is no need to default to PSA skeletion now*/
  1189. /* Wow, now I look smart able to switch out bones on the fly */
  1190. //mBones = anim->mBones;
  1191. //
  1192. /* Setup bones */
  1193. //if (mBoneRotationCache)
  1194. // delete [] mBoneRotationCache;
  1195. //
  1196. //mBoneRotationCache = new float[anim->mNumBones*4];
  1197. /* No use loading PSA frame 0 anymore by default */
  1198. //convertBoneAnglesPSA(0);
  1199. }
  1200. void PSKModelRenderer::setModel(PSKModel *model)
  1201. {
  1202. unsigned int i;
  1203. if (!model)
  1204. {
  1205. return;
  1206. }
  1207. mModel = model;
  1208. /* Setup cheesy unneeded attributes to simulate inhertence */
  1209. mNumFrames = model->mNumFrames;
  1210. mNumVertices = model->mNumVertices;
  1211. mNumFaces = model->mNumFaces;
  1212. mNumVTXWs = model->mNumVTXWs;
  1213. mNumMaterials = model->mNumMaterials;
  1214. mNumBones = model->mNumBones;
  1215. mNumWeights = model->mNumWeights;
  1216. mVTXWs = model->mVTXWs;
  1217. mFaces = model->mFaces;
  1218. mMaterials = model->mMaterials;
  1219. mBones = model->mBones;
  1220. mWeights = model->mWeights;
  1221. /* Setup vertices */
  1222. if (mVertexTransformCache)
  1223. delete [] mVertexTransformCache;
  1224. mVertexTransformCache = new float[model->mNumVertices*3];
  1225. copyVertices();
  1226. /* Setup bones */
  1227. if (mBoneRotationCache)
  1228. delete [] mBoneRotationCache;
  1229. mBoneRotationCache = new float[model->mNumBones*4];
  1230. convertBoneAngles();
  1231. /* Setup rest / transform matrices */
  1232. mNumMatrices = model->mNumBones;
  1233. mInvertedMatrices = new float*[mNumMatrices];
  1234. for (i = 0; i < mNumMatrices; ++i)
  1235. mInvertedMatrices[i] = new float[16];
  1236. mWorldMatrices = new float*[mNumMatrices];
  1237. for (i = 0; i < mNumMatrices; ++i)
  1238. mWorldMatrices[i] = new float[16];
  1239. /* Setup matrices for mesh deformation use */
  1240. setupRestMatrices(0);
  1241. }
  1242. void PSKModelRenderer::setupRestMatrices(unsigned int id)
  1243. {
  1244. #ifdef USING_OPENGL
  1245. unsigned int i;
  1246. float theta, x, y, z;
  1247. float m[16];
  1248. if (!mModel || !mModel->mBones)
  1249. return;
  1250. glPushMatrix();
  1251. glTranslatef(mModel->mBones[id].restLoc[0],
  1252. mModel->mBones[id].restLoc[1],
  1253. mModel->mBones[id].restLoc[2]);
  1254. /* Get the rotation in axis angles */
  1255. quaternion_to_axis_angles(mModel->mBones[id].restDir[0], // qw
  1256. mModel->mBones[id].restDir[1], // qx
  1257. mModel->mBones[id].restDir[2], // qy
  1258. mModel->mBones[id].restDir[3], // qz
  1259. &theta, &x, &y, &z);
  1260. /* Convert radians to degrees */
  1261. theta *= -57.295779513082323;
  1262. /* OpenGL axis angles rotation */
  1263. glRotatef(theta, x, y, z);
  1264. /* Store this bones tranform matrix inverted to
  1265. get bone rest position of model vertices */
  1266. glGetFloatv(GL_MODELVIEW_MATRIX, m);
  1267. invert_matrix(m, mInvertedMatrices[id]);
  1268. if (mBones[id].numChildren > 0)
  1269. {
  1270. for (i = 0; i < mNumBones; ++i)
  1271. {
  1272. if (i == mBones[i].parentIndex)
  1273. continue;
  1274. if (mBones[i].parentIndex == id)
  1275. {
  1276. setupRestMatrices(i);
  1277. }
  1278. }
  1279. }
  1280. glPopMatrix();
  1281. #endif
  1282. }
  1283. /* Currently doesn't do what is says it does heh */
  1284. void PSKModelRenderer::setupWorldMatrices(unsigned int id)
  1285. {
  1286. #ifdef USING_OPENGL
  1287. unsigned int i, f;
  1288. glPushMatrix();
  1289. /* Handle PSA animation use */
  1290. if (mAnimationFrame > 0 &&
  1291. mAnimation && mAnimation->mBones == mBones)
  1292. {
  1293. f = id + (mAnimationFrame*mAnimation->mNumBones);
  1294. glTranslatef(mAnimation->mKeyFrames[f].trans[0],
  1295. mAnimation->mKeyFrames[f].trans[1],
  1296. mAnimation->mKeyFrames[f].trans[2]);
  1297. }
  1298. else
  1299. {
  1300. glTranslatef(mBones[id].restLoc[0],
  1301. mBones[id].restLoc[1],
  1302. mBones[id].restLoc[2]);
  1303. }
  1304. if (mFlags & fConvertEuler)
  1305. {
  1306. // euler angles
  1307. glRotatef(mBoneRotationCache[id*4], 1, 0, 0);
  1308. glRotatef(mBoneRotationCache[id*4+1], 0, 1, 0);
  1309. glRotatef(mBoneRotationCache[id*4+2], 0, 0, 1);
  1310. }
  1311. else
  1312. {
  1313. // axis angles
  1314. glRotatef(mBoneRotationCache[id*4],
  1315. mBoneRotationCache[id*4+1],
  1316. mBoneRotationCache[id*4+2],
  1317. mBoneRotationCache[id*4+3]);
  1318. }
  1319. /* Store world transform matrix */
  1320. glGetFloatv(GL_MODELVIEW_MATRIX, mWorldMatrices[id]);
  1321. if (mBones[id].numChildren > 0)
  1322. {
  1323. for (i = 0; i < mNumBones; ++i)
  1324. {
  1325. if (i == mBones[i].parentIndex)
  1326. continue;
  1327. if (mBones[i].parentIndex == id)
  1328. {
  1329. setupWorldMatrices(i);
  1330. }
  1331. }
  1332. }
  1333. glPopMatrix();
  1334. #elif FIXME_SOON
  1335. unsigned int i, j;
  1336. float x, y, z;
  1337. float m[16], m2[16], m3[16];
  1338. /* Currently inline transforms all vertices to produce
  1339. one deformed mesh keyframe */
  1340. if (id == 0)
  1341. copyVertices();
  1342. /* Rest skeleton matrices */
  1343. m[ 0]=1; m[ 1]=0; m[ 2]=0; m[ 3]=0;
  1344. m[ 4]=0; m[ 5]=1; m[ 6]=0; m[ 7]=0;
  1345. m[ 8]=0; m[ 9]=0; m[10]=1; m[11]=0;
  1346. m[12]=mModel->mBones[id].restLoc[0]; m[13]=mModel->mBones[id].restLoc[1]; m[14]=mModel->mBones[id].restLoc[2]; m[15]=1;
  1347. quaternion_to_matrix(mModel->mBones[id].restDir[3], // qw
  1348. mModel->mBones[id].restDir[0], // qx
  1349. mModel->mBones[id].restDir[1], // qy
  1350. mModel->mBones[id].restDir[2], // qz
  1351. m2);
  1352. multiply_matrix(m, m2, m3);
  1353. if (id == 0)
  1354. {
  1355. invert_matrix(m3, mInvertedMatrices[id]);
  1356. }
  1357. else
  1358. {
  1359. invert_matrix(m3, m2);
  1360. multiply_matrix(mInvertedMatrices[mModel->mBones[id].parent],
  1361. m2, mInvertedMatrices[id]);
  1362. }
  1363. /* Transformed skeleton matrices */
  1364. m[ 0]=1; m[ 1]=0; m[ 2]=0; m[ 3]=0;
  1365. m[ 4]=0; m[ 5]=1; m[ 6]=0; m[ 7]=0;
  1366. m[ 8]=0; m[ 9]=0; m[10]=1; m[11]=0;
  1367. m[12]=mAnimation->mBones[id].restLoc[0]; m[13]=mAnimation->mBones[id].restLoc[1]; m[14]=mAnimation->mBones[id].restLoc[2]; m[15]=1;
  1368. quaternion_to_matrix(mAnimation->mBones[id].restDir[3], // qw
  1369. mAnimation->mBones[id].restDir[0], // qx
  1370. mAnimation->mBones[id].restDir[1], // qy
  1371. mAnimation->mBones[id].restDir[2], // qz
  1372. m2);
  1373. multiply_matrix(m, m2, m3);
  1374. /* Transform vertices */
  1375. //multiply_matrix(mInvertedMatrices[id], m3, m);
  1376. if (id == 0)
  1377. {
  1378. multiply_matrix(mInvertedMatrices[id], m3, m);
  1379. }
  1380. else
  1381. {
  1382. multiply_matrix(mInvertedMatrices[id], m3, m2);
  1383. multiply_matrix(mInvertedMatrices[mModel->mBones[id].parent],
  1384. m2, m);
  1385. }
  1386. //copy_matrix(m, mInvertedMatrices[id]);
  1387. for (i = 0; i < mModel->mNumWeights; ++i)
  1388. {
  1389. if (mModel->mWeights[i].boneIndex == id)
  1390. {
  1391. j = mModel->mWeights[i].vertexIndex;
  1392. x = mVertexTransformCache[j*3+0] * mModel->mWeights[i].weight;
  1393. y = mVertexTransformCache[j*3+1] * mModel->mWeights[i].weight;
  1394. z = mVertexTransformCache[j*3+2] * mModel->mWeights[i].weight;
  1395. mVertexTransformCache[j*3+0] = m[0]*x + m[1]*y + m[ 2]*z + m[ 3];
  1396. mVertexTransformCache[j*3+1] = m[4]*x + m[5]*y + m[ 6]*z + m[ 7];
  1397. mVertexTransformCache[j*3+2] = m[8]*x + m[9]*y + m[10]*z + m[11];
  1398. }
  1399. }
  1400. /* Transform by rest of model */
  1401. for (i = 0; i < mAnimation->mNumBones; ++i)
  1402. {
  1403. if (i == mAnimation->mBones[i].parent)
  1404. continue;
  1405. if (mAnimation->mBones[i].parent == id)
  1406. {
  1407. setupMatrixTransforms(i);
  1408. }
  1409. }
  1410. #endif
  1411. }
  1412. void PSKModelRenderer::transformVertices()
  1413. {
  1414. unsigned int id, i, j;
  1415. float x, y, z;
  1416. float m[16];
  1417. for (id = 0; id < mModel->mNumBones; ++id)
  1418. {
  1419. multiply_matrix(mWorldMatrices[id], mInvertedMatrices[id], m);
  1420. //copy_matrix(mWorldMatrices[id], m);
  1421. for (i = 0; i < mModel->mNumWeights; ++i)
  1422. {
  1423. if (mModel->mWeights[i].boneIndex == id)
  1424. {
  1425. j = mModel->mWeights[i].vertexIndex;
  1426. x = mVertexTransformCache[j*3+0] * mModel->mWeights[i].weight;
  1427. y = mVertexTransformCache[j*3+1] * mModel->mWeights[i].weight;
  1428. z = mVertexTransformCache[j*3+2] * mModel->mWeights[i].weight;
  1429. mVertexTransformCache[j*3+0] = m[0]*x + m[4]*y + m[ 8]*z + m[12];
  1430. mVertexTransformCache[j*3+1] = m[1]*x + m[5]*y + m[ 9]*z + m[13];
  1431. mVertexTransformCache[j*3+2] = m[2]*x + m[6]*y + m[10]*z + m[14];
  1432. }
  1433. }
  1434. }
  1435. }
  1436. ////////////////////////////////////////////////////////////
  1437. // Unit Test code
  1438. ////////////////////////////////////////////////////////////
  1439. #ifdef UNIT_TEST_PSKMODEL
  1440. PSKModel gModel;
  1441. PSAAnimation gAnim;
  1442. #ifdef USING_OPENGL
  1443. PSKModelRenderer gModelRenderer;
  1444. # include <SDL/SDL.h>
  1445. # include <Texture.h>
  1446. Texture gTexture;
  1447. char gTitle[128];
  1448. char gMessage[256];
  1449. float gYaw = 0.0f;
  1450. bool gYawOn = true;
  1451. float gPitch = -20.0f;
  1452. float gScale = 1.75f;
  1453. float gLightPos[] = { 0.0, 512.0, 1024.0, 0.0 };
  1454. unsigned int gWidth = 640, gHeight = 460;
  1455. bool gWireframe = false;
  1456. unsigned int getTicks();
  1457. void event_resize(unsigned int width, unsigned int height);
  1458. void renderScene()
  1459. {
  1460. static float lastTime = 0.0f;
  1461. const float size = 500.0f, step = 50.0f;
  1462. float x, y, time;
  1463. gluLookAt(0.0, 0.0, -256.0,
  1464. 0.0, 8.0, 0.0,
  1465. 0.0, 1.0, 0.0);
  1466. glDisable(GL_TEXTURE_2D);
  1467. time = getTicks() * 0.1f;
  1468. if (time - lastTime > 5.0f)
  1469. {
  1470. lastTime = time;
  1471. if (gYawOn)
  1472. ++gYaw;
  1473. }
  1474. #ifdef DRAW_ACTUAL_ORIGIN_AXIS_WITH_ROTATION
  1475. glPushMatrix();
  1476. glRotatef(yaw, 0, 1, 0);
  1477. glBegin(GL_LINES);
  1478. glColor3f(1.0f, 0.0f, 0.0f);
  1479. glVertex3f(0.0f, 0.0f, 0.0f);
  1480. glVertex3f(10.0f, 0.0f, 0.0f);
  1481. glColor3f(0.0f, 1.0f, 0.0f);
  1482. glVertex3f(0.0f, 0.0f, 0.0f);
  1483. glVertex3f(0.0f, 10.0f, 0.0f);
  1484. glColor3f(0.0f, 0.0f, 1.0f);
  1485. glVertex3f(0.0f, 0.0f, 0.0f);
  1486. glVertex3f(0.0f, 0.0f, 10.0f);
  1487. glEnd();
  1488. glPopMatrix();
  1489. #endif
  1490. // Draw light symbol
  1491. glPushMatrix();
  1492. glTranslatef(gLightPos[0], gLightPos[1], gLightPos[2]);
  1493. glBegin(GL_LINES);
  1494. glColor3f(1.0f, 1.0f, 1.0f);
  1495. glVertex3f(0.0f, 0.0f, 0.0f);
  1496. glVertex3f(10.0f, 0.0f, 0.0f);
  1497. glVertex3f(0.0f, 0.0f, 0.0f);
  1498. glVertex3f(0.0f, 10.0f, 0.0f);
  1499. glVertex3f(0.0f, 0.0f, 0.0f);
  1500. glVertex3f(0.0f, 0.0f, 10.0f);
  1501. glEnd();
  1502. glPopMatrix();
  1503. glTranslatef(0.0f, -128.0f, 512.0f);
  1504. glRotatef(gPitch, 1, 0, 0);
  1505. glRotatef(gYaw, 0, 1, 0);
  1506. // Draw transformed origin axis
  1507. const float axisLength = 30.0f;
  1508. const float axisLength2 = 26.25f;
  1509. const float axisTip = 3.75f;
  1510. glBegin(GL_LINES);
  1511. /* X axis */
  1512. glColor3f(1.0f, 0.0f, 0.0f);
  1513. glVertex3f(-axisLength, 0.0f, 0.0f);
  1514. glVertex3f(axisLength, 0.0f, 0.0f);
  1515. /* X direction */
  1516. glVertex3f(axisLength, 0.0f, 0.0f);
  1517. glVertex3f(axisLength2, axisTip, 0.0f);
  1518. glVertex3f(axisLength, 0.0f, 0.0f);
  1519. glVertex3f(axisLength2, -axisTip, 0.0f);
  1520. /* Y axis */
  1521. glColor3f(0.0f, 1.0f, 0.0f);
  1522. glVertex3f(0.0f, -axisLength, 0.0f);
  1523. glVertex3f(0.0f, axisLength, 0.0f);
  1524. /* Y direction */
  1525. glVertex3f(0.0f, axisLength, 0.0f);
  1526. glVertex3f(0.0f, axisLength2, axisTip);
  1527. glVertex3f(0.0f, axisLength, 0.0f);
  1528. glVertex3f(0.0f, axisLength2, -axisTip);
  1529. /* Z axis */
  1530. glColor3f(0.0f, 0.0f, 1.0f);
  1531. glVertex3f(0.0f, 0.0f, -axisLength);
  1532. glVertex3f(0.0f, 0.0f, axisLength);
  1533. /* Z direction */
  1534. glVertex3f(0.0f, 0.0f, axisLength);
  1535. glVertex3f(0.0f, axisTip, axisLength2);
  1536. glVertex3f(0.0f, 0.0f, axisLength);
  1537. glVertex3f(0.0f, -axisTip, axisLength2);
  1538. glEnd();
  1539. // Draw grid
  1540. glPushMatrix();
  1541. glScalef(2.0f, 2.0f, 2.0f);
  1542. glColor3f(0.4f, 0.4f, 0.6f);
  1543. for (x = -size; x < size; x += step)
  1544. {
  1545. glBegin(GL_LINE_LOOP);
  1546. for (y = -size; y < size; y += step)
  1547. {
  1548. glVertex3f(x + step, 0.0f, y);
  1549. glVertex3f(x, 0.0f, y);
  1550. glVertex3f(x, 0.0f, y + step);
  1551. glVertex3f(x + step, 0.0f, y + step);
  1552. }
  1553. glEnd();
  1554. }
  1555. glPopMatrix();
  1556. // Draw model
  1557. glEnable(GL_TEXTURE_2D);
  1558. glColor3f(1.0f, 1.0f, 1.0f);
  1559. glPushMatrix();
  1560. glTranslatef(0.0f, 8.0f, 0.0f);
  1561. glScalef(gScale, gScale, gScale);
  1562. gModelRenderer.render();
  1563. glPopMatrix();
  1564. #ifdef HAVE_SDL_TTF
  1565. if (gWireframe)
  1566. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  1567. /* Render 2d text */
  1568. glEnterMode2d(gWidth, gHeight);
  1569. glColor3f(1.0, 1.0, 1.0);
  1570. glPrint2d(8, 8, 0.90, gMessage);
  1571. glExitMode2d();
  1572. if (gWireframe)
  1573. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  1574. #endif
  1575. }
  1576. void initScene(int argc, char *argv[])
  1577. {
  1578. //glEnable(GL_CULL_FACE);
  1579. //glCullFace(GL_FRONT); // Q3A uses front face culling
  1580. glEnable(GL_TEXTURE_2D);
  1581. glEnable(GL_DEPTH_TEST);
  1582. glDepthFunc(GL_LESS);
  1583. glShadeModel(GL_SMOOTH);
  1584. glDisable(GL_LIGHTING);
  1585. if (argc > 3)
  1586. {
  1587. //loadMd3(argv[1], argv[2], argv[3]);
  1588. }
  1589. else if (argc > 2)
  1590. {
  1591. //loadMd3(argv[1], argv[2], "machinegun");
  1592. }
  1593. else if (argc > 1)
  1594. {
  1595. //loadMd3(argv[1], "default", "machinegun");
  1596. }
  1597. else
  1598. {
  1599. printf("Usage:\n");
  1600. printf("%s ModelDir [Skin] [WeaponDir]\n", argv[0]);
  1601. exit(0);
  1602. }
  1603. // Texture setup
  1604. gTexture.reset();
  1605. gTexture.setFlag(Texture::fUseMipmaps);
  1606. gTexture.setMaxTextureCount(64);
  1607. printf("Loading textures: ");
  1608. gModelRenderer.mFlags |= PSKModelRenderer::fRenderTexture;
  1609. for (unsigned int i = 0; i < gModel.mNumMaterials; ++i)
  1610. {
  1611. char texture[256];
  1612. sprintf(texture, "%s/%s.tga", argv[3], gModel.mMaterials[i].name);
  1613. gModelRenderer.mTextures[i] = gTexture.loadTGA(texture);
  1614. }
  1615. printf(" done\n\n");
  1616. #ifdef HAVE_SDL_TTF
  1617. gTexture.loadFontTTF("data/test.ttf", 32, 126 - 32);
  1618. bufferedPrintf(gMessage, 256, "PSKModel Unit Test");
  1619. #endif
  1620. printf("\n");
  1621. printf("----------------------------------\n");
  1622. printf("ESC - Exit\n");
  1623. printf("F1 - Take screenshot\n");
  1624. printf("F10 - Set window to 640x460\n");
  1625. printf("F11 - Set window to 800x600\n");
  1626. printf("F12 - Set window to 1024x768\n");
  1627. printf("ALT+ENTER - Toogle fullscreen\n");
  1628. printf("UP/DOWN - Adjust scene pitch\n");
  1629. printf("RIGHT/LEFT - Adjust scene yaw\n");
  1630. printf("-/= - Adjust model scaling\n");
  1631. printf(";/' - Switch PSA frame (EXPERIMENTAL)\n");
  1632. printf("/ - Show PSK frame\n");
  1633. printf("----------------------------------\n");
  1634. printf("1 - Toggle polygon rendering\n");
  1635. printf("2 - Toggle polygon render debugging\n");
  1636. printf("3 - Toggle textured polygons\n");
  1637. printf("4 - Toggle points\n");
  1638. printf("7 - Toggle scene rotation\n");
  1639. printf("8 - Toggle alpha blending\n");
  1640. printf("8 - Toggle OpenGL Lighting\n");
  1641. printf("r - Reset mesh to default\n");
  1642. printf("w - Toggle wireframe rendering\n");
  1643. #ifdef INTERACTIVE_BONE_RENDER
  1644. printf("g,h - Select bone Id\n");
  1645. printf("z,x,c, b,n,m - Adjust bone rotatation\n");
  1646. #endif
  1647. printf("f - Convert rotation to angles to render correctly\n");
  1648. printf("i,k,j,l - Move light\n");
  1649. printf("[ - Loop through lower animations\n");
  1650. printf("] - Loop through upper animations\n");
  1651. printf("\\ - Loop through both animations\n");
  1652. printf("----------------------------------\n");
  1653. }
  1654. void handleKey(int key)
  1655. {
  1656. static bool alphaBlend = false;
  1657. static bool lighting = false;
  1658. switch (key)
  1659. {
  1660. case SDLK_F1:
  1661. gTexture.glScreenShot("PSKModel.test", gWidth, gHeight);
  1662. bufferedPrintf(gMessage, 256, "Took screenshot...");
  1663. break;
  1664. case SDLK_F10:
  1665. event_resize(640, 460);
  1666. bufferedPrintf(gMessage, 256, "Switching to VGA resolution");
  1667. break;
  1668. case SDLK_F11:
  1669. event_resize(800, 600);
  1670. bufferedPrintf(gMessage, 256, "Switching to SVGA resolution");
  1671. break;
  1672. case SDLK_F12:
  1673. event_resize(1024, 768);
  1674. bufferedPrintf(gMessage, 256, "Switching to XGA resolution");
  1675. break;
  1676. case SDLK_RIGHT:
  1677. ++gYaw;
  1678. break;
  1679. case SDLK_LEFT:
  1680. --gYaw;
  1681. break;
  1682. case SDLK_UP:
  1683. ++gPitch;
  1684. break;
  1685. case SDLK_DOWN:
  1686. --gPitch;
  1687. break;
  1688. case 'f':
  1689. printf("Converting bone angles...\n");
  1690. gModelRenderer.convertBoneAngles();
  1691. break;
  1692. #ifdef INTERACTIVE_BONE_RENDER
  1693. case 'g':
  1694. --gBoneId;
  1695. printf("gBoneId = %i\n", gBoneId);
  1696. bufferedPrintf(gMessage, 256, "gBoneId = %i\n", gBoneId);
  1697. break;
  1698. case 'h':
  1699. ++gBoneId;
  1700. printf("gBoneId = %i\n", gBoneId);
  1701. bufferedPrintf(gMessage, 256, "gBoneId = %i\n", gBoneId);
  1702. break;
  1703. case 'z':
  1704. --gBoneRotHackX;
  1705. printf("gBoneRotHackX = %f\n", gBoneRotHackX);
  1706. bufferedPrintf(gMessage, 256, "gBoneRotHackX = %f\n", gBoneRotHackX);
  1707. break;
  1708. case 'x':
  1709. --gBoneRotHackY;
  1710. printf("gBoneRotHackY = %f\n", gBoneRotHackY);
  1711. bufferedPrintf(gMessage, 256, "gBoneRotHackY = %f\n", gBoneRotHackY);
  1712. break;
  1713. case 'c':
  1714. --gBoneRotHackZ;
  1715. printf("gBoneRotHackZ = %f\n", gBoneRotHackZ);
  1716. bufferedPrintf(gMessage, 256, "gBoneRotHackZ = %f\n", gBoneRotHackZ);
  1717. break;
  1718. case 'b':
  1719. ++gBoneRotHackX;
  1720. printf("gBoneRotHackX = %f\n", gBoneRotHackX);
  1721. bufferedPrintf(gMessage, 256, "gBoneRotHackX = %f\n", gBoneRotHackX);
  1722. break;
  1723. case 'n':
  1724. ++gBoneRotHackY;
  1725. printf("gBoneRotHackY = %f\n", gBoneRotHackY);
  1726. bufferedPrintf(gMessage, 256, "gBoneRotHackY = %f\n", gBoneRotHackY);
  1727. break;
  1728. case 'm':
  1729. ++gBoneRotHackZ;
  1730. printf("gBoneRotHackZ = %f\n", gBoneRotHackZ);
  1731. bufferedPrintf(gMessage, 256, "gBoneRotHackZ = %f\n", gBoneRotHackZ);
  1732. break;
  1733. #endif
  1734. case '-':
  1735. if (gScale * 0.15 > 0.000001)
  1736. gScale *= 0.15f;
  1737. break;
  1738. case '=':
  1739. gScale *= 1.15f;
  1740. break;
  1741. case '0':
  1742. gModelRenderer.mFlags ^= PSKModelRenderer::fRenderBones;
  1743. break;
  1744. case '1':
  1745. gModelRenderer.mFlags ^= PSKModelRenderer::fRenderFaces;
  1746. break;
  1747. case '2':
  1748. gModelRenderer.mFlags ^= PSKModelRenderer::fDebugFaceRender;
  1749. break;
  1750. case '3':
  1751. gModelRenderer.mFlags ^= PSKModelRenderer::fRenderTexture;
  1752. if (gModelRenderer.mFlags & PSKModelRenderer::fRenderTexture)
  1753. glEnable(GL_TEXTURE_2D);
  1754. else
  1755. glDisable(GL_TEXTURE_2D);
  1756. break;
  1757. case '4':
  1758. gModelRenderer.mFlags ^= PSKModelRenderer::fRenderPoints;
  1759. break;
  1760. case '7':
  1761. gYawOn = !gYawOn;
  1762. break;
  1763. case '8':
  1764. alphaBlend = !alphaBlend;
  1765. if (alphaBlend)
  1766. {
  1767. glEnable(GL_BLEND);
  1768. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  1769. glAlphaFunc(GL_GREATER, 0);
  1770. }
  1771. else
  1772. {
  1773. glDisable(GL_BLEND);
  1774. }
  1775. printf("%sabled alpha blending\n", alphaBlend ? "En" : "Dis");
  1776. break;
  1777. case '9':
  1778. lighting = !lighting;
  1779. if (lighting)
  1780. {
  1781. static bool firsttime = true;
  1782. glEnable(GL_LIGHTING);
  1783. if (firsttime)
  1784. {
  1785. float specular[] = { 0.6, 0.6, 0.6, 1.0 };
  1786. float ambient[] = { 0.64, 0.64, 0.64, 1.0 };
  1787. float shine[] = { 50.0 };
  1788. // Caustic fx possible with mod amb?
  1789. glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
  1790. glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
  1791. //glMaterialfv(GL_FRONT, GL_DIFFUSE, ambient);
  1792. glMaterialfv(GL_FRONT, GL_SHININESS, shine);
  1793. glLightfv(GL_LIGHT0, GL_POSITION, gLightPos);
  1794. glEnable(GL_LIGHT0);
  1795. glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
  1796. //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
  1797. //glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, WHITE);
  1798. //glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, WHITE);
  1799. //glLightModelfv(GL_LIGHT_MODEL_AMBIENT, DIM_WHITE);
  1800. //glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, cutoff);
  1801. //glLightfv(GL_LIGHT0, GL_POSITION, pos);
  1802. //glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
  1803. //glLightfv(GL_LIGHT0, GL_DIFFUSE, color);
  1804. firsttime = false;
  1805. }
  1806. }
  1807. else
  1808. {
  1809. glDisable(GL_LIGHTING);
  1810. }
  1811. break;
  1812. case '/':
  1813. gModelRenderer.convertBoneAngles();
  1814. break;
  1815. case '\'':
  1816. gModelRenderer.convertBoneAnglesPSA(gModelRenderer.mAnimationFrame+1);
  1817. break;
  1818. case ';':
  1819. gModelRenderer.convertBoneAnglesPSA(gModelRenderer.mAnimationFrame-1);
  1820. break;
  1821. case 'r':
  1822. gModelRenderer.copyVertices();
  1823. break;
  1824. case 'i':
  1825. gLightPos[1] += 16.0f;
  1826. glLightfv(GL_LIGHT0, GL_POSITION, gLightPos);
  1827. break;
  1828. case 'k':
  1829. gLightPos[1] -= 16.0f;
  1830. glLightfv(GL_LIGHT0, GL_POSITION, gLightPos);
  1831. break;
  1832. case 'j':
  1833. gLightPos[0] -= 16.0f;
  1834. glLightfv(GL_LIGHT0, GL_POSITION, gLightPos);
  1835. break;
  1836. case 'l':
  1837. gLightPos[0] += 16.0f;
  1838. glLightfv(GL_LIGHT0, GL_POSITION, gLightPos);
  1839. break;
  1840. case 'w':
  1841. gWireframe = !gWireframe;
  1842. if (gWireframe)
  1843. {
  1844. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  1845. }
  1846. else
  1847. {
  1848. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  1849. }
  1850. printf("%sabled wireframe rendering\n", gWireframe ? "En" : "Dis");
  1851. break;
  1852. }
  1853. }
  1854. ///////////////////////////////////////////////////
  1855. SDL_Surface *gSDLWindow = NULL;
  1856. void updateWindowTitle(char *newTitle)
  1857. {
  1858. static char title[64];
  1859. memcpy(title, newTitle, 64);
  1860. title[63] = 0;
  1861. SDL_WM_SetCaption(title, "PSKModel.test");
  1862. }
  1863. unsigned int getTicks()
  1864. {
  1865. return SDL_GetTicks();
  1866. }
  1867. void swap_buffers()
  1868. {
  1869. SDL_GL_SwapBuffers();
  1870. }
  1871. void event_resize(unsigned int width, unsigned int height)
  1872. {
  1873. GLfloat aspect;
  1874. gWidth = width;
  1875. gHeight = height;
  1876. aspect = (GLfloat)width/(GLfloat)height;
  1877. glViewport(0, 0, width, height);
  1878. glMatrixMode(GL_PROJECTION);
  1879. glLoadIdentity();
  1880. gluPerspective(45.0f, aspect, 8.0f, 4600.0f);
  1881. glMatrixMode(GL_MODELVIEW);
  1882. glLoadIdentity();
  1883. // Resize window
  1884. gSDLWindow = SDL_SetVideoMode(width, height, 16, SDL_OPENGL);
  1885. }
  1886. void event_display(int width, int height)
  1887. {
  1888. glClearColor(0.3, 0.3, 0.5, 1.0);
  1889. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  1890. glLoadIdentity();
  1891. renderScene();
  1892. glFlush();
  1893. swap_buffers();
  1894. }
  1895. void shutdown_gl()
  1896. {
  1897. SDL_Quit();
  1898. }
  1899. void init_gl(unsigned int width, unsigned int height)
  1900. {
  1901. // Print driver support information
  1902. printf("\n\n\t## GL Driver Info ##\n");
  1903. printf("\tVendor : %s\n", glGetString(GL_VENDOR));
  1904. printf("\tRenderer : %s\n", glGetString(GL_RENDERER));
  1905. printf("\tVersion : %s\n", glGetString(GL_VERSION));
  1906. printf("\tExtensions : %s\n\n\n", (char*)glGetString(GL_EXTENSIONS));
  1907. // Setup GL
  1908. glClearColor(0.3, 0.3, 0.5, 1.0);
  1909. event_resize(width, height);
  1910. }
  1911. int main_gl(int argc, char *argv[])
  1912. {
  1913. SDL_Event event;
  1914. unsigned int mkeys, mod, key, flags;
  1915. unsigned int width = gWidth;
  1916. unsigned int height = gHeight;
  1917. bool fullscreen = false;
  1918. char *driver = 0x0;
  1919. // Setup clean up on exit
  1920. atexit(shutdown_gl);
  1921. // NOTE: Removed fullscreen/driver option parser args
  1922. // Create GL context
  1923. SDL_Init(SDL_INIT_VIDEO);
  1924. printf("\n@Created OpenGL Context...\n");
  1925. if (!driver || !driver[0] || SDL_GL_LoadLibrary(driver) < 0)
  1926. {
  1927. SDL_ClearError();
  1928. // Fallback 1
  1929. if (SDL_GL_LoadLibrary("libGL.so") < 0)
  1930. {
  1931. SDL_ClearError();
  1932. // Fallback 2
  1933. if (SDL_GL_LoadLibrary("libGL.so.1") < 0)
  1934. {
  1935. fprintf(stderr, "main_gl> SDL_GL_LoadLibrary failed!\n");
  1936. fprintf(stderr, "main_gl> Error is [%s].\n", SDL_GetError());
  1937. exit(1);
  1938. }
  1939. }
  1940. }
  1941. flags = SDL_OPENGL;
  1942. if (fullscreen)
  1943. {
  1944. flags |= SDL_FULLSCREEN;
  1945. SDL_ShowCursor(SDL_DISABLE);
  1946. }
  1947. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  1948. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
  1949. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  1950. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  1951. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  1952. gSDLWindow = SDL_SetVideoMode(width, height, 16, flags);
  1953. SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
  1954. // Init rendering
  1955. init_gl(width, height);
  1956. initScene(argc, argv);
  1957. printf("\n@Starting event loop...\n");
  1958. updateWindowTitle("Unreal Tournament 2003 Model test");
  1959. for (;;)
  1960. {
  1961. while (SDL_PollEvent(&event))
  1962. {
  1963. switch (event.type)
  1964. {
  1965. case SDL_QUIT:
  1966. exit(0);
  1967. break;
  1968. case SDL_MOUSEMOTION:
  1969. break;
  1970. case SDL_MOUSEBUTTONDOWN:
  1971. case SDL_MOUSEBUTTONUP:
  1972. break;
  1973. case SDL_KEYDOWN:
  1974. mkeys = (unsigned int)SDL_GetModState();
  1975. mod = 0;
  1976. if (mkeys & KMOD_LSHIFT)
  1977. mod |= KMOD_LSHIFT;
  1978. if (mkeys & KMOD_RSHIFT)
  1979. mod |= KMOD_RSHIFT;
  1980. if (mkeys & KMOD_LCTRL)
  1981. mod |= KMOD_LCTRL;
  1982. if (mkeys & KMOD_RCTRL)
  1983. mod |= KMOD_RCTRL;
  1984. if (mkeys & KMOD_LALT)
  1985. mod |= KMOD_LALT;
  1986. if (mkeys & KMOD_RALT)
  1987. mod |= KMOD_RALT;
  1988. key = event.key.keysym.sym;
  1989. switch (key)
  1990. {
  1991. case SDLK_ESCAPE: // 0x1B, 27d, ESC
  1992. exit(0);
  1993. break;
  1994. case SDLK_RETURN:
  1995. if (mod & KMOD_LALT)
  1996. {
  1997. SDL_ShowCursor(SDL_DISABLE);
  1998. SDL_WM_ToggleFullScreen(gSDLWindow);
  1999. }
  2000. }
  2001. handleKey(key);
  2002. break;
  2003. case SDL_KEYUP:
  2004. break;
  2005. case SDL_VIDEORESIZE:
  2006. event_resize(event.resize.w, event.resize.h);
  2007. width = event.resize.w;
  2008. height = event.resize.h;
  2009. event_display(width, height);
  2010. break;
  2011. }
  2012. }
  2013. event_display(width, height);
  2014. }
  2015. return 0;
  2016. }
  2017. #endif
  2018. int runPSKModelUnitTest(int argc, char *argv[])
  2019. {
  2020. bool gl = false;
  2021. if (argc > 1)
  2022. {
  2023. for (int i = 0; argv[1][i] != 0; ++i)
  2024. {
  2025. switch (argv[1][i])
  2026. {
  2027. case '.':
  2028. gAnim.mFlags = (PSAAnimation::fDebugBones |
  2029. PSAAnimation::fDebugAnimInfos |
  2030. PSAAnimation::fDebugKeyFrames);
  2031. gAnim.load(argv[2]);
  2032. break;
  2033. case 'p':
  2034. gAnim.mFlags = (PSAAnimation::fDebugBones |
  2035. PSAAnimation::fDebugAnimInfos |
  2036. PSAAnimation::fDebugKeyFrames);
  2037. break;
  2038. case 'g':
  2039. gl = true;
  2040. break;
  2041. case 'w':
  2042. gModel.mFlags |= PSKModel::fDebugWeightLoad;
  2043. break;
  2044. case 'b':
  2045. gModel.mFlags |= PSKModel::fDebugBoneLoad;
  2046. break;
  2047. case 'v':
  2048. gModel.mFlags |= PSKModel::fDebugPointLoad;
  2049. break;
  2050. case 'm':
  2051. gModel.mFlags |= PSKModel::fDebugMattLoad;
  2052. break;
  2053. case 'f':
  2054. gModel.mFlags |= PSKModel::fDebugFaceLoad;
  2055. break;
  2056. case 'u':
  2057. gModel.mFlags |= PSKModel::fDebugUVLoad;
  2058. break;
  2059. case 'h':
  2060. printf("PSKModel.test <options> file.psk [skindir] [file.psa]\n");
  2061. printf(" eg 'PSKModel.test gvmfu Callisto.psk'\n");
  2062. printf("\tl - Load model\n");
  2063. printf("\tg - Start GL visual debugger\n");
  2064. printf("\tv - Debug Vertex loader\n");
  2065. printf("\tm - Debug Material loader\n");
  2066. printf("\tf - Debug Face loader\n");
  2067. printf("\tw - Debug Weight loader\n");
  2068. printf("\tb - Debug Bone loader\n");
  2069. printf("\tu - Debug UV loader\n");
  2070. printf("\tp - Debug PSA loader\n");
  2071. printf("\t. - PSA debugger (PSKModel.test . file.psa)\n");
  2072. printf("\t0 - UMOD debugger (PSKModel.test 0 file.utx)\n");
  2073. printf("\th - Help\n");
  2074. break;
  2075. }
  2076. }
  2077. if (argc > 3)
  2078. {
  2079. /* Load PSK model */
  2080. gModel.load(argv[2]);
  2081. /* Load PSK model to renderer */
  2082. gModelRenderer.mFlags |= PSKModelRenderer::fRenderFaces;
  2083. gModelRenderer.setModel(&gModel);
  2084. if (argc > 4)
  2085. {
  2086. gAnim.load(argv[4]);
  2087. gModelRenderer.setAnimation(&gAnim);
  2088. }
  2089. if (gl)
  2090. {
  2091. #ifdef USING_OPENGL
  2092. main_gl(argc, argv);
  2093. #else
  2094. printf("Build not OpenGL enabled\n");
  2095. #endif
  2096. }
  2097. }
  2098. }
  2099. else
  2100. {
  2101. printf("'%s h' for help\n", argv[0]);
  2102. }
  2103. return 0;
  2104. }
  2105. int main(int argc, char *argv[])
  2106. {
  2107. printf("[PSKModel class test]\n");
  2108. runPSKModelUnitTest(argc, argv);
  2109. return 0;
  2110. }
  2111. #endif