Model_lwo.cpp 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "Model_lwo.h"
  23. /*
  24. ======================================================================
  25. Converted from lwobject sample prog from LW 6.5 SDK.
  26. ======================================================================
  27. */
  28. /*
  29. ======================================================================
  30. lwFreeClip()
  31. Free memory used by an lwClip.
  32. ====================================================================== */
  33. void lwFreeClip( lwClip *clip )
  34. {
  35. if ( clip ) {
  36. lwListFree( clip->ifilter, (void (__cdecl *)(void *))lwFreePlugin );
  37. lwListFree( clip->pfilter, (void (__cdecl *)(void *))lwFreePlugin );
  38. switch( clip->type ) {
  39. case ID_STIL: {
  40. if ( clip->source.still.name ) Mem_Free( clip->source.still.name );
  41. break;
  42. }
  43. case ID_ISEQ: {
  44. if ( clip->source.seq.suffix ) Mem_Free( clip->source.seq.suffix );
  45. if ( clip->source.seq.prefix ) Mem_Free( clip->source.seq.prefix );
  46. break;
  47. }
  48. case ID_ANIM: {
  49. if ( clip->source.anim.server ) Mem_Free( clip->source.anim.server );
  50. if ( clip->source.anim.name ) Mem_Free( clip->source.anim.name );
  51. break;
  52. }
  53. case ID_XREF: {
  54. if ( clip->source.xref.string ) Mem_Free( clip->source.xref.string );
  55. break;
  56. }
  57. case ID_STCC: {
  58. if ( clip->source.cycle.name ) Mem_Free( clip->source.cycle.name );
  59. break;
  60. }
  61. }
  62. Mem_Free( clip );
  63. }
  64. }
  65. /*
  66. ======================================================================
  67. lwGetClip()
  68. Read image references from a CLIP chunk in an LWO2 file.
  69. ====================================================================== */
  70. lwClip *lwGetClip( idFile *fp, int cksize )
  71. {
  72. lwClip *clip;
  73. lwPlugin *filt;
  74. unsigned int id;
  75. unsigned short sz;
  76. int pos, rlen;
  77. /* allocate the Clip structure */
  78. clip = (lwClip*)Mem_ClearedAlloc( sizeof( lwClip ) );
  79. if ( !clip ) goto Fail;
  80. clip->contrast.val = 1.0f;
  81. clip->brightness.val = 1.0f;
  82. clip->saturation.val = 1.0f;
  83. clip->gamma.val = 1.0f;
  84. /* remember where we started */
  85. set_flen( 0 );
  86. pos = fp->Tell();
  87. /* index */
  88. clip->index = getI4( fp );
  89. /* first subchunk header */
  90. clip->type = getU4( fp );
  91. sz = getU2( fp );
  92. if ( 0 > get_flen() ) goto Fail;
  93. sz += sz & 1;
  94. set_flen( 0 );
  95. switch ( clip->type ) {
  96. case ID_STIL:
  97. clip->source.still.name = getS0( fp );
  98. break;
  99. case ID_ISEQ:
  100. clip->source.seq.digits = getU1( fp );
  101. clip->source.seq.flags = getU1( fp );
  102. clip->source.seq.offset = getI2( fp );
  103. clip->source.seq.start = getI2( fp );
  104. clip->source.seq.end = getI2( fp );
  105. clip->source.seq.prefix = getS0( fp );
  106. clip->source.seq.suffix = getS0( fp );
  107. break;
  108. case ID_ANIM:
  109. clip->source.anim.name = getS0( fp );
  110. clip->source.anim.server = getS0( fp );
  111. rlen = get_flen();
  112. clip->source.anim.data = getbytes( fp, sz - rlen );
  113. break;
  114. case ID_XREF:
  115. clip->source.xref.index = getI4( fp );
  116. clip->source.xref.string = getS0( fp );
  117. break;
  118. case ID_STCC:
  119. clip->source.cycle.lo = getI2( fp );
  120. clip->source.cycle.hi = getI2( fp );
  121. clip->source.cycle.name = getS0( fp );
  122. break;
  123. default:
  124. break;
  125. }
  126. /* error while reading current subchunk? */
  127. rlen = get_flen();
  128. if ( rlen < 0 || rlen > sz ) goto Fail;
  129. /* skip unread parts of the current subchunk */
  130. if ( rlen < sz )
  131. fp->Seek( sz - rlen, FS_SEEK_CUR );
  132. /* end of the CLIP chunk? */
  133. rlen = fp->Tell() - pos;
  134. if ( cksize < rlen ) goto Fail;
  135. if ( cksize == rlen )
  136. return clip;
  137. /* process subchunks as they're encountered */
  138. id = getU4( fp );
  139. sz = getU2( fp );
  140. if ( 0 > get_flen() ) goto Fail;
  141. while ( 1 ) {
  142. sz += sz & 1;
  143. set_flen( 0 );
  144. switch ( id ) {
  145. case ID_TIME:
  146. clip->start_time = getF4( fp );
  147. clip->duration = getF4( fp );
  148. clip->frame_rate = getF4( fp );
  149. break;
  150. case ID_CONT:
  151. clip->contrast.val = getF4( fp );
  152. clip->contrast.eindex = getVX( fp );
  153. break;
  154. case ID_BRIT:
  155. clip->brightness.val = getF4( fp );
  156. clip->brightness.eindex = getVX( fp );
  157. break;
  158. case ID_SATR:
  159. clip->saturation.val = getF4( fp );
  160. clip->saturation.eindex = getVX( fp );
  161. break;
  162. case ID_HUE:
  163. clip->hue.val = getF4( fp );
  164. clip->hue.eindex = getVX( fp );
  165. break;
  166. case ID_GAMM:
  167. clip->gamma.val = getF4( fp );
  168. clip->gamma.eindex = getVX( fp );
  169. break;
  170. case ID_NEGA:
  171. clip->negative = getU2( fp );
  172. break;
  173. case ID_IFLT:
  174. case ID_PFLT:
  175. filt = (lwPlugin*)Mem_ClearedAlloc( sizeof( lwPlugin ) );
  176. if ( !filt ) goto Fail;
  177. filt->name = getS0( fp );
  178. filt->flags = getU2( fp );
  179. rlen = get_flen();
  180. filt->data = getbytes( fp, sz - rlen );
  181. if ( id == ID_IFLT ) {
  182. lwListAdd( (void**)&clip->ifilter, filt );
  183. clip->nifilters++;
  184. }
  185. else {
  186. lwListAdd( (void**)&clip->pfilter, filt );
  187. clip->npfilters++;
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. /* error while reading current subchunk? */
  194. rlen = get_flen();
  195. if ( rlen < 0 || rlen > sz ) goto Fail;
  196. /* skip unread parts of the current subchunk */
  197. if ( rlen < sz )
  198. fp->Seek( sz - rlen, FS_SEEK_CUR );
  199. /* end of the CLIP chunk? */
  200. rlen = fp->Tell() - pos;
  201. if ( cksize < rlen ) goto Fail;
  202. if ( cksize == rlen ) break;
  203. /* get the next chunk header */
  204. set_flen( 0 );
  205. id = getU4( fp );
  206. sz = getU2( fp );
  207. if ( 6 != get_flen() ) goto Fail;
  208. }
  209. return clip;
  210. Fail:
  211. lwFreeClip( clip );
  212. return NULL;
  213. }
  214. /*
  215. ======================================================================
  216. lwFindClip()
  217. Returns an lwClip pointer, given a clip index.
  218. ====================================================================== */
  219. lwClip *lwFindClip( lwClip *list, int index )
  220. {
  221. lwClip *clip;
  222. clip = list;
  223. while ( clip ) {
  224. if ( clip->index == index ) break;
  225. clip = clip->next;
  226. }
  227. return clip;
  228. }
  229. /*
  230. ======================================================================
  231. lwFreeEnvelope()
  232. Free the memory used by an lwEnvelope.
  233. ====================================================================== */
  234. void lwFree( void *ptr ) {
  235. Mem_Free( ptr );
  236. }
  237. void lwFreeEnvelope( lwEnvelope *env )
  238. {
  239. if ( env ) {
  240. if ( env->name ) Mem_Free( env->name );
  241. lwListFree( env->key, lwFree );
  242. lwListFree( env->cfilter, (void (__cdecl *)(void *))lwFreePlugin );
  243. Mem_Free( env );
  244. }
  245. }
  246. static int compare_keys( lwKey *k1, lwKey *k2 )
  247. {
  248. return k1->time > k2->time ? 1 : k1->time < k2->time ? -1 : 0;
  249. }
  250. /*
  251. ======================================================================
  252. lwGetEnvelope()
  253. Read an ENVL chunk from an LWO2 file.
  254. ====================================================================== */
  255. lwEnvelope *lwGetEnvelope( idFile *fp, int cksize )
  256. {
  257. lwEnvelope *env;
  258. lwKey *key;
  259. lwPlugin *plug;
  260. unsigned int id;
  261. unsigned short sz;
  262. float f[ 4 ];
  263. int i, nparams, pos, rlen;
  264. /* allocate the Envelope structure */
  265. env = (lwEnvelope*)Mem_ClearedAlloc( sizeof( lwEnvelope ) );
  266. if ( !env ) goto Fail;
  267. /* remember where we started */
  268. set_flen( 0 );
  269. pos = fp->Tell();
  270. /* index */
  271. env->index = getVX( fp );
  272. /* first subchunk header */
  273. id = getU4( fp );
  274. sz = getU2( fp );
  275. if ( 0 > get_flen() ) goto Fail;
  276. /* process subchunks as they're encountered */
  277. while ( 1 ) {
  278. sz += sz & 1;
  279. set_flen( 0 );
  280. switch ( id ) {
  281. case ID_TYPE:
  282. env->type = getU2( fp );
  283. break;
  284. case ID_NAME:
  285. env->name = getS0( fp );
  286. break;
  287. case ID_PRE:
  288. env->behavior[ 0 ] = getU2( fp );
  289. break;
  290. case ID_POST:
  291. env->behavior[ 1 ] = getU2( fp );
  292. break;
  293. case ID_KEY:
  294. key = (lwKey*)Mem_ClearedAlloc( sizeof( lwKey ) );
  295. if ( !key ) goto Fail;
  296. key->time = getF4( fp );
  297. key->value = getF4( fp );
  298. lwListInsert( (void**)&env->key, key, (int (__cdecl *)(void *,void *))compare_keys );
  299. env->nkeys++;
  300. break;
  301. case ID_SPAN:
  302. if ( !key ) goto Fail;
  303. key->shape = getU4( fp );
  304. nparams = ( sz - 4 ) / 4;
  305. if ( nparams > 4 ) nparams = 4;
  306. for ( i = 0; i < nparams; i++ )
  307. f[ i ] = getF4( fp );
  308. switch ( key->shape ) {
  309. case ID_TCB:
  310. key->tension = f[ 0 ];
  311. key->continuity = f[ 1 ];
  312. key->bias = f[ 2 ];
  313. break;
  314. case ID_BEZI:
  315. case ID_HERM:
  316. case ID_BEZ2:
  317. for ( i = 0; i < nparams; i++ )
  318. key->param[ i ] = f[ i ];
  319. break;
  320. }
  321. break;
  322. case ID_CHAN:
  323. plug = (lwPlugin*)Mem_ClearedAlloc( sizeof( lwPlugin ) );
  324. if ( !plug ) goto Fail;
  325. plug->name = getS0( fp );
  326. plug->flags = getU2( fp );
  327. plug->data = getbytes( fp, sz - get_flen() );
  328. lwListAdd( (void**)&env->cfilter, plug );
  329. env->ncfilters++;
  330. break;
  331. default:
  332. break;
  333. }
  334. /* error while reading current subchunk? */
  335. rlen = get_flen();
  336. if ( rlen < 0 || rlen > sz ) goto Fail;
  337. /* skip unread parts of the current subchunk */
  338. if ( rlen < sz )
  339. fp->Seek( sz - rlen, FS_SEEK_CUR );
  340. /* end of the ENVL chunk? */
  341. rlen = fp->Tell() - pos;
  342. if ( cksize < rlen ) goto Fail;
  343. if ( cksize == rlen ) break;
  344. /* get the next subchunk header */
  345. set_flen( 0 );
  346. id = getU4( fp );
  347. sz = getU2( fp );
  348. if ( 6 != get_flen() ) goto Fail;
  349. }
  350. return env;
  351. Fail:
  352. lwFreeEnvelope( env );
  353. return NULL;
  354. }
  355. /*
  356. ======================================================================
  357. lwFindEnvelope()
  358. Returns an lwEnvelope pointer, given an envelope index.
  359. ====================================================================== */
  360. lwEnvelope *lwFindEnvelope( lwEnvelope *list, int index )
  361. {
  362. lwEnvelope *env;
  363. env = list;
  364. while ( env ) {
  365. if ( env->index == index ) break;
  366. env = env->next;
  367. }
  368. return env;
  369. }
  370. /*
  371. ======================================================================
  372. range()
  373. Given the value v of a periodic function, returns the equivalent value
  374. v2 in the principal interval [lo, hi]. If i isn't NULL, it receives
  375. the number of wavelengths between v and v2.
  376. v2 = v - i * (hi - lo)
  377. For example, range( 3 pi, 0, 2 pi, i ) returns pi, with i = 1.
  378. ====================================================================== */
  379. static float range( float v, float lo, float hi, int *i )
  380. {
  381. float v2, r = hi - lo;
  382. if ( r == 0.0 ) {
  383. if ( i ) *i = 0;
  384. return lo;
  385. }
  386. v2 = lo + v - r * ( float ) floor(( double ) v / r );
  387. if ( i ) *i = -( int )(( v2 - v ) / r + ( v2 > v ? 0.5 : -0.5 ));
  388. return v2;
  389. }
  390. /*
  391. ======================================================================
  392. hermite()
  393. Calculate the Hermite coefficients.
  394. ====================================================================== */
  395. static void hermite( float t, float *h1, float *h2, float *h3, float *h4 )
  396. {
  397. float t2, t3;
  398. t2 = t * t;
  399. t3 = t * t2;
  400. *h2 = 3.0f * t2 - t3 - t3;
  401. *h1 = 1.0f - *h2;
  402. *h4 = t3 - t2;
  403. *h3 = *h4 - t2 + t;
  404. }
  405. /*
  406. ======================================================================
  407. bezier()
  408. Interpolate the value of a 1D Bezier curve.
  409. ====================================================================== */
  410. static float bezier( float x0, float x1, float x2, float x3, float t )
  411. {
  412. float a, b, c, t2, t3;
  413. t2 = t * t;
  414. t3 = t2 * t;
  415. c = 3.0f * ( x1 - x0 );
  416. b = 3.0f * ( x2 - x1 ) - c;
  417. a = x3 - x0 - c - b;
  418. return a * t3 + b * t2 + c * t + x0;
  419. }
  420. /*
  421. ======================================================================
  422. bez2_time()
  423. Find the t for which bezier() returns the input time. The handle
  424. endpoints of a BEZ2 curve represent the control points, and these have
  425. (time, value) coordinates, so time is used as both a coordinate and a
  426. parameter for this curve type.
  427. ====================================================================== */
  428. static float bez2_time( float x0, float x1, float x2, float x3, float time,
  429. float *t0, float *t1 )
  430. {
  431. float v, t;
  432. t = *t0 + ( *t1 - *t0 ) * 0.5f;
  433. v = bezier( x0, x1, x2, x3, t );
  434. if ( idMath::Fabs( time - v ) > .0001f ) {
  435. if ( v > time )
  436. *t1 = t;
  437. else
  438. *t0 = t;
  439. return bez2_time( x0, x1, x2, x3, time, t0, t1 );
  440. }
  441. else
  442. return t;
  443. }
  444. /*
  445. ======================================================================
  446. bez2()
  447. Interpolate the value of a BEZ2 curve.
  448. ====================================================================== */
  449. static float bez2( lwKey *key0, lwKey *key1, float time )
  450. {
  451. float x, y, t, t0 = 0.0f, t1 = 1.0f;
  452. if ( key0->shape == ID_BEZ2 )
  453. x = key0->time + key0->param[ 2 ];
  454. else
  455. x = key0->time + ( key1->time - key0->time ) / 3.0f;
  456. t = bez2_time( key0->time, x, key1->time + key1->param[ 0 ], key1->time,
  457. time, &t0, &t1 );
  458. if ( key0->shape == ID_BEZ2 )
  459. y = key0->value + key0->param[ 3 ];
  460. else
  461. y = key0->value + key0->param[ 1 ] / 3.0f;
  462. return bezier( key0->value, y, key1->param[ 1 ] + key1->value, key1->value, t );
  463. }
  464. /*
  465. ======================================================================
  466. outgoing()
  467. Return the outgoing tangent to the curve at key0. The value returned
  468. for the BEZ2 case is used when extrapolating a linear pre behavior and
  469. when interpolating a non-BEZ2 span.
  470. ====================================================================== */
  471. static float outgoing( lwKey *key0, lwKey *key1 )
  472. {
  473. float a, b, d, t, out;
  474. switch ( key0->shape )
  475. {
  476. case ID_TCB:
  477. a = ( 1.0f - key0->tension )
  478. * ( 1.0f + key0->continuity )
  479. * ( 1.0f + key0->bias );
  480. b = ( 1.0f - key0->tension )
  481. * ( 1.0f - key0->continuity )
  482. * ( 1.0f - key0->bias );
  483. d = key1->value - key0->value;
  484. if ( key0->prev ) {
  485. t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
  486. out = t * ( a * ( key0->value - key0->prev->value ) + b * d );
  487. }
  488. else
  489. out = b * d;
  490. break;
  491. case ID_LINE:
  492. d = key1->value - key0->value;
  493. if ( key0->prev ) {
  494. t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
  495. out = t * ( key0->value - key0->prev->value + d );
  496. }
  497. else
  498. out = d;
  499. break;
  500. case ID_BEZI:
  501. case ID_HERM:
  502. out = key0->param[ 1 ];
  503. if ( key0->prev )
  504. out *= ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
  505. break;
  506. case ID_BEZ2:
  507. out = key0->param[ 3 ] * ( key1->time - key0->time );
  508. if ( idMath::Fabs( key0->param[ 2 ] ) > 1e-5f )
  509. out /= key0->param[ 2 ];
  510. else
  511. out *= 1e5f;
  512. break;
  513. case ID_STEP:
  514. default:
  515. out = 0.0f;
  516. break;
  517. }
  518. return out;
  519. }
  520. /*
  521. ======================================================================
  522. incoming()
  523. Return the incoming tangent to the curve at key1. The value returned
  524. for the BEZ2 case is used when extrapolating a linear post behavior.
  525. ====================================================================== */
  526. static float incoming( lwKey *key0, lwKey *key1 )
  527. {
  528. float a, b, d, t, in;
  529. switch ( key1->shape )
  530. {
  531. case ID_LINE:
  532. d = key1->value - key0->value;
  533. if ( key1->next ) {
  534. t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
  535. in = t * ( key1->next->value - key1->value + d );
  536. }
  537. else
  538. in = d;
  539. break;
  540. case ID_TCB:
  541. a = ( 1.0f - key1->tension )
  542. * ( 1.0f - key1->continuity )
  543. * ( 1.0f + key1->bias );
  544. b = ( 1.0f - key1->tension )
  545. * ( 1.0f + key1->continuity )
  546. * ( 1.0f - key1->bias );
  547. d = key1->value - key0->value;
  548. if ( key1->next ) {
  549. t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
  550. in = t * ( b * ( key1->next->value - key1->value ) + a * d );
  551. }
  552. else
  553. in = a * d;
  554. break;
  555. case ID_BEZI:
  556. case ID_HERM:
  557. in = key1->param[ 0 ];
  558. if ( key1->next )
  559. in *= ( key1->time - key0->time ) / ( key1->next->time - key0->time );
  560. break;
  561. return in;
  562. case ID_BEZ2:
  563. in = key1->param[ 1 ] * ( key1->time - key0->time );
  564. if ( idMath::Fabs( key1->param[ 0 ] ) > 1e-5f )
  565. in /= key1->param[ 0 ];
  566. else
  567. in *= 1e5f;
  568. break;
  569. case ID_STEP:
  570. default:
  571. in = 0.0f;
  572. break;
  573. }
  574. return in;
  575. }
  576. /*
  577. ======================================================================
  578. evalEnvelope()
  579. Given a list of keys and a time, returns the interpolated value of the
  580. envelope at that time.
  581. ====================================================================== */
  582. float evalEnvelope( lwEnvelope *env, float time )
  583. {
  584. lwKey *key0, *key1, *skey, *ekey;
  585. float t, h1, h2, h3, h4, in, out, offset = 0.0f;
  586. int noff;
  587. /* if there's no key, the value is 0 */
  588. if ( env->nkeys == 0 ) return 0.0f;
  589. /* if there's only one key, the value is constant */
  590. if ( env->nkeys == 1 )
  591. return env->key->value;
  592. /* find the first and last keys */
  593. skey = ekey = env->key;
  594. while ( ekey->next ) ekey = ekey->next;
  595. /* use pre-behavior if time is before first key time */
  596. if ( time < skey->time ) {
  597. switch ( env->behavior[ 0 ] )
  598. {
  599. case BEH_RESET:
  600. return 0.0f;
  601. case BEH_CONSTANT:
  602. return skey->value;
  603. case BEH_REPEAT:
  604. time = range( time, skey->time, ekey->time, NULL );
  605. break;
  606. case BEH_OSCILLATE:
  607. time = range( time, skey->time, ekey->time, &noff );
  608. if ( noff % 2 )
  609. time = ekey->time - skey->time - time;
  610. break;
  611. case BEH_OFFSET:
  612. time = range( time, skey->time, ekey->time, &noff );
  613. offset = noff * ( ekey->value - skey->value );
  614. break;
  615. case BEH_LINEAR:
  616. out = outgoing( skey, skey->next )
  617. / ( skey->next->time - skey->time );
  618. return out * ( time - skey->time ) + skey->value;
  619. }
  620. }
  621. /* use post-behavior if time is after last key time */
  622. else if ( time > ekey->time ) {
  623. switch ( env->behavior[ 1 ] )
  624. {
  625. case BEH_RESET:
  626. return 0.0f;
  627. case BEH_CONSTANT:
  628. return ekey->value;
  629. case BEH_REPEAT:
  630. time = range( time, skey->time, ekey->time, NULL );
  631. break;
  632. case BEH_OSCILLATE:
  633. time = range( time, skey->time, ekey->time, &noff );
  634. if ( noff % 2 )
  635. time = ekey->time - skey->time - time;
  636. break;
  637. case BEH_OFFSET:
  638. time = range( time, skey->time, ekey->time, &noff );
  639. offset = noff * ( ekey->value - skey->value );
  640. break;
  641. case BEH_LINEAR:
  642. in = incoming( ekey->prev, ekey )
  643. / ( ekey->time - ekey->prev->time );
  644. return in * ( time - ekey->time ) + ekey->value;
  645. }
  646. }
  647. /* get the endpoints of the interval being evaluated */
  648. key0 = env->key;
  649. while ( time > key0->next->time )
  650. key0 = key0->next;
  651. key1 = key0->next;
  652. /* check for singularities first */
  653. if ( time == key0->time )
  654. return key0->value + offset;
  655. else if ( time == key1->time )
  656. return key1->value + offset;
  657. /* get interval length, time in [0, 1] */
  658. t = ( time - key0->time ) / ( key1->time - key0->time );
  659. /* interpolate */
  660. switch ( key1->shape )
  661. {
  662. case ID_TCB:
  663. case ID_BEZI:
  664. case ID_HERM:
  665. out = outgoing( key0, key1 );
  666. in = incoming( key0, key1 );
  667. hermite( t, &h1, &h2, &h3, &h4 );
  668. return h1 * key0->value + h2 * key1->value + h3 * out + h4 * in + offset;
  669. case ID_BEZ2:
  670. return bez2( key0, key1, time ) + offset;
  671. case ID_LINE:
  672. return key0->value + t * ( key1->value - key0->value ) + offset;
  673. case ID_STEP:
  674. return key0->value + offset;
  675. default:
  676. return offset;
  677. }
  678. }
  679. /*
  680. ======================================================================
  681. lwListFree()
  682. Free the items in a list.
  683. ====================================================================== */
  684. void lwListFree( void *list, void ( *freeNode )( void * ))
  685. {
  686. lwNode *node, *next;
  687. node = ( lwNode * ) list;
  688. while ( node ) {
  689. next = node->next;
  690. freeNode( node );
  691. node = next;
  692. }
  693. }
  694. /*
  695. ======================================================================
  696. lwListAdd()
  697. Append a node to a list.
  698. ====================================================================== */
  699. void lwListAdd( void **list, void *node )
  700. {
  701. lwNode *head, *tail;
  702. head = *(( lwNode ** ) list );
  703. if ( !head ) {
  704. *list = node;
  705. return;
  706. }
  707. while ( head ) {
  708. tail = head;
  709. head = head->next;
  710. }
  711. tail->next = ( lwNode * ) node;
  712. (( lwNode * ) node )->prev = tail;
  713. }
  714. /*
  715. ======================================================================
  716. lwListInsert()
  717. Insert a node into a list in sorted order.
  718. ====================================================================== */
  719. void lwListInsert( void **vlist, void *vitem, int ( *compare )( void *, void * ))
  720. {
  721. lwNode **list, *item, *node, *prev;
  722. if ( !*vlist ) {
  723. *vlist = vitem;
  724. return;
  725. }
  726. list = ( lwNode ** ) vlist;
  727. item = ( lwNode * ) vitem;
  728. node = *list;
  729. prev = NULL;
  730. while ( node ) {
  731. if ( 0 < compare( node, item )) break;
  732. prev = node;
  733. node = node->next;
  734. }
  735. if ( !prev ) {
  736. *list = item;
  737. node->prev = item;
  738. item->next = node;
  739. }
  740. else if ( !node ) {
  741. prev->next = item;
  742. item->prev = prev;
  743. }
  744. else {
  745. item->next = node;
  746. item->prev = prev;
  747. prev->next = item;
  748. node->prev = item;
  749. }
  750. }
  751. /*
  752. ======================================================================
  753. flen
  754. This accumulates a count of the number of bytes read. Callers can set
  755. it at the beginning of a sequence of reads and then retrieve it to get
  756. the number of bytes actually read. If one of the I/O functions fails,
  757. flen is set to an error code, after which the I/O functions ignore
  758. read requests until flen is reset.
  759. ====================================================================== */
  760. #define FLEN_ERROR -9999
  761. static int flen;
  762. void set_flen( int i ) { flen = i; }
  763. int get_flen( void ) { return flen; }
  764. void *getbytes( idFile *fp, int size )
  765. {
  766. void *data;
  767. if ( flen == FLEN_ERROR ) return NULL;
  768. if ( size < 0 ) {
  769. flen = FLEN_ERROR;
  770. return NULL;
  771. }
  772. data = Mem_ClearedAlloc( size );
  773. if ( !data ) {
  774. flen = FLEN_ERROR;
  775. return NULL;
  776. }
  777. if ( size != fp->Read( data, size ) ) {
  778. flen = FLEN_ERROR;
  779. Mem_Free( data );
  780. return NULL;
  781. }
  782. flen += size;
  783. return data;
  784. }
  785. void skipbytes( idFile *fp, int n )
  786. {
  787. if ( flen == FLEN_ERROR ) return;
  788. if ( fp->Seek( n, FS_SEEK_CUR ))
  789. flen = FLEN_ERROR;
  790. else
  791. flen += n;
  792. }
  793. int getI1( idFile *fp )
  794. {
  795. int i, c;
  796. if ( flen == FLEN_ERROR ) return 0;
  797. c = 0;
  798. i = fp->Read(&c, 1);
  799. if ( i < 0 ) {
  800. flen = FLEN_ERROR;
  801. return 0;
  802. }
  803. if ( c > 127 ) c -= 256;
  804. flen += 1;
  805. return c;
  806. }
  807. short getI2( idFile *fp )
  808. {
  809. short i;
  810. if ( flen == FLEN_ERROR ) return 0;
  811. if ( 2 != fp->Read( &i, 2 )) {
  812. flen = FLEN_ERROR;
  813. return 0;
  814. }
  815. BigRevBytes( &i, 2, 1 );
  816. flen += 2;
  817. return i;
  818. }
  819. int getI4( idFile *fp )
  820. {
  821. int i;
  822. if ( flen == FLEN_ERROR ) return 0;
  823. if ( 4 != fp->Read( &i, 4 )) {
  824. flen = FLEN_ERROR;
  825. return 0;
  826. }
  827. BigRevBytes( &i, 4, 1 );
  828. flen += 4;
  829. return i;
  830. }
  831. unsigned char getU1( idFile *fp )
  832. {
  833. int i, c;
  834. if ( flen == FLEN_ERROR ) return 0;
  835. c = 0;
  836. i = fp->Read(&c, 1);
  837. if ( i < 0 ) {
  838. flen = FLEN_ERROR;
  839. return 0;
  840. }
  841. flen += 1;
  842. return c;
  843. }
  844. unsigned short getU2( idFile *fp )
  845. {
  846. unsigned short i;
  847. if ( flen == FLEN_ERROR ) return 0;
  848. if ( 2 != fp->Read( &i, 2 )) {
  849. flen = FLEN_ERROR;
  850. return 0;
  851. }
  852. BigRevBytes( &i, 2, 1 );
  853. flen += 2;
  854. return i;
  855. }
  856. unsigned int getU4( idFile *fp )
  857. {
  858. unsigned int i;
  859. if ( flen == FLEN_ERROR ) return 0;
  860. if ( 4 != fp->Read( &i, 4 )) {
  861. flen = FLEN_ERROR;
  862. return 0;
  863. }
  864. BigRevBytes( &i, 4, 1 );
  865. flen += 4;
  866. return i;
  867. }
  868. int getVX( idFile *fp )
  869. {
  870. byte c;
  871. int i;
  872. if ( flen == FLEN_ERROR ) return 0;
  873. c = 0;
  874. if (fp->Read(&c, 1) == -1) {
  875. return 0;
  876. }
  877. if ( c != 0xFF ) {
  878. i = c << 8;
  879. c = 0;
  880. if (fp->Read(&c, 1) == -1) {
  881. return 0;
  882. }
  883. i |= c;
  884. flen += 2;
  885. }
  886. else {
  887. c = 0;
  888. if (fp->Read(&c, 1) == -1) {
  889. return 0;
  890. }
  891. i = c << 16;
  892. c = 0;
  893. if (fp->Read(&c, 1) == -1) {
  894. return 0;
  895. }
  896. i |= c << 8;
  897. c = 0;
  898. if (fp->Read(&c, 1) == -1) {
  899. return 0;
  900. }
  901. i |= c;
  902. flen += 4;
  903. }
  904. return i;
  905. }
  906. float getF4( idFile *fp )
  907. {
  908. float f;
  909. if ( flen == FLEN_ERROR ) return 0.0f;
  910. if ( 4 != fp->Read( &f, 4 ) ) {
  911. flen = FLEN_ERROR;
  912. return 0.0f;
  913. }
  914. BigRevBytes( &f, 4, 1 );
  915. flen += 4;
  916. if ( FLOAT_IS_DENORMAL( f ) ) {
  917. f = 0.0f;
  918. }
  919. return f;
  920. }
  921. char *getS0( idFile *fp )
  922. {
  923. char *s;
  924. int i, c, len, pos;
  925. if ( flen == FLEN_ERROR ) return NULL;
  926. pos = fp->Tell();
  927. for ( i = 1; ; i++ ) {
  928. c = 0;
  929. if (fp->Read(&c, 1) == -1) {
  930. flen = FLEN_ERROR;
  931. return NULL;
  932. }
  933. if ( c == 0 ) break;
  934. }
  935. if ( i == 1 ) {
  936. if ( fp->Seek( pos + 2, FS_SEEK_SET ))
  937. flen = FLEN_ERROR;
  938. else
  939. flen += 2;
  940. return NULL;
  941. }
  942. len = i + ( i & 1 );
  943. s = (char*)Mem_ClearedAlloc( len );
  944. if ( !s ) {
  945. flen = FLEN_ERROR;
  946. return NULL;
  947. }
  948. if ( fp->Seek( pos, FS_SEEK_SET )) {
  949. flen = FLEN_ERROR;
  950. return NULL;
  951. }
  952. if ( len != fp->Read( s, len )) {
  953. flen = FLEN_ERROR;
  954. return NULL;
  955. }
  956. flen += len;
  957. return s;
  958. }
  959. int sgetI1( unsigned char **bp )
  960. {
  961. int i;
  962. if ( flen == FLEN_ERROR ) return 0;
  963. i = **bp;
  964. if ( i > 127 ) i -= 256;
  965. flen += 1;
  966. *bp++;
  967. return i;
  968. }
  969. short sgetI2( unsigned char **bp )
  970. {
  971. short i;
  972. if ( flen == FLEN_ERROR ) return 0;
  973. memcpy( &i, *bp, 2 );
  974. BigRevBytes( &i, 2, 1 );
  975. flen += 2;
  976. *bp += 2;
  977. return i;
  978. }
  979. int sgetI4( unsigned char **bp )
  980. {
  981. int i;
  982. if ( flen == FLEN_ERROR ) return 0;
  983. memcpy( &i, *bp, 4 );
  984. BigRevBytes( &i, 4, 1 );
  985. flen += 4;
  986. *bp += 4;
  987. return i;
  988. }
  989. unsigned char sgetU1( unsigned char **bp )
  990. {
  991. unsigned char c;
  992. if ( flen == FLEN_ERROR ) return 0;
  993. c = **bp;
  994. flen += 1;
  995. *bp++;
  996. return c;
  997. }
  998. unsigned short sgetU2( unsigned char **bp )
  999. {
  1000. unsigned char *buf = *bp;
  1001. unsigned short i;
  1002. if ( flen == FLEN_ERROR ) return 0;
  1003. i = ( buf[ 0 ] << 8 ) | buf[ 1 ];
  1004. flen += 2;
  1005. *bp += 2;
  1006. return i;
  1007. }
  1008. unsigned int sgetU4( unsigned char **bp )
  1009. {
  1010. unsigned int i;
  1011. if ( flen == FLEN_ERROR ) return 0;
  1012. memcpy( &i, *bp, 4 );
  1013. BigRevBytes( &i, 4, 1 );
  1014. flen += 4;
  1015. *bp += 4;
  1016. return i;
  1017. }
  1018. int sgetVX( unsigned char **bp )
  1019. {
  1020. unsigned char *buf = *bp;
  1021. int i;
  1022. if ( flen == FLEN_ERROR ) return 0;
  1023. if ( buf[ 0 ] != 0xFF ) {
  1024. i = buf[ 0 ] << 8 | buf[ 1 ];
  1025. flen += 2;
  1026. *bp += 2;
  1027. }
  1028. else {
  1029. i = ( buf[ 1 ] << 16 ) | ( buf[ 2 ] << 8 ) | buf[ 3 ];
  1030. flen += 4;
  1031. *bp += 4;
  1032. }
  1033. return i;
  1034. }
  1035. float sgetF4( unsigned char **bp )
  1036. {
  1037. float f;
  1038. if ( flen == FLEN_ERROR ) return 0.0f;
  1039. memcpy( &f, *bp, 4 );
  1040. BigRevBytes( &f, 4, 1 );
  1041. flen += 4;
  1042. *bp += 4;
  1043. if ( FLOAT_IS_DENORMAL( f ) ) {
  1044. f = 0.0f;
  1045. }
  1046. return f;
  1047. }
  1048. char *sgetS0( unsigned char **bp )
  1049. {
  1050. char *s;
  1051. unsigned char *buf = *bp;
  1052. int len;
  1053. if ( flen == FLEN_ERROR ) return NULL;
  1054. len = strlen( (const char*)buf ) + 1;
  1055. if ( len == 1 ) {
  1056. flen += 2;
  1057. *bp += 2;
  1058. return NULL;
  1059. }
  1060. len += len & 1;
  1061. s = (char*)Mem_ClearedAlloc( len );
  1062. if ( !s ) {
  1063. flen = FLEN_ERROR;
  1064. return NULL;
  1065. }
  1066. memcpy( s, buf, len );
  1067. flen += len;
  1068. *bp += len;
  1069. return s;
  1070. }
  1071. /*
  1072. ======================================================================
  1073. lwFreeLayer()
  1074. Free memory used by an lwLayer.
  1075. ====================================================================== */
  1076. void lwFreeLayer( lwLayer *layer )
  1077. {
  1078. if ( layer ) {
  1079. if ( layer->name ) Mem_Free( layer->name );
  1080. lwFreePoints( &layer->point );
  1081. lwFreePolygons( &layer->polygon );
  1082. lwListFree( layer->vmap, (void (__cdecl *)(void *))lwFreeVMap );
  1083. Mem_Free( layer );
  1084. }
  1085. }
  1086. /*
  1087. ======================================================================
  1088. lwFreeObject()
  1089. Free memory used by an lwObject.
  1090. ====================================================================== */
  1091. void lwFreeObject( lwObject *object )
  1092. {
  1093. if ( object ) {
  1094. lwListFree( object->layer, (void (__cdecl *)(void *))lwFreeLayer );
  1095. lwListFree( object->env, (void (__cdecl *)(void *))lwFreeEnvelope );
  1096. lwListFree( object->clip, (void (__cdecl *)(void *))lwFreeClip );
  1097. lwListFree( object->surf, (void (__cdecl *)(void *))lwFreeSurface );
  1098. lwFreeTags( &object->taglist );
  1099. Mem_Free( object );
  1100. }
  1101. }
  1102. /*
  1103. ======================================================================
  1104. lwGetObject()
  1105. Returns the contents of a LightWave object, given its filename, or
  1106. NULL if the file couldn't be loaded. On failure, failID and failpos
  1107. can be used to diagnose the cause.
  1108. 1. If the file isn't an LWO2 or an LWOB, failpos will contain 12 and
  1109. failID will be unchanged.
  1110. 2. If an error occurs while reading, failID will contain the most
  1111. recently read IFF chunk ID, and failpos will contain the value
  1112. returned by fp->Tell() at the time of the failure.
  1113. 3. If the file couldn't be opened, or an error occurs while reading
  1114. the first 12 bytes, both failID and failpos will be unchanged.
  1115. If you don't need this information, failID and failpos can be NULL.
  1116. ====================================================================== */
  1117. lwObject *lwGetObject( const char *filename, unsigned int *failID, int *failpos )
  1118. {
  1119. idFile *fp = NULL;
  1120. lwObject *object;
  1121. lwLayer *layer;
  1122. lwNode *node;
  1123. int id, formsize, type, cksize;
  1124. int i, rlen;
  1125. fp = fileSystem->OpenFileRead( filename );
  1126. if ( !fp ) {
  1127. return NULL;
  1128. }
  1129. /* read the first 12 bytes */
  1130. set_flen( 0 );
  1131. id = getU4( fp );
  1132. formsize = getU4( fp );
  1133. type = getU4( fp );
  1134. if ( 12 != get_flen() ) {
  1135. fileSystem->CloseFile( fp );
  1136. return NULL;
  1137. }
  1138. /* is this a LW object? */
  1139. if ( id != ID_FORM ) {
  1140. fileSystem->CloseFile( fp );
  1141. if ( failpos ) *failpos = 12;
  1142. return NULL;
  1143. }
  1144. if ( type != ID_LWO2 ) {
  1145. fileSystem->CloseFile( fp );
  1146. if ( type == ID_LWOB )
  1147. return lwGetObject5( filename, failID, failpos );
  1148. else {
  1149. if ( failpos ) *failpos = 12;
  1150. return NULL;
  1151. }
  1152. }
  1153. /* allocate an object and a default layer */
  1154. object = (lwObject*)Mem_ClearedAlloc( sizeof( lwObject ) );
  1155. if ( !object ) goto Fail;
  1156. layer = (lwLayer*)Mem_ClearedAlloc( sizeof( lwLayer ) );
  1157. if ( !layer ) goto Fail;
  1158. object->layer = layer;
  1159. object->timeStamp = fp->Timestamp();
  1160. /* get the first chunk header */
  1161. id = getU4( fp );
  1162. cksize = getU4( fp );
  1163. if ( 0 > get_flen() ) goto Fail;
  1164. /* process chunks as they're encountered */
  1165. while ( 1 ) {
  1166. cksize += cksize & 1;
  1167. switch ( id )
  1168. {
  1169. case ID_LAYR:
  1170. if ( object->nlayers > 0 ) {
  1171. layer = (lwLayer*)Mem_ClearedAlloc( sizeof( lwLayer ) );
  1172. if ( !layer ) goto Fail;
  1173. lwListAdd( (void**)&object->layer, layer );
  1174. }
  1175. object->nlayers++;
  1176. set_flen( 0 );
  1177. layer->index = getU2( fp );
  1178. layer->flags = getU2( fp );
  1179. layer->pivot[ 0 ] = getF4( fp );
  1180. layer->pivot[ 1 ] = getF4( fp );
  1181. layer->pivot[ 2 ] = getF4( fp );
  1182. layer->name = getS0( fp );
  1183. rlen = get_flen();
  1184. if ( rlen < 0 || rlen > cksize ) goto Fail;
  1185. if ( rlen <= cksize - 2 )
  1186. layer->parent = getU2( fp );
  1187. rlen = get_flen();
  1188. if ( rlen < cksize )
  1189. fp->Seek( cksize - rlen, FS_SEEK_CUR );
  1190. break;
  1191. case ID_PNTS:
  1192. if ( !lwGetPoints( fp, cksize, &layer->point ))
  1193. goto Fail;
  1194. break;
  1195. case ID_POLS:
  1196. if ( !lwGetPolygons( fp, cksize, &layer->polygon,
  1197. layer->point.offset ))
  1198. goto Fail;
  1199. break;
  1200. case ID_VMAP:
  1201. case ID_VMAD:
  1202. node = ( lwNode * ) lwGetVMap( fp, cksize, layer->point.offset,
  1203. layer->polygon.offset, id == ID_VMAD );
  1204. if ( !node ) goto Fail;
  1205. lwListAdd( (void**)&layer->vmap, node );
  1206. layer->nvmaps++;
  1207. break;
  1208. case ID_PTAG:
  1209. if ( !lwGetPolygonTags( fp, cksize, &object->taglist,
  1210. &layer->polygon ))
  1211. goto Fail;
  1212. break;
  1213. case ID_BBOX:
  1214. set_flen( 0 );
  1215. for ( i = 0; i < 6; i++ )
  1216. layer->bbox[ i ] = getF4( fp );
  1217. rlen = get_flen();
  1218. if ( rlen < 0 || rlen > cksize ) goto Fail;
  1219. if ( rlen < cksize )
  1220. fp->Seek( cksize - rlen, FS_SEEK_CUR );
  1221. break;
  1222. case ID_TAGS:
  1223. if ( !lwGetTags( fp, cksize, &object->taglist ))
  1224. goto Fail;
  1225. break;
  1226. case ID_ENVL:
  1227. node = ( lwNode * ) lwGetEnvelope( fp, cksize );
  1228. if ( !node ) goto Fail;
  1229. lwListAdd( (void**)&object->env, node );
  1230. object->nenvs++;
  1231. break;
  1232. case ID_CLIP:
  1233. node = ( lwNode * ) lwGetClip( fp, cksize );
  1234. if ( !node ) goto Fail;
  1235. lwListAdd( (void**)&object->clip, node );
  1236. object->nclips++;
  1237. break;
  1238. case ID_SURF:
  1239. node = ( lwNode * ) lwGetSurface( fp, cksize );
  1240. if ( !node ) goto Fail;
  1241. lwListAdd( (void**)&object->surf, node );
  1242. object->nsurfs++;
  1243. break;
  1244. case ID_DESC:
  1245. case ID_TEXT:
  1246. case ID_ICON:
  1247. default:
  1248. fp->Seek( cksize, FS_SEEK_CUR );
  1249. break;
  1250. }
  1251. /* end of the file? */
  1252. if ( formsize <= fp->Tell() - 8 ) break;
  1253. /* get the next chunk header */
  1254. set_flen( 0 );
  1255. id = getU4( fp );
  1256. cksize = getU4( fp );
  1257. if ( 8 != get_flen() ) goto Fail;
  1258. }
  1259. fileSystem->CloseFile( fp );
  1260. fp = NULL;
  1261. if ( object->nlayers == 0 )
  1262. object->nlayers = 1;
  1263. layer = object->layer;
  1264. while ( layer ) {
  1265. lwGetBoundingBox( &layer->point, layer->bbox );
  1266. lwGetPolyNormals( &layer->point, &layer->polygon );
  1267. if ( !lwGetPointPolygons( &layer->point, &layer->polygon )) goto Fail;
  1268. if ( !lwResolvePolySurfaces( &layer->polygon, &object->taglist,
  1269. &object->surf, &object->nsurfs )) goto Fail;
  1270. lwGetVertNormals( &layer->point, &layer->polygon );
  1271. if ( !lwGetPointVMaps( &layer->point, layer->vmap )) goto Fail;
  1272. if ( !lwGetPolyVMaps( &layer->polygon, layer->vmap )) goto Fail;
  1273. layer = layer->next;
  1274. }
  1275. return object;
  1276. Fail:
  1277. if ( failID ) *failID = id;
  1278. if ( fp ) {
  1279. if ( failpos ) *failpos = fp->Tell();
  1280. fileSystem->CloseFile( fp );
  1281. }
  1282. lwFreeObject( object );
  1283. return NULL;
  1284. }
  1285. /* IDs specific to LWOB */
  1286. #define ID_SRFS LWID_('S','R','F','S')
  1287. #define ID_FLAG LWID_('F','L','A','G')
  1288. #define ID_VLUM LWID_('V','L','U','M')
  1289. #define ID_VDIF LWID_('V','D','I','F')
  1290. #define ID_VSPC LWID_('V','S','P','C')
  1291. #define ID_RFLT LWID_('R','F','L','T')
  1292. #define ID_BTEX LWID_('B','T','E','X')
  1293. #define ID_CTEX LWID_('C','T','E','X')
  1294. #define ID_DTEX LWID_('D','T','E','X')
  1295. #define ID_LTEX LWID_('L','T','E','X')
  1296. #define ID_RTEX LWID_('R','T','E','X')
  1297. #define ID_STEX LWID_('S','T','E','X')
  1298. #define ID_TTEX LWID_('T','T','E','X')
  1299. #define ID_TFLG LWID_('T','F','L','G')
  1300. #define ID_TSIZ LWID_('T','S','I','Z')
  1301. #define ID_TCTR LWID_('T','C','T','R')
  1302. #define ID_TFAL LWID_('T','F','A','L')
  1303. #define ID_TVEL LWID_('T','V','E','L')
  1304. #define ID_TCLR LWID_('T','C','L','R')
  1305. #define ID_TVAL LWID_('T','V','A','L')
  1306. #define ID_TAMP LWID_('T','A','M','P')
  1307. #define ID_TIMG LWID_('T','I','M','G')
  1308. #define ID_TAAS LWID_('T','A','A','S')
  1309. #define ID_TREF LWID_('T','R','E','F')
  1310. #define ID_TOPC LWID_('T','O','P','C')
  1311. #define ID_SDAT LWID_('S','D','A','T')
  1312. #define ID_TFP0 LWID_('T','F','P','0')
  1313. #define ID_TFP1 LWID_('T','F','P','1')
  1314. /*
  1315. ======================================================================
  1316. add_clip()
  1317. Add a clip to the clip list. Used to store the contents of an RIMG or
  1318. TIMG surface subchunk.
  1319. ====================================================================== */
  1320. static int add_clip( char *s, lwClip **clist, int *nclips )
  1321. {
  1322. lwClip *clip;
  1323. char *p;
  1324. clip = (lwClip*)Mem_ClearedAlloc( sizeof( lwClip ) );
  1325. if ( !clip ) return 0;
  1326. clip->contrast.val = 1.0f;
  1327. clip->brightness.val = 1.0f;
  1328. clip->saturation.val = 1.0f;
  1329. clip->gamma.val = 1.0f;
  1330. if ( p = strstr( s, "(sequence)" )) {
  1331. p[ -1 ] = 0;
  1332. clip->type = ID_ISEQ;
  1333. clip->source.seq.prefix = s;
  1334. clip->source.seq.digits = 3;
  1335. }
  1336. else {
  1337. clip->type = ID_STIL;
  1338. clip->source.still.name = s;
  1339. }
  1340. *nclips++;
  1341. clip->index = *nclips;
  1342. lwListAdd( (void**)clist, clip );
  1343. return clip->index;
  1344. }
  1345. /*
  1346. ======================================================================
  1347. add_tvel()
  1348. Add a triple of envelopes to simulate the old texture velocity
  1349. parameters.
  1350. ====================================================================== */
  1351. static int add_tvel( float pos[], float vel[], lwEnvelope **elist, int *nenvs )
  1352. {
  1353. lwEnvelope *env;
  1354. lwKey *key0, *key1;
  1355. int i;
  1356. for ( i = 0; i < 3; i++ ) {
  1357. env = (lwEnvelope*)Mem_ClearedAlloc( sizeof( lwEnvelope ) );
  1358. key0 = (lwKey*)Mem_ClearedAlloc( sizeof( lwKey ) );
  1359. key1 = (lwKey*)Mem_ClearedAlloc( sizeof( lwKey ) );
  1360. if ( !env || !key0 || !key1 ) return 0;
  1361. key0->next = key1;
  1362. key0->value = pos[ i ];
  1363. key0->time = 0.0f;
  1364. key1->prev = key0;
  1365. key1->value = pos[ i ] + vel[ i ] * 30.0f;
  1366. key1->time = 1.0f;
  1367. key0->shape = key1->shape = ID_LINE;
  1368. env->index = *nenvs + i + 1;
  1369. env->type = 0x0301 + i;
  1370. env->name = (char*)Mem_ClearedAlloc( 11 );
  1371. if ( env->name ) {
  1372. strcpy( env->name, "Position.X" );
  1373. env->name[ 9 ] += i;
  1374. }
  1375. env->key = key0;
  1376. env->nkeys = 2;
  1377. env->behavior[ 0 ] = BEH_LINEAR;
  1378. env->behavior[ 1 ] = BEH_LINEAR;
  1379. lwListAdd( (void**)elist, env );
  1380. }
  1381. *nenvs += 3;
  1382. return env->index - 2;
  1383. }
  1384. /*
  1385. ======================================================================
  1386. get_texture()
  1387. Create a new texture for BTEX, CTEX, etc. subchunks.
  1388. ====================================================================== */
  1389. static lwTexture *get_texture( char *s )
  1390. {
  1391. lwTexture *tex;
  1392. tex = (lwTexture*)Mem_ClearedAlloc( sizeof( lwTexture ) );
  1393. if ( !tex ) return NULL;
  1394. tex->tmap.size.val[ 0 ] =
  1395. tex->tmap.size.val[ 1 ] =
  1396. tex->tmap.size.val[ 2 ] = 1.0f;
  1397. tex->opacity.val = 1.0f;
  1398. tex->enabled = 1;
  1399. if ( strstr( s, "Image Map" )) {
  1400. tex->type = ID_IMAP;
  1401. if ( strstr( s, "Planar" )) tex->param.imap.projection = 0;
  1402. else if ( strstr( s, "Cylindrical" )) tex->param.imap.projection = 1;
  1403. else if ( strstr( s, "Spherical" )) tex->param.imap.projection = 2;
  1404. else if ( strstr( s, "Cubic" )) tex->param.imap.projection = 3;
  1405. else if ( strstr( s, "Front" )) tex->param.imap.projection = 4;
  1406. tex->param.imap.aa_strength = 1.0f;
  1407. tex->param.imap.amplitude.val = 1.0f;
  1408. Mem_Free( s );
  1409. }
  1410. else {
  1411. tex->type = ID_PROC;
  1412. tex->param.proc.name = s;
  1413. }
  1414. return tex;
  1415. }
  1416. /*
  1417. ======================================================================
  1418. lwGetSurface5()
  1419. Read an lwSurface from an LWOB file.
  1420. ====================================================================== */
  1421. lwSurface *lwGetSurface5( idFile *fp, int cksize, lwObject *obj )
  1422. {
  1423. lwSurface *surf;
  1424. lwTexture *tex;
  1425. lwPlugin *shdr;
  1426. char *s;
  1427. float v[ 3 ];
  1428. unsigned int id, flags;
  1429. unsigned short sz;
  1430. int pos, rlen, i;
  1431. /* allocate the Surface structure */
  1432. surf = (lwSurface*)Mem_ClearedAlloc( sizeof( lwSurface ) );
  1433. if ( !surf ) goto Fail;
  1434. /* non-zero defaults */
  1435. surf->color.rgb[ 0 ] = 0.78431f;
  1436. surf->color.rgb[ 1 ] = 0.78431f;
  1437. surf->color.rgb[ 2 ] = 0.78431f;
  1438. surf->diffuse.val = 1.0f;
  1439. surf->glossiness.val = 0.4f;
  1440. surf->bump.val = 1.0f;
  1441. surf->eta.val = 1.0f;
  1442. surf->sideflags = 1;
  1443. /* remember where we started */
  1444. set_flen( 0 );
  1445. pos = fp->Tell();
  1446. /* name */
  1447. surf->name = getS0( fp );
  1448. /* first subchunk header */
  1449. id = getU4( fp );
  1450. sz = getU2( fp );
  1451. if ( 0 > get_flen() ) goto Fail;
  1452. /* process subchunks as they're encountered */
  1453. while ( 1 ) {
  1454. sz += sz & 1;
  1455. set_flen( 0 );
  1456. switch ( id ) {
  1457. case ID_COLR:
  1458. surf->color.rgb[ 0 ] = getU1( fp ) / 255.0f;
  1459. surf->color.rgb[ 1 ] = getU1( fp ) / 255.0f;
  1460. surf->color.rgb[ 2 ] = getU1( fp ) / 255.0f;
  1461. break;
  1462. case ID_FLAG:
  1463. flags = getU2( fp );
  1464. if ( flags & 4 ) surf->smooth = 1.56207f;
  1465. if ( flags & 8 ) surf->color_hilite.val = 1.0f;
  1466. if ( flags & 16 ) surf->color_filter.val = 1.0f;
  1467. if ( flags & 128 ) surf->dif_sharp.val = 0.5f;
  1468. if ( flags & 256 ) surf->sideflags = 3;
  1469. if ( flags & 512 ) surf->add_trans.val = 1.0f;
  1470. break;
  1471. case ID_LUMI:
  1472. surf->luminosity.val = getI2( fp ) / 256.0f;
  1473. break;
  1474. case ID_VLUM:
  1475. surf->luminosity.val = getF4( fp );
  1476. break;
  1477. case ID_DIFF:
  1478. surf->diffuse.val = getI2( fp ) / 256.0f;
  1479. break;
  1480. case ID_VDIF:
  1481. surf->diffuse.val = getF4( fp );
  1482. break;
  1483. case ID_SPEC:
  1484. surf->specularity.val = getI2( fp ) / 256.0f;
  1485. break;
  1486. case ID_VSPC:
  1487. surf->specularity.val = getF4( fp );
  1488. break;
  1489. case ID_GLOS:
  1490. surf->glossiness.val = ( float ) logf( ( float) getU2( fp )) / 20.7944f;
  1491. break;
  1492. case ID_SMAN:
  1493. surf->smooth = getF4( fp );
  1494. break;
  1495. case ID_REFL:
  1496. surf->reflection.val.val = getI2( fp ) / 256.0f;
  1497. break;
  1498. case ID_RFLT:
  1499. surf->reflection.options = getU2( fp );
  1500. break;
  1501. case ID_RIMG:
  1502. s = getS0( fp );
  1503. surf->reflection.cindex = add_clip( s, &obj->clip, &obj->nclips );
  1504. surf->reflection.options = 3;
  1505. break;
  1506. case ID_RSAN:
  1507. surf->reflection.seam_angle = getF4( fp );
  1508. break;
  1509. case ID_TRAN:
  1510. surf->transparency.val.val = getI2( fp ) / 256.0f;
  1511. break;
  1512. case ID_RIND:
  1513. surf->eta.val = getF4( fp );
  1514. break;
  1515. case ID_BTEX:
  1516. s = (char*)getbytes( fp, sz );
  1517. tex = get_texture( s );
  1518. lwListAdd( (void**)&surf->bump.tex, tex );
  1519. break;
  1520. case ID_CTEX:
  1521. s = (char*)getbytes( fp, sz );
  1522. tex = get_texture( s );
  1523. lwListAdd( (void**)&surf->color.tex, tex );
  1524. break;
  1525. case ID_DTEX:
  1526. s = (char*)getbytes( fp, sz );
  1527. tex = get_texture( s );
  1528. lwListAdd( (void**)&surf->diffuse.tex, tex );
  1529. break;
  1530. case ID_LTEX:
  1531. s = (char*)getbytes( fp, sz );
  1532. tex = get_texture( s );
  1533. lwListAdd( (void**)&surf->luminosity.tex, tex );
  1534. break;
  1535. case ID_RTEX:
  1536. s = (char*)getbytes( fp, sz );
  1537. tex = get_texture( s );
  1538. lwListAdd( (void**)&surf->reflection.val.tex, tex );
  1539. break;
  1540. case ID_STEX:
  1541. s = (char*)getbytes( fp, sz );
  1542. tex = get_texture( s );
  1543. lwListAdd( (void**)&surf->specularity.tex, tex );
  1544. break;
  1545. case ID_TTEX:
  1546. s = (char*)getbytes( fp, sz );
  1547. tex = get_texture( s );
  1548. lwListAdd( (void**)&surf->transparency.val.tex, tex );
  1549. break;
  1550. case ID_TFLG:
  1551. flags = getU2( fp );
  1552. if ( flags & 1 ) i = 0;
  1553. if ( flags & 2 ) i = 1;
  1554. if ( flags & 4 ) i = 2;
  1555. tex->axis = i;
  1556. if ( tex->type == ID_IMAP )
  1557. tex->param.imap.axis = i;
  1558. else
  1559. tex->param.proc.axis = i;
  1560. if ( flags & 8 ) tex->tmap.coord_sys = 1;
  1561. if ( flags & 16 ) tex->negative = 1;
  1562. if ( flags & 32 ) tex->param.imap.pblend = 1;
  1563. if ( flags & 64 ) {
  1564. tex->param.imap.aa_strength = 1.0f;
  1565. tex->param.imap.aas_flags = 1;
  1566. }
  1567. break;
  1568. case ID_TSIZ:
  1569. for ( i = 0; i < 3; i++ )
  1570. tex->tmap.size.val[ i ] = getF4( fp );
  1571. break;
  1572. case ID_TCTR:
  1573. for ( i = 0; i < 3; i++ )
  1574. tex->tmap.center.val[ i ] = getF4( fp );
  1575. break;
  1576. case ID_TFAL:
  1577. for ( i = 0; i < 3; i++ )
  1578. tex->tmap.falloff.val[ i ] = getF4( fp );
  1579. break;
  1580. case ID_TVEL:
  1581. for ( i = 0; i < 3; i++ )
  1582. v[ i ] = getF4( fp );
  1583. tex->tmap.center.eindex = add_tvel( tex->tmap.center.val, v,
  1584. &obj->env, &obj->nenvs );
  1585. break;
  1586. case ID_TCLR:
  1587. if ( tex->type == ID_PROC )
  1588. for ( i = 0; i < 3; i++ )
  1589. tex->param.proc.value[ i ] = getU1( fp ) / 255.0f;
  1590. break;
  1591. case ID_TVAL:
  1592. tex->param.proc.value[ 0 ] = getI2( fp ) / 256.0f;
  1593. break;
  1594. case ID_TAMP:
  1595. if ( tex->type == ID_IMAP )
  1596. tex->param.imap.amplitude.val = getF4( fp );
  1597. break;
  1598. case ID_TIMG:
  1599. s = getS0( fp );
  1600. tex->param.imap.cindex = add_clip( s, &obj->clip, &obj->nclips );
  1601. break;
  1602. case ID_TAAS:
  1603. tex->param.imap.aa_strength = getF4( fp );
  1604. tex->param.imap.aas_flags = 1;
  1605. break;
  1606. case ID_TREF:
  1607. tex->tmap.ref_object = (char*)getbytes( fp, sz );
  1608. break;
  1609. case ID_TOPC:
  1610. tex->opacity.val = getF4( fp );
  1611. break;
  1612. case ID_TFP0:
  1613. if ( tex->type == ID_IMAP )
  1614. tex->param.imap.wrapw.val = getF4( fp );
  1615. break;
  1616. case ID_TFP1:
  1617. if ( tex->type == ID_IMAP )
  1618. tex->param.imap.wraph.val = getF4( fp );
  1619. break;
  1620. case ID_SHDR:
  1621. shdr = (lwPlugin*)Mem_ClearedAlloc( sizeof( lwPlugin ) );
  1622. if ( !shdr ) goto Fail;
  1623. shdr->name = (char*)getbytes( fp, sz );
  1624. lwListAdd( (void**)&surf->shader, shdr );
  1625. surf->nshaders++;
  1626. break;
  1627. case ID_SDAT:
  1628. shdr->data = getbytes( fp, sz );
  1629. break;
  1630. default:
  1631. break;
  1632. }
  1633. /* error while reading current subchunk? */
  1634. rlen = get_flen();
  1635. if ( rlen < 0 || rlen > sz ) goto Fail;
  1636. /* skip unread parts of the current subchunk */
  1637. if ( rlen < sz )
  1638. fp->Seek( sz - rlen, FS_SEEK_CUR );
  1639. /* end of the SURF chunk? */
  1640. if ( cksize <= fp->Tell() - pos )
  1641. break;
  1642. /* get the next subchunk header */
  1643. set_flen( 0 );
  1644. id = getU4( fp );
  1645. sz = getU2( fp );
  1646. if ( 6 != get_flen() ) goto Fail;
  1647. }
  1648. return surf;
  1649. Fail:
  1650. if ( surf ) lwFreeSurface( surf );
  1651. return NULL;
  1652. }
  1653. /*
  1654. ======================================================================
  1655. lwGetPolygons5()
  1656. Read polygon records from a POLS chunk in an LWOB file. The polygons
  1657. are added to the array in the lwPolygonList.
  1658. ====================================================================== */
  1659. int lwGetPolygons5( idFile *fp, int cksize, lwPolygonList *plist, int ptoffset )
  1660. {
  1661. lwPolygon *pp;
  1662. lwPolVert *pv;
  1663. unsigned char *buf, *bp;
  1664. int i, j, nv, nverts, npols;
  1665. if ( cksize == 0 ) return 1;
  1666. /* read the whole chunk */
  1667. set_flen( 0 );
  1668. buf = (unsigned char*)getbytes( fp, cksize );
  1669. if ( !buf ) goto Fail;
  1670. /* count the polygons and vertices */
  1671. nverts = 0;
  1672. npols = 0;
  1673. bp = buf;
  1674. while ( bp < buf + cksize ) {
  1675. nv = sgetU2( &bp );
  1676. nverts += nv;
  1677. npols++;
  1678. bp += 2 * nv;
  1679. i = sgetI2( &bp );
  1680. if ( i < 0 ) bp += 2; /* detail polygons */
  1681. }
  1682. if ( !lwAllocPolygons( plist, npols, nverts ))
  1683. goto Fail;
  1684. /* fill in the new polygons */
  1685. bp = buf;
  1686. pp = plist->pol + plist->offset;
  1687. pv = plist->pol[ 0 ].v + plist->voffset;
  1688. for ( i = 0; i < npols; i++ ) {
  1689. nv = sgetU2( &bp );
  1690. pp->nverts = nv;
  1691. pp->type = ID_FACE;
  1692. if ( !pp->v ) pp->v = pv;
  1693. for ( j = 0; j < nv; j++ )
  1694. pv[ j ].index = sgetU2( &bp ) + ptoffset;
  1695. j = sgetI2( &bp );
  1696. if ( j < 0 ) {
  1697. j = -j;
  1698. bp += 2;
  1699. }
  1700. j -= 1;
  1701. pp->surf = ( lwSurface * ) j;
  1702. pp++;
  1703. pv += nv;
  1704. }
  1705. Mem_Free( buf );
  1706. return 1;
  1707. Fail:
  1708. if ( buf ) Mem_Free( buf );
  1709. lwFreePolygons( plist );
  1710. return 0;
  1711. }
  1712. /*
  1713. ======================================================================
  1714. getLWObject5()
  1715. Returns the contents of an LWOB, given its filename, or NULL if the
  1716. file couldn't be loaded. On failure, failID and failpos can be used
  1717. to diagnose the cause.
  1718. 1. If the file isn't an LWOB, failpos will contain 12 and failID will
  1719. be unchanged.
  1720. 2. If an error occurs while reading an LWOB, failID will contain the
  1721. most recently read IFF chunk ID, and failpos will contain the
  1722. value returned by fp->Tell() at the time of the failure.
  1723. 3. If the file couldn't be opened, or an error occurs while reading
  1724. the first 12 bytes, both failID and failpos will be unchanged.
  1725. If you don't need this information, failID and failpos can be NULL.
  1726. ====================================================================== */
  1727. lwObject *lwGetObject5( const char *filename, unsigned int *failID, int *failpos )
  1728. {
  1729. idFile *fp = NULL;
  1730. lwObject *object;
  1731. lwLayer *layer;
  1732. lwNode *node;
  1733. int id, formsize, type, cksize;
  1734. /* open the file */
  1735. //fp = fopen( filename, "rb" );
  1736. //if ( !fp ) return NULL;
  1737. /* read the first 12 bytes */
  1738. fp = fileSystem->OpenFileRead( filename );
  1739. if ( !fp ) {
  1740. return NULL;
  1741. }
  1742. set_flen( 0 );
  1743. id = getU4( fp );
  1744. formsize = getU4( fp );
  1745. type = getU4( fp );
  1746. if ( 12 != get_flen() ) {
  1747. fileSystem->CloseFile( fp );
  1748. return NULL;
  1749. }
  1750. /* LWOB? */
  1751. if ( id != ID_FORM || type != ID_LWOB ) {
  1752. fileSystem->CloseFile( fp );
  1753. if ( failpos ) *failpos = 12;
  1754. return NULL;
  1755. }
  1756. /* allocate an object and a default layer */
  1757. object = (lwObject*)Mem_ClearedAlloc( sizeof( lwObject ) );
  1758. if ( !object ) goto Fail2;
  1759. layer = (lwLayer*)Mem_ClearedAlloc( sizeof( lwLayer ) );
  1760. if ( !layer ) goto Fail2;
  1761. object->layer = layer;
  1762. object->nlayers = 1;
  1763. /* get the first chunk header */
  1764. id = getU4( fp );
  1765. cksize = getU4( fp );
  1766. if ( 0 > get_flen() ) goto Fail2;
  1767. /* process chunks as they're encountered */
  1768. while ( 1 ) {
  1769. cksize += cksize & 1;
  1770. switch ( id )
  1771. {
  1772. case ID_PNTS:
  1773. if ( !lwGetPoints( fp, cksize, &layer->point ))
  1774. goto Fail2;
  1775. break;
  1776. case ID_POLS:
  1777. if ( !lwGetPolygons5( fp, cksize, &layer->polygon,
  1778. layer->point.offset ))
  1779. goto Fail2;
  1780. break;
  1781. case ID_SRFS:
  1782. if ( !lwGetTags( fp, cksize, &object->taglist ))
  1783. goto Fail2;
  1784. break;
  1785. case ID_SURF:
  1786. node = ( lwNode * ) lwGetSurface5( fp, cksize, object );
  1787. if ( !node ) goto Fail2;
  1788. lwListAdd( (void**)&object->surf, node );
  1789. object->nsurfs++;
  1790. break;
  1791. default:
  1792. fp->Seek( cksize, FS_SEEK_CUR );
  1793. break;
  1794. }
  1795. /* end of the file? */
  1796. if ( formsize <= fp->Tell() - 8 ) break;
  1797. /* get the next chunk header */
  1798. set_flen( 0 );
  1799. id = getU4( fp );
  1800. cksize = getU4( fp );
  1801. if ( 8 != get_flen() ) goto Fail2;
  1802. }
  1803. fileSystem->CloseFile( fp );
  1804. fp = NULL;
  1805. lwGetBoundingBox( &layer->point, layer->bbox );
  1806. lwGetPolyNormals( &layer->point, &layer->polygon );
  1807. if ( !lwGetPointPolygons( &layer->point, &layer->polygon )) goto Fail2;
  1808. if ( !lwResolvePolySurfaces( &layer->polygon, &object->taglist,
  1809. &object->surf, &object->nsurfs )) goto Fail2;
  1810. lwGetVertNormals( &layer->point, &layer->polygon );
  1811. return object;
  1812. Fail2:
  1813. if ( failID ) *failID = id;
  1814. if ( fp ) {
  1815. if ( failpos ) *failpos = fp->Tell();
  1816. fileSystem->CloseFile( fp );
  1817. }
  1818. lwFreeObject( object );
  1819. return NULL;
  1820. }
  1821. /*
  1822. ======================================================================
  1823. lwFreePoints()
  1824. Free the memory used by an lwPointList.
  1825. ====================================================================== */
  1826. void lwFreePoints( lwPointList *point )
  1827. {
  1828. int i;
  1829. if ( point ) {
  1830. if ( point->pt ) {
  1831. for ( i = 0; i < point->count; i++ ) {
  1832. if ( point->pt[ i ].pol ) Mem_Free( point->pt[ i ].pol );
  1833. if ( point->pt[ i ].vm ) Mem_Free( point->pt[ i ].vm );
  1834. }
  1835. Mem_Free( point->pt );
  1836. }
  1837. memset( point, 0, sizeof( lwPointList ));
  1838. }
  1839. }
  1840. /*
  1841. ======================================================================
  1842. lwFreePolygons()
  1843. Free the memory used by an lwPolygonList.
  1844. ====================================================================== */
  1845. void lwFreePolygons( lwPolygonList *plist )
  1846. {
  1847. int i, j;
  1848. if ( plist ) {
  1849. if ( plist->pol ) {
  1850. for ( i = 0; i < plist->count; i++ ) {
  1851. if ( plist->pol[ i ].v ) {
  1852. for ( j = 0; j < plist->pol[ i ].nverts; j++ )
  1853. if ( plist->pol[ i ].v[ j ].vm )
  1854. Mem_Free( plist->pol[ i ].v[ j ].vm );
  1855. }
  1856. }
  1857. if ( plist->pol[ 0 ].v )
  1858. Mem_Free( plist->pol[ 0 ].v );
  1859. Mem_Free( plist->pol );
  1860. }
  1861. memset( plist, 0, sizeof( lwPolygonList ));
  1862. }
  1863. }
  1864. /*
  1865. ======================================================================
  1866. lwGetPoints()
  1867. Read point records from a PNTS chunk in an LWO2 file. The points are
  1868. added to the array in the lwPointList.
  1869. ====================================================================== */
  1870. int lwGetPoints( idFile *fp, int cksize, lwPointList *point )
  1871. {
  1872. float *f;
  1873. int np, i, j;
  1874. if ( cksize == 1 ) return 1;
  1875. /* extend the point array to hold the new points */
  1876. np = cksize / 12;
  1877. point->offset = point->count;
  1878. point->count += np;
  1879. lwPoint *oldpt = point->pt;
  1880. point->pt = (lwPoint*)Mem_Alloc( point->count * sizeof( lwPoint ) );
  1881. if ( !point->pt ) return 0;
  1882. if ( oldpt ) {
  1883. memcpy( point->pt, oldpt, point->offset * sizeof( lwPoint ) );
  1884. Mem_Free( oldpt );
  1885. }
  1886. memset( &point->pt[ point->offset ], 0, np * sizeof( lwPoint ) );
  1887. /* read the whole chunk */
  1888. f = ( float * ) getbytes( fp, cksize );
  1889. if ( !f ) return 0;
  1890. BigRevBytes( f, 4, np * 3 );
  1891. /* assign position values */
  1892. for ( i = 0, j = 0; i < np; i++, j += 3 ) {
  1893. point->pt[ i ].pos[ 0 ] = f[ j ];
  1894. point->pt[ i ].pos[ 1 ] = f[ j + 1 ];
  1895. point->pt[ i ].pos[ 2 ] = f[ j + 2 ];
  1896. }
  1897. Mem_Free( f );
  1898. return 1;
  1899. }
  1900. /*
  1901. ======================================================================
  1902. lwGetBoundingBox()
  1903. Calculate the bounding box for a point list, but only if the bounding
  1904. box hasn't already been initialized.
  1905. ====================================================================== */
  1906. void lwGetBoundingBox( lwPointList *point, float bbox[] )
  1907. {
  1908. int i, j;
  1909. if ( point->count == 0 ) return;
  1910. for ( i = 0; i < 6; i++ )
  1911. if ( bbox[ i ] != 0.0f ) return;
  1912. bbox[ 0 ] = bbox[ 1 ] = bbox[ 2 ] = 1e20f;
  1913. bbox[ 3 ] = bbox[ 4 ] = bbox[ 5 ] = -1e20f;
  1914. for ( i = 0; i < point->count; i++ ) {
  1915. for ( j = 0; j < 3; j++ ) {
  1916. if ( bbox[ j ] > point->pt[ i ].pos[ j ] )
  1917. bbox[ j ] = point->pt[ i ].pos[ j ];
  1918. if ( bbox[ j + 3 ] < point->pt[ i ].pos[ j ] )
  1919. bbox[ j + 3 ] = point->pt[ i ].pos[ j ];
  1920. }
  1921. }
  1922. }
  1923. /*
  1924. ======================================================================
  1925. lwAllocPolygons()
  1926. Allocate or extend the polygon arrays to hold new records.
  1927. ====================================================================== */
  1928. int lwAllocPolygons( lwPolygonList *plist, int npols, int nverts )
  1929. {
  1930. int i;
  1931. plist->offset = plist->count;
  1932. plist->count += npols;
  1933. lwPolygon *oldpol = plist->pol;
  1934. plist->pol = (lwPolygon*)Mem_Alloc( plist->count * sizeof( lwPolygon ) );
  1935. if ( !plist->pol ) return 0;
  1936. if ( oldpol ) {
  1937. memcpy( plist->pol, oldpol, plist->offset * sizeof( lwPolygon ) );
  1938. Mem_Free( oldpol );
  1939. }
  1940. memset( plist->pol + plist->offset, 0, npols * sizeof( lwPolygon ) );
  1941. plist->voffset = plist->vcount;
  1942. plist->vcount += nverts;
  1943. lwPolVert *oldpolv = plist->pol[0].v;
  1944. plist->pol[0].v = (lwPolVert*)Mem_Alloc( plist->vcount * sizeof( lwPolVert ) );
  1945. if ( !plist->pol[ 0 ].v ) return 0;
  1946. if ( oldpolv ) {
  1947. memcpy( plist->pol[0].v, oldpolv, plist->voffset * sizeof( lwPolVert ) );
  1948. Mem_Free( oldpolv );
  1949. }
  1950. memset( plist->pol[ 0 ].v + plist->voffset, 0, nverts * sizeof( lwPolVert ) );
  1951. /* fix up the old vertex pointers */
  1952. for ( i = 1; i < plist->offset; i++ )
  1953. plist->pol[ i ].v = plist->pol[ i - 1 ].v + plist->pol[ i - 1 ].nverts;
  1954. return 1;
  1955. }
  1956. /*
  1957. ======================================================================
  1958. lwGetPolygons()
  1959. Read polygon records from a POLS chunk in an LWO2 file. The polygons
  1960. are added to the array in the lwPolygonList.
  1961. ====================================================================== */
  1962. int lwGetPolygons( idFile *fp, int cksize, lwPolygonList *plist, int ptoffset )
  1963. {
  1964. lwPolygon *pp;
  1965. lwPolVert *pv;
  1966. unsigned char *buf, *bp;
  1967. int i, j, flags, nv, nverts, npols;
  1968. unsigned int type;
  1969. if ( cksize == 0 ) return 1;
  1970. /* read the whole chunk */
  1971. set_flen( 0 );
  1972. type = getU4( fp );
  1973. buf = (unsigned char*)getbytes( fp, cksize - 4 );
  1974. if ( cksize != get_flen() ) goto Fail;
  1975. /* count the polygons and vertices */
  1976. nverts = 0;
  1977. npols = 0;
  1978. bp = buf;
  1979. while ( bp < buf + cksize - 4 ) {
  1980. nv = sgetU2( &bp );
  1981. nv &= 0x03FF;
  1982. nverts += nv;
  1983. npols++;
  1984. for ( i = 0; i < nv; i++ )
  1985. j = sgetVX( &bp );
  1986. }
  1987. if ( !lwAllocPolygons( plist, npols, nverts ))
  1988. goto Fail;
  1989. /* fill in the new polygons */
  1990. bp = buf;
  1991. pp = plist->pol + plist->offset;
  1992. pv = plist->pol[ 0 ].v + plist->voffset;
  1993. for ( i = 0; i < npols; i++ ) {
  1994. nv = sgetU2( &bp );
  1995. flags = nv & 0xFC00;
  1996. nv &= 0x03FF;
  1997. pp->nverts = nv;
  1998. pp->flags = flags;
  1999. pp->type = type;
  2000. if ( !pp->v ) pp->v = pv;
  2001. for ( j = 0; j < nv; j++ )
  2002. pp->v[ j ].index = sgetVX( &bp ) + ptoffset;
  2003. pp++;
  2004. pv += nv;
  2005. }
  2006. Mem_Free( buf );
  2007. return 1;
  2008. Fail:
  2009. if ( buf ) Mem_Free( buf );
  2010. lwFreePolygons( plist );
  2011. return 0;
  2012. }
  2013. /*
  2014. ======================================================================
  2015. lwGetPolyNormals()
  2016. Calculate the polygon normals. By convention, LW's polygon normals
  2017. are found as the cross product of the first and last edges. It's
  2018. undefined for one- and two-point polygons.
  2019. ====================================================================== */
  2020. void lwGetPolyNormals( lwPointList *point, lwPolygonList *polygon )
  2021. {
  2022. int i, j;
  2023. float p1[ 3 ], p2[ 3 ], pn[ 3 ], v1[ 3 ], v2[ 3 ];
  2024. for ( i = 0; i < polygon->count; i++ ) {
  2025. if ( polygon->pol[ i ].nverts < 3 ) continue;
  2026. for ( j = 0; j < 3; j++ ) {
  2027. // FIXME: track down why indexes are way out of range
  2028. p1[ j ] = point->pt[ polygon->pol[ i ].v[ 0 ].index ].pos[ j ];
  2029. p2[ j ] = point->pt[ polygon->pol[ i ].v[ 1 ].index ].pos[ j ];
  2030. pn[ j ] = point->pt[ polygon->pol[ i ].v[ polygon->pol[ i ].nverts - 1 ].index ].pos[ j ];
  2031. }
  2032. for ( j = 0; j < 3; j++ ) {
  2033. v1[ j ] = p2[ j ] - p1[ j ];
  2034. v2[ j ] = pn[ j ] - p1[ j ];
  2035. }
  2036. cross( v1, v2, polygon->pol[ i ].norm );
  2037. normalize( polygon->pol[ i ].norm );
  2038. }
  2039. }
  2040. /*
  2041. ======================================================================
  2042. lwGetPointPolygons()
  2043. For each point, fill in the indexes of the polygons that share the
  2044. point. Returns 0 if any of the memory allocations fail, otherwise
  2045. returns 1.
  2046. ====================================================================== */
  2047. int lwGetPointPolygons( lwPointList *point, lwPolygonList *polygon )
  2048. {
  2049. int i, j, k;
  2050. /* count the number of polygons per point */
  2051. for ( i = 0; i < polygon->count; i++ )
  2052. for ( j = 0; j < polygon->pol[ i ].nverts; j++ )
  2053. ++point->pt[ polygon->pol[ i ].v[ j ].index ].npols;
  2054. /* alloc per-point polygon arrays */
  2055. for ( i = 0; i < point->count; i++ ) {
  2056. if ( point->pt[ i ].npols == 0 ) continue;
  2057. point->pt[ i ].pol = (int*)Mem_ClearedAlloc( point->pt[ i ].npols * sizeof( int ) );
  2058. if ( !point->pt[ i ].pol ) return 0;
  2059. point->pt[ i ].npols = 0;
  2060. }
  2061. /* fill in polygon array for each point */
  2062. for ( i = 0; i < polygon->count; i++ ) {
  2063. for ( j = 0; j < polygon->pol[ i ].nverts; j++ ) {
  2064. k = polygon->pol[ i ].v[ j ].index;
  2065. point->pt[ k ].pol[ point->pt[ k ].npols ] = i;
  2066. ++point->pt[ k ].npols;
  2067. }
  2068. }
  2069. return 1;
  2070. }
  2071. /*
  2072. ======================================================================
  2073. lwResolvePolySurfaces()
  2074. Convert tag indexes into actual lwSurface pointers. If any polygons
  2075. point to tags for which no corresponding surface can be found, a
  2076. default surface is created.
  2077. ====================================================================== */
  2078. int lwResolvePolySurfaces( lwPolygonList *polygon, lwTagList *tlist,
  2079. lwSurface **surf, int *nsurfs )
  2080. {
  2081. lwSurface **s, *st;
  2082. int i, index;
  2083. if ( tlist->count == 0 ) return 1;
  2084. s = (lwSurface**)Mem_ClearedAlloc( tlist->count * sizeof( lwSurface * ) );
  2085. if ( !s ) return 0;
  2086. for ( i = 0; i < tlist->count; i++ ) {
  2087. st = *surf;
  2088. while ( st ) {
  2089. if ( !strcmp( st->name, tlist->tag[ i ] )) {
  2090. s[ i ] = st;
  2091. break;
  2092. }
  2093. st = st->next;
  2094. }
  2095. }
  2096. for ( i = 0; i < polygon->count; i++ ) {
  2097. index = ( int ) polygon->pol[ i ].surf;
  2098. if ( index < 0 || index > tlist->count ) return 0;
  2099. if ( !s[ index ] ) {
  2100. s[ index ] = lwDefaultSurface();
  2101. if ( !s[ index ] ) return 0;
  2102. s[ index ]->name = (char*)Mem_ClearedAlloc( strlen( tlist->tag[ index ] ) + 1 );
  2103. if ( !s[ index ]->name ) return 0;
  2104. strcpy( s[ index ]->name, tlist->tag[ index ] );
  2105. lwListAdd( (void**)surf, s[ index ] );
  2106. *nsurfs = *nsurfs + 1;
  2107. }
  2108. polygon->pol[ i ].surf = s[ index ];
  2109. }
  2110. Mem_Free( s );
  2111. return 1;
  2112. }
  2113. /*
  2114. ======================================================================
  2115. lwGetVertNormals()
  2116. Calculate the vertex normals. For each polygon vertex, sum the
  2117. normals of the polygons that share the point. If the normals of the
  2118. current and adjacent polygons form an angle greater than the max
  2119. smoothing angle for the current polygon's surface, the normal of the
  2120. adjacent polygon is excluded from the sum. It's also excluded if the
  2121. polygons aren't in the same smoothing group.
  2122. Assumes that lwGetPointPolygons(), lwGetPolyNormals() and
  2123. lwResolvePolySurfaces() have already been called.
  2124. ====================================================================== */
  2125. void lwGetVertNormals( lwPointList *point, lwPolygonList *polygon )
  2126. {
  2127. int j, k, n, g, h, p;
  2128. float a;
  2129. for ( j = 0; j < polygon->count; j++ ) {
  2130. for ( n = 0; n < polygon->pol[ j ].nverts; n++ ) {
  2131. for ( k = 0; k < 3; k++ )
  2132. polygon->pol[ j ].v[ n ].norm[ k ] = polygon->pol[ j ].norm[ k ];
  2133. if ( polygon->pol[ j ].surf->smooth <= 0 ) continue;
  2134. p = polygon->pol[ j ].v[ n ].index;
  2135. for ( g = 0; g < point->pt[ p ].npols; g++ ) {
  2136. h = point->pt[ p ].pol[ g ];
  2137. if ( h == j ) continue;
  2138. if ( polygon->pol[ j ].smoothgrp != polygon->pol[ h ].smoothgrp )
  2139. continue;
  2140. a = vecangle( polygon->pol[ j ].norm, polygon->pol[ h ].norm );
  2141. if ( a > polygon->pol[ j ].surf->smooth ) continue;
  2142. for ( k = 0; k < 3; k++ )
  2143. polygon->pol[ j ].v[ n ].norm[ k ] += polygon->pol[ h ].norm[ k ];
  2144. }
  2145. normalize( polygon->pol[ j ].v[ n ].norm );
  2146. }
  2147. }
  2148. }
  2149. /*
  2150. ======================================================================
  2151. lwFreeTags()
  2152. Free memory used by an lwTagList.
  2153. ====================================================================== */
  2154. void lwFreeTags( lwTagList *tlist )
  2155. {
  2156. int i;
  2157. if ( tlist ) {
  2158. if ( tlist->tag ) {
  2159. for ( i = 0; i < tlist->count; i++ )
  2160. if ( tlist->tag[ i ] ) {
  2161. Mem_Free( tlist->tag[ i ] );
  2162. }
  2163. Mem_Free( tlist->tag );
  2164. }
  2165. memset( tlist, 0, sizeof( lwTagList ));
  2166. }
  2167. }
  2168. /*
  2169. ======================================================================
  2170. lwGetTags()
  2171. Read tag strings from a TAGS chunk in an LWO2 file. The tags are
  2172. added to the lwTagList array.
  2173. ====================================================================== */
  2174. int lwGetTags( idFile *fp, int cksize, lwTagList *tlist )
  2175. {
  2176. char *buf, *bp;
  2177. int i, len, ntags;
  2178. if ( cksize == 0 ) return 1;
  2179. /* read the whole chunk */
  2180. set_flen( 0 );
  2181. buf = (char*)getbytes( fp, cksize );
  2182. if ( !buf ) return 0;
  2183. /* count the strings */
  2184. ntags = 0;
  2185. bp = buf;
  2186. while ( bp < buf + cksize ) {
  2187. len = strlen( bp ) + 1;
  2188. len += len & 1;
  2189. bp += len;
  2190. ++ntags;
  2191. }
  2192. /* expand the string array to hold the new tags */
  2193. tlist->offset = tlist->count;
  2194. tlist->count += ntags;
  2195. char **oldtag = tlist->tag;
  2196. tlist->tag = (char**)Mem_Alloc( tlist->count * sizeof( char * ) );
  2197. if ( !tlist->tag ) goto Fail;
  2198. if ( oldtag ) {
  2199. memcpy( tlist->tag, oldtag, tlist->offset * sizeof( char * ) );
  2200. Mem_Free( oldtag );
  2201. }
  2202. memset( &tlist->tag[ tlist->offset ], 0, ntags * sizeof( char * ) );
  2203. /* copy the new tags to the tag array */
  2204. bp = buf;
  2205. for ( i = 0; i < ntags; i++ )
  2206. tlist->tag[ i + tlist->offset ] = sgetS0( (unsigned char**)&bp );
  2207. Mem_Free( buf );
  2208. return 1;
  2209. Fail:
  2210. if ( buf ) Mem_Free( buf );
  2211. return 0;
  2212. }
  2213. /*
  2214. ======================================================================
  2215. lwGetPolygonTags()
  2216. Read polygon tags from a PTAG chunk in an LWO2 file.
  2217. ====================================================================== */
  2218. int lwGetPolygonTags( idFile *fp, int cksize, lwTagList *tlist, lwPolygonList *plist )
  2219. {
  2220. unsigned int type;
  2221. int rlen = 0, i, j;
  2222. set_flen( 0 );
  2223. type = getU4( fp );
  2224. rlen = get_flen();
  2225. if ( rlen < 0 ) return 0;
  2226. if ( type != ID_SURF && type != ID_PART && type != ID_SMGP ) {
  2227. fp->Seek( cksize - 4, FS_SEEK_CUR );
  2228. return 1;
  2229. }
  2230. while ( rlen < cksize ) {
  2231. i = getVX( fp ) + plist->offset;
  2232. j = getVX( fp ) + tlist->offset;
  2233. rlen = get_flen();
  2234. if ( rlen < 0 || rlen > cksize ) return 0;
  2235. switch ( type ) {
  2236. case ID_SURF: plist->pol[ i ].surf = ( lwSurface * ) j; break;
  2237. case ID_PART: plist->pol[ i ].part = j; break;
  2238. case ID_SMGP: plist->pol[ i ].smoothgrp = j; break;
  2239. }
  2240. }
  2241. return 1;
  2242. }
  2243. /*
  2244. ======================================================================
  2245. lwFreePlugin()
  2246. Free the memory used by an lwPlugin.
  2247. ====================================================================== */
  2248. void lwFreePlugin( lwPlugin *p )
  2249. {
  2250. if ( p ) {
  2251. if ( p->ord ) Mem_Free( p->ord );
  2252. if ( p->name ) Mem_Free( p->name );
  2253. if ( p->data ) Mem_Free( p->data );
  2254. Mem_Free( p );
  2255. }
  2256. }
  2257. /*
  2258. ======================================================================
  2259. lwFreeTexture()
  2260. Free the memory used by an lwTexture.
  2261. ====================================================================== */
  2262. void lwFreeTexture( lwTexture *t )
  2263. {
  2264. if ( t ) {
  2265. if ( t->ord ) Mem_Free( t->ord );
  2266. switch ( t->type ) {
  2267. case ID_IMAP:
  2268. if ( t->param.imap.vmap_name ) Mem_Free( t->param.imap.vmap_name );
  2269. break;
  2270. case ID_PROC:
  2271. if ( t->param.proc.name ) Mem_Free( t->param.proc.name );
  2272. if ( t->param.proc.data ) Mem_Free( t->param.proc.data );
  2273. break;
  2274. case ID_GRAD:
  2275. if ( t->param.grad.key ) Mem_Free( t->param.grad.key );
  2276. if ( t->param.grad.ikey ) Mem_Free( t->param.grad.ikey );
  2277. break;
  2278. }
  2279. if ( t->tmap.ref_object ) Mem_Free( t->tmap.ref_object );
  2280. Mem_Free( t );
  2281. }
  2282. }
  2283. /*
  2284. ======================================================================
  2285. lwFreeSurface()
  2286. Free the memory used by an lwSurface.
  2287. ====================================================================== */
  2288. void lwFreeSurface( lwSurface *surf )
  2289. {
  2290. if ( surf ) {
  2291. if ( surf->name ) Mem_Free( surf->name );
  2292. if ( surf->srcname ) Mem_Free( surf->srcname );
  2293. lwListFree( surf->shader, (void (__cdecl *)(void *))lwFreePlugin );
  2294. lwListFree( surf->color.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2295. lwListFree( surf->luminosity.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2296. lwListFree( surf->diffuse.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2297. lwListFree( surf->specularity.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2298. lwListFree( surf->glossiness.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2299. lwListFree( surf->reflection.val.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2300. lwListFree( surf->transparency.val.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2301. lwListFree( surf->eta.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2302. lwListFree( surf->translucency.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2303. lwListFree( surf->bump.tex, (void (__cdecl *)(void *))lwFreeTexture );
  2304. Mem_Free( surf );
  2305. }
  2306. }
  2307. /*
  2308. ======================================================================
  2309. lwGetTHeader()
  2310. Read a texture map header from a SURF.BLOK in an LWO2 file. This is
  2311. the first subchunk in a BLOK, and its contents are common to all three
  2312. texture types.
  2313. ====================================================================== */
  2314. int lwGetTHeader( idFile *fp, int hsz, lwTexture *tex )
  2315. {
  2316. unsigned int id;
  2317. unsigned short sz;
  2318. int pos, rlen;
  2319. /* remember where we started */
  2320. set_flen( 0 );
  2321. pos = fp->Tell();
  2322. /* ordinal string */
  2323. tex->ord = getS0( fp );
  2324. /* first subchunk header */
  2325. id = getU4( fp );
  2326. sz = getU2( fp );
  2327. if ( 0 > get_flen() ) return 0;
  2328. /* process subchunks as they're encountered */
  2329. while ( 1 ) {
  2330. sz += sz & 1;
  2331. set_flen( 0 );
  2332. switch ( id ) {
  2333. case ID_CHAN:
  2334. tex->chan = getU4( fp );
  2335. break;
  2336. case ID_OPAC:
  2337. tex->opac_type = getU2( fp );
  2338. tex->opacity.val = getF4( fp );
  2339. tex->opacity.eindex = getVX( fp );
  2340. break;
  2341. case ID_ENAB:
  2342. tex->enabled = getU2( fp );
  2343. break;
  2344. case ID_NEGA:
  2345. tex->negative = getU2( fp );
  2346. break;
  2347. case ID_AXIS:
  2348. tex->axis = getU2( fp );
  2349. break;
  2350. default:
  2351. break;
  2352. }
  2353. /* error while reading current subchunk? */
  2354. rlen = get_flen();
  2355. if ( rlen < 0 || rlen > sz ) return 0;
  2356. /* skip unread parts of the current subchunk */
  2357. if ( rlen < sz )
  2358. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2359. /* end of the texture header subchunk? */
  2360. if ( hsz <= fp->Tell() - pos )
  2361. break;
  2362. /* get the next subchunk header */
  2363. set_flen( 0 );
  2364. id = getU4( fp );
  2365. sz = getU2( fp );
  2366. if ( 6 != get_flen() ) return 0;
  2367. }
  2368. set_flen( fp->Tell() - pos );
  2369. return 1;
  2370. }
  2371. /*
  2372. ======================================================================
  2373. lwGetTMap()
  2374. Read a texture map from a SURF.BLOK in an LWO2 file. The TMAP
  2375. defines the mapping from texture to world or object coordinates.
  2376. ====================================================================== */
  2377. int lwGetTMap( idFile *fp, int tmapsz, lwTMap *tmap )
  2378. {
  2379. unsigned int id;
  2380. unsigned short sz;
  2381. int rlen, pos, i;
  2382. pos = fp->Tell();
  2383. id = getU4( fp );
  2384. sz = getU2( fp );
  2385. if ( 0 > get_flen() ) return 0;
  2386. while ( 1 ) {
  2387. sz += sz & 1;
  2388. set_flen( 0 );
  2389. switch ( id ) {
  2390. case ID_SIZE:
  2391. for ( i = 0; i < 3; i++ )
  2392. tmap->size.val[ i ] = getF4( fp );
  2393. tmap->size.eindex = getVX( fp );
  2394. break;
  2395. case ID_CNTR:
  2396. for ( i = 0; i < 3; i++ )
  2397. tmap->center.val[ i ] = getF4( fp );
  2398. tmap->center.eindex = getVX( fp );
  2399. break;
  2400. case ID_ROTA:
  2401. for ( i = 0; i < 3; i++ )
  2402. tmap->rotate.val[ i ] = getF4( fp );
  2403. tmap->rotate.eindex = getVX( fp );
  2404. break;
  2405. case ID_FALL:
  2406. tmap->fall_type = getU2( fp );
  2407. for ( i = 0; i < 3; i++ )
  2408. tmap->falloff.val[ i ] = getF4( fp );
  2409. tmap->falloff.eindex = getVX( fp );
  2410. break;
  2411. case ID_OREF:
  2412. tmap->ref_object = getS0( fp );
  2413. break;
  2414. case ID_CSYS:
  2415. tmap->coord_sys = getU2( fp );
  2416. break;
  2417. default:
  2418. break;
  2419. }
  2420. /* error while reading the current subchunk? */
  2421. rlen = get_flen();
  2422. if ( rlen < 0 || rlen > sz ) return 0;
  2423. /* skip unread parts of the current subchunk */
  2424. if ( rlen < sz )
  2425. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2426. /* end of the TMAP subchunk? */
  2427. if ( tmapsz <= fp->Tell() - pos )
  2428. break;
  2429. /* get the next subchunk header */
  2430. set_flen( 0 );
  2431. id = getU4( fp );
  2432. sz = getU2( fp );
  2433. if ( 6 != get_flen() ) return 0;
  2434. }
  2435. set_flen( fp->Tell() - pos );
  2436. return 1;
  2437. }
  2438. /*
  2439. ======================================================================
  2440. lwGetImageMap()
  2441. Read an lwImageMap from a SURF.BLOK in an LWO2 file.
  2442. ====================================================================== */
  2443. int lwGetImageMap( idFile *fp, int rsz, lwTexture *tex )
  2444. {
  2445. unsigned int id;
  2446. unsigned short sz;
  2447. int rlen, pos;
  2448. pos = fp->Tell();
  2449. id = getU4( fp );
  2450. sz = getU2( fp );
  2451. if ( 0 > get_flen() ) return 0;
  2452. while ( 1 ) {
  2453. sz += sz & 1;
  2454. set_flen( 0 );
  2455. switch ( id ) {
  2456. case ID_TMAP:
  2457. if ( !lwGetTMap( fp, sz, &tex->tmap )) return 0;
  2458. break;
  2459. case ID_PROJ:
  2460. tex->param.imap.projection = getU2( fp );
  2461. break;
  2462. case ID_VMAP:
  2463. tex->param.imap.vmap_name = getS0( fp );
  2464. break;
  2465. case ID_AXIS:
  2466. tex->param.imap.axis = getU2( fp );
  2467. break;
  2468. case ID_IMAG:
  2469. tex->param.imap.cindex = getVX( fp );
  2470. break;
  2471. case ID_WRAP:
  2472. tex->param.imap.wrapw_type = getU2( fp );
  2473. tex->param.imap.wraph_type = getU2( fp );
  2474. break;
  2475. case ID_WRPW:
  2476. tex->param.imap.wrapw.val = getF4( fp );
  2477. tex->param.imap.wrapw.eindex = getVX( fp );
  2478. break;
  2479. case ID_WRPH:
  2480. tex->param.imap.wraph.val = getF4( fp );
  2481. tex->param.imap.wraph.eindex = getVX( fp );
  2482. break;
  2483. case ID_AAST:
  2484. tex->param.imap.aas_flags = getU2( fp );
  2485. tex->param.imap.aa_strength = getF4( fp );
  2486. break;
  2487. case ID_PIXB:
  2488. tex->param.imap.pblend = getU2( fp );
  2489. break;
  2490. case ID_STCK:
  2491. tex->param.imap.stck.val = getF4( fp );
  2492. tex->param.imap.stck.eindex = getVX( fp );
  2493. break;
  2494. case ID_TAMP:
  2495. tex->param.imap.amplitude.val = getF4( fp );
  2496. tex->param.imap.amplitude.eindex = getVX( fp );
  2497. break;
  2498. default:
  2499. break;
  2500. }
  2501. /* error while reading the current subchunk? */
  2502. rlen = get_flen();
  2503. if ( rlen < 0 || rlen > sz ) return 0;
  2504. /* skip unread parts of the current subchunk */
  2505. if ( rlen < sz )
  2506. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2507. /* end of the image map? */
  2508. if ( rsz <= fp->Tell() - pos )
  2509. break;
  2510. /* get the next subchunk header */
  2511. set_flen( 0 );
  2512. id = getU4( fp );
  2513. sz = getU2( fp );
  2514. if ( 6 != get_flen() ) return 0;
  2515. }
  2516. set_flen( fp->Tell() - pos );
  2517. return 1;
  2518. }
  2519. /*
  2520. ======================================================================
  2521. lwGetProcedural()
  2522. Read an lwProcedural from a SURF.BLOK in an LWO2 file.
  2523. ====================================================================== */
  2524. int lwGetProcedural( idFile *fp, int rsz, lwTexture *tex )
  2525. {
  2526. unsigned int id;
  2527. unsigned short sz;
  2528. int rlen, pos;
  2529. pos = fp->Tell();
  2530. id = getU4( fp );
  2531. sz = getU2( fp );
  2532. if ( 0 > get_flen() ) return 0;
  2533. while ( 1 ) {
  2534. sz += sz & 1;
  2535. set_flen( 0 );
  2536. switch ( id ) {
  2537. case ID_TMAP:
  2538. if ( !lwGetTMap( fp, sz, &tex->tmap )) return 0;
  2539. break;
  2540. case ID_AXIS:
  2541. tex->param.proc.axis = getU2( fp );
  2542. break;
  2543. case ID_VALU:
  2544. tex->param.proc.value[ 0 ] = getF4( fp );
  2545. if ( sz >= 8 ) tex->param.proc.value[ 1 ] = getF4( fp );
  2546. if ( sz >= 12 ) tex->param.proc.value[ 2 ] = getF4( fp );
  2547. break;
  2548. case ID_FUNC:
  2549. tex->param.proc.name = getS0( fp );
  2550. rlen = get_flen();
  2551. tex->param.proc.data = getbytes( fp, sz - rlen );
  2552. break;
  2553. default:
  2554. break;
  2555. }
  2556. /* error while reading the current subchunk? */
  2557. rlen = get_flen();
  2558. if ( rlen < 0 || rlen > sz ) return 0;
  2559. /* skip unread parts of the current subchunk */
  2560. if ( rlen < sz )
  2561. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2562. /* end of the procedural block? */
  2563. if ( rsz <= fp->Tell() - pos )
  2564. break;
  2565. /* get the next subchunk header */
  2566. set_flen( 0 );
  2567. id = getU4( fp );
  2568. sz = getU2( fp );
  2569. if ( 6 != get_flen() ) return 0;
  2570. }
  2571. set_flen( fp->Tell() - pos );
  2572. return 1;
  2573. }
  2574. /*
  2575. ======================================================================
  2576. lwGetGradient()
  2577. Read an lwGradient from a SURF.BLOK in an LWO2 file.
  2578. ====================================================================== */
  2579. int lwGetGradient( idFile *fp, int rsz, lwTexture *tex )
  2580. {
  2581. unsigned int id;
  2582. unsigned short sz;
  2583. int rlen, pos, i, j, nkeys;
  2584. pos = fp->Tell();
  2585. id = getU4( fp );
  2586. sz = getU2( fp );
  2587. if ( 0 > get_flen() ) return 0;
  2588. while ( 1 ) {
  2589. sz += sz & 1;
  2590. set_flen( 0 );
  2591. switch ( id ) {
  2592. case ID_TMAP:
  2593. if ( !lwGetTMap( fp, sz, &tex->tmap )) return 0;
  2594. break;
  2595. case ID_PNAM:
  2596. tex->param.grad.paramname = getS0( fp );
  2597. break;
  2598. case ID_INAM:
  2599. tex->param.grad.itemname = getS0( fp );
  2600. break;
  2601. case ID_GRST:
  2602. tex->param.grad.start = getF4( fp );
  2603. break;
  2604. case ID_GREN:
  2605. tex->param.grad.end = getF4( fp );
  2606. break;
  2607. case ID_GRPT:
  2608. tex->param.grad.repeat = getU2( fp );
  2609. break;
  2610. case ID_FKEY:
  2611. nkeys = sz / sizeof( lwGradKey );
  2612. tex->param.grad.key = (lwGradKey*)Mem_ClearedAlloc( nkeys * sizeof( lwGradKey ) );
  2613. if ( !tex->param.grad.key ) return 0;
  2614. for ( i = 0; i < nkeys; i++ ) {
  2615. tex->param.grad.key[ i ].value = getF4( fp );
  2616. for ( j = 0; j < 4; j++ )
  2617. tex->param.grad.key[ i ].rgba[ j ] = getF4( fp );
  2618. }
  2619. break;
  2620. case ID_IKEY:
  2621. nkeys = sz / 2;
  2622. tex->param.grad.ikey = (short*)Mem_ClearedAlloc( nkeys * sizeof( short ) );
  2623. if ( !tex->param.grad.ikey ) return 0;
  2624. for ( i = 0; i < nkeys; i++ )
  2625. tex->param.grad.ikey[ i ] = getU2( fp );
  2626. break;
  2627. default:
  2628. break;
  2629. }
  2630. /* error while reading the current subchunk? */
  2631. rlen = get_flen();
  2632. if ( rlen < 0 || rlen > sz ) return 0;
  2633. /* skip unread parts of the current subchunk */
  2634. if ( rlen < sz )
  2635. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2636. /* end of the gradient? */
  2637. if ( rsz <= fp->Tell() - pos )
  2638. break;
  2639. /* get the next subchunk header */
  2640. set_flen( 0 );
  2641. id = getU4( fp );
  2642. sz = getU2( fp );
  2643. if ( 6 != get_flen() ) return 0;
  2644. }
  2645. set_flen( fp->Tell() - pos );
  2646. return 1;
  2647. }
  2648. /*
  2649. ======================================================================
  2650. lwGetTexture()
  2651. Read an lwTexture from a SURF.BLOK in an LWO2 file.
  2652. ====================================================================== */
  2653. lwTexture *lwGetTexture( idFile *fp, int bloksz, unsigned int type )
  2654. {
  2655. lwTexture *tex;
  2656. unsigned short sz;
  2657. int ok;
  2658. tex = (lwTexture*)Mem_ClearedAlloc( sizeof( lwTexture ) );
  2659. if ( !tex ) return NULL;
  2660. tex->type = type;
  2661. tex->tmap.size.val[ 0 ] =
  2662. tex->tmap.size.val[ 1 ] =
  2663. tex->tmap.size.val[ 2 ] = 1.0f;
  2664. tex->opacity.val = 1.0f;
  2665. tex->enabled = 1;
  2666. sz = getU2( fp );
  2667. if ( !lwGetTHeader( fp, sz, tex )) {
  2668. Mem_Free( tex );
  2669. return NULL;
  2670. }
  2671. sz = bloksz - sz - 6;
  2672. switch ( type ) {
  2673. case ID_IMAP: ok = lwGetImageMap( fp, sz, tex ); break;
  2674. case ID_PROC: ok = lwGetProcedural( fp, sz, tex ); break;
  2675. case ID_GRAD: ok = lwGetGradient( fp, sz, tex ); break;
  2676. default:
  2677. ok = !fp->Seek( sz, FS_SEEK_CUR );
  2678. }
  2679. if ( !ok ) {
  2680. lwFreeTexture( tex );
  2681. return NULL;
  2682. }
  2683. set_flen( bloksz );
  2684. return tex;
  2685. }
  2686. /*
  2687. ======================================================================
  2688. lwGetShader()
  2689. Read a shader record from a SURF.BLOK in an LWO2 file.
  2690. ====================================================================== */
  2691. lwPlugin *lwGetShader( idFile *fp, int bloksz )
  2692. {
  2693. lwPlugin *shdr;
  2694. unsigned int id;
  2695. unsigned short sz;
  2696. int hsz, rlen, pos;
  2697. shdr = (lwPlugin*)Mem_ClearedAlloc( sizeof( lwPlugin ) );
  2698. if ( !shdr ) return NULL;
  2699. pos = fp->Tell();
  2700. set_flen( 0 );
  2701. hsz = getU2( fp );
  2702. shdr->ord = getS0( fp );
  2703. id = getU4( fp );
  2704. sz = getU2( fp );
  2705. if ( 0 > get_flen() ) goto Fail;
  2706. while ( hsz > 0 ) {
  2707. sz += sz & 1;
  2708. hsz -= sz;
  2709. if ( id == ID_ENAB ) {
  2710. shdr->flags = getU2( fp );
  2711. break;
  2712. }
  2713. else {
  2714. fp->Seek( sz, FS_SEEK_CUR );
  2715. id = getU4( fp );
  2716. sz = getU2( fp );
  2717. }
  2718. }
  2719. id = getU4( fp );
  2720. sz = getU2( fp );
  2721. if ( 0 > get_flen() ) goto Fail;
  2722. while ( 1 ) {
  2723. sz += sz & 1;
  2724. set_flen( 0 );
  2725. switch ( id ) {
  2726. case ID_FUNC:
  2727. shdr->name = getS0( fp );
  2728. rlen = get_flen();
  2729. shdr->data = getbytes( fp, sz - rlen );
  2730. break;
  2731. default:
  2732. break;
  2733. }
  2734. /* error while reading the current subchunk? */
  2735. rlen = get_flen();
  2736. if ( rlen < 0 || rlen > sz ) goto Fail;
  2737. /* skip unread parts of the current subchunk */
  2738. if ( rlen < sz )
  2739. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2740. /* end of the shader block? */
  2741. if ( bloksz <= fp->Tell() - pos )
  2742. break;
  2743. /* get the next subchunk header */
  2744. set_flen( 0 );
  2745. id = getU4( fp );
  2746. sz = getU2( fp );
  2747. if ( 6 != get_flen() ) goto Fail;
  2748. }
  2749. set_flen( fp->Tell() - pos );
  2750. return shdr;
  2751. Fail:
  2752. lwFreePlugin( shdr );
  2753. return NULL;
  2754. }
  2755. /*
  2756. ======================================================================
  2757. compare_textures()
  2758. compare_shaders()
  2759. Callbacks for the lwListInsert() function, which is called to add
  2760. textures to surface channels and shaders to surfaces.
  2761. ====================================================================== */
  2762. static int compare_textures( lwTexture *a, lwTexture *b )
  2763. {
  2764. return strcmp( a->ord, b->ord );
  2765. }
  2766. static int compare_shaders( lwPlugin *a, lwPlugin *b )
  2767. {
  2768. return strcmp( a->ord, b->ord );
  2769. }
  2770. /*
  2771. ======================================================================
  2772. add_texture()
  2773. Finds the surface channel (lwTParam or lwCParam) to which a texture is
  2774. applied, then calls lwListInsert().
  2775. ====================================================================== */
  2776. static int add_texture( lwSurface *surf, lwTexture *tex )
  2777. {
  2778. lwTexture **list;
  2779. switch ( tex->chan ) {
  2780. case ID_COLR: list = &surf->color.tex; break;
  2781. case ID_LUMI: list = &surf->luminosity.tex; break;
  2782. case ID_DIFF: list = &surf->diffuse.tex; break;
  2783. case ID_SPEC: list = &surf->specularity.tex; break;
  2784. case ID_GLOS: list = &surf->glossiness.tex; break;
  2785. case ID_REFL: list = &surf->reflection.val.tex; break;
  2786. case ID_TRAN: list = &surf->transparency.val.tex; break;
  2787. case ID_RIND: list = &surf->eta.tex; break;
  2788. case ID_TRNL: list = &surf->translucency.tex; break;
  2789. case ID_BUMP: list = &surf->bump.tex; break;
  2790. default: return 0;
  2791. }
  2792. lwListInsert( (void**)list, tex, (int (__cdecl *)(void *,void *))compare_textures );
  2793. return 1;
  2794. }
  2795. /*
  2796. ======================================================================
  2797. lwDefaultSurface()
  2798. Allocate and initialize a surface.
  2799. ====================================================================== */
  2800. lwSurface *lwDefaultSurface( void )
  2801. {
  2802. lwSurface *surf;
  2803. surf = (lwSurface*)Mem_ClearedAlloc( sizeof( lwSurface ) );
  2804. if ( !surf ) return NULL;
  2805. surf->color.rgb[ 0 ] = 0.78431f;
  2806. surf->color.rgb[ 1 ] = 0.78431f;
  2807. surf->color.rgb[ 2 ] = 0.78431f;
  2808. surf->diffuse.val = 1.0f;
  2809. surf->glossiness.val = 0.4f;
  2810. surf->bump.val = 1.0f;
  2811. surf->eta.val = 1.0f;
  2812. surf->sideflags = 1;
  2813. return surf;
  2814. }
  2815. /*
  2816. ======================================================================
  2817. lwGetSurface()
  2818. Read an lwSurface from an LWO2 file.
  2819. ====================================================================== */
  2820. lwSurface *lwGetSurface( idFile *fp, int cksize )
  2821. {
  2822. lwSurface *surf;
  2823. lwTexture *tex;
  2824. lwPlugin *shdr;
  2825. unsigned int id, type;
  2826. unsigned short sz;
  2827. int pos, rlen;
  2828. /* allocate the Surface structure */
  2829. surf = (lwSurface*)Mem_ClearedAlloc( sizeof( lwSurface ) );
  2830. if ( !surf ) goto Fail;
  2831. /* non-zero defaults */
  2832. surf->color.rgb[ 0 ] = 0.78431f;
  2833. surf->color.rgb[ 1 ] = 0.78431f;
  2834. surf->color.rgb[ 2 ] = 0.78431f;
  2835. surf->diffuse.val = 1.0f;
  2836. surf->glossiness.val = 0.4f;
  2837. surf->bump.val = 1.0f;
  2838. surf->eta.val = 1.0f;
  2839. surf->sideflags = 1;
  2840. /* remember where we started */
  2841. set_flen( 0 );
  2842. pos = fp->Tell();
  2843. /* names */
  2844. surf->name = getS0( fp );
  2845. surf->srcname = getS0( fp );
  2846. /* first subchunk header */
  2847. id = getU4( fp );
  2848. sz = getU2( fp );
  2849. if ( 0 > get_flen() ) goto Fail;
  2850. /* process subchunks as they're encountered */
  2851. while ( 1 ) {
  2852. sz += sz & 1;
  2853. set_flen( 0 );
  2854. switch ( id ) {
  2855. case ID_COLR:
  2856. surf->color.rgb[ 0 ] = getF4( fp );
  2857. surf->color.rgb[ 1 ] = getF4( fp );
  2858. surf->color.rgb[ 2 ] = getF4( fp );
  2859. surf->color.eindex = getVX( fp );
  2860. break;
  2861. case ID_LUMI:
  2862. surf->luminosity.val = getF4( fp );
  2863. surf->luminosity.eindex = getVX( fp );
  2864. break;
  2865. case ID_DIFF:
  2866. surf->diffuse.val = getF4( fp );
  2867. surf->diffuse.eindex = getVX( fp );
  2868. break;
  2869. case ID_SPEC:
  2870. surf->specularity.val = getF4( fp );
  2871. surf->specularity.eindex = getVX( fp );
  2872. break;
  2873. case ID_GLOS:
  2874. surf->glossiness.val = getF4( fp );
  2875. surf->glossiness.eindex = getVX( fp );
  2876. break;
  2877. case ID_REFL:
  2878. surf->reflection.val.val = getF4( fp );
  2879. surf->reflection.val.eindex = getVX( fp );
  2880. break;
  2881. case ID_RFOP:
  2882. surf->reflection.options = getU2( fp );
  2883. break;
  2884. case ID_RIMG:
  2885. surf->reflection.cindex = getVX( fp );
  2886. break;
  2887. case ID_RSAN:
  2888. surf->reflection.seam_angle = getF4( fp );
  2889. break;
  2890. case ID_TRAN:
  2891. surf->transparency.val.val = getF4( fp );
  2892. surf->transparency.val.eindex = getVX( fp );
  2893. break;
  2894. case ID_TROP:
  2895. surf->transparency.options = getU2( fp );
  2896. break;
  2897. case ID_TIMG:
  2898. surf->transparency.cindex = getVX( fp );
  2899. break;
  2900. case ID_RIND:
  2901. surf->eta.val = getF4( fp );
  2902. surf->eta.eindex = getVX( fp );
  2903. break;
  2904. case ID_TRNL:
  2905. surf->translucency.val = getF4( fp );
  2906. surf->translucency.eindex = getVX( fp );
  2907. break;
  2908. case ID_BUMP:
  2909. surf->bump.val = getF4( fp );
  2910. surf->bump.eindex = getVX( fp );
  2911. break;
  2912. case ID_SMAN:
  2913. surf->smooth = getF4( fp );
  2914. break;
  2915. case ID_SIDE:
  2916. surf->sideflags = getU2( fp );
  2917. break;
  2918. case ID_CLRH:
  2919. surf->color_hilite.val = getF4( fp );
  2920. surf->color_hilite.eindex = getVX( fp );
  2921. break;
  2922. case ID_CLRF:
  2923. surf->color_filter.val = getF4( fp );
  2924. surf->color_filter.eindex = getVX( fp );
  2925. break;
  2926. case ID_ADTR:
  2927. surf->add_trans.val = getF4( fp );
  2928. surf->add_trans.eindex = getVX( fp );
  2929. break;
  2930. case ID_SHRP:
  2931. surf->dif_sharp.val = getF4( fp );
  2932. surf->dif_sharp.eindex = getVX( fp );
  2933. break;
  2934. case ID_GVAL:
  2935. surf->glow.val = getF4( fp );
  2936. surf->glow.eindex = getVX( fp );
  2937. break;
  2938. case ID_LINE:
  2939. surf->line.enabled = 1;
  2940. if ( sz >= 2 ) surf->line.flags = getU2( fp );
  2941. if ( sz >= 6 ) surf->line.size.val = getF4( fp );
  2942. if ( sz >= 8 ) surf->line.size.eindex = getVX( fp );
  2943. break;
  2944. case ID_ALPH:
  2945. surf->alpha_mode = getU2( fp );
  2946. surf->alpha = getF4( fp );
  2947. break;
  2948. case ID_AVAL:
  2949. surf->alpha = getF4( fp );
  2950. break;
  2951. case ID_BLOK:
  2952. type = getU4( fp );
  2953. switch ( type ) {
  2954. case ID_IMAP:
  2955. case ID_PROC:
  2956. case ID_GRAD:
  2957. tex = lwGetTexture( fp, sz - 4, type );
  2958. if ( !tex ) goto Fail;
  2959. if ( !add_texture( surf, tex ))
  2960. lwFreeTexture( tex );
  2961. set_flen( 4 + get_flen() );
  2962. break;
  2963. case ID_SHDR:
  2964. shdr = lwGetShader( fp, sz - 4 );
  2965. if ( !shdr ) goto Fail;
  2966. lwListInsert( (void**)&surf->shader, shdr, (int (__cdecl *)(void *,void *))compare_shaders );
  2967. ++surf->nshaders;
  2968. set_flen( 4 + get_flen() );
  2969. break;
  2970. }
  2971. break;
  2972. default:
  2973. break;
  2974. }
  2975. /* error while reading current subchunk? */
  2976. rlen = get_flen();
  2977. if ( rlen < 0 || rlen > sz ) goto Fail;
  2978. /* skip unread parts of the current subchunk */
  2979. if ( rlen < sz )
  2980. fp->Seek( sz - rlen, FS_SEEK_CUR );
  2981. /* end of the SURF chunk? */
  2982. if ( cksize <= fp->Tell() - pos )
  2983. break;
  2984. /* get the next subchunk header */
  2985. set_flen( 0 );
  2986. id = getU4( fp );
  2987. sz = getU2( fp );
  2988. if ( 6 != get_flen() ) goto Fail;
  2989. }
  2990. return surf;
  2991. Fail:
  2992. if ( surf ) lwFreeSurface( surf );
  2993. return NULL;
  2994. }
  2995. float dot( float a[], float b[] )
  2996. {
  2997. return a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 1 ] + a[ 2 ] * b[ 2 ];
  2998. }
  2999. void cross( float a[], float b[], float c[] )
  3000. {
  3001. c[ 0 ] = a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ];
  3002. c[ 1 ] = a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ];
  3003. c[ 2 ] = a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ];
  3004. }
  3005. void normalize( float v[] )
  3006. {
  3007. float r;
  3008. r = ( float ) idMath::Sqrt( dot( v, v ));
  3009. if ( r > 0 ) {
  3010. v[ 0 ] /= r;
  3011. v[ 1 ] /= r;
  3012. v[ 2 ] /= r;
  3013. }
  3014. }
  3015. /*
  3016. ======================================================================
  3017. lwFreeVMap()
  3018. Free memory used by an lwVMap.
  3019. ====================================================================== */
  3020. void lwFreeVMap( lwVMap *vmap )
  3021. {
  3022. if ( vmap ) {
  3023. if ( vmap->name ) Mem_Free( vmap->name );
  3024. if ( vmap->vindex ) Mem_Free( vmap->vindex );
  3025. if ( vmap->pindex ) Mem_Free( vmap->pindex );
  3026. if ( vmap->val ) {
  3027. if ( vmap->val[ 0 ] ) Mem_Free( vmap->val[ 0 ] );
  3028. Mem_Free( vmap->val );
  3029. }
  3030. Mem_Free( vmap );
  3031. }
  3032. }
  3033. /*
  3034. ======================================================================
  3035. lwGetVMap()
  3036. Read an lwVMap from a VMAP or VMAD chunk in an LWO2.
  3037. ====================================================================== */
  3038. lwVMap *lwGetVMap( idFile *fp, int cksize, int ptoffset, int poloffset,
  3039. int perpoly )
  3040. {
  3041. unsigned char *buf, *bp;
  3042. lwVMap *vmap;
  3043. float *f;
  3044. int i, j, npts, rlen;
  3045. /* read the whole chunk */
  3046. set_flen( 0 );
  3047. buf = (unsigned char*)getbytes( fp, cksize );
  3048. if ( !buf ) return NULL;
  3049. vmap = (lwVMap*)Mem_ClearedAlloc( sizeof( lwVMap ) );
  3050. if ( !vmap ) {
  3051. Mem_Free( buf );
  3052. return NULL;
  3053. }
  3054. /* initialize the vmap */
  3055. vmap->perpoly = perpoly;
  3056. bp = buf;
  3057. set_flen( 0 );
  3058. vmap->type = sgetU4( &bp );
  3059. vmap->dim = sgetU2( &bp );
  3060. vmap->name = sgetS0( &bp );
  3061. rlen = get_flen();
  3062. /* count the vmap records */
  3063. npts = 0;
  3064. while ( bp < buf + cksize ) {
  3065. i = sgetVX( &bp );
  3066. if ( perpoly )
  3067. i = sgetVX( &bp );
  3068. bp += vmap->dim * sizeof( float );
  3069. ++npts;
  3070. }
  3071. /* allocate the vmap */
  3072. vmap->nverts = npts;
  3073. vmap->vindex = (int*)Mem_ClearedAlloc( npts * sizeof( int ) );
  3074. if ( !vmap->vindex ) goto Fail;
  3075. if ( perpoly ) {
  3076. vmap->pindex = (int*)Mem_ClearedAlloc( npts * sizeof( int ) );
  3077. if ( !vmap->pindex ) goto Fail;
  3078. }
  3079. if ( vmap->dim > 0 ) {
  3080. vmap->val = (float**)Mem_ClearedAlloc( npts * sizeof( float * ) );
  3081. if ( !vmap->val ) goto Fail;
  3082. f = (float*)Mem_ClearedAlloc( npts * vmap->dim * sizeof( float ) );
  3083. if ( !f ) goto Fail;
  3084. for ( i = 0; i < npts; i++ )
  3085. vmap->val[ i ] = f + i * vmap->dim;
  3086. }
  3087. /* fill in the vmap values */
  3088. bp = buf + rlen;
  3089. for ( i = 0; i < npts; i++ ) {
  3090. vmap->vindex[ i ] = sgetVX( &bp );
  3091. if ( perpoly )
  3092. vmap->pindex[ i ] = sgetVX( &bp );
  3093. for ( j = 0; j < vmap->dim; j++ )
  3094. vmap->val[ i ][ j ] = sgetF4( &bp );
  3095. }
  3096. Mem_Free( buf );
  3097. return vmap;
  3098. Fail:
  3099. if ( buf ) Mem_Free( buf );
  3100. lwFreeVMap( vmap );
  3101. return NULL;
  3102. }
  3103. /*
  3104. ======================================================================
  3105. lwGetPointVMaps()
  3106. Fill in the lwVMapPt structure for each point.
  3107. ====================================================================== */
  3108. int lwGetPointVMaps( lwPointList *point, lwVMap *vmap )
  3109. {
  3110. lwVMap *vm;
  3111. int i, j, n;
  3112. /* count the number of vmap values for each point */
  3113. vm = vmap;
  3114. while ( vm ) {
  3115. if ( !vm->perpoly )
  3116. for ( i = 0; i < vm->nverts; i++ )
  3117. ++point->pt[ vm->vindex[ i ]].nvmaps;
  3118. vm = vm->next;
  3119. }
  3120. /* allocate vmap references for each mapped point */
  3121. for ( i = 0; i < point->count; i++ ) {
  3122. if ( point->pt[ i ].nvmaps ) {
  3123. point->pt[ i ].vm = (lwVMapPt*)Mem_ClearedAlloc( point->pt[ i ].nvmaps * sizeof( lwVMapPt ) );
  3124. if ( !point->pt[ i ].vm ) return 0;
  3125. point->pt[ i ].nvmaps = 0;
  3126. }
  3127. }
  3128. /* fill in vmap references for each mapped point */
  3129. vm = vmap;
  3130. while ( vm ) {
  3131. if ( !vm->perpoly ) {
  3132. for ( i = 0; i < vm->nverts; i++ ) {
  3133. j = vm->vindex[ i ];
  3134. n = point->pt[ j ].nvmaps;
  3135. point->pt[ j ].vm[ n ].vmap = vm;
  3136. point->pt[ j ].vm[ n ].index = i;
  3137. ++point->pt[ j ].nvmaps;
  3138. }
  3139. }
  3140. vm = vm->next;
  3141. }
  3142. return 1;
  3143. }
  3144. /*
  3145. ======================================================================
  3146. lwGetPolyVMaps()
  3147. Fill in the lwVMapPt structure for each polygon vertex.
  3148. ====================================================================== */
  3149. int lwGetPolyVMaps( lwPolygonList *polygon, lwVMap *vmap )
  3150. {
  3151. lwVMap *vm;
  3152. lwPolVert *pv;
  3153. int i, j;
  3154. /* count the number of vmap values for each polygon vertex */
  3155. vm = vmap;
  3156. while ( vm ) {
  3157. if ( vm->perpoly ) {
  3158. for ( i = 0; i < vm->nverts; i++ ) {
  3159. for ( j = 0; j < polygon->pol[ vm->pindex[ i ]].nverts; j++ ) {
  3160. pv = &polygon->pol[ vm->pindex[ i ]].v[ j ];
  3161. if ( vm->vindex[ i ] == pv->index ) {
  3162. ++pv->nvmaps;
  3163. break;
  3164. }
  3165. }
  3166. }
  3167. }
  3168. vm = vm->next;
  3169. }
  3170. /* allocate vmap references for each mapped vertex */
  3171. for ( i = 0; i < polygon->count; i++ ) {
  3172. for ( j = 0; j < polygon->pol[ i ].nverts; j++ ) {
  3173. pv = &polygon->pol[ i ].v[ j ];
  3174. if ( pv->nvmaps ) {
  3175. pv->vm = (lwVMapPt*)Mem_ClearedAlloc( pv->nvmaps * sizeof( lwVMapPt ) );
  3176. if ( !pv->vm ) return 0;
  3177. pv->nvmaps = 0;
  3178. }
  3179. }
  3180. }
  3181. /* fill in vmap references for each mapped point */
  3182. vm = vmap;
  3183. while ( vm ) {
  3184. if ( vm->perpoly ) {
  3185. for ( i = 0; i < vm->nverts; i++ ) {
  3186. for ( j = 0; j < polygon->pol[ vm->pindex[ i ]].nverts; j++ ) {
  3187. pv = &polygon->pol[ vm->pindex[ i ]].v[ j ];
  3188. if ( vm->vindex[ i ] == pv->index ) {
  3189. pv->vm[ pv->nvmaps ].vmap = vm;
  3190. pv->vm[ pv->nvmaps ].index = i;
  3191. ++pv->nvmaps;
  3192. break;
  3193. }
  3194. }
  3195. }
  3196. }
  3197. vm = vm->next;
  3198. }
  3199. return 1;
  3200. }