]> icculus.org git repositories - divverent/darkplaces.git/blob - model_alias.c
add ALSA support
[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 aliashdr_t      *pheader;
33
34 typedef struct
35 {
36         int v[3];
37         vec3_t normal;
38 } temptris_t;
39 temptris_t *temptris;
40 //stvert_t      stverts[MAXALIASVERTS];
41 //mtriangle_t   triangles[MAXALIASTRIS];
42
43 // a pose is a single set of vertexes.  a frame may be
44 // an animating sequence of poses
45 //trivertx_t    *poseverts[MAXALIASFRAMES];
46 int                     posenum;
47
48 byte            **player_8bit_texels_tbl;
49 byte            *player_8bit_texels;
50
51 float           aliasbboxmin[3], aliasbboxmax[3]; // LordHavoc: proper bounding box considerations
52
53 // LordHavoc: changed to use the normals from the model data itself
54 #define NUMVERTEXNORMALS        162
55 extern  float   r_avertexnormals[NUMVERTEXNORMALS][3];
56 void Mod_ConvertAliasVerts (int numverts, int numtris, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
57 {
58         int i;
59         vec3_t temp;
60         for (i = 0;i < numverts;i++)
61         {
62                 VectorCopy(v[i].v, out[i].v);
63                 temp[0] = v[i].v[0] * scale[0] + translate[0];
64                 temp[1] = v[i].v[1] * scale[1] + translate[1];
65                 temp[2] = v[i].v[2] * scale[2] + translate[2];
66                 // update bounding box
67                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
68                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
69                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
70                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
71                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
72                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
73                 out[i].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
74                 out[i].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
75                 out[i].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
76         }               
77         /*
78         int i, j;
79         vec3_t t1, t2;
80         struct
81         {
82                 vec3_t v;
83                 vec3_t normal;
84                 int count;
85         } tempvert[MD2MAX_VERTS];
86         temptris_t *tris;
87         // decompress vertices
88         for (i = 0;i < numverts;i++)
89         {
90                 VectorCopy(v[i].v, out[i].v);
91                 tempvert[i].v[0] = v[i].v[0] * scale[0] + translate[0];
92                 tempvert[i].v[1] = v[i].v[1] * scale[1] + translate[1];
93                 tempvert[i].v[2] = v[i].v[2] * scale[2] + translate[2];
94                 tempvert[i].normal[0] = tempvert[i].normal[1] = tempvert[i].normal[2] = 0;
95                 tempvert[i].count = 0;
96                 // update bounding box
97                 if (tempvert[i].v[0] < aliasbboxmin[0]) aliasbboxmin[0] = tempvert[i].v[0];
98                 if (tempvert[i].v[1] < aliasbboxmin[1]) aliasbboxmin[1] = tempvert[i].v[1];
99                 if (tempvert[i].v[2] < aliasbboxmin[2]) aliasbboxmin[2] = tempvert[i].v[2];
100                 if (tempvert[i].v[0] > aliasbboxmax[0]) aliasbboxmax[0] = tempvert[i].v[0];
101                 if (tempvert[i].v[1] > aliasbboxmax[1]) aliasbboxmax[1] = tempvert[i].v[1];
102                 if (tempvert[i].v[2] > aliasbboxmax[2]) aliasbboxmax[2] = tempvert[i].v[2];
103         }
104         // calculate surface normals
105         tris = temptris;
106         for (i = 0;i < numtris;i++)
107         {
108                 VectorSubtract(tempvert[tris->v[0]].v, tempvert[tris->v[1]].v, t1);
109                 VectorSubtract(tempvert[tris->v[2]].v, tempvert[tris->v[1]].v, t2);
110                 CrossProduct(t1, t2, tris->normal);
111                 VectorNormalize(tris->normal);
112                 // add surface normal to vertices
113                 for (j = 0;j < 3;j++)
114                 {
115                         VectorAdd(tris->normal, tempvert[tris->v[j]].normal, tempvert[tris->v[j]].normal);
116                         tempvert[tris->v[j]].count++;
117                 }
118                 tris++;
119         }
120         // average normals and write out 1.7bit format
121         for (i = 0;i < pheader->numtris;i++)
122         {
123                 VectorNormalize(tempvert[i].normal);
124                 out[i].n[0] = (signed char) (tempvert[i].normal[0] * 127.0);
125                 out[i].n[1] = (signed char) (tempvert[i].normal[1] * 127.0);
126                 out[i].n[2] = (signed char) (tempvert[i].normal[2] * 127.0);
127         }
128         */
129 }
130
131 /*
132 =================
133 Mod_LoadAliasFrame
134 =================
135 */
136 void * Mod_LoadAliasFrame (void * pin, maliasframedesc_t *frame)
137 {
138         trivertx_t              *pinframe;
139         int                             i;
140         daliasframe_t   *pdaliasframe;
141         
142         pdaliasframe = (daliasframe_t *)pin;
143
144         strcpy (frame->name, pdaliasframe->name);
145         frame->firstpose = posenum;
146         frame->numposes = 1;
147
148         for (i=0 ; i<3 ; i++)
149         {
150         // these are byte values, so we don't have to worry about
151         // endianness
152                 frame->bboxmin.v[i] = pdaliasframe->bboxmin.v[i];
153                 frame->bboxmax.v[i] = pdaliasframe->bboxmax.v[i]; // LordHavoc: was setting bboxmin a second time (bug)
154         }
155
156         pinframe = (trivertx_t *)(pdaliasframe + 1);
157
158 //      poseverts[posenum] = pinframe;
159         Mod_ConvertAliasVerts(pheader->numverts, pheader->numtris, pheader->scale, pheader->scale_origin, pinframe, (void *)((int) pheader + pheader->posedata + sizeof(trivert2) * pheader->numverts * posenum));
160 //      // LordHavoc: copy the frame data
161 //      memcpy((void *)((int) pheader + pheader->posedata + sizeof(trivertx_t) * pheader->numverts * posenum), pinframe, sizeof(trivertx_t) * pheader->numverts);
162         posenum++;
163
164         pinframe += pheader->numverts;
165
166         return (void *)pinframe;
167 }
168
169
170 /*
171 =================
172 Mod_LoadAliasGroup
173 =================
174 */
175 void *Mod_LoadAliasGroup (void * pin,  maliasframedesc_t *frame)
176 {
177         daliasgroup_t           *pingroup;
178         int                                     i, numframes;
179         daliasinterval_t        *pin_intervals;
180         void                            *ptemp;
181         
182         pingroup = (daliasgroup_t *)pin;
183
184         numframes = LittleLong (pingroup->numframes);
185
186         frame->firstpose = posenum;
187         frame->numposes = numframes;
188
189         for (i=0 ; i<3 ; i++)
190         {
191         // these are byte values, so we don't have to worry about endianness
192                 frame->bboxmin.v[i] = pingroup->bboxmin.v[i];
193                 frame->bboxmax.v[i] = pingroup->bboxmax.v[i]; // LordHavoc: was setting bboxmin a second time (bug)
194         }
195
196         pin_intervals = (daliasinterval_t *)(pingroup + 1);
197
198         frame->interval = LittleFloat (pin_intervals->interval);
199
200         pin_intervals += numframes;
201
202         ptemp = (void *)pin_intervals;
203
204         for (i=0 ; i<numframes ; i++)
205         {
206 //              poseverts[posenum] = (trivertx_t *)((daliasframe_t *)ptemp + 1);
207                 Mod_ConvertAliasVerts(pheader->numverts, pheader->numtris, pheader->scale, pheader->scale_origin, (void *)((daliasframe_t *)ptemp + 1), (void *)((int) pheader + pheader->posedata + sizeof(trivert2) * pheader->numverts * posenum));
208 //              // LordHavoc: copy the frame data
209 //              memcpy((void *)((int) pheader + pheader->posedata + sizeof(trivertx_t) * pheader->numverts * posenum), (void *)((daliasframe_t *)ptemp + 1), sizeof(trivertx_t) * pheader->numverts);
210                 posenum++;
211
212                 ptemp = (trivertx_t *)((daliasframe_t *)ptemp + 1) + pheader->numverts;
213         }
214
215         return ptemp;
216 }
217
218 //=========================================================
219
220 /*
221 =================
222 Mod_FloodFillSkin
223
224 Fill background pixels so mipmapping doesn't have haloes - Ed
225 =================
226 */
227
228 typedef struct
229 {
230         short           x, y;
231 } floodfill_t;
232
233 extern unsigned d_8to24table[];
234
235 // must be a power of 2
236 #define FLOODFILL_FIFO_SIZE 0x1000
237 #define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
238
239 #define FLOODFILL_STEP( off, dx, dy ) \
240 { \
241         if (pos[off] == fillcolor) \
242         { \
243                 pos[off] = 255; \
244                 fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
245                 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
246         } \
247         else if (pos[off] != 255) fdc = pos[off]; \
248 }
249
250 void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
251 {
252         byte                            fillcolor = *skin; // assume this is the pixel to fill
253         floodfill_t                     fifo[FLOODFILL_FIFO_SIZE];
254         int                                     inpt = 0, outpt = 0;
255         int                                     filledcolor = -1;
256         int                                     i;
257
258         if (filledcolor == -1)
259         {
260                 filledcolor = 0;
261                 // attempt to find opaque black
262                 for (i = 0; i < 256; ++i)
263                         if (d_8to24table[i] == (255 << 0)) // alpha 1.0
264                         {
265                                 filledcolor = i;
266                                 break;
267                         }
268         }
269
270         // can't fill to filled color or to transparent color (used as visited marker)
271         if ((fillcolor == filledcolor) || (fillcolor == 255))
272         {
273                 //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
274                 return;
275         }
276
277         fifo[inpt].x = 0, fifo[inpt].y = 0;
278         inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
279
280         while (outpt != inpt)
281         {
282                 int                     x = fifo[outpt].x, y = fifo[outpt].y;
283                 int                     fdc = filledcolor;
284                 byte            *pos = &skin[x + skinwidth * y];
285
286                 outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
287
288                 if (x > 0)                              FLOODFILL_STEP( -1, -1, 0 );
289                 if (x < skinwidth - 1)  FLOODFILL_STEP( 1, 1, 0 );
290                 if (y > 0)                              FLOODFILL_STEP( -skinwidth, 0, -1 );
291                 if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
292                 skin[x + skinwidth * y] = fdc;
293         }
294 }
295
296 /*
297 ===============
298 Mod_LoadAllSkins
299 ===============
300 */
301 void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int bytesperpixel)
302 {
303         int             i, j, k;
304         char    name[32];
305         int             s;
306         byte    *skin;
307         byte    *texels;
308         daliasskingroup_t               *pinskingroup;
309         int             groupskins;
310         daliasskininterval_t    *pinskinintervals;
311         
312         skin = (byte *)(pskintype + 1);
313
314         if (numskins < 1 || numskins > MAX_SKINS)
315                 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
316
317         s = pheader->skinwidth * pheader->skinheight;
318
319         for (i = 0;i < numskins;i++)
320         {
321                 if (pskintype->type == ALIAS_SKIN_SINGLE)
322                 {
323                         if (bytesperpixel == 1)
324                                 Mod_FloodFillSkin( skin, pheader->skinwidth, pheader->skinheight );
325
326                         // save 8 bit texels for the player model to remap
327         //              if (!strcmp(loadmodel->name,"progs/player.mdl")) {
328                                 texels = Hunk_AllocName(s, loadname);
329                                 pheader->texels[i] = texels - (byte *)pheader;
330                                 memcpy (texels, (byte *)(pskintype + 1), s);
331         //              }
332                         sprintf (name, "%s_%i", loadmodel->name, i);
333                         pheader->gl_texturenum[i][0] =
334                         pheader->gl_texturenum[i][1] =
335                         pheader->gl_texturenum[i][2] =
336                         pheader->gl_texturenum[i][3] =
337                                 GL_LoadTexture (name, pheader->skinwidth, pheader->skinheight, (byte *)(pskintype + 1), true, false, bytesperpixel);
338                         pskintype = (daliasskintype_t *)((byte *)(pskintype+1) + s);
339                 }
340                 else
341                 {
342                         // animating skin group.  yuck.
343                         pskintype++;
344                         pinskingroup = (daliasskingroup_t *)pskintype;
345                         groupskins = LittleLong (pinskingroup->numskins);
346                         pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
347
348                         pskintype = (void *)(pinskinintervals + groupskins);
349
350                         for (j = 0;j < groupskins;j++)
351                         {
352                                         if (bytesperpixel == 1)
353                                                 Mod_FloodFillSkin( skin, pheader->skinwidth, pheader->skinheight );
354                                         if (j == 0)
355                                         {
356                                                 texels = Hunk_AllocName(s, loadname);
357                                                 pheader->texels[i] = texels - (byte *)pheader;
358                                                 memcpy (texels, (byte *)(pskintype), s);
359                                         }
360                                         sprintf (name, "%s_%i_%i", loadmodel->name, i,j);
361                                         pheader->gl_texturenum[i][j&3] = 
362                                                 GL_LoadTexture (name, pheader->skinwidth, pheader->skinheight, (byte *)(pskintype), true, false, bytesperpixel);
363                                         pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
364                         }
365                         k = j;
366                         for (;j < 4;j++)
367                                 pheader->gl_texturenum[i][j&3] = pheader->gl_texturenum[i][j - k]; 
368                 }
369         }
370
371         return (void *)pskintype;
372 }
373
374 //=========================================================================
375
376 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
377
378 /*
379 =================
380 Mod_LoadAliasModel
381 =================
382 */
383 #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);
384 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid VALUE (%g exceeds %g - %g)\n", mod->name, VALUE, MIN, MAX);
385 void Mod_LoadAliasModel (model_t *mod, void *buffer)
386 {
387         int                                     i, j, version, numframes, size, start, end, total;
388         mdl_t                           *pinmodel;
389         stvert_t                        *pinstverts;
390         dtriangle_t                     *pintriangles;
391         daliasframetype_t       *pframetype;
392         daliasskintype_t        *pskintype;
393         // LordHavoc: 32bit textures
394         int                                     bytesperpixel;
395         unsigned short          *poutvertindices;
396         float                           *pouttexcoords, scales, scalet;
397         temptris_t                      *tris;
398
399         start = Hunk_LowMark ();
400
401         if (!temptris)
402                 temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
403
404         pinmodel = (mdl_t *)buffer;
405
406         version = LittleLong (pinmodel->version);
407         if (version != ALIAS_VERSION && version != ALIAS32_VERSION)
408                 Host_Error ("%s has wrong version number (%i should be %i or %i)",
409                                  mod->name, version, ALIAS_VERSION, ALIAS32_VERSION);
410
411         mod->type = ALIASTYPE_MDL;
412
413 //
414 // allocate space for a working header, plus all the data except the frames,
415 // skin and group info
416 //
417 //      size = sizeof (aliashdr_t) + (LittleLong (pinmodel->numframes) - 1) * sizeof (pinmodel->frames[0]));
418         size = sizeof (aliashdr_t);
419         size += LittleLong (pinmodel->numverts) * sizeof(float[2][2]);
420         size += LittleLong (pinmodel->numtris) * sizeof(unsigned short[3]);
421         size += LittleLong (pinmodel->numframes) * (sizeof(trivert2) * LittleLong (pinmodel->numverts) + sizeof(maliasframedesc_t));
422         BOUNDI(size,256,4194304);
423         pheader = Hunk_AllocName (size, loadname);
424         
425         mod->flags = LittleLong (pinmodel->flags);
426         mod->type = mod_alias;
427
428 // endian-adjust and copy the data, starting with the alias model header
429         pheader->boundingradius = LittleFloat (pinmodel->boundingradius);
430         BOUNDF(pheader->boundingradius,0,65536);
431         pheader->numskins = LittleLong (pinmodel->numskins);
432         BOUNDI(pheader->numskins,0,256);
433         pheader->skinwidth = LittleLong (pinmodel->skinwidth);
434         BOUNDI(pheader->skinwidth,0,4096);
435         pheader->skinheight = LittleLong (pinmodel->skinheight);
436         BOUNDI(pheader->skinheight,0,1024);
437 //LordHavoc: 32bit textures
438         bytesperpixel = version == ALIAS32_VERSION ? 4 : 1;
439
440 //      if (pheader->skinheight > MAX_LBM_HEIGHT)
441 //              Host_Error ("model %s has a skin taller than %d", mod->name, MAX_LBM_HEIGHT);
442
443         pheader->numverts = LittleLong (pinmodel->numverts);
444         BOUNDI(pheader->numverts,0,MAXALIASVERTS);
445         /*
446         if (pheader->numverts <= 0)
447                 Host_Error ("model %s has no vertices", mod->name);
448         if (pheader->numverts > MAXALIASVERTS)
449                 Host_Error ("model %s has too many vertices", mod->name);
450         */
451
452         pheader->numtris = LittleLong (pinmodel->numtris);
453         BOUNDI(pheader->numtris,0,65536);
454 //      if (pheader->numtris <= 0)
455 //              Host_Error ("model %s has no triangles", mod->name);
456
457         pheader->numframes = LittleLong (pinmodel->numframes);
458         BOUNDI(pheader->numframes,0,65536);
459         numframes = pheader->numframes;
460 //      if (numframes < 1)
461 //              Host_Error ("Mod_LoadAliasModel: Invalid # of frames: %d\n", numframes);
462
463         pheader->size = LittleFloat (pinmodel->size) * ALIAS_BASE_SIZE_RATIO;
464         BOUNDF(pheader->size,0,65536);
465         mod->synctype = LittleLong (pinmodel->synctype);
466         BOUNDI(pheader->synctype,0,2);
467         mod->numframes = pheader->numframes;
468
469         for (i=0 ; i<3 ; i++)
470         {
471                 pheader->scale[i] = LittleFloat (pinmodel->scale[i]);
472                 BOUNDF(pheader->scale[i],0,65536);
473                 pheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
474                 BOUNDF(pheader->scale_origin[i],-65536,65536);
475                 pheader->eyeposition[i] = LittleFloat (pinmodel->eyeposition[i]);
476                 BOUNDF(pheader->eyeposition[i],-65536,65536);
477         }
478
479 // load the skins
480         pskintype = (daliasskintype_t *)&pinmodel[1];
481         pskintype = Mod_LoadAllSkins (pheader->numskins, pskintype, bytesperpixel);
482
483 // load base s and t vertices
484         pinstverts = (stvert_t *)pskintype;
485         pouttexcoords = (float *)&pheader->frames[numframes];
486         pheader->texcoords = (int) pouttexcoords - (int) pheader;
487
488         // LordHavoc: byteswap and convert stvert data
489         scales = 1.0 / pheader->skinwidth;
490         scalet = 1.0 / pheader->skinheight;
491         for (i = 0;i < pheader->numverts;i++)
492         {
493                 pouttexcoords[i*2] = LittleLong (pinstverts[i].s) * scales;
494                 pouttexcoords[i*2+1] = LittleLong (pinstverts[i].t) * scalet;
495                 pouttexcoords[(i+pheader->numverts)*2] = LittleLong (pinstverts[i].s) * scales + 0.5;
496                 pouttexcoords[(i+pheader->numverts)*2+1] = LittleLong (pinstverts[i].t) * scalet;
497                 if (pouttexcoords[i*2] >= 0.5) // already a back side coordinate
498                 {
499                         pouttexcoords[i*2] -= 0.5;
500                         pouttexcoords[(i+pheader->numverts)*2] -= 0.5;
501                 }
502                 BOUNDF(pouttexcoords[i*2],0,1);
503                 BOUNDF(pouttexcoords[i*2+1],0,1);
504                 BOUNDF(pouttexcoords[(i+pheader->numverts)*2],0,1);
505                 BOUNDF(pouttexcoords[(i+pheader->numverts)*2+1],0,1);
506         }
507
508 // load triangle data
509         pintriangles = (dtriangle_t *)&pinstverts[pheader->numverts];
510         poutvertindices = (unsigned short *)&pouttexcoords[pheader->numverts*4];
511         pheader->vertindices = (int) poutvertindices - (int) pheader;
512         // LordHavoc: sort triangles into front and back lists
513         // so they can be drawn refering to different texture coordinate arrays,
514         // but sharing vertex data
515         pheader->frontfaces = 0;
516         pheader->backfaces = 0;
517         tris = temptris;
518         for (i=0 ; i<pheader->numtris ; i++)
519         {
520                 if (LittleLong(pintriangles[i].facesfront))
521                 {
522                         pheader->frontfaces++;
523                         for (j=0 ; j<3 ; j++)
524                                 *poutvertindices++ = LittleLong (pintriangles[i].vertindex[j]);
525                 }
526                 for (j=0 ; j<3 ; j++)
527                         tris->v[j] = LittleLong (pintriangles[i].vertindex[j]);
528                 tris++;
529         }
530         for (i=0 ; i<pheader->numtris ; i++)
531         {
532                 if (!LittleLong(pintriangles[i].facesfront))
533                 {
534                         pheader->backfaces++;
535                         for (j=0 ; j<3 ; j++)
536                                 *poutvertindices++ = LittleLong (pintriangles[i].vertindex[j]);
537                 }
538         }
539
540 // load the frames
541         posenum = 0;
542         pheader->posedata = (int) poutvertindices - (int) pheader;
543         pframetype = (daliasframetype_t *)&pintriangles[pheader->numtris];
544
545         // LordHavoc: doing proper bbox for model
546         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
547         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
548
549         for (i=0 ; i<numframes ; i++)
550         {
551                 aliasframetype_t        frametype;
552
553                 frametype = LittleLong (pframetype->type);
554
555                 if (frametype == ALIAS_SINGLE)
556                         pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, &pheader->frames[i]);
557                 else
558                         pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, &pheader->frames[i]);
559         }
560
561         pheader->numposes = posenum;
562
563         // LordHavoc: fixed model bbox - was //FIXME: do this right
564         //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
565         //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
566         for (j = 0;j < 3;j++)
567         {
568                 mod->mins[j] = aliasbboxmin[j];
569                 mod->maxs[j] = aliasbboxmax[j];
570         }
571
572 // move the complete, relocatable alias model to the cache
573         end = Hunk_LowMark ();
574         total = end - start;
575         
576         Cache_Alloc (&mod->cache, total, loadname);
577         if (!mod->cache.data)
578                 return;
579         memcpy (mod->cache.data, pheader, total);
580
581         Hunk_FreeToLowMark (start);
582 }
583
584 /*
585 =================
586 Mod_LoadQ2AliasModel
587 =================
588 */
589 int loadtextureimage (int texnum, char* filename, qboolean complain, int matchwidth, int matchheight);
590 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
591 {
592         int                                     i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
593         md2_t                           *pinmodel;
594         md2mem_t                        *pheader;
595         md2triangle_t           *pintriangles, *pouttriangles;
596         md2frame_t                      *pinframe;
597         md2memframe_t           *poutframe;
598         char                            *pinskins;
599         temptris_t                      *tris;
600
601         start = Hunk_LowMark ();
602
603         if (!temptris)
604                 temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
605
606         pinmodel = (md2_t *)buffer;
607
608         version = LittleLong (pinmodel->version);
609         if (version != MD2ALIAS_VERSION)
610                 Host_Error ("%s has wrong version number (%i should be %i)",
611                                  mod->name, version, MD2ALIAS_VERSION);
612
613         mod->type = mod_alias;
614         mod->aliastype = ALIASTYPE_MD2;
615
616         framesize = sizeof(md2memframe_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivert2);
617         // LordHavoc: calculate size for in memory version
618         size = sizeof(md2mem_t)
619                  + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
620                  + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
621                  + LittleLong(pinmodel->num_frames) * framesize
622                  + LittleLong(pinmodel->num_glcmds) * sizeof(int);
623         if (size <= 0 || size >= MD2MAX_SIZE)
624                 Host_Error ("%s is not a valid model", mod->name);
625         pheader = Hunk_AllocName (size, loadname);
626         
627         mod->flags = 0; // there are no MD2 flags
628         mod->numframes = LittleLong(pinmodel->num_frames);
629         mod->synctype = ST_RAND;
630
631         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
632                 Host_Error ("%s is not a valid model", mod->name);
633         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
634                 Host_Error ("%s is not a valid model", mod->name);
635         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
636                 Host_Error ("%s is not a valid model", mod->name);
637         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
638                 Host_Error ("%s is not a valid model", mod->name);
639         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
640                 Host_Error ("%s is not a valid model", mod->name);
641
642         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
643                 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
644         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
645                 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
646         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > 256) //MD2MAX_FRAMES)
647                 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
648         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MD2MAX_SKINS)
649                 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
650
651         pheader->framesize = framesize;
652         pheader->num_skins = LittleLong(pinmodel->num_skins);
653         pheader->num_xyz = LittleLong(pinmodel->num_xyz);
654         pheader->num_st = LittleLong(pinmodel->num_st);
655         pheader->num_tris = LittleLong(pinmodel->num_tris);
656         pheader->num_frames = LittleLong(pinmodel->num_frames);
657         pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
658
659 // load the skins
660         if (pheader->num_skins)
661         {
662                 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
663                 for (i = 0;i < pheader->num_skins;i++)
664                 {
665                         pheader->gl_texturenum[i] = loadtextureimage (-1, pinskins, TRUE, 0, 0);
666                         pinskins += MD2MAX_SKINNAME;
667                 }
668         }
669
670 // load triangles
671         pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
672         pouttriangles = (void*)&pheader[1];
673         pheader->ofs_tris = (int) pouttriangles - (int) pheader;
674         tris = temptris;
675         // swap the triangle list
676         for (i=0 ; i<pheader->num_tris ; i++)
677         {
678                 for (j=0 ; j<3 ; j++)
679                 {
680                         tris->v[j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
681                         pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
682                         if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
683                                 Host_Error ("%s has invalid vertex indices", mod->name);
684                         if (pouttriangles->index_st[j] >= pheader->num_st)
685                                 Host_Error ("%s has invalid vertex indices", mod->name);
686                 }
687                 pintriangles++;
688                 pouttriangles++;
689                 tris++;
690         }
691
692         // LordHavoc: doing proper bbox for model
693         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
694         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
695
696 // load the frames
697         pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
698         poutframe = (void*) pouttriangles;
699         pheader->ofs_frames = (int) poutframe - (int) pheader;
700         for (i=0 ; i<pheader->num_frames ; i++)
701         {
702                 for (j = 0;j < 3;j++)
703                 {
704                         poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
705                         poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
706                 }
707                 Mod_ConvertAliasVerts (pheader->num_xyz, pheader->num_tris, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
708                 pinframe = (void*) &pinframe->verts[j];
709                 poutframe = (void*) &poutframe->verts[j];
710         }
711
712         // LordHavoc: model bbox
713         for (j = 0;j < 3;j++)
714         {
715                 mod->mins[j] = aliasbboxmin[j];
716                 mod->maxs[j] = aliasbboxmax[j];
717         }
718
719         // load the draw list
720         pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
721         poutglcmd = (void*) poutframe;
722         pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
723         for (i = 0;i < pheader->num_glcmds;i++)
724                 *poutglcmd++ = LittleLong(*pinglcmd++);
725
726 // move the complete, relocatable alias model to the cache
727         end = Hunk_LowMark ();
728         total = end - start;
729         
730         Cache_Alloc (&mod->cache, total, loadname);
731         if (!mod->cache.data)
732                 return;
733         memcpy (mod->cache.data, pheader, total);
734
735         Hunk_FreeToLowMark (start);
736 }