]> icculus.org git repositories - divverent/darkplaces.git/blob - model_alias.c
Coordinates are now floats in network protocol (bloats it yes, but the accuracy allow...
[divverent/darkplaces.git] / model_alias.c
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
21 #include "quakedef.h"
22
23 /*
24 ===============
25 Mod_AliasInit
26 ===============
27 */
28 void Mod_AliasInit (void)
29 {
30 }
31
32 int                     posenum;
33
34 float           aliasbboxmin[3], aliasbboxmax[3]; // LordHavoc: proper bounding box considerations
35
36 #define MAXVERTS 8192
37 float vertst[MAXVERTS][2];
38 int vertusage[MAXVERTS];
39 int vertonseam[MAXVERTS];
40 int vertremap[MAXVERTS];
41 unsigned short temptris[MAXVERTS][3];
42
43 #define NUMVERTEXNORMALS        162
44 extern  float   r_avertexnormals[NUMVERTEXNORMALS][3];
45 void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
46 {
47         int i, j;
48         vec3_t temp;
49         for (i = 0;i < inverts;i++)
50         {
51                 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
52                         continue;
53                 temp[0] = v[i].v[0] * scale[0] + translate[0];
54                 temp[1] = v[i].v[1] * scale[1] + translate[1];
55                 temp[2] = v[i].v[2] * scale[2] + translate[2];
56                 // update bounding box
57                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
58                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
59                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
60                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
61                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
62                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
63                 j = vertremap[i]; // not onseam
64                 if (j >= 0)
65                 {
66                         VectorCopy(v[i].v, out[j].v);
67                         out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
68                         out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
69                         out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
70                 }
71                 j = vertremap[i+inverts]; // onseam
72                 if (j >= 0)
73                 {
74                         VectorCopy(v[i].v, out[j].v);
75                         out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
76                         out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
77                         out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
78                 }
79         }
80 }
81
82 /*
83 =================
84 Mod_LoadAliasFrame
85 =================
86 */
87 void * Mod_LoadAliasFrame (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
88 {
89         trivertx_t              *pinframe;
90         daliasframe_t   *pdaliasframe;
91         
92         pdaliasframe = (daliasframe_t *)pin;
93
94         strcpy(frame->name, pdaliasframe->name);
95         frame->start = posenum;
96         frame->length = 1;
97         frame->rate = 10.0f; // unnecessary but...
98
99         pinframe = (trivertx_t *)(pdaliasframe + 1);
100
101         Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
102         *posevert += outverts;
103         posenum++;
104
105         pinframe += inverts;
106
107         return (void *)pinframe;
108 }
109
110
111 /*
112 =================
113 Mod_LoadAliasGroup
114 =================
115 */
116 void *Mod_LoadAliasGroup (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
117 {
118         int             i, numframes;
119         void    *ptemp;
120         float   interval;
121         
122         numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
123
124         strcpy(frame->name, "group");
125         frame->start = posenum;
126         frame->length = numframes;
127         interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
128         if (interval < 0.01f)
129                 Host_Error("Mod_LoadAliasGroup: invalid interval");
130         frame->rate = 1.0f / interval;
131
132         ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
133
134         for (i=0 ; i<numframes ; i++)
135         {
136                 ((daliasframe_t *)ptemp)++;
137                 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
138                 *posevert += outverts;
139                 posenum++;
140                 ptemp = (trivertx_t *)ptemp + inverts;
141         }
142
143         return ptemp;
144 }
145
146 //=========================================================
147
148 /*
149 =================
150 Mod_FloodFillSkin
151
152 Fill background pixels so mipmapping doesn't have haloes - Ed
153 =================
154 */
155
156 typedef struct
157 {
158         short           x, y;
159 } floodfill_t;
160
161 // must be a power of 2
162 #define FLOODFILL_FIFO_SIZE 0x1000
163 #define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
164
165 #define FLOODFILL_STEP( off, dx, dy ) \
166 { \
167         if (pos[off] == fillcolor) \
168         { \
169                 pos[off] = 255; \
170                 fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
171                 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
172         } \
173         else if (pos[off] != 255) fdc = pos[off]; \
174 }
175
176 void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
177 {
178         byte                            fillcolor = *skin; // assume this is the pixel to fill
179         floodfill_t                     fifo[FLOODFILL_FIFO_SIZE];
180         int                                     inpt = 0, outpt = 0;
181         int                                     filledcolor = -1;
182         int                                     i;
183
184         if (filledcolor == -1)
185         {
186                 filledcolor = 0;
187                 // attempt to find opaque black
188                 for (i = 0; i < 256; ++i)
189                         if (d_8to24table[i] == (255 << 0)) // alpha 1.0
190                         {
191                                 filledcolor = i;
192                                 break;
193                         }
194         }
195
196         // can't fill to filled color or to transparent color (used as visited marker)
197         if ((fillcolor == filledcolor) || (fillcolor == 255))
198         {
199                 //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
200                 return;
201         }
202
203         fifo[inpt].x = 0, fifo[inpt].y = 0;
204         inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
205
206         while (outpt != inpt)
207         {
208                 int                     x = fifo[outpt].x, y = fifo[outpt].y;
209                 int                     fdc = filledcolor;
210                 byte            *pos = &skin[x + skinwidth * y];
211
212                 outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
213
214                 if (x > 0)                              FLOODFILL_STEP( -1, -1, 0 );
215                 if (x < skinwidth - 1)  FLOODFILL_STEP( 1, 1, 0 );
216                 if (y > 0)                              FLOODFILL_STEP( -skinwidth, 0, -1 );
217                 if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
218                 skin[x + skinwidth * y] = fdc;
219         }
220 }
221
222 int GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
223 {
224         int i, pixels, passed;
225         byte pixeltest[16];
226         for (i = 0;i < 16;i++)
227                 pixeltest[i] = (bits & (1 << i)) != 0;
228         pixels = width*height;
229         passed = 0;
230         while(pixels--)
231         {
232                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
233                 {
234                         passed++;
235                         // turn to white while copying
236                         if (*in >= 128 && *in < 224) // backwards ranges
237                                 *out = (*in & 15) ^ 15;
238                         else
239                                 *out = *in & 15;
240                 }
241                 else
242                         *out = 0;
243                 in++;
244                 out++;
245         }
246         if (passed)
247                 return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
248         else
249                 return 0;
250 }
251
252 int GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
253 {
254         int i, pixels, passed;
255         byte pixeltest[16];
256         for (i = 0;i < 16;i++)
257                 pixeltest[i] = (bits & (1 << i)) != 0;
258         pixels = width*height;
259         passed = 0;
260         while(pixels--)
261         {
262                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
263                 {
264                         passed++;
265                         *out = *in;
266                 }
267                 else
268                         *out = 0;
269                 in++;
270                 out++;
271         }
272         if (passed)
273                 return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
274         else
275                 return 0;
276 }
277
278 int GL_SkinCheck(byte *in, int width, int height, unsigned short bits)
279 {
280         int i, pixels, passed;
281         byte pixeltest[16];
282         for (i = 0;i < 16;i++)
283                 pixeltest[i] = (bits & (1 << i)) != 0;
284         pixels = width*height;
285         passed = 0;
286         while(pixels--)
287         {
288                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
289                         return true;
290                 in++;
291         }
292         return false;
293 }
294
295 void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, int *skintexnum)
296 {
297 #if 0
298         int skin_normal, skin_pants, skin_shirt, skin_glow, skin_body, temp;
299         skin_normal = loadtextureimage(va("%s_normal", basename));
300         skin_pants  = loadtextureimage(va("%s_pants" , basename));
301         skin_shirt  = loadtextureimage(va("%s_shirt" , basename));
302         skin_glow   = loadtextureimage(va("%s_glow"  , basename));
303         skin_body   = loadtextureimage(va("%s_body"  , basename));
304         if (!(skin_normal || skin_pants || skin_shirt || skin_glow || skin_body))
305                 skin_body = loadtextureimage(name);
306         if (skin_normal || skin_pants || skin_shirt || skin_glow || skin_body)
307         {
308                 skintexnum[0] = skin_normal;
309                 skintexnum[1] = skin_pants;
310                 skintexnum[2] = skin_shirt;
311                 skintexnum[3] = skin_glow;
312                 skintexnum[4] = skin_body;
313         }
314         else
315         {
316                 Mod_FloodFillSkin(skin, width, height);
317                 skin_normal = GL_SkinCheck((byte *)pskintype, width, height, 0x3FBD);
318                 skin_pants = GL_SkinCheck((byte *)pskintype, width, height, 0x0040);
319                 skin_shirt = GL_SkinCheck((byte *)pskintype, width, height, 0x0002);
320                 skin_glow = GL_SkinCheck((byte *)pskintype, width, height, 0xC000);
321                 skin_body = GL_SkinCheck((byte *)pskintype, width, height, 0x3FFF);
322                 if (skin_pants || skin_shirt)
323                 {
324                         byte *saveskin;
325                         saveskin = Hunk_AllocName(width*height, va("%s skin", loadname));
326                         memcpy((saveskin, byte *)pskintype, width*height);
327                         temp = (int) saveskin - (int) mheader;
328                         skintexnum[0] = skin_normal ? -temp : 0;
329                         skintexnum[1] = skin_pants ? -temp : 0;
330                         skintexnum[2] = skin_shirt ? -temp : 0;
331                         skintexnum[3] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", basename)); // glow
332                         skintexnum[4] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", basename)); // body (normal + pants + shirt, but not glow)
333                 }
334                 else
335                 {
336                         skintexnum[0] = 0;
337                         skintexnum[1] = 0;
338                         skintexnum[2] = 0;
339                         skintexnum[3] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", basename)); // glow
340                         skintexnum[4] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", basename)); // body (normal + pants + shirt, but not glow)
341                 }
342         }
343 #else
344         skintexnum[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, true);
345         skintexnum[1] = 0;
346         skintexnum[2] = 0;
347         skintexnum[3] = loadtextureimage(va("%s_glow"  , basename), 0, 0, false, true);
348         skintexnum[4] = 0;
349         if (skintexnum[0])
350         {
351                 skintexnum[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, true);
352                 skintexnum[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, true);
353         }
354         else
355         {
356                 skintexnum[0] = loadtextureimage(basename, 0, 0, false, true);
357                 if (!skintexnum[0])
358                 {
359                         skintexnum[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("&%s_normal", basename)); // normal (no special colors)
360                         skintexnum[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("&%s_pants", basename)); // pants
361                         skintexnum[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("&%s_shirt", basename)); // shirt
362                         skintexnum[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename)); // glow
363                         skintexnum[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename)); // body (normal + pants + shirt, but not glow)
364                 }
365         }
366 #endif
367 }
368
369 /*
370 ===============
371 Mod_LoadAllSkins
372 ===============
373 */
374 void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
375 {
376         int             i, j;
377         char    name[32];
378         int             s;
379         byte    *skin;
380         daliasskingroup_t               *pinskingroup;
381         int             groupskins;
382         daliasskininterval_t    *pinskinintervals;
383         int             skinranges, skincount, *skintexnum, *skinrange, skinnum;
384         void    *temp;
385         byte    *skintemp = NULL;
386         
387         skin = (byte *)(pskintype + 1);
388
389         if (numskins < 1 || numskins > MAX_SKINS)
390                 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
391
392         s = width * height;
393         skintemp = qmalloc(s);
394
395         // LordHavoc: skim the data, measure the number of skins and number of groups
396         skinranges = numskins;
397         skincount = 0;
398         temp = pskintype;
399         for (i = 0;i < numskins;i++)
400         {
401                 pskintype++;
402                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
403                 {
404                         skincount++;
405                         (byte *)pskintype += s;
406                 }
407                 else
408                 {
409                         groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
410                         skincount += groupskins;
411                         (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
412                 }
413         }
414         pskintype = temp;
415
416         skinrange = loadmodel->skinanimrange;
417         skintexnum = loadmodel->skinanim;
418 //      skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);  
419 //      skintexnum = skinrange + skinranges * 2;
420 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
421 //      loadmodel->skinanim = (int) skintexnum - (int) pheader;
422         skinnum = 0;
423         for (i = 0;i < numskins;i++)
424         {
425                 *skinrange++ = skinnum; // start of the range
426                 pskintype++;
427                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
428                 {
429                         *skinrange++ = 1; // single skin
430                         skinnum++;
431                         sprintf (name, "%s_%i", loadmodel->name, i);
432                         Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintexnum);
433                         skintexnum += 5;
434                         pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
435                 }
436                 else
437                 {
438                         // animating skin group.  yuck.
439                         pinskingroup = (daliasskingroup_t *)pskintype;
440                         groupskins = LittleLong (pinskingroup->numskins);
441                         pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
442
443                         pskintype = (void *)(pinskinintervals + groupskins);
444
445                         *skinrange++ = groupskins; // number of skins
446                         skinnum += groupskins;
447                         for (j = 0;j < groupskins;j++)
448                         {
449                                 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
450                                 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintexnum);
451                                 skintexnum += 5;
452                                 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
453                         }
454                 }
455         }
456         loadmodel->numskins = numskins;
457         qfree(skintemp);
458
459         return (void *)pskintype;
460 }
461
462 void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
463 {
464         int             i;
465         for (i = 0;i < numskins;i++)
466         {
467                 pskintype++;
468                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
469                         (byte *)pskintype += skinsize;
470                 else
471                         (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
472         }
473         return pskintype;
474 }
475
476 //=========================================================================
477
478 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
479
480 /*
481 =================
482 Mod_LoadAliasModel
483 =================
484 */
485 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid VALUE (%d exceeds %d - %d)\n", mod->name, VALUE, MIN, MAX);
486 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid VALUE (%f exceeds %f - %f)\n", mod->name, VALUE, MIN, MAX);
487 void Mod_LoadAliasModel (model_t *mod, void *buffer)
488 {
489         int                                     i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
490         mdl_t                           *pinmodel;
491         stvert_t                        *pinstverts;
492         dtriangle_t                     *pintriangles;
493         daliasframetype_t       *pframetype;
494         daliasskintype_t        *pskintype;
495         float                           *pouttexcoords, scales, scalet;
496         maliashdr_t                     *mheader;
497         unsigned short          *pouttris;
498         maliasframe_t           *frame;
499         trivert2                        *posevert;
500
501         start = Hunk_LowMark ();
502
503         pinmodel = (mdl_t *)buffer;
504
505         version = LittleLong (pinmodel->version);
506         if (version != ALIAS_VERSION)
507                 Host_Error ("%s has wrong version number (%i should be %i)",
508                                  mod->name, version, ALIAS_VERSION);
509
510         mod->type = mod_alias;
511         mod->aliastype = ALIASTYPE_MDL;
512
513         numframes = LittleLong(pinmodel->numframes);
514         BOUNDI(numframes,0,65536);
515         numverts = LittleLong(pinmodel->numverts);
516         BOUNDI(numverts,0,MAXALIASVERTS);
517         numtris = LittleLong(pinmodel->numtris);
518         BOUNDI(numtris,0,65536);
519         numskins = LittleLong(pinmodel->numskins);
520         BOUNDI(numskins,0,256);
521         skinwidth = LittleLong (pinmodel->skinwidth);
522         BOUNDI(skinwidth,0,4096);
523         skinheight = LittleLong (pinmodel->skinheight);
524         BOUNDI(skinheight,0,1024);
525         
526         pskintype = (daliasskintype_t *)&pinmodel[1];
527         pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
528         pintriangles = (dtriangle_t *)&pinstverts[numverts];
529         pframetype = (daliasframetype_t *)&pintriangles[numtris];
530
531         numposes = 0;
532         for (i=0 ; i<numframes ; i++)
533         {
534                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
535                 {
536                         numposes++;
537                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t)                         + (sizeof(daliasframe_t)                            + sizeof(trivertx_t) * numverts)    );
538                 }
539                 else
540                 {
541                         f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
542                         numposes += f;
543                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
544                 }
545         }
546
547         // rebuild the model
548         mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
549         mod->flags = LittleLong (pinmodel->flags);
550         mod->type = mod_alias;
551 // endian-adjust and copy the data, starting with the alias model header
552         mheader->numverts = numverts;
553         mod->numtris = mheader->numtris = numtris;
554         mod->numframes = mheader->numframes = numframes;
555         mod->synctype = LittleLong (pinmodel->synctype);
556         BOUNDI(mod->synctype,0,2);
557
558         for (i=0 ; i<3 ; i++)
559         {
560                 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
561                 BOUNDF(mheader->scale[i],0,65536);
562                 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
563                 BOUNDF(mheader->scale_origin[i],-65536,65536);
564         }
565
566         // load the skins
567         pskintype = (daliasskintype_t *)&pinmodel[1];
568         pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
569
570         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
571         pinstverts = (stvert_t *)pskintype;
572
573         // LordHavoc: byteswap and convert stvert data
574         scales = 1.0 / skinwidth;
575         scalet = 1.0 / skinheight;
576         for (i = 0;i < numverts;i++)
577         {
578                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
579                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
580                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
581                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
582                 vertst[i+numverts][1] = vertst[i][1];
583                 vertusage[i] = 0;
584                 vertusage[i+numverts] = 0;
585         }
586
587 // load triangle data
588         pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
589         mheader->tridata = (int) pouttris - (int) mheader;
590         pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
591
592         // count the vertices used
593         for (i = 0;i < numverts*2;i++)
594                 vertusage[i] = 0;
595         for (i = 0;i < numtris;i++)
596         {
597                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
598                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
599                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
600                 if (!LittleLong(pintriangles[i].facesfront)) // backface
601                 {
602                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
603                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
604                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
605                 }
606                 vertusage[temptris[i][0]]++;
607                 vertusage[temptris[i][1]]++;
608                 vertusage[temptris[i][2]]++;
609         }
610         // build remapping table and compact array
611         totalverts = 0;
612         for (i = 0;i < numverts*2;i++)
613         {
614                 if (vertusage[i])
615                 {
616                         vertremap[i] = totalverts;
617                         vertst[totalverts][0] = vertst[i][0];
618                         vertst[totalverts][1] = vertst[i][1];
619                         totalverts++;
620                 }
621                 else
622                         vertremap[i] = -1; // not used at all
623         }
624         mheader->numverts = totalverts;
625         // remap the triangle references
626         for (i = 0;i < numtris;i++)
627         {
628                 *pouttris++ = vertremap[temptris[i][0]];
629                 *pouttris++ = vertremap[temptris[i][1]];
630                 *pouttris++ = vertremap[temptris[i][2]];
631         }
632         // store the texture coordinates
633         pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
634         mheader->texdata = (int) pouttexcoords - (int) mheader;
635         for (i = 0;i < totalverts;i++)
636         {
637                 *pouttexcoords++ = vertst[i][0];
638                 *pouttexcoords++ = vertst[i][1];
639         }
640
641 // load the frames
642         posenum = 0;
643         frame = Hunk_AllocName(sizeof(maliasframe_t) * numframes, va("%s frame info", loadname));
644         mheader->framedata = (int) frame - (int) mheader;
645         posevert = Hunk_AllocName(sizeof(trivert2) * numposes * totalverts, va("%s vertex data", loadname));
646         mheader->posedata = (int) posevert - (int) mheader;
647         pframetype = (daliasframetype_t *)&pintriangles[numtris];
648
649         // LordHavoc: doing proper bbox for model
650         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
651         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
652
653         for (i=0 ; i<numframes ; i++)
654         {
655                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
656                         pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
657                 else
658                         pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
659         }
660
661         // LordHavoc: fixed model bbox - was //FIXME: do this right
662         //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
663         //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
664         for (j = 0;j < 3;j++)
665         {
666                 mod->mins[j] = aliasbboxmin[j];
667                 mod->maxs[j] = aliasbboxmax[j];
668         }
669
670 // move the complete, relocatable alias model to the cache
671         end = Hunk_LowMark ();
672         total = end - start;
673         
674         Cache_Alloc (&mod->cache, total, loadname);
675         if (!mod->cache.data)
676                 return;
677         memcpy (mod->cache.data, mheader, total);
678
679         Hunk_FreeToLowMark (start);
680 }
681
682 void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
683 {
684         int i;
685         vec3_t temp;
686         for (i = 0;i < numverts;i++)
687         {
688                 VectorCopy(v[i].v, out[i].v);
689                 temp[0] = v[i].v[0] * scale[0] + translate[0];
690                 temp[1] = v[i].v[1] * scale[1] + translate[1];
691                 temp[2] = v[i].v[2] * scale[2] + translate[2];
692                 // update bounding box
693                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
694                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
695                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
696                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
697                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
698                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
699                 out[i].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
700                 out[i].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
701                 out[i].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
702         }               
703         /*
704         int i, j;
705         vec3_t t1, t2;
706         struct
707         {
708                 vec3_t v;
709                 vec3_t normal;
710                 int count;
711         } tempvert[MD2MAX_VERTS];
712         temptris_t *tris;
713         // decompress vertices
714         for (i = 0;i < numverts;i++)
715         {
716                 VectorCopy(v[i].v, out[i].v);
717                 tempvert[i].v[0] = v[i].v[0] * scale[0] + translate[0];
718                 tempvert[i].v[1] = v[i].v[1] * scale[1] + translate[1];
719                 tempvert[i].v[2] = v[i].v[2] * scale[2] + translate[2];
720                 tempvert[i].normal[0] = tempvert[i].normal[1] = tempvert[i].normal[2] = 0;
721                 tempvert[i].count = 0;
722                 // update bounding box
723                 if (tempvert[i].v[0] < aliasbboxmin[0]) aliasbboxmin[0] = tempvert[i].v[0];
724                 if (tempvert[i].v[1] < aliasbboxmin[1]) aliasbboxmin[1] = tempvert[i].v[1];
725                 if (tempvert[i].v[2] < aliasbboxmin[2]) aliasbboxmin[2] = tempvert[i].v[2];
726                 if (tempvert[i].v[0] > aliasbboxmax[0]) aliasbboxmax[0] = tempvert[i].v[0];
727                 if (tempvert[i].v[1] > aliasbboxmax[1]) aliasbboxmax[1] = tempvert[i].v[1];
728                 if (tempvert[i].v[2] > aliasbboxmax[2]) aliasbboxmax[2] = tempvert[i].v[2];
729         }
730         // calculate surface normals
731         tris = temptris;
732         for (i = 0;i < numtris;i++)
733         {
734                 VectorSubtract(tempvert[tris->v[0]].v, tempvert[tris->v[1]].v, t1);
735                 VectorSubtract(tempvert[tris->v[2]].v, tempvert[tris->v[1]].v, t2);
736                 CrossProduct(t1, t2, tris->normal);
737                 VectorNormalize(tris->normal);
738                 // add surface normal to vertices
739                 for (j = 0;j < 3;j++)
740                 {
741                         VectorAdd(tris->normal, tempvert[tris->v[j]].normal, tempvert[tris->v[j]].normal);
742                         tempvert[tris->v[j]].count++;
743                 }
744                 tris++;
745         }
746         // average normals and write out 1.7bit format
747         for (i = 0;i < pheader->numtris;i++)
748         {
749                 VectorNormalize(tempvert[i].normal);
750                 out[i].n[0] = (signed char) (tempvert[i].normal[0] * 127.0);
751                 out[i].n[1] = (signed char) (tempvert[i].normal[1] * 127.0);
752                 out[i].n[2] = (signed char) (tempvert[i].normal[2] * 127.0);
753         }
754         */
755 }
756
757 /*
758 =================
759 Mod_LoadQ2AliasModel
760 =================
761 */
762 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
763 {
764         int                                     i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
765         md2_t                           *pinmodel;
766         md2mem_t                        *pheader;
767         md2triangle_t           *pintriangles, *pouttriangles;
768         md2frame_t                      *pinframe;
769         md2memframe_t           *poutframe;
770         char                            *pinskins;
771 //      temptris_t                      *tris;
772
773         start = Hunk_LowMark ();
774
775 //      if (!temptris)
776 //              temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
777
778         pinmodel = (md2_t *)buffer;
779
780         version = LittleLong (pinmodel->version);
781         if (version != MD2ALIAS_VERSION)
782                 Host_Error ("%s has wrong version number (%i should be %i)",
783                                  mod->name, version, MD2ALIAS_VERSION);
784
785         mod->type = mod_alias;
786         mod->aliastype = ALIASTYPE_MD2;
787
788         framesize = sizeof(md2memframesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivert2);
789         // LordHavoc: calculate size for in memory version
790         size = sizeof(md2mem_t)
791                  + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
792                  + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
793                  + LittleLong(pinmodel->num_frames) * framesize
794                  + LittleLong(pinmodel->num_glcmds) * sizeof(int);
795         if (size <= 0 || size >= MD2MAX_SIZE)
796                 Host_Error ("%s is not a valid model", mod->name);
797         pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
798         
799         mod->flags = 0; // there are no MD2 flags
800         mod->numframes = LittleLong(pinmodel->num_frames);
801         mod->synctype = ST_RAND;
802         mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
803
804         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
805                 Host_Error ("%s is not a valid model", mod->name);
806         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
807                 Host_Error ("%s is not a valid model", mod->name);
808         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
809                 Host_Error ("%s is not a valid model", mod->name);
810         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
811                 Host_Error ("%s is not a valid model", mod->name);
812         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
813                 Host_Error ("%s is not a valid model", mod->name);
814
815         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
816                 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
817         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
818                 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
819         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
820                 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
821         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MD2MAX_SKINS)
822                 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
823
824         pheader->framesize = framesize;
825         pheader->num_skins = LittleLong(pinmodel->num_skins);
826         pheader->num_xyz = LittleLong(pinmodel->num_xyz);
827         pheader->num_st = LittleLong(pinmodel->num_st);
828         pheader->num_tris = LittleLong(pinmodel->num_tris);
829         pheader->num_frames = LittleLong(pinmodel->num_frames);
830         pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
831
832 // load the skins
833         if (pheader->num_skins)
834         {
835                 int *skin, *skinrange;
836                 skinrange = loadmodel->skinanimrange;
837                 skin = loadmodel->skinanim;
838 //              skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);  
839 //              skin = skinrange + pheader->num_skins * 2;
840 //              loadmodel->skinanimrange = (int) skinrange - (int) pheader;
841 //              loadmodel->skinanim = (int) skin - (int) pheader;
842                 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
843                 for (i = 0;i < pheader->num_skins;i++)
844                 {
845                         *skinrange++ = i;
846                         *skinrange++ = 1;
847                         *skin++ = loadtextureimage (pinskins, 0, 0, true, true);
848                         *skin++ = 0; // the extra 4 layers are currently unused
849                         *skin++ = 0;
850                         *skin++ = 0;
851                         *skin++ = 0;
852                         pinskins += MD2MAX_SKINNAME;
853                 }
854         }
855         loadmodel->numskins = pheader->num_skins;
856
857 // load triangles
858         pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
859         pouttriangles = (void*)&pheader[1];
860         pheader->ofs_tris = (int) pouttriangles - (int) pheader;
861 //      tris = temptris;
862         // swap the triangle list
863         for (i=0 ; i<pheader->num_tris ; i++)
864         {
865                 for (j=0 ; j<3 ; j++)
866                 {
867                         temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
868                         pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
869                         if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
870                                 Host_Error ("%s has invalid vertex indices", mod->name);
871                         if (pouttriangles->index_st[j] >= pheader->num_st)
872                                 Host_Error ("%s has invalid vertex indices", mod->name);
873                 }
874                 pintriangles++;
875                 pouttriangles++;
876         }
877
878         // LordHavoc: doing proper bbox for model
879         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
880         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
881
882 // load the frames
883         pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
884         poutframe = (void*) pouttriangles;
885         pheader->ofs_frames = (int) poutframe - (int) pheader;
886         for (i=0 ; i<pheader->num_frames ; i++)
887         {
888                 for (j = 0;j < 3;j++)
889                 {
890                         strcpy(poutframe->name, pinframe->name);
891                         poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
892                         poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
893                 }
894                 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
895                 pinframe = (void*) &pinframe->verts[j];
896                 poutframe = (void*) &poutframe->verts[j];
897         }
898
899         // LordHavoc: model bbox
900         for (j = 0;j < 3;j++)
901         {
902                 mod->mins[j] = aliasbboxmin[j];
903                 mod->maxs[j] = aliasbboxmax[j];
904         }
905
906         // load the draw list
907         pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
908         poutglcmd = (void*) poutframe;
909         pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
910         for (i = 0;i < pheader->num_glcmds;i++)
911                 *poutglcmd++ = LittleLong(*pinglcmd++);
912
913 // move the complete, relocatable alias model to the cache
914         end = Hunk_LowMark ();
915         total = end - start;
916         
917         Cache_Alloc (&mod->cache, total, loadname);
918         if (!mod->cache.data)
919                 return;
920         memcpy (mod->cache.data, pheader, total);
921
922         Hunk_FreeToLowMark (start);
923 }
924
925 void swapintblock(int *m, int size)
926 {
927         size /= 4;
928         while(size--)
929                 *m++ = BigLong(*m);
930 }
931
932 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
933 {
934         int i, pbase, start, end, total, *skin, *skinrange, *texturenum;
935         char *shadername;
936         zymtype1header_t *pinmodel, *pheader;
937         zymscene_t *scene;
938         zymbone_t *bone;
939
940         start = Hunk_LowMark ();
941
942         pinmodel = (void *)buffer;
943
944         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
945                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
946
947         if (BigLong(pinmodel->type) != 1)
948                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
949
950         mod->type = mod_alias;
951         mod->aliastype = ALIASTYPE_ZYM;
952
953         pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
954
955         pbase = (int) pheader;
956
957         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
958
959         // byteswap header
960         memcpy(pheader->id, pinmodel->id, 12);
961         pheader->type = BigLong(pheader->type);
962         pheader->filesize = BigLong(pheader->filesize);
963         pheader->mins[0] = BigFloat(pheader->mins[0]);
964         pheader->mins[1] = BigFloat(pheader->mins[1]);
965         pheader->mins[2] = BigFloat(pheader->mins[2]);
966         pheader->maxs[0] = BigFloat(pheader->maxs[0]);
967         pheader->maxs[1] = BigFloat(pheader->maxs[1]);
968         pheader->maxs[2] = BigFloat(pheader->maxs[2]);
969         pheader->radius = BigFloat(pheader->radius);
970         pheader->numverts = BigLong(pheader->numverts);
971         pheader->numtris = BigLong(pheader->numtris);
972         pheader->numshaders = BigLong(pheader->numshaders);
973         pheader->numbones = BigLong(pheader->numbones);
974         pheader->numscenes = BigLong(pheader->numscenes);
975
976
977         pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
978         pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
979         pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
980         pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
981         pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
982         pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
983         pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
984         pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
985         pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
986
987         mod->flags = 0; // there are no flags
988         mod->numframes = pheader->numscenes;
989         mod->synctype = ST_SYNC;
990         mod->numtris = pheader->numtris;
991
992         // FIXME: add skin support and texturing and shaders and...
993 // load the skins
994         skinrange = loadmodel->skinanimrange;
995         skin = loadmodel->skinanim;
996 //      skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);  
997 //      skin = skinrange + pheader->num_skins * 2;
998 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
999 //      loadmodel->skinanim = (int) skin - (int) pheader;
1000         *skinrange++ = 0;
1001         *skinrange++ = 1;
1002         *skin++ = 0;
1003         *skin++ = 0;
1004         *skin++ = 0;
1005         *skin++ = 0;
1006         *skin++ = 0;
1007         loadmodel->numskins = 1;
1008
1009         // go through the lumps, swapping things
1010
1011 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1012         scene = (void *) (pheader->lump_scenes.start + pbase);
1013         for (i = 0;i < pheader->numscenes;i++)
1014         {
1015                 scene->mins[0] = BigFloat(scene->mins[0]);
1016                 scene->mins[1] = BigFloat(scene->mins[1]);
1017                 scene->mins[2] = BigFloat(scene->mins[2]);
1018                 scene->maxs[0] = BigFloat(scene->maxs[0]);
1019                 scene->maxs[1] = BigFloat(scene->maxs[1]);
1020                 scene->maxs[2] = BigFloat(scene->maxs[2]);
1021                 scene->radius = BigFloat(scene->radius);
1022                 scene->framerate = BigFloat(scene->framerate);
1023                 scene->flags = BigLong(scene->flags);
1024                 scene->start = BigLong(scene->start);
1025                 scene->length = BigLong(scene->length);
1026                 scene++;
1027         }
1028
1029 //      zymlump_t lump_poses; // float pose[numposes][numbones][6]; // animation data
1030         swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
1031
1032 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
1033         bone = (void *) (pheader->lump_bones.start + pbase);
1034         for (i = 0;i < pheader->numbones;i++)
1035         {
1036                 bone->flags = BigLong(bone->flags);
1037                 bone->parent = BigLong(bone->parent);
1038                 bone++;
1039         }
1040
1041 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1042         swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
1043
1044 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1045         swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
1046
1047 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1048         swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
1049
1050 //      zymlump_t lump_render; // int renderlist[rendersize]; // sorted by shader with run lengths (int shader, count), each run can be used with glDrawElements (each triangle is 3 int indices)
1051         swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
1052
1053 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1054         shadername = (void *) (pheader->lump_shaders.start + pbase);
1055         texturenum = (void *) shadername;
1056         for (i = 0;i < pheader->numshaders;i++)
1057         {
1058                 int j;
1059                 j = loadtextureimage(shadername, 0, 0, true, true);
1060                 shadername += 32;
1061                 *texturenum++ = j; // reuse shader name list for texture numbers
1062         }
1063
1064 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1065         swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
1066
1067         // model bbox
1068         for (i = 0;i < 3;i++)
1069         {
1070                 mod->mins[i] = pheader->mins[i];
1071                 mod->maxs[i] = pheader->maxs[i];
1072         }
1073
1074 // move the complete, relocatable alias model to the cache
1075         end = Hunk_LowMark ();
1076         total = end - start;
1077         
1078         Cache_Alloc (&mod->cache, total, loadname);
1079         if (!mod->cache.data)
1080                 return;
1081         memcpy (mod->cache.data, pheader, total);
1082
1083         Hunk_FreeToLowMark (start);
1084 }