]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_alias.c
removed unused cvars
[xonotic/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 /*
279 ===============
280 Mod_LoadAllSkins
281 ===============
282 */
283 void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int width, int height)
284 {
285         int             i, j;
286         char    name[32];
287         int             s;
288         byte    *skin;
289         daliasskingroup_t               *pinskingroup;
290         int             groupskins;
291         daliasskininterval_t    *pinskinintervals;
292         int             skinranges, skincount, *skintexnum, *skinrange, skinnum;
293         void    *temp;
294         byte    *skintemp = NULL;
295         
296         skin = (byte *)(pskintype + 1);
297
298         if (numskins < 1 || numskins > MAX_SKINS)
299                 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
300
301         s = width * height;
302         skintemp = qmalloc(s);
303
304         // LordHavoc: skim the data, measure the number of skins and number of groups
305         skinranges = numskins;
306         skincount = 0;
307         temp = pskintype;
308         for (i = 0;i < numskins;i++)
309         {
310                 pskintype++;
311                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
312                 {
313                         skincount++;
314                         (byte *)pskintype += s;
315                 }
316                 else
317                 {
318                         groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
319                         skincount += groupskins;
320                         (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
321                 }
322         }
323         pskintype = temp;
324
325         skinrange = loadmodel->skinanimrange;
326         skintexnum = loadmodel->skinanim;
327 //      skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);  
328 //      skintexnum = skinrange + skinranges * 2;
329 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
330 //      loadmodel->skinanim = (int) skintexnum - (int) pheader;
331         skinnum = 0;
332         for (i = 0;i < numskins;i++)
333         {
334                 *skinrange++ = skinnum; // start of the range
335                 pskintype++;
336                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
337                 {
338                         *skinrange++ = 1; // single skin
339                         skinnum++;
340                         sprintf (name, "%s_%i", loadmodel->name, i);
341
342                         Mod_FloodFillSkin( skin, width, height );
343                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("&%s_normal", name)); // normal (no special colors)
344                         *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("&%s_pants",  name)); // pants
345                         *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("&%s_shirt",  name)); // shirt
346                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow",   name)); // glow
347                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body",   name)); // body (normal + pants + shirt, but not glow)
348                         pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
349                 }
350                 else
351                 {
352                         // animating skin group.  yuck.
353                         pinskingroup = (daliasskingroup_t *)pskintype;
354                         groupskins = LittleLong (pinskingroup->numskins);
355                         pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
356
357                         pskintype = (void *)(pinskinintervals + groupskins);
358
359                         *skinrange++ = groupskins; // number of skins
360                         skinnum += groupskins;
361                         for (j = 0;j < groupskins;j++)
362                         {
363                                 sprintf (name, "%s_%i_%i", loadmodel->name, i,j);
364
365                                 Mod_FloodFillSkin( skin, width, height );
366                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("&%s_normal", name)); // normal (no special colors)
367                                 *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("&%s_pants",  name)); // pants
368                                 *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("&%s_shirt",  name)); // shirt
369                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow",   name)); // glow
370                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body",   name)); // body (normal + pants + shirt, but not glow)
371                                 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
372                         }
373                 }
374         }
375         loadmodel->numskins = numskins;
376         qfree(skintemp);
377
378         return (void *)pskintype;
379 }
380
381 void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
382 {
383         int             i;
384         for (i = 0;i < numskins;i++)
385         {
386                 pskintype++;
387                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
388                         (byte *)pskintype += skinsize;
389                 else
390                         (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
391         }
392         return pskintype;
393 }
394
395 //=========================================================================
396
397 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
398
399 /*
400 =================
401 Mod_LoadAliasModel
402 =================
403 */
404 #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);
405 #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);
406 void Mod_LoadAliasModel (model_t *mod, void *buffer)
407 {
408         int                                     i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
409         mdl_t                           *pinmodel;
410         stvert_t                        *pinstverts;
411         dtriangle_t                     *pintriangles;
412         daliasframetype_t       *pframetype;
413         daliasskintype_t        *pskintype;
414         float                           *pouttexcoords, scales, scalet;
415         maliashdr_t                     *mheader;
416         unsigned short          *pouttris;
417         maliasframe_t           *frame;
418         trivert2                        *posevert;
419
420         start = Hunk_LowMark ();
421
422         pinmodel = (mdl_t *)buffer;
423
424         version = LittleLong (pinmodel->version);
425         if (version != ALIAS_VERSION)
426                 Host_Error ("%s has wrong version number (%i should be %i)",
427                                  mod->name, version, ALIAS_VERSION);
428
429         mod->type = ALIASTYPE_MDL;
430
431         numframes = LittleLong(pinmodel->numframes);
432         BOUNDI(numframes,0,65536);
433         numverts = LittleLong(pinmodel->numverts);
434         BOUNDI(numverts,0,MAXALIASVERTS);
435         numtris = LittleLong(pinmodel->numtris);
436         BOUNDI(numtris,0,65536);
437         numskins = LittleLong(pinmodel->numskins);
438         BOUNDI(numskins,0,256);
439         skinwidth = LittleLong (pinmodel->skinwidth);
440         BOUNDI(skinwidth,0,4096);
441         skinheight = LittleLong (pinmodel->skinheight);
442         BOUNDI(skinheight,0,1024);
443         
444         pskintype = (daliasskintype_t *)&pinmodel[1];
445         pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
446         pintriangles = (dtriangle_t *)&pinstverts[numverts];
447         pframetype = (daliasframetype_t *)&pintriangles[numtris];
448
449         numposes = 0;
450         for (i=0 ; i<numframes ; i++)
451         {
452                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
453                 {
454                         numposes++;
455                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t)                         + (sizeof(daliasframe_t)                            + sizeof(trivertx_t) * numverts)    );
456                 }
457                 else
458                 {
459                         f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
460                         numposes += f;
461                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
462                 }
463         }
464
465         // rebuild the model
466         mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
467         mod->flags = LittleLong (pinmodel->flags);
468         mod->type = mod_alias;
469 // endian-adjust and copy the data, starting with the alias model header
470         mheader->numverts = numverts;
471         mod->numtris = mheader->numtris = numtris;
472         mod->numframes = mheader->numframes = numframes;
473         mod->synctype = LittleLong (pinmodel->synctype);
474         BOUNDI(mod->synctype,0,2);
475
476         for (i=0 ; i<3 ; i++)
477         {
478                 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
479                 BOUNDF(mheader->scale[i],0,65536);
480                 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
481                 BOUNDF(mheader->scale_origin[i],-65536,65536);
482         }
483
484         // load the skins
485         pskintype = (daliasskintype_t *)&pinmodel[1];
486         pskintype = Mod_LoadAllSkins(numskins, pskintype, skinwidth, skinheight);
487
488         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
489         pinstverts = (stvert_t *)pskintype;
490
491         // LordHavoc: byteswap and convert stvert data
492         scales = 1.0 / skinwidth;
493         scalet = 1.0 / skinheight;
494         for (i = 0;i < numverts;i++)
495         {
496                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
497                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
498                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
499                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
500                 vertst[i+numverts][1] = vertst[i][1];
501                 vertusage[i] = 0;
502                 vertusage[i+numverts] = 0;
503         }
504
505 // load triangle data
506         pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
507         mheader->tridata = (int) pouttris - (int) mheader;
508         pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
509
510         // count the vertices used
511         for (i = 0;i < numverts*2;i++)
512                 vertusage[i] = 0;
513         for (i = 0;i < numtris;i++)
514         {
515                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
516                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
517                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
518                 if (!LittleLong(pintriangles[i].facesfront)) // backface
519                 {
520                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
521                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
522                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
523                 }
524                 vertusage[temptris[i][0]]++;
525                 vertusage[temptris[i][1]]++;
526                 vertusage[temptris[i][2]]++;
527         }
528         // build remapping table and compact array
529         totalverts = 0;
530         for (i = 0;i < numverts*2;i++)
531         {
532                 if (vertusage[i])
533                 {
534                         vertremap[i] = totalverts;
535                         vertst[totalverts][0] = vertst[i][0];
536                         vertst[totalverts][1] = vertst[i][1];
537                         totalverts++;
538                 }
539                 else
540                         vertremap[i] = -1; // not used at all
541         }
542         mheader->numverts = totalverts;
543         // remap the triangle references
544         for (i = 0;i < numtris;i++)
545         {
546                 *pouttris++ = vertremap[temptris[i][0]];
547                 *pouttris++ = vertremap[temptris[i][1]];
548                 *pouttris++ = vertremap[temptris[i][2]];
549         }
550         // store the texture coordinates
551         pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
552         mheader->texdata = (int) pouttexcoords - (int) mheader;
553         for (i = 0;i < totalverts;i++)
554         {
555                 *pouttexcoords++ = vertst[i][0];
556                 *pouttexcoords++ = vertst[i][1];
557         }
558
559 // load the frames
560         posenum = 0;
561         frame = Hunk_AllocName(sizeof(maliasframe_t) * numframes, va("%s frame info", loadname));
562         mheader->framedata = (int) frame - (int) mheader;
563         posevert = Hunk_AllocName(sizeof(trivert2) * numposes * totalverts, va("%s vertex data", loadname));
564         mheader->posedata = (int) posevert - (int) mheader;
565         pframetype = (daliasframetype_t *)&pintriangles[numtris];
566
567         // LordHavoc: doing proper bbox for model
568         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
569         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
570
571         for (i=0 ; i<numframes ; i++)
572         {
573                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
574                         pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
575                 else
576                         pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
577         }
578
579         // LordHavoc: fixed model bbox - was //FIXME: do this right
580         //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
581         //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
582         for (j = 0;j < 3;j++)
583         {
584                 mod->mins[j] = aliasbboxmin[j];
585                 mod->maxs[j] = aliasbboxmax[j];
586         }
587
588 // move the complete, relocatable alias model to the cache
589         end = Hunk_LowMark ();
590         total = end - start;
591         
592         Cache_Alloc (&mod->cache, total, loadname);
593         if (!mod->cache.data)
594                 return;
595         memcpy (mod->cache.data, mheader, total);
596
597         Hunk_FreeToLowMark (start);
598 }
599
600 void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
601 {
602         int i;
603         vec3_t temp;
604         for (i = 0;i < numverts;i++)
605         {
606                 VectorCopy(v[i].v, out[i].v);
607                 temp[0] = v[i].v[0] * scale[0] + translate[0];
608                 temp[1] = v[i].v[1] * scale[1] + translate[1];
609                 temp[2] = v[i].v[2] * scale[2] + translate[2];
610                 // update bounding box
611                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
612                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
613                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
614                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
615                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
616                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
617                 out[i].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
618                 out[i].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
619                 out[i].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
620         }               
621         /*
622         int i, j;
623         vec3_t t1, t2;
624         struct
625         {
626                 vec3_t v;
627                 vec3_t normal;
628                 int count;
629         } tempvert[MD2MAX_VERTS];
630         temptris_t *tris;
631         // decompress vertices
632         for (i = 0;i < numverts;i++)
633         {
634                 VectorCopy(v[i].v, out[i].v);
635                 tempvert[i].v[0] = v[i].v[0] * scale[0] + translate[0];
636                 tempvert[i].v[1] = v[i].v[1] * scale[1] + translate[1];
637                 tempvert[i].v[2] = v[i].v[2] * scale[2] + translate[2];
638                 tempvert[i].normal[0] = tempvert[i].normal[1] = tempvert[i].normal[2] = 0;
639                 tempvert[i].count = 0;
640                 // update bounding box
641                 if (tempvert[i].v[0] < aliasbboxmin[0]) aliasbboxmin[0] = tempvert[i].v[0];
642                 if (tempvert[i].v[1] < aliasbboxmin[1]) aliasbboxmin[1] = tempvert[i].v[1];
643                 if (tempvert[i].v[2] < aliasbboxmin[2]) aliasbboxmin[2] = tempvert[i].v[2];
644                 if (tempvert[i].v[0] > aliasbboxmax[0]) aliasbboxmax[0] = tempvert[i].v[0];
645                 if (tempvert[i].v[1] > aliasbboxmax[1]) aliasbboxmax[1] = tempvert[i].v[1];
646                 if (tempvert[i].v[2] > aliasbboxmax[2]) aliasbboxmax[2] = tempvert[i].v[2];
647         }
648         // calculate surface normals
649         tris = temptris;
650         for (i = 0;i < numtris;i++)
651         {
652                 VectorSubtract(tempvert[tris->v[0]].v, tempvert[tris->v[1]].v, t1);
653                 VectorSubtract(tempvert[tris->v[2]].v, tempvert[tris->v[1]].v, t2);
654                 CrossProduct(t1, t2, tris->normal);
655                 VectorNormalize(tris->normal);
656                 // add surface normal to vertices
657                 for (j = 0;j < 3;j++)
658                 {
659                         VectorAdd(tris->normal, tempvert[tris->v[j]].normal, tempvert[tris->v[j]].normal);
660                         tempvert[tris->v[j]].count++;
661                 }
662                 tris++;
663         }
664         // average normals and write out 1.7bit format
665         for (i = 0;i < pheader->numtris;i++)
666         {
667                 VectorNormalize(tempvert[i].normal);
668                 out[i].n[0] = (signed char) (tempvert[i].normal[0] * 127.0);
669                 out[i].n[1] = (signed char) (tempvert[i].normal[1] * 127.0);
670                 out[i].n[2] = (signed char) (tempvert[i].normal[2] * 127.0);
671         }
672         */
673 }
674
675 /*
676 =================
677 Mod_LoadQ2AliasModel
678 =================
679 */
680 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
681 {
682         int                                     i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
683         md2_t                           *pinmodel;
684         md2mem_t                        *pheader;
685         md2triangle_t           *pintriangles, *pouttriangles;
686         md2frame_t                      *pinframe;
687         md2memframe_t           *poutframe;
688         char                            *pinskins;
689 //      temptris_t                      *tris;
690
691         start = Hunk_LowMark ();
692
693 //      if (!temptris)
694 //              temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
695
696         pinmodel = (md2_t *)buffer;
697
698         version = LittleLong (pinmodel->version);
699         if (version != MD2ALIAS_VERSION)
700                 Host_Error ("%s has wrong version number (%i should be %i)",
701                                  mod->name, version, MD2ALIAS_VERSION);
702
703         mod->type = mod_alias;
704         mod->aliastype = ALIASTYPE_MD2;
705
706         framesize = sizeof(md2memframesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivert2);
707         // LordHavoc: calculate size for in memory version
708         size = sizeof(md2mem_t)
709                  + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
710                  + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
711                  + LittleLong(pinmodel->num_frames) * framesize
712                  + LittleLong(pinmodel->num_glcmds) * sizeof(int);
713         if (size <= 0 || size >= MD2MAX_SIZE)
714                 Host_Error ("%s is not a valid model", mod->name);
715         pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
716         
717         mod->flags = 0; // there are no MD2 flags
718         mod->numframes = LittleLong(pinmodel->num_frames);
719         mod->synctype = ST_RAND;
720         mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
721
722         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
723                 Host_Error ("%s is not a valid model", mod->name);
724         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
725                 Host_Error ("%s is not a valid model", mod->name);
726         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
727                 Host_Error ("%s is not a valid model", mod->name);
728         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
729                 Host_Error ("%s is not a valid model", mod->name);
730         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
731                 Host_Error ("%s is not a valid model", mod->name);
732
733         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
734                 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
735         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
736                 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
737         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
738                 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
739         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MD2MAX_SKINS)
740                 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
741
742         pheader->framesize = framesize;
743         pheader->num_skins = LittleLong(pinmodel->num_skins);
744         pheader->num_xyz = LittleLong(pinmodel->num_xyz);
745         pheader->num_st = LittleLong(pinmodel->num_st);
746         pheader->num_tris = LittleLong(pinmodel->num_tris);
747         pheader->num_frames = LittleLong(pinmodel->num_frames);
748         pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
749
750 // load the skins
751         if (pheader->num_skins)
752         {
753                 int *skin, *skinrange;
754                 skinrange = loadmodel->skinanimrange;
755                 skin = loadmodel->skinanim;
756 //              skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);  
757 //              skin = skinrange + pheader->num_skins * 2;
758 //              loadmodel->skinanimrange = (int) skinrange - (int) pheader;
759 //              loadmodel->skinanim = (int) skin - (int) pheader;
760                 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
761                 for (i = 0;i < pheader->num_skins;i++)
762                 {
763                         *skinrange++ = i;
764                         *skinrange++ = 1;
765                         *skin++ = loadtextureimage (pinskins, 0, 0, true, true);
766                         *skin++ = 0; // the extra 4 layers are currently unused
767                         *skin++ = 0;
768                         *skin++ = 0;
769                         *skin++ = 0;
770                         pinskins += MD2MAX_SKINNAME;
771                 }
772         }
773         loadmodel->numskins = pheader->num_skins;
774
775 // load triangles
776         pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
777         pouttriangles = (void*)&pheader[1];
778         pheader->ofs_tris = (int) pouttriangles - (int) pheader;
779 //      tris = temptris;
780         // swap the triangle list
781         for (i=0 ; i<pheader->num_tris ; i++)
782         {
783                 for (j=0 ; j<3 ; j++)
784                 {
785                         temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
786                         pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
787                         if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
788                                 Host_Error ("%s has invalid vertex indices", mod->name);
789                         if (pouttriangles->index_st[j] >= pheader->num_st)
790                                 Host_Error ("%s has invalid vertex indices", mod->name);
791                 }
792                 pintriangles++;
793                 pouttriangles++;
794         }
795
796         // LordHavoc: doing proper bbox for model
797         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
798         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
799
800 // load the frames
801         pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
802         poutframe = (void*) pouttriangles;
803         pheader->ofs_frames = (int) poutframe - (int) pheader;
804         for (i=0 ; i<pheader->num_frames ; i++)
805         {
806                 for (j = 0;j < 3;j++)
807                 {
808                         poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
809                         poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
810                 }
811                 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
812                 pinframe = (void*) &pinframe->verts[j];
813                 poutframe = (void*) &poutframe->verts[j];
814         }
815
816         // LordHavoc: model bbox
817         for (j = 0;j < 3;j++)
818         {
819                 mod->mins[j] = aliasbboxmin[j];
820                 mod->maxs[j] = aliasbboxmax[j];
821         }
822
823         // load the draw list
824         pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
825         poutglcmd = (void*) poutframe;
826         pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
827         for (i = 0;i < pheader->num_glcmds;i++)
828                 *poutglcmd++ = LittleLong(*pinglcmd++);
829
830 // move the complete, relocatable alias model to the cache
831         end = Hunk_LowMark ();
832         total = end - start;
833         
834         Cache_Alloc (&mod->cache, total, loadname);
835         if (!mod->cache.data)
836                 return;
837         memcpy (mod->cache.data, pheader, total);
838
839         Hunk_FreeToLowMark (start);
840 }