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