home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 113 / EnigmaAmiga113CD.iso / software / sviluppo / quakeworld_src / client / gl_model.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-17  |  42.8 KB  |  1,885 lines

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