]> icculus.org git repositories - divverent/darkplaces.git/blob - model_alias.c
-safe now does something approximately close to correct. Not a full fix
[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
228 static void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, rtexture_t **skintex)
229 {
230         skintex[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, r_mipskins.value, true);
231         skintex[1] = NULL;
232         skintex[2] = NULL;
233         skintex[3] = loadtextureimage(va("%s_glow"  , basename), 0, 0, false, r_mipskins.value, true);
234         skintex[4] = NULL;
235         if (skintex[0])
236         {
237                 skintex[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, r_mipskins.value, true);
238                 skintex[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, r_mipskins.value, true);
239         }
240         else
241         {
242                 skintex[0] = loadtextureimage(basename, 0, 0, false, true, true);
243                 if (!skintex[0])
244                 {
245                         skintex[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
246                         skintex[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
247                         skintex[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), true); // glow
248                         if (skintex[1] || skintex[2])
249                         {
250                                 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
251                                 skintex[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), true); // body (normal + pants + shirt, but not glow)
252                         }
253                         else
254                                 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // no special colors
255                 }
256         }
257         if (R_TextureHasAlpha(skintex[0]))
258                 loadmodel->flags2 |= MODF_TRANSPARENT;
259 }
260
261 /*
262 ===============
263 Mod_LoadAllSkins
264 ===============
265 */
266 static void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
267 {
268         int             i, j;
269         char    name[32];
270         int             s;
271         byte    *skin;
272         daliasskingroup_t               *pinskingroup;
273         int             groupskins;
274         daliasskininterval_t    *pinskinintervals;
275         int             skinranges, skincount, *skinrange, skinnum;
276         rtexture_t **skintex;
277         void    *temp;
278         byte    *skintemp = NULL;
279
280         skin = (byte *)(pskintype + 1);
281
282         if (numskins < 1 || numskins > MAX_SKINS)
283                 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
284
285         s = width * height;
286         skintemp = qmalloc(s);
287
288         // LordHavoc: skim the data, measure the number of skins and number of groups
289         skinranges = numskins;
290         skincount = 0;
291         temp = pskintype;
292         for (i = 0;i < numskins;i++)
293         {
294                 pskintype++;
295                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
296                 {
297                         skincount++;
298                         (byte *)pskintype += s;
299                 }
300                 else
301                 {
302                         groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
303                         skincount += groupskins;
304                         (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
305                 }
306         }
307         pskintype = temp;
308
309         skinrange = loadmodel->skinanimrange;
310         skintex = loadmodel->skinanim;
311 //      skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
312 //      skintexnum = skinrange + skinranges * 2;
313 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
314 //      loadmodel->skinanim = (int) skintexnum - (int) pheader;
315         skinnum = 0;
316         for (i = 0;i < numskins;i++)
317         {
318                 *skinrange++ = skinnum; // start of the range
319                 pskintype++;
320                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
321                 {
322                         *skinrange++ = 1; // single skin
323                         skinnum++;
324                         sprintf (name, "%s_%i", loadmodel->name, i);
325                         Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
326                         skintex += 5;
327                         pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
328                 }
329                 else
330                 {
331                         // animating skin group.  yuck.
332                         pinskingroup = (daliasskingroup_t *)pskintype;
333                         groupskins = LittleLong (pinskingroup->numskins);
334                         pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
335
336                         pskintype = (void *)(pinskinintervals + groupskins);
337
338                         *skinrange++ = groupskins; // number of skins
339                         skinnum += groupskins;
340                         for (j = 0;j < groupskins;j++)
341                         {
342                                 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
343                                 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
344                                 skintex += 5;
345                                 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
346                         }
347                 }
348         }
349         loadmodel->numskins = numskins;
350         qfree(skintemp);
351
352         return (void *)pskintype;
353 }
354
355 static void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
356 {
357         int             i;
358         for (i = 0;i < numskins;i++)
359         {
360                 pskintype++;
361                 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
362                         (byte *)pskintype += skinsize;
363                 else
364                         (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
365         }
366         return pskintype;
367 }
368
369 //=========================================================================
370
371 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
372
373 /*
374 =================
375 Mod_LoadAliasModel
376 =================
377 */
378 #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);
379 #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);
380 void Mod_LoadAliasModel (model_t *mod, void *buffer)
381 {
382         int                                     i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
383         mdl_t                           *pinmodel;
384         stvert_t                        *pinstverts;
385         dtriangle_t                     *pintriangles;
386         daliasframetype_t       *pframetype;
387         daliasskintype_t        *pskintype;
388         float                           *pouttexcoords, scales, scalet;
389         maliashdr_t                     *mheader;
390         unsigned short          *pouttris;
391         trivertx_t                      *posevert;
392         animscene_t                     *animscenes;
393
394         modelyawradius = 0;
395         modelradius = 0;
396
397         start = Hunk_LowMark ();
398
399         pinmodel = (mdl_t *)buffer;
400
401         version = LittleLong (pinmodel->version);
402         if (version != ALIAS_VERSION)
403                 Host_Error ("%s has wrong version number (%i should be %i)",
404                                  mod->name, version, ALIAS_VERSION);
405
406         mod->type = mod_alias;
407         mod->aliastype = ALIASTYPE_MDL;
408
409         numframes = LittleLong(pinmodel->numframes);
410         BOUNDI(numframes,0,65536);
411         numverts = LittleLong(pinmodel->numverts);
412         BOUNDI(numverts,0,MAXALIASVERTS);
413         numtris = LittleLong(pinmodel->numtris);
414         BOUNDI(numtris,0,MAXALIASTRIS);
415         numskins = LittleLong(pinmodel->numskins);
416         BOUNDI(numskins,0,256);
417         skinwidth = LittleLong (pinmodel->skinwidth);
418         BOUNDI(skinwidth,0,4096);
419         skinheight = LittleLong (pinmodel->skinheight);
420         BOUNDI(skinheight,0,1024);
421
422         pskintype = (daliasskintype_t *)&pinmodel[1];
423         pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
424         pintriangles = (dtriangle_t *)&pinstverts[numverts];
425         pframetype = (daliasframetype_t *)&pintriangles[numtris];
426
427         numposes = 0;
428         for (i=0 ; i<numframes ; i++)
429         {
430                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
431                 {
432                         numposes++;
433                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t)                         + (sizeof(daliasframe_t)                            + sizeof(trivertx_t) * numverts)    );
434                 }
435                 else
436                 {
437                         f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
438                         numposes += f;
439                         pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
440                 }
441         }
442
443         // rebuild the model
444         mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
445         mod->flags = LittleLong (pinmodel->flags);
446 // endian-adjust and copy the data, starting with the alias model header
447         mheader->numverts = numverts;
448         mod->numtris = mheader->numtris = numtris;
449         mod->numframes = mheader->numframes = numframes;
450         mod->synctype = LittleLong (pinmodel->synctype);
451         BOUNDI(mod->synctype,0,2);
452
453         for (i=0 ; i<3 ; i++)
454         {
455                 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
456                 BOUNDF(mheader->scale[i],0,65536);
457                 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
458                 BOUNDF(mheader->scale_origin[i],-65536,65536);
459         }
460
461         // load the skins
462         pskintype = (daliasskintype_t *)&pinmodel[1];
463         pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
464
465         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
466         pinstverts = (stvert_t *)pskintype;
467
468         // LordHavoc: byteswap and convert stvert data
469         scales = 1.0 / skinwidth;
470         scalet = 1.0 / skinheight;
471         for (i = 0;i < numverts;i++)
472         {
473                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
474                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
475                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
476                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
477                 vertst[i+numverts][1] = vertst[i][1];
478                 vertusage[i] = 0;
479                 vertusage[i+numverts] = 0;
480         }
481
482 // load triangle data
483         pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
484         mheader->tridata = (int) pouttris - (int) mheader;
485         pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
486
487         // count the vertices used
488         for (i = 0;i < numverts*2;i++)
489                 vertusage[i] = 0;
490         for (i = 0;i < numtris;i++)
491         {
492                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
493                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
494                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
495                 if (!LittleLong(pintriangles[i].facesfront)) // backface
496                 {
497                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
498                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
499                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
500                 }
501                 vertusage[temptris[i][0]]++;
502                 vertusage[temptris[i][1]]++;
503                 vertusage[temptris[i][2]]++;
504         }
505         // build remapping table and compact array
506         totalverts = 0;
507         for (i = 0;i < numverts*2;i++)
508         {
509                 if (vertusage[i])
510                 {
511                         vertremap[i] = totalverts;
512                         vertst[totalverts][0] = vertst[i][0];
513                         vertst[totalverts][1] = vertst[i][1];
514                         totalverts++;
515                 }
516                 else
517                         vertremap[i] = -1; // not used at all
518         }
519         mheader->numverts = totalverts;
520         // remap the triangle references
521         for (i = 0;i < numtris;i++)
522         {
523                 *pouttris++ = vertremap[temptris[i][0]];
524                 *pouttris++ = vertremap[temptris[i][1]];
525                 *pouttris++ = vertremap[temptris[i][2]];
526         }
527         // store the texture coordinates
528         pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
529         mheader->texdata = (int) pouttexcoords - (int) mheader;
530         for (i = 0;i < totalverts;i++)
531         {
532                 *pouttexcoords++ = vertst[i][0];
533                 *pouttexcoords++ = vertst[i][1];
534         }
535
536 // load the frames
537         posenum = 0;
538         posevert = Hunk_AllocName(sizeof(trivertx_t) * numposes * totalverts, va("%s vertex data", loadname));
539         mheader->posedata = (int) posevert - (int) mheader;
540         pframetype = (daliasframetype_t *)&pintriangles[numtris];
541
542         // LordHavoc: doing proper bbox for model
543         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
544         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
545
546         animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
547
548         for (i = 0;i < numframes;i++)
549         {
550                 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
551                         pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
552                 else
553                         pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
554         }
555
556         mod->ofs_scenes = (int) animscenes - (int) mheader;
557
558         // LordHavoc: fixed model bbox - was //FIXME: do this right
559         //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
560         //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
561         modelyawradius = sqrt(modelyawradius);
562         modelradius = sqrt(modelradius);
563 //      mod->modelradius = modelradius;
564         for (j = 0;j < 3;j++)
565         {
566                 mod->normalmins[j] = aliasbboxmin[j];
567                 mod->normalmaxs[j] = aliasbboxmax[j];
568                 mod->rotatedmins[j] = -modelradius;
569                 mod->rotatedmaxs[j] = modelradius;
570         }
571         mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
572         mod->yawmins[2] = mod->normalmins[2];
573         mod->yawmaxs[2] = mod->normalmaxs[2];
574
575         mod->SERAddEntity = Mod_Alias_SERAddEntity;
576         mod->DrawEarly = NULL;
577         mod->DrawLate = R_DrawAliasModel;
578         mod->DrawShadow = NULL;
579
580 // move the complete, relocatable alias model to the cache
581         end = Hunk_LowMark ();
582         mod->cachesize = total = end - start;
583
584         Cache_Alloc (&mod->cache, total, loadname);
585         if (!mod->cache.data)
586                 return;
587         memcpy (mod->cache.data, mheader, total);
588
589         Hunk_FreeToLowMark (start);
590 }
591
592 static void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
593 {
594         int i, invalidnormals = 0;
595         float dist;
596         vec3_t temp;
597         for (i = 0;i < numverts;i++)
598         {
599                 VectorCopy(v[i].v, out[i].v);
600                 temp[0] = v[i].v[0] * scale[0] + translate[0];
601                 temp[1] = v[i].v[1] * scale[1] + translate[1];
602                 temp[2] = v[i].v[2] * scale[2] + translate[2];
603                 // update bounding box
604                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
605                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
606                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
607                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
608                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
609                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
610                 dist = temp[0]*temp[0]+temp[1]*temp[1];
611                 if (modelyawradius < dist)
612                         modelyawradius = dist;
613                 dist += temp[2]*temp[2];
614                 if (modelradius < dist)
615                         modelradius = dist;
616                 out[i].lightnormalindex = v[i].lightnormalindex;
617                 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
618                 {
619                         invalidnormals++;
620                         out[i].lightnormalindex = 0;
621                 }
622         }
623         if (invalidnormals)
624                 Con_Printf("Mod_ConvertQ2AliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
625 }
626
627 /*
628 =================
629 Mod_LoadQ2AliasModel
630 =================
631 */
632 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
633 {
634         int                                     i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
635         md2_t                           *pinmodel;
636         md2mem_t                        *pheader;
637         md2triangle_t           *pintriangles, *pouttriangles;
638         md2frame_t                      *pinframe;
639         md2frame_t                      *poutframe;
640         char                            *pinskins;
641 //      temptris_t                      *tris;
642         animscene_t                     *animscenes;
643
644         modelyawradius = 0;
645         modelradius = 0;
646
647         start = Hunk_LowMark ();
648
649 //      if (!temptris)
650 //              temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
651
652         pinmodel = (md2_t *)buffer;
653
654         version = LittleLong (pinmodel->version);
655         if (version != MD2ALIAS_VERSION)
656                 Host_Error ("%s has wrong version number (%i should be %i)",
657                         mod->name, version, MD2ALIAS_VERSION);
658
659         mod->type = mod_alias;
660         mod->aliastype = ALIASTYPE_MD2;
661
662         framesize = sizeof(md2framesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivertx_t);
663         // LordHavoc: calculate size for in memory version
664         size = sizeof(md2mem_t)
665                  + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
666                  + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
667                  + LittleLong(pinmodel->num_frames) * framesize
668                  + LittleLong(pinmodel->num_glcmds) * sizeof(int);
669         if (size <= 0 || size >= MD2MAX_SIZE)
670                 Host_Error ("%s is not a valid model", mod->name);
671         pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
672
673         mod->flags = 0; // there are no MD2 flags
674         mod->numframes = LittleLong(pinmodel->num_frames);
675         mod->synctype = ST_RAND;
676         mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
677
678         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
679                 Host_Error ("%s is not a valid model", mod->name);
680         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
681                 Host_Error ("%s is not a valid model", mod->name);
682         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
683                 Host_Error ("%s is not a valid model", mod->name);
684         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
685                 Host_Error ("%s is not a valid model", mod->name);
686         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
687                 Host_Error ("%s is not a valid model", mod->name);
688
689         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
690                 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
691         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
692                 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
693         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
694                 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
695         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
696                 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
697
698         pheader->framesize = framesize;
699         pheader->num_skins = LittleLong(pinmodel->num_skins);
700         pheader->num_xyz = LittleLong(pinmodel->num_xyz);
701         pheader->num_st = LittleLong(pinmodel->num_st);
702         pheader->num_tris = LittleLong(pinmodel->num_tris);
703         pheader->num_frames = LittleLong(pinmodel->num_frames);
704         pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
705
706 // load the skins
707         if (pheader->num_skins)
708         {
709                 rtexture_t **skin;
710                 int *skinrange;
711                 skinrange = loadmodel->skinanimrange;
712                 skin = loadmodel->skinanim;
713 //              skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
714 //              skin = skinrange + pheader->num_skins * 2;
715 //              loadmodel->skinanimrange = (int) skinrange - (int) pheader;
716 //              loadmodel->skinanim = (int) skin - (int) pheader;
717                 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
718                 for (i = 0;i < pheader->num_skins;i++)
719                 {
720                         *skinrange++ = i;
721                         *skinrange++ = 1;
722                         *skin++ = loadtextureimage (pinskins, 0, 0, true, r_mipskins.value, true);
723                         *skin++ = NULL; // the extra 4 layers are currently unused
724                         *skin++ = NULL;
725                         *skin++ = NULL;
726                         *skin++ = NULL;
727                         pinskins += MD2MAX_SKINNAME;
728                 }
729         }
730         loadmodel->numskins = pheader->num_skins;
731
732 // load triangles
733         pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
734         pouttriangles = (void*)&pheader[1];
735         pheader->ofs_tris = (int) pouttriangles - (int) pheader;
736 //      tris = temptris;
737         // swap the triangle list
738         for (i = 0;i < pheader->num_tris;i++)
739         {
740                 for (j = 0;j < 3;j++)
741                 {
742                         temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
743                         pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
744                         if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
745                                 Host_Error ("%s has invalid vertex indices", mod->name);
746                         if (pouttriangles->index_st[j] >= pheader->num_st)
747                                 Host_Error ("%s has invalid vertex indices", mod->name);
748                 }
749                 pintriangles++;
750                 pouttriangles++;
751         }
752
753         // LordHavoc: doing proper bbox for model
754         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
755         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
756
757 // load the frames
758         pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
759         poutframe = (void*) pouttriangles;
760         pheader->ofs_frames = (int) poutframe - (int) pheader;
761
762         animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
763
764         for (i = 0;i < pheader->num_frames;i++)
765         {
766                 strcpy(poutframe->name, pinframe->name);
767                 for (j = 0;j < 3;j++)
768                 {
769                         poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
770                         poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
771                 }
772                 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
773
774                 strcpy(animscenes[i].name, poutframe->name);
775                 animscenes[i].firstframe = i;
776                 animscenes[i].framecount = 1;
777                 animscenes[i].framerate = 10;
778                 animscenes[i].loop = true;
779
780                 pinframe = (void*) &pinframe->verts[j];
781                 poutframe = (void*) &poutframe->verts[j];
782         }
783
784         mod->ofs_scenes = (int) animscenes - (int) pheader;
785
786         // LordHavoc: model bbox
787         modelyawradius = sqrt(modelyawradius);
788         modelradius = sqrt(modelradius);
789 //      mod->modelradius = modelradius;
790         for (j = 0;j < 3;j++)
791         {
792                 mod->normalmins[j] = aliasbboxmin[j];
793                 mod->normalmaxs[j] = aliasbboxmax[j];
794                 mod->rotatedmins[j] = -modelradius;
795                 mod->rotatedmaxs[j] = modelradius;
796         }
797         mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
798         mod->yawmins[2] = mod->normalmins[2];
799         mod->yawmaxs[2] = mod->normalmaxs[2];
800
801         // load the draw list
802         pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
803         poutglcmd = (void*) poutframe;
804         pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
805         for (i = 0;i < pheader->num_glcmds;i++)
806                 *poutglcmd++ = LittleLong(*pinglcmd++);
807
808         mod->SERAddEntity = Mod_Alias_SERAddEntity;
809         mod->DrawEarly = NULL;
810         mod->DrawLate = R_DrawAliasModel;
811         mod->DrawShadow = NULL;
812
813 // move the complete, relocatable alias model to the cache
814         end = Hunk_LowMark ();
815         mod->cachesize = total = end - start;
816
817         Cache_Alloc (&mod->cache, total, loadname);
818         if (!mod->cache.data)
819                 return;
820         memcpy (mod->cache.data, pheader, total);
821
822         Hunk_FreeToLowMark (start);
823 }
824
825 static void swapintblock(int *m, int size)
826 {
827         size /= 4;
828         while(size--)
829         {
830                 *m = BigLong(*m);
831                 m++;
832         }
833 }
834
835 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
836 {
837         int i, pbase, start, end, total, *skinrange;
838         rtexture_t **texture, **skin;
839         char *shadername;
840         zymtype1header_t *pinmodel, *pheader;
841         zymscene_t *scene;
842         zymbone_t *bone;
843         animscene_t *animscenes;
844         float corner[2], modelradius;
845
846         start = Hunk_LowMark ();
847
848         pinmodel = (void *)buffer;
849
850         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
851                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
852
853         if (BigLong(pinmodel->type) != 1)
854                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
855
856         mod->type = mod_alias;
857         mod->aliastype = ALIASTYPE_ZYM;
858
859         pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
860
861         pbase = (int) pheader;
862
863         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
864
865         // byteswap header
866         memcpy(pheader->id, pinmodel->id, 12);
867         pheader->type = BigLong(pheader->type);
868         pheader->filesize = BigLong(pheader->filesize);
869         pheader->mins[0] = BigFloat(pheader->mins[0]);
870         pheader->mins[1] = BigFloat(pheader->mins[1]);
871         pheader->mins[2] = BigFloat(pheader->mins[2]);
872         pheader->maxs[0] = BigFloat(pheader->maxs[0]);
873         pheader->maxs[1] = BigFloat(pheader->maxs[1]);
874         pheader->maxs[2] = BigFloat(pheader->maxs[2]);
875         pheader->radius = BigFloat(pheader->radius);
876         pheader->numverts = BigLong(pheader->numverts);
877         pheader->numtris = BigLong(pheader->numtris);
878         pheader->numshaders = BigLong(pheader->numshaders);
879         pheader->numbones = BigLong(pheader->numbones);
880         pheader->numscenes = BigLong(pheader->numscenes);
881
882
883         pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
884         pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
885         pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
886         pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
887         pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
888         pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
889         pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
890         pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
891         pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
892
893         mod->flags = 0; // there are no flags
894         mod->numframes = pheader->numscenes;
895         mod->synctype = ST_SYNC;
896         mod->numtris = pheader->numtris;
897
898         // FIXME: add skin support and texturing and shaders and...
899 // load the skins
900         skinrange = loadmodel->skinanimrange;
901         skin = loadmodel->skinanim;
902 //      skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
903 //      skin = skinrange + pheader->num_skins * 2;
904 //      loadmodel->skinanimrange = (int) skinrange - (int) pheader;
905 //      loadmodel->skinanim = (int) skin - (int) pheader;
906         *skinrange++ = 0;
907         *skinrange++ = 1;
908         *skin++ = NULL;
909         *skin++ = NULL;
910         *skin++ = NULL;
911         *skin++ = NULL;
912         *skin++ = NULL;
913         loadmodel->numskins = 1;
914
915         // go through the lumps, swapping things
916
917 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
918         scene = (void *) (pheader->lump_scenes.start + pbase);
919         animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
920         for (i = 0;i < pheader->numscenes;i++)
921         {
922                 scene->mins[0] = BigFloat(scene->mins[0]);
923                 scene->mins[1] = BigFloat(scene->mins[1]);
924                 scene->mins[2] = BigFloat(scene->mins[2]);
925                 scene->maxs[0] = BigFloat(scene->maxs[0]);
926                 scene->maxs[1] = BigFloat(scene->maxs[1]);
927                 scene->maxs[2] = BigFloat(scene->maxs[2]);
928                 scene->radius = BigFloat(scene->radius);
929                 scene->framerate = BigFloat(scene->framerate);
930                 scene->flags = BigLong(scene->flags);
931                 scene->start = BigLong(scene->start);
932                 scene->length = BigLong(scene->length);
933
934                 memcpy(animscenes[i].name, scene->name, 32);
935                 animscenes[i].firstframe = scene->start;
936                 animscenes[i].framecount = scene->length;
937                 animscenes[i].framerate = scene->framerate;
938                 animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
939
940                 scene++;
941         }
942         mod->ofs_scenes = (int) animscenes - pbase;
943
944 //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
945         swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
946
947 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
948         bone = (void *) (pheader->lump_bones.start + pbase);
949         for (i = 0;i < pheader->numbones;i++)
950         {
951                 bone->flags = BigLong(bone->flags);
952                 bone->parent = BigLong(bone->parent);
953                 bone++;
954         }
955
956 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
957         swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
958
959 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
960         swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
961
962 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
963         swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
964
965 //      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)
966         swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
967
968 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
969         shadername = (void *) (pheader->lump_shaders.start + pbase);
970         texture = (void *) shadername;
971         for (i = 0;i < pheader->numshaders;i++)
972         {
973                 rtexture_t *rt;
974                 rt = loadtextureimage(shadername, 0, 0, true, r_mipskins.value, true);
975                 shadername += 32;
976                 *texture++ = rt; // reuse shader name list for texture pointers
977         }
978
979 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
980         swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
981
982         // model bbox
983         modelradius = pheader->radius;
984 //      mod->modelradius = pheader->radius;
985         for (i = 0;i < 3;i++)
986         {
987                 mod->normalmins[i] = pheader->mins[i];
988                 mod->normalmaxs[i] = pheader->maxs[i];
989                 mod->rotatedmins[i] = -modelradius;
990                 mod->rotatedmaxs[i] = modelradius;
991         }
992         corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
993         corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
994         mod->yawmaxs[0] = mod->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
995         if (mod->yawmaxs[0] > modelradius)
996                 mod->yawmaxs[0] = mod->yawmaxs[1] = modelradius;
997         mod->yawmins[0] = mod->yawmins[1] = -mod->yawmaxs[0];
998         mod->yawmins[2] = mod->normalmins[2];
999         mod->yawmaxs[2] = mod->normalmaxs[2];
1000
1001         mod->SERAddEntity = Mod_Alias_SERAddEntity;
1002         mod->DrawEarly = NULL;
1003         mod->DrawLate = R_DrawAliasModel;
1004         mod->DrawShadow = NULL;
1005
1006 // move the complete, relocatable alias model to the cache
1007         end = Hunk_LowMark ();
1008         mod->cachesize = total = end - start;
1009
1010         Cache_Alloc (&mod->cache, total, loadname);
1011         if (!mod->cache.data)
1012                 return;
1013         memcpy (mod->cache.data, pheader, total);
1014
1015         Hunk_FreeToLowMark (start);
1016 }