model.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // models.c -- model loading and caching
  16. // models are the only shared resource between a client and server running
  17. // on the same machine.
  18. #include "quakedef.h"
  19. #include "r_local.h"
  20. model_t *loadmodel;
  21. char loadname[32]; // for hunk tags
  22. void Mod_LoadSpriteModel (model_t *mod, void *buffer);
  23. void Mod_LoadBrushModel (model_t *mod, void *buffer);
  24. void Mod_LoadAliasModel (model_t *mod, void *buffer);
  25. model_t *Mod_LoadModel (model_t *mod, qboolean crash);
  26. byte mod_novis[MAX_MAP_LEAFS/8];
  27. #define MAX_MOD_KNOWN 256
  28. model_t mod_known[MAX_MOD_KNOWN];
  29. int mod_numknown;
  30. /*
  31. ===============
  32. Mod_Init
  33. ===============
  34. */
  35. void Mod_Init (void)
  36. {
  37. memset (mod_novis, 0xff, sizeof(mod_novis));
  38. }
  39. /*
  40. ===============
  41. Mod_Init
  42. Caches the data if needed
  43. ===============
  44. */
  45. void *Mod_Extradata (model_t *mod)
  46. {
  47. void *r;
  48. r = Cache_Check (&mod->cache);
  49. if (r)
  50. return r;
  51. Mod_LoadModel (mod, true);
  52. if (!mod->cache.data)
  53. Sys_Error ("Mod_Extradata: caching failed");
  54. return mod->cache.data;
  55. }
  56. /*
  57. ===============
  58. Mod_PointInLeaf
  59. ===============
  60. */
  61. mleaf_t *Mod_PointInLeaf (vec3_t p, model_t *model)
  62. {
  63. mnode_t *node;
  64. float d;
  65. mplane_t *plane;
  66. if (!model || !model->nodes)
  67. Sys_Error ("Mod_PointInLeaf: bad model");
  68. node = model->nodes;
  69. while (1)
  70. {
  71. if (node->contents < 0)
  72. return (mleaf_t *)node;
  73. plane = node->plane;
  74. d = DotProduct (p,plane->normal) - plane->dist;
  75. if (d > 0)
  76. node = node->children[0];
  77. else
  78. node = node->children[1];
  79. }
  80. return NULL; // never reached
  81. }
  82. /*
  83. ===================
  84. Mod_DecompressVis
  85. ===================
  86. */
  87. byte *Mod_DecompressVis (byte *in, model_t *model)
  88. {
  89. static byte decompressed[MAX_MAP_LEAFS/8];
  90. int c;
  91. byte *out;
  92. int row;
  93. row = (model->numleafs+7)>>3;
  94. out = decompressed;
  95. #if 0
  96. memcpy (out, in, row);
  97. #else
  98. if (!in)
  99. { // no vis info, so make all visible
  100. while (row)
  101. {
  102. *out++ = 0xff;
  103. row--;
  104. }
  105. return decompressed;
  106. }
  107. do
  108. {
  109. if (*in)
  110. {
  111. *out++ = *in++;
  112. continue;
  113. }
  114. c = in[1];
  115. in += 2;
  116. while (c)
  117. {
  118. *out++ = 0;
  119. c--;
  120. }
  121. } while (out - decompressed < row);
  122. #endif
  123. return decompressed;
  124. }
  125. byte *Mod_LeafPVS (mleaf_t *leaf, model_t *model)
  126. {
  127. if (leaf == model->leafs)
  128. return mod_novis;
  129. return Mod_DecompressVis (leaf->compressed_vis, model);
  130. }
  131. /*
  132. ===================
  133. Mod_ClearAll
  134. ===================
  135. */
  136. void Mod_ClearAll (void)
  137. {
  138. int i;
  139. model_t *mod;
  140. for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
  141. if (mod->type != mod_alias)
  142. mod->needload = true;
  143. }
  144. /*
  145. ==================
  146. Mod_FindName
  147. ==================
  148. */
  149. model_t *Mod_FindName (char *name)
  150. {
  151. int i;
  152. model_t *mod;
  153. if (!name[0])
  154. Sys_Error ("Mod_ForName: NULL name");
  155. //
  156. // search the currently loaded models
  157. //
  158. for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
  159. if (!strcmp (mod->name, name) )
  160. break;
  161. if (i == mod_numknown)
  162. {
  163. if (mod_numknown == MAX_MOD_KNOWN)
  164. Sys_Error ("mod_numknown == MAX_MOD_KNOWN");
  165. strcpy (mod->name, name);
  166. mod->needload = true;
  167. mod_numknown++;
  168. }
  169. return mod;
  170. }
  171. /*
  172. ==================
  173. Mod_TouchModel
  174. ==================
  175. */
  176. void Mod_TouchModel (char *name)
  177. {
  178. model_t *mod;
  179. mod = Mod_FindName (name);
  180. if (!mod->needload)
  181. {
  182. if (mod->type == mod_alias)
  183. Cache_Check (&mod->cache);
  184. }
  185. }
  186. /*
  187. ==================
  188. Mod_LoadModel
  189. Loads a model into the cache
  190. ==================
  191. */
  192. model_t *Mod_LoadModel (model_t *mod, qboolean crash)
  193. {
  194. void *d;
  195. unsigned *buf;
  196. byte stackbuf[1024]; // avoid dirtying the cache heap
  197. if (!mod->needload)
  198. {
  199. if (mod->type == mod_alias)
  200. {
  201. d = Cache_Check (&mod->cache);
  202. if (d)
  203. return mod;
  204. }
  205. else
  206. return mod; // not cached at all
  207. }
  208. //
  209. // because the world is so huge, load it one piece at a time
  210. //
  211. if (!crash)
  212. {
  213. }
  214. //
  215. // load the file
  216. //
  217. buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf));
  218. if (!buf)
  219. {
  220. if (crash)
  221. Sys_Error ("Mod_NumForName: %s not found", mod->name);
  222. return NULL;
  223. }
  224. //
  225. // allocate a new model
  226. //
  227. COM_FileBase (mod->name, loadname);
  228. loadmodel = mod;
  229. //
  230. // fill it in
  231. //
  232. // call the apropriate loader
  233. mod->needload = false;
  234. switch (LittleLong(*(unsigned *)buf))
  235. {
  236. case IDPOLYHEADER:
  237. Mod_LoadAliasModel (mod, buf);
  238. break;
  239. case IDSPRITEHEADER:
  240. Mod_LoadSpriteModel (mod, buf);
  241. break;
  242. default:
  243. Mod_LoadBrushModel (mod, buf);
  244. break;
  245. }
  246. return mod;
  247. }
  248. /*
  249. ==================
  250. Mod_ForName
  251. Loads in a model for the given name
  252. ==================
  253. */
  254. model_t *Mod_ForName (char *name, qboolean crash)
  255. {
  256. model_t *mod;
  257. mod = Mod_FindName (name);
  258. return Mod_LoadModel (mod, crash);
  259. }
  260. /*
  261. ===============================================================================
  262. BRUSHMODEL LOADING
  263. ===============================================================================
  264. */
  265. byte *mod_base;
  266. /*
  267. =================
  268. Mod_LoadTextures
  269. =================
  270. */
  271. void Mod_LoadTextures (lump_t *l)
  272. {
  273. int i, j, pixels, num, max, altmax;
  274. miptex_t *mt;
  275. texture_t *tx, *tx2;
  276. texture_t *anims[10];
  277. texture_t *altanims[10];
  278. dmiptexlump_t *m;
  279. if (!l->filelen)
  280. {
  281. loadmodel->textures = NULL;
  282. return;
  283. }
  284. m = (dmiptexlump_t *)(mod_base + l->fileofs);
  285. m->nummiptex = LittleLong (m->nummiptex);
  286. loadmodel->numtextures = m->nummiptex;
  287. loadmodel->textures = Hunk_AllocName (m->nummiptex * sizeof(*loadmodel->textures) , loadname);
  288. for (i=0 ; i<m->nummiptex ; i++)
  289. {
  290. m->dataofs[i] = LittleLong(m->dataofs[i]);
  291. if (m->dataofs[i] == -1)
  292. continue;
  293. mt = (miptex_t *)((byte *)m + m->dataofs[i]);
  294. mt->width = LittleLong (mt->width);
  295. mt->height = LittleLong (mt->height);
  296. for (j=0 ; j<MIPLEVELS ; j++)
  297. mt->offsets[j] = LittleLong (mt->offsets[j]);
  298. if ( (mt->width & 15) || (mt->height & 15) )
  299. Sys_Error ("Texture %s is not 16 aligned", mt->name);
  300. pixels = mt->width*mt->height/64*85;
  301. tx = Hunk_AllocName (sizeof(texture_t) +pixels, loadname );
  302. loadmodel->textures[i] = tx;
  303. memcpy (tx->name, mt->name, sizeof(tx->name));
  304. tx->width = mt->width;
  305. tx->height = mt->height;
  306. for (j=0 ; j<MIPLEVELS ; j++)
  307. tx->offsets[j] = mt->offsets[j] + sizeof(texture_t) - sizeof(miptex_t);
  308. // the pixels immediately follow the structures
  309. memcpy ( tx+1, mt+1, pixels);
  310. if (!Q_strncmp(mt->name,"sky",3))
  311. R_InitSky (tx);
  312. }
  313. //
  314. // sequence the animations
  315. //
  316. for (i=0 ; i<m->nummiptex ; i++)
  317. {
  318. tx = loadmodel->textures[i];
  319. if (!tx || tx->name[0] != '+')
  320. continue;
  321. if (tx->anim_next)
  322. continue; // allready sequenced
  323. // find the number of frames in the animation
  324. memset (anims, 0, sizeof(anims));
  325. memset (altanims, 0, sizeof(altanims));
  326. max = tx->name[1];
  327. altmax = 0;
  328. if (max >= 'a' && max <= 'z')
  329. max -= 'a' - 'A';
  330. if (max >= '0' && max <= '9')
  331. {
  332. max -= '0';
  333. altmax = 0;
  334. anims[max] = tx;
  335. max++;
  336. }
  337. else if (max >= 'A' && max <= 'J')
  338. {
  339. altmax = max - 'A';
  340. max = 0;
  341. altanims[altmax] = tx;
  342. altmax++;
  343. }
  344. else
  345. Sys_Error ("Bad animating texture %s", tx->name);
  346. for (j=i+1 ; j<m->nummiptex ; j++)
  347. {
  348. tx2 = loadmodel->textures[j];
  349. if (!tx2 || tx2->name[0] != '+')
  350. continue;
  351. if (strcmp (tx2->name+2, tx->name+2))
  352. continue;
  353. num = tx2->name[1];
  354. if (num >= 'a' && num <= 'z')
  355. num -= 'a' - 'A';
  356. if (num >= '0' && num <= '9')
  357. {
  358. num -= '0';
  359. anims[num] = tx2;
  360. if (num+1 > max)
  361. max = num + 1;
  362. }
  363. else if (num >= 'A' && num <= 'J')
  364. {
  365. num = num - 'A';
  366. altanims[num] = tx2;
  367. if (num+1 > altmax)
  368. altmax = num+1;
  369. }
  370. else
  371. Sys_Error ("Bad animating texture %s", tx->name);
  372. }
  373. #define ANIM_CYCLE 2
  374. // link them all together
  375. for (j=0 ; j<max ; j++)
  376. {
  377. tx2 = anims[j];
  378. if (!tx2)
  379. Sys_Error ("Missing frame %i of %s",j, tx->name);
  380. tx2->anim_total = max * ANIM_CYCLE;
  381. tx2->anim_min = j * ANIM_CYCLE;
  382. tx2->anim_max = (j+1) * ANIM_CYCLE;
  383. tx2->anim_next = anims[ (j+1)%max ];
  384. if (altmax)
  385. tx2->alternate_anims = altanims[0];
  386. }
  387. for (j=0 ; j<altmax ; j++)
  388. {
  389. tx2 = altanims[j];
  390. if (!tx2)
  391. Sys_Error ("Missing frame %i of %s",j, tx->name);
  392. tx2->anim_total = altmax * ANIM_CYCLE;
  393. tx2->anim_min = j * ANIM_CYCLE;
  394. tx2->anim_max = (j+1) * ANIM_CYCLE;
  395. tx2->anim_next = altanims[ (j+1)%altmax ];
  396. if (max)
  397. tx2->alternate_anims = anims[0];
  398. }
  399. }
  400. }
  401. /*
  402. =================
  403. Mod_LoadLighting
  404. =================
  405. */
  406. void Mod_LoadLighting (lump_t *l)
  407. {
  408. if (!l->filelen)
  409. {
  410. loadmodel->lightdata = NULL;
  411. return;
  412. }
  413. loadmodel->lightdata = Hunk_AllocName ( l->filelen, loadname);
  414. memcpy (loadmodel->lightdata, mod_base + l->fileofs, l->filelen);
  415. }
  416. /*
  417. =================
  418. Mod_LoadVisibility
  419. =================
  420. */
  421. void Mod_LoadVisibility (lump_t *l)
  422. {
  423. if (!l->filelen)
  424. {
  425. loadmodel->visdata = NULL;
  426. return;
  427. }
  428. loadmodel->visdata = Hunk_AllocName ( l->filelen, loadname);
  429. memcpy (loadmodel->visdata, mod_base + l->fileofs, l->filelen);
  430. }
  431. /*
  432. =================
  433. Mod_LoadEntities
  434. =================
  435. */
  436. void Mod_LoadEntities (lump_t *l)
  437. {
  438. if (!l->filelen)
  439. {
  440. loadmodel->entities = NULL;
  441. return;
  442. }
  443. loadmodel->entities = Hunk_AllocName ( l->filelen, loadname);
  444. memcpy (loadmodel->entities, mod_base + l->fileofs, l->filelen);
  445. }
  446. /*
  447. =================
  448. Mod_LoadVertexes
  449. =================
  450. */
  451. void Mod_LoadVertexes (lump_t *l)
  452. {
  453. dvertex_t *in;
  454. mvertex_t *out;
  455. int i, count;
  456. in = (void *)(mod_base + l->fileofs);
  457. if (l->filelen % sizeof(*in))
  458. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  459. count = l->filelen / sizeof(*in);
  460. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  461. loadmodel->vertexes = out;
  462. loadmodel->numvertexes = count;
  463. for ( i=0 ; i<count ; i++, in++, out++)
  464. {
  465. out->position[0] = LittleFloat (in->point[0]);
  466. out->position[1] = LittleFloat (in->point[1]);
  467. out->position[2] = LittleFloat (in->point[2]);
  468. }
  469. }
  470. /*
  471. =================
  472. Mod_LoadSubmodels
  473. =================
  474. */
  475. void Mod_LoadSubmodels (lump_t *l)
  476. {
  477. dmodel_t *in;
  478. dmodel_t *out;
  479. int i, j, count;
  480. in = (void *)(mod_base + l->fileofs);
  481. if (l->filelen % sizeof(*in))
  482. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  483. count = l->filelen / sizeof(*in);
  484. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  485. loadmodel->submodels = out;
  486. loadmodel->numsubmodels = count;
  487. for ( i=0 ; i<count ; i++, in++, out++)
  488. {
  489. for (j=0 ; j<3 ; j++)
  490. { // spread the mins / maxs by a pixel
  491. out->mins[j] = LittleFloat (in->mins[j]) - 1;
  492. out->maxs[j] = LittleFloat (in->maxs[j]) + 1;
  493. out->origin[j] = LittleFloat (in->origin[j]);
  494. }
  495. for (j=0 ; j<MAX_MAP_HULLS ; j++)
  496. out->headnode[j] = LittleLong (in->headnode[j]);
  497. out->visleafs = LittleLong (in->visleafs);
  498. out->firstface = LittleLong (in->firstface);
  499. out->numfaces = LittleLong (in->numfaces);
  500. }
  501. }
  502. /*
  503. =================
  504. Mod_LoadEdges
  505. =================
  506. */
  507. void Mod_LoadEdges (lump_t *l)
  508. {
  509. dedge_t *in;
  510. medge_t *out;
  511. int i, count;
  512. in = (void *)(mod_base + l->fileofs);
  513. if (l->filelen % sizeof(*in))
  514. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  515. count = l->filelen / sizeof(*in);
  516. out = Hunk_AllocName ( (count + 1) * sizeof(*out), loadname);
  517. loadmodel->edges = out;
  518. loadmodel->numedges = count;
  519. for ( i=0 ; i<count ; i++, in++, out++)
  520. {
  521. out->v[0] = (unsigned short)LittleShort(in->v[0]);
  522. out->v[1] = (unsigned short)LittleShort(in->v[1]);
  523. }
  524. }
  525. /*
  526. =================
  527. Mod_LoadTexinfo
  528. =================
  529. */
  530. void Mod_LoadTexinfo (lump_t *l)
  531. {
  532. texinfo_t *in;
  533. mtexinfo_t *out;
  534. int i, j, count;
  535. int miptex;
  536. float len1, len2;
  537. in = (void *)(mod_base + l->fileofs);
  538. if (l->filelen % sizeof(*in))
  539. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  540. count = l->filelen / sizeof(*in);
  541. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  542. loadmodel->texinfo = out;
  543. loadmodel->numtexinfo = count;
  544. for ( i=0 ; i<count ; i++, in++, out++)
  545. {
  546. for (j=0 ; j<8 ; j++)
  547. out->vecs[0][j] = LittleFloat (in->vecs[0][j]);
  548. len1 = Length (out->vecs[0]);
  549. len2 = Length (out->vecs[1]);
  550. len1 = (len1 + len2)/2;
  551. if (len1 < 0.32)
  552. out->mipadjust = 4;
  553. else if (len1 < 0.49)
  554. out->mipadjust = 3;
  555. else if (len1 < 0.99)
  556. out->mipadjust = 2;
  557. else
  558. out->mipadjust = 1;
  559. #if 0
  560. if (len1 + len2 < 0.001)
  561. out->mipadjust = 1; // don't crash
  562. else
  563. out->mipadjust = 1 / floor( (len1+len2)/2 + 0.1 );
  564. #endif
  565. miptex = LittleLong (in->miptex);
  566. out->flags = LittleLong (in->flags);
  567. if (!loadmodel->textures)
  568. {
  569. out->texture = r_notexture_mip; // checkerboard texture
  570. out->flags = 0;
  571. }
  572. else
  573. {
  574. if (miptex >= loadmodel->numtextures)
  575. Sys_Error ("miptex >= loadmodel->numtextures");
  576. out->texture = loadmodel->textures[miptex];
  577. if (!out->texture)
  578. {
  579. out->texture = r_notexture_mip; // texture not found
  580. out->flags = 0;
  581. }
  582. }
  583. }
  584. }
  585. /*
  586. ================
  587. CalcSurfaceExtents
  588. Fills in s->texturemins[] and s->extents[]
  589. ================
  590. */
  591. void CalcSurfaceExtents (msurface_t *s)
  592. {
  593. float mins[2], maxs[2], val;
  594. int i,j, e;
  595. mvertex_t *v;
  596. mtexinfo_t *tex;
  597. int bmins[2], bmaxs[2];
  598. mins[0] = mins[1] = 999999;
  599. maxs[0] = maxs[1] = -99999;
  600. tex = s->texinfo;
  601. for (i=0 ; i<s->numedges ; i++)
  602. {
  603. e = loadmodel->surfedges[s->firstedge+i];
  604. if (e >= 0)
  605. v = &loadmodel->vertexes[loadmodel->edges[e].v[0]];
  606. else
  607. v = &loadmodel->vertexes[loadmodel->edges[-e].v[1]];
  608. for (j=0 ; j<2 ; j++)
  609. {
  610. val = v->position[0] * tex->vecs[j][0] +
  611. v->position[1] * tex->vecs[j][1] +
  612. v->position[2] * tex->vecs[j][2] +
  613. tex->vecs[j][3];
  614. if (val < mins[j])
  615. mins[j] = val;
  616. if (val > maxs[j])
  617. maxs[j] = val;
  618. }
  619. }
  620. for (i=0 ; i<2 ; i++)
  621. {
  622. bmins[i] = floor(mins[i]/16);
  623. bmaxs[i] = ceil(maxs[i]/16);
  624. s->texturemins[i] = bmins[i] * 16;
  625. s->extents[i] = (bmaxs[i] - bmins[i]) * 16;
  626. if ( !(tex->flags & TEX_SPECIAL) && s->extents[i] > 256)
  627. Sys_Error ("Bad surface extents");
  628. }
  629. }
  630. /*
  631. =================
  632. Mod_LoadFaces
  633. =================
  634. */
  635. void Mod_LoadFaces (lump_t *l)
  636. {
  637. dface_t *in;
  638. msurface_t *out;
  639. int i, count, surfnum;
  640. int planenum, side;
  641. in = (void *)(mod_base + l->fileofs);
  642. if (l->filelen % sizeof(*in))
  643. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  644. count = l->filelen / sizeof(*in);
  645. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  646. loadmodel->surfaces = out;
  647. loadmodel->numsurfaces = count;
  648. for ( surfnum=0 ; surfnum<count ; surfnum++, in++, out++)
  649. {
  650. out->firstedge = LittleLong(in->firstedge);
  651. out->numedges = LittleShort(in->numedges);
  652. out->flags = 0;
  653. planenum = LittleShort(in->planenum);
  654. side = LittleShort(in->side);
  655. if (side)
  656. out->flags |= SURF_PLANEBACK;
  657. out->plane = loadmodel->planes + planenum;
  658. out->texinfo = loadmodel->texinfo + LittleShort (in->texinfo);
  659. CalcSurfaceExtents (out);
  660. // lighting info
  661. for (i=0 ; i<MAXLIGHTMAPS ; i++)
  662. out->styles[i] = in->styles[i];
  663. i = LittleLong(in->lightofs);
  664. if (i == -1)
  665. out->samples = NULL;
  666. else
  667. out->samples = loadmodel->lightdata + i;
  668. // set the drawing flags flag
  669. if (!Q_strncmp(out->texinfo->texture->name,"sky",3)) // sky
  670. {
  671. out->flags |= (SURF_DRAWSKY | SURF_DRAWTILED);
  672. continue;
  673. }
  674. if (!Q_strncmp(out->texinfo->texture->name,"*",1)) // turbulent
  675. {
  676. out->flags |= (SURF_DRAWTURB | SURF_DRAWTILED);
  677. for (i=0 ; i<2 ; i++)
  678. {
  679. out->extents[i] = 16384;
  680. out->texturemins[i] = -8192;
  681. }
  682. continue;
  683. }
  684. }
  685. }
  686. /*
  687. =================
  688. Mod_SetParent
  689. =================
  690. */
  691. void Mod_SetParent (mnode_t *node, mnode_t *parent)
  692. {
  693. node->parent = parent;
  694. if (node->contents < 0)
  695. return;
  696. Mod_SetParent (node->children[0], node);
  697. Mod_SetParent (node->children[1], node);
  698. }
  699. /*
  700. =================
  701. Mod_LoadNodes
  702. =================
  703. */
  704. void Mod_LoadNodes (lump_t *l)
  705. {
  706. int i, j, count, p;
  707. dnode_t *in;
  708. mnode_t *out;
  709. in = (void *)(mod_base + l->fileofs);
  710. if (l->filelen % sizeof(*in))
  711. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  712. count = l->filelen / sizeof(*in);
  713. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  714. loadmodel->nodes = out;
  715. loadmodel->numnodes = count;
  716. for ( i=0 ; i<count ; i++, in++, out++)
  717. {
  718. for (j=0 ; j<3 ; j++)
  719. {
  720. out->minmaxs[j] = LittleShort (in->mins[j]);
  721. out->minmaxs[3+j] = LittleShort (in->maxs[j]);
  722. }
  723. p = LittleLong(in->planenum);
  724. out->plane = loadmodel->planes + p;
  725. out->firstsurface = LittleShort (in->firstface);
  726. out->numsurfaces = LittleShort (in->numfaces);
  727. for (j=0 ; j<2 ; j++)
  728. {
  729. p = LittleShort (in->children[j]);
  730. if (p >= 0)
  731. out->children[j] = loadmodel->nodes + p;
  732. else
  733. out->children[j] = (mnode_t *)(loadmodel->leafs + (-1 - p));
  734. }
  735. }
  736. Mod_SetParent (loadmodel->nodes, NULL); // sets nodes and leafs
  737. }
  738. /*
  739. =================
  740. Mod_LoadLeafs
  741. =================
  742. */
  743. void Mod_LoadLeafs (lump_t *l)
  744. {
  745. dleaf_t *in;
  746. mleaf_t *out;
  747. int i, j, count, p;
  748. in = (void *)(mod_base + l->fileofs);
  749. if (l->filelen % sizeof(*in))
  750. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  751. count = l->filelen / sizeof(*in);
  752. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  753. loadmodel->leafs = out;
  754. loadmodel->numleafs = count;
  755. for ( i=0 ; i<count ; i++, in++, out++)
  756. {
  757. for (j=0 ; j<3 ; j++)
  758. {
  759. out->minmaxs[j] = LittleShort (in->mins[j]);
  760. out->minmaxs[3+j] = LittleShort (in->maxs[j]);
  761. }
  762. p = LittleLong(in->contents);
  763. out->contents = p;
  764. out->firstmarksurface = loadmodel->marksurfaces +
  765. LittleShort(in->firstmarksurface);
  766. out->nummarksurfaces = LittleShort(in->nummarksurfaces);
  767. p = LittleLong(in->visofs);
  768. if (p == -1)
  769. out->compressed_vis = NULL;
  770. else
  771. out->compressed_vis = loadmodel->visdata + p;
  772. out->efrags = NULL;
  773. for (j=0 ; j<4 ; j++)
  774. out->ambient_sound_level[j] = in->ambient_level[j];
  775. }
  776. }
  777. /*
  778. =================
  779. Mod_LoadClipnodes
  780. =================
  781. */
  782. void Mod_LoadClipnodes (lump_t *l)
  783. {
  784. dclipnode_t *in, *out;
  785. int i, count;
  786. hull_t *hull;
  787. in = (void *)(mod_base + l->fileofs);
  788. if (l->filelen % sizeof(*in))
  789. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  790. count = l->filelen / sizeof(*in);
  791. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  792. loadmodel->clipnodes = out;
  793. loadmodel->numclipnodes = count;
  794. hull = &loadmodel->hulls[1];
  795. hull->clipnodes = out;
  796. hull->firstclipnode = 0;
  797. hull->lastclipnode = count-1;
  798. hull->planes = loadmodel->planes;
  799. hull->clip_mins[0] = -16;
  800. hull->clip_mins[1] = -16;
  801. hull->clip_mins[2] = -24;
  802. hull->clip_maxs[0] = 16;
  803. hull->clip_maxs[1] = 16;
  804. hull->clip_maxs[2] = 32;
  805. hull = &loadmodel->hulls[2];
  806. hull->clipnodes = out;
  807. hull->firstclipnode = 0;
  808. hull->lastclipnode = count-1;
  809. hull->planes = loadmodel->planes;
  810. hull->clip_mins[0] = -32;
  811. hull->clip_mins[1] = -32;
  812. hull->clip_mins[2] = -24;
  813. hull->clip_maxs[0] = 32;
  814. hull->clip_maxs[1] = 32;
  815. hull->clip_maxs[2] = 64;
  816. for (i=0 ; i<count ; i++, out++, in++)
  817. {
  818. out->planenum = LittleLong(in->planenum);
  819. out->children[0] = LittleShort(in->children[0]);
  820. out->children[1] = LittleShort(in->children[1]);
  821. }
  822. }
  823. /*
  824. =================
  825. Mod_MakeHull0
  826. Deplicate the drawing hull structure as a clipping hull
  827. =================
  828. */
  829. void Mod_MakeHull0 (void)
  830. {
  831. mnode_t *in, *child;
  832. dclipnode_t *out;
  833. int i, j, count;
  834. hull_t *hull;
  835. hull = &loadmodel->hulls[0];
  836. in = loadmodel->nodes;
  837. count = loadmodel->numnodes;
  838. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  839. hull->clipnodes = out;
  840. hull->firstclipnode = 0;
  841. hull->lastclipnode = count-1;
  842. hull->planes = loadmodel->planes;
  843. for (i=0 ; i<count ; i++, out++, in++)
  844. {
  845. out->planenum = in->plane - loadmodel->planes;
  846. for (j=0 ; j<2 ; j++)
  847. {
  848. child = in->children[j];
  849. if (child->contents < 0)
  850. out->children[j] = child->contents;
  851. else
  852. out->children[j] = child - loadmodel->nodes;
  853. }
  854. }
  855. }
  856. /*
  857. =================
  858. Mod_LoadMarksurfaces
  859. =================
  860. */
  861. void Mod_LoadMarksurfaces (lump_t *l)
  862. {
  863. int i, j, count;
  864. short *in;
  865. msurface_t **out;
  866. in = (void *)(mod_base + l->fileofs);
  867. if (l->filelen % sizeof(*in))
  868. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  869. count = l->filelen / sizeof(*in);
  870. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  871. loadmodel->marksurfaces = out;
  872. loadmodel->nummarksurfaces = count;
  873. for ( i=0 ; i<count ; i++)
  874. {
  875. j = LittleShort(in[i]);
  876. if (j >= loadmodel->numsurfaces)
  877. Sys_Error ("Mod_ParseMarksurfaces: bad surface number");
  878. out[i] = loadmodel->surfaces + j;
  879. }
  880. }
  881. /*
  882. =================
  883. Mod_LoadSurfedges
  884. =================
  885. */
  886. void Mod_LoadSurfedges (lump_t *l)
  887. {
  888. int i, count;
  889. int *in, *out;
  890. in = (void *)(mod_base + l->fileofs);
  891. if (l->filelen % sizeof(*in))
  892. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  893. count = l->filelen / sizeof(*in);
  894. out = Hunk_AllocName ( count*sizeof(*out), loadname);
  895. loadmodel->surfedges = out;
  896. loadmodel->numsurfedges = count;
  897. for ( i=0 ; i<count ; i++)
  898. out[i] = LittleLong (in[i]);
  899. }
  900. /*
  901. =================
  902. Mod_LoadPlanes
  903. =================
  904. */
  905. void Mod_LoadPlanes (lump_t *l)
  906. {
  907. int i, j;
  908. mplane_t *out;
  909. dplane_t *in;
  910. int count;
  911. int bits;
  912. in = (void *)(mod_base + l->fileofs);
  913. if (l->filelen % sizeof(*in))
  914. Sys_Error ("MOD_LoadBmodel: funny lump size in %s",loadmodel->name);
  915. count = l->filelen / sizeof(*in);
  916. out = Hunk_AllocName ( count*2*sizeof(*out), loadname);
  917. loadmodel->planes = out;
  918. loadmodel->numplanes = count;
  919. for ( i=0 ; i<count ; i++, in++, out++)
  920. {
  921. bits = 0;
  922. for (j=0 ; j<3 ; j++)
  923. {
  924. out->normal[j] = LittleFloat (in->normal[j]);
  925. if (out->normal[j] < 0)
  926. bits |= 1<<j;
  927. }
  928. out->dist = LittleFloat (in->dist);
  929. out->type = LittleLong (in->type);
  930. out->signbits = bits;
  931. }
  932. }
  933. /*
  934. =================
  935. RadiusFromBounds
  936. =================
  937. */
  938. float RadiusFromBounds (vec3_t mins, vec3_t maxs)
  939. {
  940. int i;
  941. vec3_t corner;
  942. for (i=0 ; i<3 ; i++)
  943. {
  944. corner[i] = fabs(mins[i]) > fabs(maxs[i]) ? fabs(mins[i]) : fabs(maxs[i]);
  945. }
  946. return Length (corner);
  947. }
  948. /*
  949. =================
  950. Mod_LoadBrushModel
  951. =================
  952. */
  953. void Mod_LoadBrushModel (model_t *mod, void *buffer)
  954. {
  955. int i, j;
  956. dheader_t *header;
  957. dmodel_t *bm;
  958. loadmodel->type = mod_brush;
  959. header = (dheader_t *)buffer;
  960. i = LittleLong (header->version);
  961. if (i != BSPVERSION)
  962. Sys_Error ("Mod_LoadBrushModel: %s has wrong version number (%i should be %i)", mod->name, i, BSPVERSION);
  963. // swap all the lumps
  964. mod_base = (byte *)header;
  965. for (i=0 ; i<sizeof(dheader_t)/4 ; i++)
  966. ((int *)header)[i] = LittleLong ( ((int *)header)[i]);
  967. mod->checksum = 0;
  968. mod->checksum2 = 0;
  969. // checksum all of the map, except for entities
  970. for (i = 0; i < HEADER_LUMPS; i++) {
  971. if (i == LUMP_ENTITIES)
  972. continue;
  973. mod->checksum ^= Com_BlockChecksum(mod_base + header->lumps[i].fileofs,
  974. header->lumps[i].filelen);
  975. if (i == LUMP_VISIBILITY || i == LUMP_LEAFS || i == LUMP_NODES)
  976. continue;
  977. mod->checksum2 ^= Com_BlockChecksum(mod_base + header->lumps[i].fileofs,
  978. header->lumps[i].filelen);
  979. }
  980. // load into heap
  981. Mod_LoadVertexes (&header->lumps[LUMP_VERTEXES]);
  982. Mod_LoadEdges (&header->lumps[LUMP_EDGES]);
  983. Mod_LoadSurfedges (&header->lumps[LUMP_SURFEDGES]);
  984. Mod_LoadTextures (&header->lumps[LUMP_TEXTURES]);
  985. Mod_LoadLighting (&header->lumps[LUMP_LIGHTING]);
  986. Mod_LoadPlanes (&header->lumps[LUMP_PLANES]);
  987. Mod_LoadTexinfo (&header->lumps[LUMP_TEXINFO]);
  988. Mod_LoadFaces (&header->lumps[LUMP_FACES]);
  989. Mod_LoadMarksurfaces (&header->lumps[LUMP_MARKSURFACES]);
  990. Mod_LoadVisibility (&header->lumps[LUMP_VISIBILITY]);
  991. Mod_LoadLeafs (&header->lumps[LUMP_LEAFS]);
  992. Mod_LoadNodes (&header->lumps[LUMP_NODES]);
  993. Mod_LoadClipnodes (&header->lumps[LUMP_CLIPNODES]);
  994. Mod_LoadEntities (&header->lumps[LUMP_ENTITIES]);
  995. Mod_LoadSubmodels (&header->lumps[LUMP_MODELS]);
  996. Mod_MakeHull0 ();
  997. mod->numframes = 2; // regular and alternate animation
  998. //
  999. // set up the submodels (FIXME: this is confusing)
  1000. //
  1001. for (i=0 ; i<mod->numsubmodels ; i++)
  1002. {
  1003. bm = &mod->submodels[i];
  1004. mod->hulls[0].firstclipnode = bm->headnode[0];
  1005. for (j=1 ; j<MAX_MAP_HULLS ; j++)
  1006. {
  1007. mod->hulls[j].firstclipnode = bm->headnode[j];
  1008. mod->hulls[j].lastclipnode = mod->numclipnodes-1;
  1009. }
  1010. mod->firstmodelsurface = bm->firstface;
  1011. mod->nummodelsurfaces = bm->numfaces;
  1012. mod->radius = RadiusFromBounds (mod->mins, mod->maxs);
  1013. VectorCopy (bm->maxs, mod->maxs);
  1014. VectorCopy (bm->mins, mod->mins);
  1015. mod->numleafs = bm->visleafs;
  1016. if (i < mod->numsubmodels-1)
  1017. { // duplicate the basic information
  1018. char name[10];
  1019. sprintf (name, "*%i", i+1);
  1020. loadmodel = Mod_FindName (name);
  1021. *loadmodel = *mod;
  1022. strcpy (loadmodel->name, name);
  1023. mod = loadmodel;
  1024. }
  1025. }
  1026. }
  1027. /*
  1028. ==============================================================================
  1029. ALIAS MODELS
  1030. ==============================================================================
  1031. */
  1032. /*
  1033. =================
  1034. Mod_LoadAliasFrame
  1035. =================
  1036. */
  1037. void * Mod_LoadAliasFrame (void * pin, int *pframeindex, int numv,
  1038. trivertx_t *pbboxmin, trivertx_t *pbboxmax, aliashdr_t *pheader, char *name)
  1039. {
  1040. trivertx_t *pframe, *pinframe;
  1041. int i, j;
  1042. daliasframe_t *pdaliasframe;
  1043. pdaliasframe = (daliasframe_t *)pin;
  1044. strcpy (name, pdaliasframe->name);
  1045. for (i=0 ; i<3 ; i++)
  1046. {
  1047. // these are byte values, so we don't have to worry about
  1048. // endianness
  1049. pbboxmin->v[i] = pdaliasframe->bboxmin.v[i];
  1050. pbboxmax->v[i] = pdaliasframe->bboxmax.v[i];
  1051. }
  1052. pinframe = (trivertx_t *)(pdaliasframe + 1);
  1053. pframe = Hunk_AllocName (numv * sizeof(*pframe), loadname);
  1054. *pframeindex = (byte *)pframe - (byte *)pheader;
  1055. for (j=0 ; j<numv ; j++)
  1056. {
  1057. int k;
  1058. // these are all byte values, so no need to deal with endianness
  1059. pframe[j].lightnormalindex = pinframe[j].lightnormalindex;
  1060. for (k=0 ; k<3 ; k++)
  1061. {
  1062. pframe[j].v[k] = pinframe[j].v[k];
  1063. }
  1064. }
  1065. pinframe += numv;
  1066. return (void *)pinframe;
  1067. }
  1068. /*
  1069. =================
  1070. Mod_LoadAliasGroup
  1071. =================
  1072. */
  1073. void * Mod_LoadAliasGroup (void * pin, int *pframeindex, int numv,
  1074. trivertx_t *pbboxmin, trivertx_t *pbboxmax, aliashdr_t *pheader, char *name)
  1075. {
  1076. daliasgroup_t *pingroup;
  1077. maliasgroup_t *paliasgroup;
  1078. int i, numframes;
  1079. daliasinterval_t *pin_intervals;
  1080. float *poutintervals;
  1081. void *ptemp;
  1082. pingroup = (daliasgroup_t *)pin;
  1083. numframes = LittleLong (pingroup->numframes);
  1084. paliasgroup = Hunk_AllocName (sizeof (maliasgroup_t) +
  1085. (numframes - 1) * sizeof (paliasgroup->frames[0]), loadname);
  1086. paliasgroup->numframes = numframes;
  1087. for (i=0 ; i<3 ; i++)
  1088. {
  1089. // these are byte values, so we don't have to worry about endianness
  1090. pbboxmin->v[i] = pingroup->bboxmin.v[i];
  1091. pbboxmax->v[i] = pingroup->bboxmax.v[i];
  1092. }
  1093. *pframeindex = (byte *)paliasgroup - (byte *)pheader;
  1094. pin_intervals = (daliasinterval_t *)(pingroup + 1);
  1095. poutintervals = Hunk_AllocName (numframes * sizeof (float), loadname);
  1096. paliasgroup->intervals = (byte *)poutintervals - (byte *)pheader;
  1097. for (i=0 ; i<numframes ; i++)
  1098. {
  1099. *poutintervals = LittleFloat (pin_intervals->interval);
  1100. if (*poutintervals <= 0.0)
  1101. Sys_Error ("Mod_LoadAliasGroup: interval<=0");
  1102. poutintervals++;
  1103. pin_intervals++;
  1104. }
  1105. ptemp = (void *)pin_intervals;
  1106. for (i=0 ; i<numframes ; i++)
  1107. {
  1108. ptemp = Mod_LoadAliasFrame (ptemp,
  1109. &paliasgroup->frames[i].frame,
  1110. numv,
  1111. &paliasgroup->frames[i].bboxmin,
  1112. &paliasgroup->frames[i].bboxmax,
  1113. pheader, name);
  1114. }
  1115. return ptemp;
  1116. }
  1117. /*
  1118. =================
  1119. Mod_LoadAliasSkin
  1120. =================
  1121. */
  1122. void * Mod_LoadAliasSkin (void * pin, int *pskinindex, int skinsize,
  1123. aliashdr_t *pheader)
  1124. {
  1125. int i;
  1126. byte *pskin, *pinskin;
  1127. unsigned short *pusskin;
  1128. pskin = Hunk_AllocName (skinsize * r_pixbytes, loadname);
  1129. pinskin = (byte *)pin;
  1130. *pskinindex = (byte *)pskin - (byte *)pheader;
  1131. if (r_pixbytes == 1)
  1132. {
  1133. Q_memcpy (pskin, pinskin, skinsize);
  1134. }
  1135. else if (r_pixbytes == 2)
  1136. {
  1137. pusskin = (unsigned short *)pskin;
  1138. for (i=0 ; i<skinsize ; i++)
  1139. pusskin[i] = d_8to16table[pinskin[i]];
  1140. }
  1141. else
  1142. {
  1143. Sys_Error ("Mod_LoadAliasSkin: driver set invalid r_pixbytes: %d\n",
  1144. r_pixbytes);
  1145. }
  1146. pinskin += skinsize;
  1147. return ((void *)pinskin);
  1148. }
  1149. /*
  1150. =================
  1151. Mod_LoadAliasSkinGroup
  1152. =================
  1153. */
  1154. void * Mod_LoadAliasSkinGroup (void * pin, int *pskinindex, int skinsize,
  1155. aliashdr_t *pheader)
  1156. {
  1157. daliasskingroup_t *pinskingroup;
  1158. maliasskingroup_t *paliasskingroup;
  1159. int i, numskins;
  1160. daliasskininterval_t *pinskinintervals;
  1161. float *poutskinintervals;
  1162. void *ptemp;
  1163. pinskingroup = (daliasskingroup_t *)pin;
  1164. numskins = LittleLong (pinskingroup->numskins);
  1165. paliasskingroup = Hunk_AllocName (sizeof (maliasskingroup_t) +
  1166. (numskins - 1) * sizeof (paliasskingroup->skindescs[0]),
  1167. loadname);
  1168. paliasskingroup->numskins = numskins;
  1169. *pskinindex = (byte *)paliasskingroup - (byte *)pheader;
  1170. pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
  1171. poutskinintervals = Hunk_AllocName (numskins * sizeof (float),loadname);
  1172. paliasskingroup->intervals = (byte *)poutskinintervals - (byte *)pheader;
  1173. for (i=0 ; i<numskins ; i++)
  1174. {
  1175. *poutskinintervals = LittleFloat (pinskinintervals->interval);
  1176. if (*poutskinintervals <= 0)
  1177. Sys_Error ("Mod_LoadAliasSkinGroup: interval<=0");
  1178. poutskinintervals++;
  1179. pinskinintervals++;
  1180. }
  1181. ptemp = (void *)pinskinintervals;
  1182. for (i=0 ; i<numskins ; i++)
  1183. {
  1184. ptemp = Mod_LoadAliasSkin (ptemp,
  1185. &paliasskingroup->skindescs[i].skin, skinsize, pheader);
  1186. }
  1187. return ptemp;
  1188. }
  1189. /*
  1190. =================
  1191. Mod_LoadAliasModel
  1192. =================
  1193. */
  1194. void Mod_LoadAliasModel (model_t *mod, void *buffer)
  1195. {
  1196. int i;
  1197. mdl_t *pmodel, *pinmodel;
  1198. stvert_t *pstverts, *pinstverts;
  1199. aliashdr_t *pheader;
  1200. mtriangle_t *ptri;
  1201. dtriangle_t *pintriangles;
  1202. int version, numframes, numskins;
  1203. int size;
  1204. daliasframetype_t *pframetype;
  1205. daliasskintype_t *pskintype;
  1206. maliasskindesc_t *pskindesc;
  1207. int skinsize;
  1208. int start, end, total;
  1209. if (!strcmp(loadmodel->name, "progs/player.mdl") ||
  1210. !strcmp(loadmodel->name, "progs/eyes.mdl")) {
  1211. unsigned short crc;
  1212. byte *p;
  1213. int len;
  1214. char st[40];
  1215. CRC_Init(&crc);
  1216. for (len = com_filesize, p = buffer; len; len--, p++)
  1217. CRC_ProcessByte(&crc, *p);
  1218. sprintf(st, "%d", (int) crc);
  1219. Info_SetValueForKey (cls.userinfo,
  1220. !strcmp(loadmodel->name, "progs/player.mdl") ? pmodel_name : emodel_name,
  1221. st, MAX_INFO_STRING);
  1222. if (cls.state >= ca_connected) {
  1223. MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
  1224. sprintf(st, "setinfo %s %d",
  1225. !strcmp(loadmodel->name, "progs/player.mdl") ? pmodel_name : emodel_name,
  1226. (int)crc);
  1227. SZ_Print (&cls.netchan.message, st);
  1228. }
  1229. }
  1230. start = Hunk_LowMark ();
  1231. pinmodel = (mdl_t *)buffer;
  1232. version = LittleLong (pinmodel->version);
  1233. if (version != ALIAS_VERSION)
  1234. Sys_Error ("%s has wrong version number (%i should be %i)",
  1235. mod->name, version, ALIAS_VERSION);
  1236. //
  1237. // allocate space for a working header, plus all the data except the frames,
  1238. // skin and group info
  1239. //
  1240. size = sizeof (aliashdr_t) + (LittleLong (pinmodel->numframes) - 1) *
  1241. sizeof (pheader->frames[0]) +
  1242. sizeof (mdl_t) +
  1243. LittleLong (pinmodel->numverts) * sizeof (stvert_t) +
  1244. LittleLong (pinmodel->numtris) * sizeof (mtriangle_t);
  1245. pheader = Hunk_AllocName (size, loadname);
  1246. pmodel = (mdl_t *) ((byte *)&pheader[1] +
  1247. (LittleLong (pinmodel->numframes) - 1) *
  1248. sizeof (pheader->frames[0]));
  1249. // mod->cache.data = pheader;
  1250. mod->flags = LittleLong (pinmodel->flags);
  1251. //
  1252. // endian-adjust and copy the data, starting with the alias model header
  1253. //
  1254. pmodel->boundingradius = LittleFloat (pinmodel->boundingradius);
  1255. pmodel->numskins = LittleLong (pinmodel->numskins);
  1256. pmodel->skinwidth = LittleLong (pinmodel->skinwidth);
  1257. pmodel->skinheight = LittleLong (pinmodel->skinheight);
  1258. if (pmodel->skinheight > MAX_LBM_HEIGHT)
  1259. Sys_Error ("model %s has a skin taller than %d", mod->name,
  1260. MAX_LBM_HEIGHT);
  1261. pmodel->numverts = LittleLong (pinmodel->numverts);
  1262. if (pmodel->numverts <= 0)
  1263. Sys_Error ("model %s has no vertices", mod->name);
  1264. if (pmodel->numverts > MAXALIASVERTS)
  1265. Sys_Error ("model %s has too many vertices", mod->name);
  1266. pmodel->numtris = LittleLong (pinmodel->numtris);
  1267. if (pmodel->numtris <= 0)
  1268. Sys_Error ("model %s has no triangles", mod->name);
  1269. pmodel->numframes = LittleLong (pinmodel->numframes);
  1270. pmodel->size = LittleFloat (pinmodel->size) * ALIAS_BASE_SIZE_RATIO;
  1271. mod->synctype = LittleLong (pinmodel->synctype);
  1272. mod->numframes = pmodel->numframes;
  1273. for (i=0 ; i<3 ; i++)
  1274. {
  1275. pmodel->scale[i] = LittleFloat (pinmodel->scale[i]);
  1276. pmodel->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
  1277. pmodel->eyeposition[i] = LittleFloat (pinmodel->eyeposition[i]);
  1278. }
  1279. numskins = pmodel->numskins;
  1280. numframes = pmodel->numframes;
  1281. if (pmodel->skinwidth & 0x03)
  1282. Sys_Error ("Mod_LoadAliasModel: skinwidth not multiple of 4");
  1283. pheader->model = (byte *)pmodel - (byte *)pheader;
  1284. //
  1285. // load the skins
  1286. //
  1287. skinsize = pmodel->skinheight * pmodel->skinwidth;
  1288. if (numskins < 1)
  1289. Sys_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
  1290. pskintype = (daliasskintype_t *)&pinmodel[1];
  1291. pskindesc = Hunk_AllocName (numskins * sizeof (maliasskindesc_t),
  1292. loadname);
  1293. pheader->skindesc = (byte *)pskindesc - (byte *)pheader;
  1294. for (i=0 ; i<numskins ; i++)
  1295. {
  1296. aliasskintype_t skintype;
  1297. skintype = LittleLong (pskintype->type);
  1298. pskindesc[i].type = skintype;
  1299. if (skintype == ALIAS_SKIN_SINGLE)
  1300. {
  1301. pskintype = (daliasskintype_t *)
  1302. Mod_LoadAliasSkin (pskintype + 1,
  1303. &pskindesc[i].skin,
  1304. skinsize, pheader);
  1305. }
  1306. else
  1307. {
  1308. pskintype = (daliasskintype_t *)
  1309. Mod_LoadAliasSkinGroup (pskintype + 1,
  1310. &pskindesc[i].skin,
  1311. skinsize, pheader);
  1312. }
  1313. }
  1314. //
  1315. // set base s and t vertices
  1316. //
  1317. pstverts = (stvert_t *)&pmodel[1];
  1318. pinstverts = (stvert_t *)pskintype;
  1319. pheader->stverts = (byte *)pstverts - (byte *)pheader;
  1320. for (i=0 ; i<pmodel->numverts ; i++)
  1321. {
  1322. pstverts[i].onseam = LittleLong (pinstverts[i].onseam);
  1323. // put s and t in 16.16 format
  1324. pstverts[i].s = LittleLong (pinstverts[i].s) << 16;
  1325. pstverts[i].t = LittleLong (pinstverts[i].t) << 16;
  1326. }
  1327. //
  1328. // set up the triangles
  1329. //
  1330. ptri = (mtriangle_t *)&pstverts[pmodel->numverts];
  1331. pintriangles = (dtriangle_t *)&pinstverts[pmodel->numverts];
  1332. pheader->triangles = (byte *)ptri - (byte *)pheader;
  1333. for (i=0 ; i<pmodel->numtris ; i++)
  1334. {
  1335. int j;
  1336. ptri[i].facesfront = LittleLong (pintriangles[i].facesfront);
  1337. for (j=0 ; j<3 ; j++)
  1338. {
  1339. ptri[i].vertindex[j] =
  1340. LittleLong (pintriangles[i].vertindex[j]);
  1341. }
  1342. }
  1343. //
  1344. // load the frames
  1345. //
  1346. if (numframes < 1)
  1347. Sys_Error ("Mod_LoadAliasModel: Invalid # of frames: %d\n", numframes);
  1348. pframetype = (daliasframetype_t *)&pintriangles[pmodel->numtris];
  1349. for (i=0 ; i<numframes ; i++)
  1350. {
  1351. aliasframetype_t frametype;
  1352. frametype = LittleLong (pframetype->type);
  1353. pheader->frames[i].type = frametype;
  1354. if (frametype == ALIAS_SINGLE)
  1355. {
  1356. pframetype = (daliasframetype_t *)
  1357. Mod_LoadAliasFrame (pframetype + 1,
  1358. &pheader->frames[i].frame,
  1359. pmodel->numverts,
  1360. &pheader->frames[i].bboxmin,
  1361. &pheader->frames[i].bboxmax,
  1362. pheader, pheader->frames[i].name);
  1363. }
  1364. else
  1365. {
  1366. pframetype = (daliasframetype_t *)
  1367. Mod_LoadAliasGroup (pframetype + 1,
  1368. &pheader->frames[i].frame,
  1369. pmodel->numverts,
  1370. &pheader->frames[i].bboxmin,
  1371. &pheader->frames[i].bboxmax,
  1372. pheader, pheader->frames[i].name);
  1373. }
  1374. }
  1375. mod->type = mod_alias;
  1376. // FIXME: do this right
  1377. mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
  1378. mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
  1379. //
  1380. // move the complete, relocatable alias model to the cache
  1381. //
  1382. end = Hunk_LowMark ();
  1383. total = end - start;
  1384. Cache_Alloc (&mod->cache, total, loadname);
  1385. if (!mod->cache.data)
  1386. return;
  1387. memcpy (mod->cache.data, pheader, total);
  1388. Hunk_FreeToLowMark (start);
  1389. }
  1390. //=============================================================================
  1391. /*
  1392. =================
  1393. Mod_LoadSpriteFrame
  1394. =================
  1395. */
  1396. void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe)
  1397. {
  1398. dspriteframe_t *pinframe;
  1399. mspriteframe_t *pspriteframe;
  1400. int i, width, height, size, origin[2];
  1401. unsigned short *ppixout;
  1402. byte *ppixin;
  1403. pinframe = (dspriteframe_t *)pin;
  1404. width = LittleLong (pinframe->width);
  1405. height = LittleLong (pinframe->height);
  1406. size = width * height;
  1407. pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t) + size*r_pixbytes,
  1408. loadname);
  1409. Q_memset (pspriteframe, 0, sizeof (mspriteframe_t) + size);
  1410. *ppframe = pspriteframe;
  1411. pspriteframe->width = width;
  1412. pspriteframe->height = height;
  1413. origin[0] = LittleLong (pinframe->origin[0]);
  1414. origin[1] = LittleLong (pinframe->origin[1]);
  1415. pspriteframe->up = origin[1];
  1416. pspriteframe->down = origin[1] - height;
  1417. pspriteframe->left = origin[0];
  1418. pspriteframe->right = width + origin[0];
  1419. if (r_pixbytes == 1)
  1420. {
  1421. Q_memcpy (&pspriteframe->pixels[0], (byte *)(pinframe + 1), size);
  1422. }
  1423. else if (r_pixbytes == 2)
  1424. {
  1425. ppixin = (byte *)(pinframe + 1);
  1426. ppixout = (unsigned short *)&pspriteframe->pixels[0];
  1427. for (i=0 ; i<size ; i++)
  1428. ppixout[i] = d_8to16table[ppixin[i]];
  1429. }
  1430. else
  1431. {
  1432. Sys_Error ("Mod_LoadSpriteFrame: driver set invalid r_pixbytes: %d\n",
  1433. r_pixbytes);
  1434. }
  1435. return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
  1436. }
  1437. /*
  1438. =================
  1439. Mod_LoadSpriteGroup
  1440. =================
  1441. */
  1442. void * Mod_LoadSpriteGroup (void * pin, mspriteframe_t **ppframe)
  1443. {
  1444. dspritegroup_t *pingroup;
  1445. mspritegroup_t *pspritegroup;
  1446. int i, numframes;
  1447. dspriteinterval_t *pin_intervals;
  1448. float *poutintervals;
  1449. void *ptemp;
  1450. pingroup = (dspritegroup_t *)pin;
  1451. numframes = LittleLong (pingroup->numframes);
  1452. pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
  1453. (numframes - 1) * sizeof (pspritegroup->frames[0]), loadname);
  1454. pspritegroup->numframes = numframes;
  1455. *ppframe = (mspriteframe_t *)pspritegroup;
  1456. pin_intervals = (dspriteinterval_t *)(pingroup + 1);
  1457. poutintervals = Hunk_AllocName (numframes * sizeof (float), loadname);
  1458. pspritegroup->intervals = poutintervals;
  1459. for (i=0 ; i<numframes ; i++)
  1460. {
  1461. *poutintervals = LittleFloat (pin_intervals->interval);
  1462. if (*poutintervals <= 0.0)
  1463. Sys_Error ("Mod_LoadSpriteGroup: interval<=0");
  1464. poutintervals++;
  1465. pin_intervals++;
  1466. }
  1467. ptemp = (void *)pin_intervals;
  1468. for (i=0 ; i<numframes ; i++)
  1469. {
  1470. ptemp = Mod_LoadSpriteFrame (ptemp, &pspritegroup->frames[i]);
  1471. }
  1472. return ptemp;
  1473. }
  1474. /*
  1475. =================
  1476. Mod_LoadSpriteModel
  1477. =================
  1478. */
  1479. void Mod_LoadSpriteModel (model_t *mod, void *buffer)
  1480. {
  1481. int i;
  1482. int version;
  1483. dsprite_t *pin;
  1484. msprite_t *psprite;
  1485. int numframes;
  1486. int size;
  1487. dspriteframetype_t *pframetype;
  1488. pin = (dsprite_t *)buffer;
  1489. version = LittleLong (pin->version);
  1490. if (version != SPRITE_VERSION)
  1491. Sys_Error ("%s has wrong version number "
  1492. "(%i should be %i)", mod->name, version, SPRITE_VERSION);
  1493. numframes = LittleLong (pin->numframes);
  1494. size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames);
  1495. psprite = Hunk_AllocName (size, loadname);
  1496. mod->cache.data = psprite;
  1497. psprite->type = LittleLong (pin->type);
  1498. psprite->maxwidth = LittleLong (pin->width);
  1499. psprite->maxheight = LittleLong (pin->height);
  1500. psprite->beamlength = LittleFloat (pin->beamlength);
  1501. mod->synctype = LittleLong (pin->synctype);
  1502. psprite->numframes = numframes;
  1503. mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2;
  1504. mod->maxs[0] = mod->maxs[1] = psprite->maxwidth/2;
  1505. mod->mins[2] = -psprite->maxheight/2;
  1506. mod->maxs[2] = psprite->maxheight/2;
  1507. //
  1508. // load the frames
  1509. //
  1510. if (numframes < 1)
  1511. Sys_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes);
  1512. mod->numframes = numframes;
  1513. pframetype = (dspriteframetype_t *)(pin + 1);
  1514. for (i=0 ; i<numframes ; i++)
  1515. {
  1516. spriteframetype_t frametype;
  1517. frametype = LittleLong (pframetype->type);
  1518. psprite->frames[i].type = frametype;
  1519. if (frametype == SPR_SINGLE)
  1520. {
  1521. pframetype = (dspriteframetype_t *)
  1522. Mod_LoadSpriteFrame (pframetype + 1,
  1523. &psprite->frames[i].frameptr);
  1524. }
  1525. else
  1526. {
  1527. pframetype = (dspriteframetype_t *)
  1528. Mod_LoadSpriteGroup (pframetype + 1,
  1529. &psprite->frames[i].frameptr);
  1530. }
  1531. }
  1532. mod->type = mod_sprite;
  1533. }
  1534. //=============================================================================
  1535. /*
  1536. ================
  1537. Mod_Print
  1538. ================
  1539. */
  1540. void Mod_Print (void)
  1541. {
  1542. int i;
  1543. model_t *mod;
  1544. Con_Printf ("Cached models:\n");
  1545. for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
  1546. {
  1547. Con_Printf ("%8p : %s\n",mod->cache.data, mod->name);
  1548. }
  1549. }