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