gl_model.c 39 KB

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