]> icculus.org git repositories - divverent/darkplaces.git/blob - model_alias.c
forgot to extern GL_LoadPicTexture
[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 #define MAXVERTS 8192
54 float vertst[MAXVERTS][2];
55 int vertusage[MAXVERTS];
56 int vertonseam[MAXVERTS];
57 int vertremap[MAXVERTS];
58 unsigned short temptris[MAXVERTS][3];
59
60 #define NUMVERTEXNORMALS        162
61 extern  float   r_avertexnormals[NUMVERTEXNORMALS][3];
62 void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
63 {
64         int i, j;
65         vec3_t temp;
66         for (i = 0;i < inverts;i++)
67         {
68                 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
69                         continue;
70                 temp[0] = v[i].v[0] * scale[0] + translate[0];
71                 temp[1] = v[i].v[1] * scale[1] + translate[1];
72                 temp[2] = v[i].v[2] * scale[2] + translate[2];
73                 // update bounding box
74                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
75                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
76                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
77                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
78                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
79                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
80                 j = vertremap[i]; // not onseam
81                 if (j >= 0)
82                 {
83                         VectorCopy(v[i].v, out[j].v);
84                         out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
85                         out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
86                         out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
87                 }
88                 j = vertremap[i+inverts]; // onseam
89                 if (j >= 0)
90                 {
91                         VectorCopy(v[i].v, out[j].v);
92                         out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
93                         out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
94                         out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
95                 }
96         }
97 }
98
99 /*
100 =================
101 Mod_LoadAliasFrame
102 =================
103 */
104 void * Mod_LoadAliasFrame (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
105 {
106         trivertx_t              *pinframe;
107         daliasframe_t   *pdaliasframe;
108         
109         pdaliasframe = (daliasframe_t *)pin;
110
111         strcpy(frame->name, pdaliasframe->name);
112         frame->start = posenum;
113         frame->length = 1;
114         frame->rate = 10.0f; // unnecessary but...
115
116         pinframe = (trivertx_t *)(pdaliasframe + 1);
117
118         Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
119         *posevert += outverts;
120         posenum++;
121
122         pinframe += inverts;
123
124         return (void *)pinframe;
125 }
126
127
128 /*
129 =================
130 Mod_LoadAliasGroup
131 =================
132 */
133 void *Mod_LoadAliasGroup (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
134 {
135         int             i, numframes;
136         void    *ptemp;
137         float   interval;
138         
139         numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
140
141         strcpy(frame->name, "group");
142         frame->start = posenum;
143         frame->length = numframes;
144         interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
145         if (interval < 0.01f)
146                 Host_Error("Mod_LoadAliasGroup: invalid interval");
147         frame->rate = 1.0f / interval;
148
149         ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
150
151         for (i=0 ; i<numframes ; i++)
152         {
153                 ((daliasframe_t *)ptemp)++;
154                 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
155                 *posevert += outverts;
156                 posenum++;
157                 ptemp = (trivertx_t *)ptemp + inverts;
158         }
159
160         return ptemp;
161 }
162
163 //=========================================================
164
165 /*
166 =================
167 Mod_FloodFillSkin
168
169 Fill background pixels so mipmapping doesn't have haloes - Ed
170 =================
171 */
172
173 typedef struct
174 {
175         short           x, y;
176 } floodfill_t;
177
178 extern unsigned d_8to24table[];
179
180 // must be a power of 2
181 #define FLOODFILL_FIFO_SIZE 0x1000
182 #define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
183
184 #define FLOODFILL_STEP( off, dx, dy ) \
185 { \
186         if (pos[off] == fillcolor) \
187         { \
188                 pos[off] = 255; \
189                 fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
190                 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
191         } \
192         else if (pos[off] != 255) fdc = pos[off]; \
193 }
194
195 void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
196 {
197         byte                            fillcolor = *skin; // assume this is the pixel to fill
198         floodfill_t                     fifo[FLOODFILL_FIFO_SIZE];
199         int                                     inpt = 0, outpt = 0;
200         int                                     filledcolor = -1;
201         int                                     i;
202
203         if (filledcolor == -1)
204         {
205                 filledcolor = 0;
206                 // attempt to find opaque black
207                 for (i = 0; i < 256; ++i)
208                         if (d_8to24table[i] == (255 << 0)) // alpha 1.0
209                         {
210                                 filledcolor = i;
211                                 break;
212                         }
213         }
214
215         // can't fill to filled color or to transparent color (used as visited marker)
216         if ((fillcolor == filledcolor) || (fillcolor == 255))
217         {
218                 //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
219                 return;
220         }
221
222         fifo[inpt].x = 0, fifo[inpt].y = 0;
223         inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
224
225         while (outpt != inpt)
226         {
227                 int                     x = fifo[outpt].x, y = fifo[outpt].y;
228                 int                     fdc = filledcolor;
229                 byte            *pos = &skin[x + skinwidth * y];
230
231                 outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
232
233                 if (x > 0)                              FLOODFILL_STEP( -1, -1, 0 );
234                 if (x < skinwidth - 1)  FLOODFILL_STEP( 1, 1, 0 );
235                 if (y > 0)                              FLOODFILL_STEP( -skinwidth, 0, -1 );
236                 if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
237                 skin[x + skinwidth * y] = fdc;
238         }
239 }
240
241 int GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
242 {
243         int i, pixels, passed;
244         byte pixeltest[16];
245         for (i = 0;i < 16;i++)
246                 pixeltest[i] = (bits & (1 << i)) != 0;
247         pixels = width*height;
248         passed = 0;
249         while(pixels--)
250         {
251                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
252                 {
253                         passed++;
254                         // turn to white while copying
255                         if (*in >= 128 && *in < 224) // backwards ranges
256                                 *out = (*in & 15) ^ 15;
257                         else
258                                 *out = *in & 15;
259                 }
260                 else
261                         *out = 0;
262                 in++;
263                 out++;
264         }
265         if (passed)
266                 return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
267         else
268                 return 0;
269 }
270
271 int GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
272 {
273         int i, pixels, passed;
274         byte pixeltest[16];
275         for (i = 0;i < 16;i++)
276                 pixeltest[i] = (bits & (1 << i)) != 0;
277         pixels = width*height;
278         passed = 0;
279         while(pixels--)
280         {
281                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
282                 {
283                         passed++;
284                         *out = *in;
285                 }
286                 else
287                         *out = 0;
288                 in++;
289                 out++;
290         }
291         if (passed)
292                 return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
293         else
294                 return 0;
295 }
296
297 /*
298 ===============
299 Mod_LoadAllSkins
300 ===============
301 */
302 void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int width, int height)
303 {
304         int             i, j;
305         char    name[32];
306         int             s;
307         byte    *skin;
308         daliasskingroup_t               *pinskingroup;
309         int             groupskins;
310         daliasskininterval_t    *pinskinintervals;
311         int             skinranges, skincount, *skintexnum, *skinrange, skinnum;
312         void    *temp;
313         byte    *skintemp = NULL;
314         
315         skin = (byte *)(pskintype + 1);
316
317         if (numskins < 1 || numskins > MAX_SKINS)
318                 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
319
320         s = width * height;
321         skintemp = malloc(s);
322
323         // LordHavoc: skim the data, measure the number of skins and number of groups
324         skinranges = numskins;
325         skincount = 0;
326         temp = pskintype;
327         for (i = 0;i < numskins;i++)
328         {
329                 pskintype++;
330                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
331                 {
332                         skincount++;
333                         (byte *)pskintype += s;
334                 }
335                 else
336                 {
337                         groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
338                         skincount += groupskins;
339                         (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
340                 }
341         }
342         pskintype = temp;
343
344         skinrange = loadmodel->skinanimrange;
345         skintexnum = loadmodel->skinanim;
346 //      skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);  
347 //      skintexnum = skinrange + skinranges * 2;
348 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
349 //      loadmodel->skinanim = (int) skintexnum - (int) pheader;
350         skinnum = 0;
351         for (i = 0;i < numskins;i++)
352         {
353                 *skinrange++ = skinnum; // start of the range
354                 pskintype++;
355                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
356                 {
357                         *skinrange++ = 1; // single skin
358                         skinnum++;
359                         sprintf (name, "%s_%i", loadmodel->name, i);
360
361                         Mod_FloodFillSkin( skin, width, height );
362                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("%s_normal", name)); // normal (no special colors)
363                         *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("%s_pants",  name)); // pants
364                         *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("%s_shirt",  name)); // shirt
365                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow",   name)); // glow
366                         *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body",   name)); // body (normal + pants + shirt, but not glow)
367                         pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
368                 }
369                 else
370                 {
371                         // animating skin group.  yuck.
372                         pinskingroup = (daliasskingroup_t *)pskintype;
373                         groupskins = LittleLong (pinskingroup->numskins);
374                         pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
375
376                         pskintype = (void *)(pinskinintervals + groupskins);
377
378                         *skinrange++ = groupskins; // number of skins
379                         skinnum += groupskins;
380                         for (j = 0;j < groupskins;j++)
381                         {
382                                 sprintf (name, "%s_%i_%i", loadmodel->name, i,j);
383
384                                 Mod_FloodFillSkin( skin, width, height );
385                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("%s_normal", name)); // normal (no special colors)
386                                 *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("%s_pants",  name)); // pants
387                                 *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("%s_shirt",  name)); // shirt
388                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow",   name)); // glow
389                                 *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body",   name)); // body (normal + pants + shirt, but not glow)
390                                 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
391                         }
392                 }
393         }
394         loadmodel->numskins = numskins;
395         free(skintemp);
396
397         return (void *)pskintype;
398 }
399
400 void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
401 {
402         int             i;
403         for (i = 0;i < numskins;i++)
404         {
405                 pskintype++;
406                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
407                         (byte *)pskintype += skinsize;
408                 else
409                         (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
410         }
411         return pskintype;
412 }
413
414 //=========================================================================
415
416 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
417
418 /*
419 =================
420 Mod_LoadAliasModel
421 =================
422 */
423 #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);
424 #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);
425 void Mod_LoadAliasModel (model_t *mod, void *buffer)
426 {
427         int                                     i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
428         mdl_t                           *pinmodel;
429         stvert_t                        *pinstverts;
430         dtriangle_t                     *pintriangles;
431         daliasframetype_t       *pframetype;
432         daliasskintype_t        *pskintype;
433         float                           *pouttexcoords, scales, scalet;
434         maliashdr_t                     *mheader;
435         unsigned short          *pouttris;
436         maliasframe_t           *frame;
437         trivert2                        *posevert;
438
439         start = Hunk_LowMark ();
440
441         pinmodel = (mdl_t *)buffer;
442
443         version = LittleLong (pinmodel->version);
444         if (version != ALIAS_VERSION)
445                 Host_Error ("%s has wrong version number (%i should be %i)",
446                                  mod->name, version, ALIAS_VERSION);
447
448         mod->type = ALIASTYPE_MDL;
449
450         numframes = LittleLong(pinmodel->numframes);
451         BOUNDI(numframes,0,65536);
452         numverts = LittleLong(pinmodel->numverts);
453         BOUNDI(numverts,0,MAXALIASVERTS);
454         numtris = LittleLong(pinmodel->numtris);
455         BOUNDI(numtris,0,65536);
456         numskins = LittleLong(pinmodel->numskins);
457         BOUNDI(numskins,0,256);
458         skinwidth = LittleLong (pinmodel->skinwidth);
459         BOUNDI(skinwidth,0,4096);
460         skinheight = LittleLong (pinmodel->skinheight);
461         BOUNDI(skinheight,0,1024);
462         
463         pskintype = (daliasskintype_t *)&pinmodel[1];
464         pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
465         pintriangles = (dtriangle_t *)&pinstverts[numverts];
466         pframetype = (daliasframetype_t *)&pintriangles[numtris];
467
468         numposes = 0;
469         for (i=0 ; i<numframes ; i++)
470         {
471                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
472                 {
473                         numposes++;
474                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t)                         + (sizeof(daliasframe_t)                            + sizeof(trivertx_t) * numverts)    );
475                 }
476                 else
477                 {
478                         f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
479                         numposes += f;
480                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
481                 }
482         }
483
484         // rebuild the model
485         mheader = Hunk_AllocName (sizeof(maliashdr_t), loadname);
486         mod->flags = LittleLong (pinmodel->flags);
487         mod->type = mod_alias;
488 // endian-adjust and copy the data, starting with the alias model header
489         mheader->numverts = numverts;
490         mod->numtris = mheader->numtris = numtris;
491         mod->numframes = mheader->numframes = numframes;
492         mod->synctype = LittleLong (pinmodel->synctype);
493         BOUNDI(mod->synctype,0,2);
494
495         for (i=0 ; i<3 ; i++)
496         {
497                 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
498                 BOUNDF(mheader->scale[i],0,65536);
499                 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
500                 BOUNDF(mheader->scale_origin[i],-65536,65536);
501         }
502
503         // load the skins
504         pskintype = (daliasskintype_t *)&pinmodel[1];
505         pskintype = Mod_LoadAllSkins(numskins, pskintype, skinwidth, skinheight);
506
507         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
508         pinstverts = (stvert_t *)pskintype;
509
510         // LordHavoc: byteswap and convert stvert data
511         scales = 1.0 / skinwidth;
512         scalet = 1.0 / skinheight;
513         for (i = 0;i < numverts;i++)
514         {
515                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
516                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
517                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
518                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
519                 vertst[i+numverts][1] = vertst[i][1];
520                 vertusage[i] = 0;
521                 vertusage[i+numverts] = 0;
522         }
523
524 // load triangle data
525         pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, loadname);
526         mheader->tridata = (int) pouttris - (int) mheader;
527         pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
528
529         // count the vertices used
530         for (i = 0;i < numverts*2;i++)
531                 vertusage[i] = 0;
532         for (i = 0;i < numtris;i++)
533         {
534                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
535                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
536                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
537                 if (!LittleLong(pintriangles[i].facesfront)) // backface
538                 {
539                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
540                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
541                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
542                 }
543                 vertusage[temptris[i][0]]++;
544                 vertusage[temptris[i][1]]++;
545                 vertusage[temptris[i][2]]++;
546         }
547         // build remapping table and compact array
548         totalverts = 0;
549         for (i = 0;i < numverts*2;i++)
550         {
551                 if (vertusage[i])
552                 {
553                         vertremap[i] = totalverts;
554                         vertst[totalverts][0] = vertst[i][0];
555                         vertst[totalverts][1] = vertst[i][1];
556                         totalverts++;
557                 }
558                 else
559                         vertremap[i] = -1; // not used at all
560         }
561         mheader->numverts = totalverts;
562         // remap the triangle references
563         for (i = 0;i < numtris;i++)
564         {
565                 *pouttris++ = vertremap[temptris[i][0]];
566                 *pouttris++ = vertremap[temptris[i][1]];
567                 *pouttris++ = vertremap[temptris[i][2]];
568         }
569         // store the texture coordinates
570         pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, loadname);
571         mheader->texdata = (int) pouttexcoords - (int) mheader;
572         for (i = 0;i < totalverts;i++)
573         {
574                 *pouttexcoords++ = vertst[i][0];
575                 *pouttexcoords++ = vertst[i][1];
576         }
577
578 // load the frames
579         posenum = 0;
580         frame = Hunk_AllocName(sizeof(maliasframe_t) * numframes, loadname);
581         mheader->framedata = (int) frame - (int) mheader;
582         posevert = Hunk_AllocName(sizeof(trivert2) * numposes * totalverts, loadname);
583         mheader->posedata = (int) posevert - (int) mheader;
584         pframetype = (daliasframetype_t *)&pintriangles[numtris];
585
586         // LordHavoc: doing proper bbox for model
587         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
588         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
589
590         for (i=0 ; i<numframes ; i++)
591         {
592                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
593                         pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
594                 else
595                         pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
596         }
597
598         // LordHavoc: fixed model bbox - was //FIXME: do this right
599         //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
600         //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
601         for (j = 0;j < 3;j++)
602         {
603                 mod->mins[j] = aliasbboxmin[j];
604                 mod->maxs[j] = aliasbboxmax[j];
605         }
606
607 // move the complete, relocatable alias model to the cache
608         end = Hunk_LowMark ();
609         total = end - start;
610         
611         Cache_Alloc (&mod->cache, total, loadname);
612         if (!mod->cache.data)
613                 return;
614         memcpy (mod->cache.data, mheader, total);
615
616         Hunk_FreeToLowMark (start);
617 }
618
619 void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
620 {
621         int i;
622         vec3_t temp;
623         for (i = 0;i < numverts;i++)
624         {
625                 VectorCopy(v[i].v, out[i].v);
626                 temp[0] = v[i].v[0] * scale[0] + translate[0];
627                 temp[1] = v[i].v[1] * scale[1] + translate[1];
628                 temp[2] = v[i].v[2] * scale[2] + translate[2];
629                 // update bounding box
630                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
631                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
632                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
633                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
634                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
635                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
636                 out[i].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
637                 out[i].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
638                 out[i].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
639         }               
640         /*
641         int i, j;
642         vec3_t t1, t2;
643         struct
644         {
645                 vec3_t v;
646                 vec3_t normal;
647                 int count;
648         } tempvert[MD2MAX_VERTS];
649         temptris_t *tris;
650         // decompress vertices
651         for (i = 0;i < numverts;i++)
652         {
653                 VectorCopy(v[i].v, out[i].v);
654                 tempvert[i].v[0] = v[i].v[0] * scale[0] + translate[0];
655                 tempvert[i].v[1] = v[i].v[1] * scale[1] + translate[1];
656                 tempvert[i].v[2] = v[i].v[2] * scale[2] + translate[2];
657                 tempvert[i].normal[0] = tempvert[i].normal[1] = tempvert[i].normal[2] = 0;
658                 tempvert[i].count = 0;
659                 // update bounding box
660                 if (tempvert[i].v[0] < aliasbboxmin[0]) aliasbboxmin[0] = tempvert[i].v[0];
661                 if (tempvert[i].v[1] < aliasbboxmin[1]) aliasbboxmin[1] = tempvert[i].v[1];
662                 if (tempvert[i].v[2] < aliasbboxmin[2]) aliasbboxmin[2] = tempvert[i].v[2];
663                 if (tempvert[i].v[0] > aliasbboxmax[0]) aliasbboxmax[0] = tempvert[i].v[0];
664                 if (tempvert[i].v[1] > aliasbboxmax[1]) aliasbboxmax[1] = tempvert[i].v[1];
665                 if (tempvert[i].v[2] > aliasbboxmax[2]) aliasbboxmax[2] = tempvert[i].v[2];
666         }
667         // calculate surface normals
668         tris = temptris;
669         for (i = 0;i < numtris;i++)
670         {
671                 VectorSubtract(tempvert[tris->v[0]].v, tempvert[tris->v[1]].v, t1);
672                 VectorSubtract(tempvert[tris->v[2]].v, tempvert[tris->v[1]].v, t2);
673                 CrossProduct(t1, t2, tris->normal);
674                 VectorNormalize(tris->normal);
675                 // add surface normal to vertices
676                 for (j = 0;j < 3;j++)
677                 {
678                         VectorAdd(tris->normal, tempvert[tris->v[j]].normal, tempvert[tris->v[j]].normal);
679                         tempvert[tris->v[j]].count++;
680                 }
681                 tris++;
682         }
683         // average normals and write out 1.7bit format
684         for (i = 0;i < pheader->numtris;i++)
685         {
686                 VectorNormalize(tempvert[i].normal);
687                 out[i].n[0] = (signed char) (tempvert[i].normal[0] * 127.0);
688                 out[i].n[1] = (signed char) (tempvert[i].normal[1] * 127.0);
689                 out[i].n[2] = (signed char) (tempvert[i].normal[2] * 127.0);
690         }
691         */
692 }
693
694 /*
695 =================
696 Mod_LoadQ2AliasModel
697 =================
698 */
699 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
700 {
701         int                                     i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
702         md2_t                           *pinmodel;
703         md2mem_t                        *pheader;
704         md2triangle_t           *pintriangles, *pouttriangles;
705         md2frame_t                      *pinframe;
706         md2memframe_t           *poutframe;
707         char                            *pinskins;
708 //      temptris_t                      *tris;
709
710         start = Hunk_LowMark ();
711
712 //      if (!temptris)
713 //              temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
714
715         pinmodel = (md2_t *)buffer;
716
717         version = LittleLong (pinmodel->version);
718         if (version != MD2ALIAS_VERSION)
719                 Host_Error ("%s has wrong version number (%i should be %i)",
720                                  mod->name, version, MD2ALIAS_VERSION);
721
722         mod->type = mod_alias;
723         mod->aliastype = ALIASTYPE_MD2;
724
725         framesize = sizeof(md2memframesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivert2);
726         // LordHavoc: calculate size for in memory version
727         size = sizeof(md2mem_t)
728                  + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
729                  + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
730                  + LittleLong(pinmodel->num_frames) * framesize
731                  + LittleLong(pinmodel->num_glcmds) * sizeof(int);
732         if (size <= 0 || size >= MD2MAX_SIZE)
733                 Host_Error ("%s is not a valid model", mod->name);
734         pheader = Hunk_AllocName (size, loadname);
735         
736         mod->flags = 0; // there are no MD2 flags
737         mod->numframes = LittleLong(pinmodel->num_frames);
738         mod->synctype = ST_RAND;
739         mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
740
741         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
742                 Host_Error ("%s is not a valid model", mod->name);
743         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
744                 Host_Error ("%s is not a valid model", mod->name);
745         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
746                 Host_Error ("%s is not a valid model", mod->name);
747         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
748                 Host_Error ("%s is not a valid model", mod->name);
749         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
750                 Host_Error ("%s is not a valid model", mod->name);
751
752         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
753                 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
754         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
755                 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
756         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
757                 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
758         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MD2MAX_SKINS)
759                 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
760
761         pheader->framesize = framesize;
762         pheader->num_skins = LittleLong(pinmodel->num_skins);
763         pheader->num_xyz = LittleLong(pinmodel->num_xyz);
764         pheader->num_st = LittleLong(pinmodel->num_st);
765         pheader->num_tris = LittleLong(pinmodel->num_tris);
766         pheader->num_frames = LittleLong(pinmodel->num_frames);
767         pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
768
769 // load the skins
770         if (pheader->num_skins)
771         {
772                 int *skin, *skinrange;
773                 skinrange = loadmodel->skinanimrange;
774                 skin = loadmodel->skinanim;
775 //              skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);  
776 //              skin = skinrange + pheader->num_skins * 2;
777 //              loadmodel->skinanimrange = (int) skinrange - (int) pheader;
778 //              loadmodel->skinanim = (int) skin - (int) pheader;
779                 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
780                 for (i = 0;i < pheader->num_skins;i++)
781                 {
782                         *skinrange++ = i;
783                         *skinrange++ = 1;
784                         *skin++ = loadtextureimage (pinskins, 0, 0, true, true);
785                         *skin++ = 0; // the extra 4 layers are currently unused
786                         *skin++ = 0;
787                         *skin++ = 0;
788                         *skin++ = 0;
789                         pinskins += MD2MAX_SKINNAME;
790                 }
791         }
792         loadmodel->numskins = pheader->num_skins;
793
794 // load triangles
795         pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
796         pouttriangles = (void*)&pheader[1];
797         pheader->ofs_tris = (int) pouttriangles - (int) pheader;
798 //      tris = temptris;
799         // swap the triangle list
800         for (i=0 ; i<pheader->num_tris ; i++)
801         {
802                 for (j=0 ; j<3 ; j++)
803                 {
804                         temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
805                         pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
806                         if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
807                                 Host_Error ("%s has invalid vertex indices", mod->name);
808                         if (pouttriangles->index_st[j] >= pheader->num_st)
809                                 Host_Error ("%s has invalid vertex indices", mod->name);
810                 }
811                 pintriangles++;
812                 pouttriangles++;
813         }
814
815         // LordHavoc: doing proper bbox for model
816         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
817         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
818
819 // load the frames
820         pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
821         poutframe = (void*) pouttriangles;
822         pheader->ofs_frames = (int) poutframe - (int) pheader;
823         for (i=0 ; i<pheader->num_frames ; i++)
824         {
825                 for (j = 0;j < 3;j++)
826                 {
827                         poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
828                         poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
829                 }
830                 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
831                 pinframe = (void*) &pinframe->verts[j];
832                 poutframe = (void*) &poutframe->verts[j];
833         }
834
835         // LordHavoc: model bbox
836         for (j = 0;j < 3;j++)
837         {
838                 mod->mins[j] = aliasbboxmin[j];
839                 mod->maxs[j] = aliasbboxmax[j];
840         }
841
842         // load the draw list
843         pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
844         poutglcmd = (void*) poutframe;
845         pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
846         for (i = 0;i < pheader->num_glcmds;i++)
847                 *poutglcmd++ = LittleLong(*pinglcmd++);
848
849 // move the complete, relocatable alias model to the cache
850         end = Hunk_LowMark ();
851         total = end - start;
852         
853         Cache_Alloc (&mod->cache, total, loadname);
854         if (!mod->cache.data)
855                 return;
856         memcpy (mod->cache.data, pheader, total);
857
858         Hunk_FreeToLowMark (start);
859 }