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