Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PSKModel.cpp 60KB

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