renamed byte to qbyte throughout engine to eliminate a mingw conflict
[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 void Mod_AliasInit (void)
26 {
27         Cvar_RegisterVariable(&r_mipskins);
28 }
29
30 static void Mod_Alias_SERAddEntity(void)
31 {
32         R_Clip_AddBox(currentrenderentity->mins, currentrenderentity->maxs, R_Entity_Callback, currentrenderentity, NULL);
33 }
34
35 // LordHavoc: proper bounding box considerations
36 static float aliasbboxmin[3], aliasbboxmax[3], modelyawradius, modelradius;
37
38 static float vertst[MAXALIASVERTS][2];
39 static int vertusage[MAXALIASVERTS];
40 static int vertonseam[MAXALIASVERTS];
41 static int vertremap[MAXALIASVERTS];
42 static int temptris[MAXALIASTRIS][3];
43
44 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
45 {
46         int i, j, invalidnormals = 0;
47         float dist;
48         vec3_t temp;
49         for (i = 0;i < inverts;i++)
50         {
51                 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
52                         continue;
53
54                 temp[0] = v[i].v[0] * scale[0] + translate[0];
55                 temp[1] = v[i].v[1] * scale[1] + translate[1];
56                 temp[2] = v[i].v[2] * scale[2] + translate[2];
57                 // update bounding box
58                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
59                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
60                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
61                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
62                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
63                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
64                 dist = temp[0]*temp[0]+temp[1]*temp[1];
65                 if (modelyawradius < dist)
66                         modelyawradius = dist;
67                 dist += temp[2]*temp[2];
68                 if (modelradius < dist)
69                         modelradius = dist;
70
71                 j = vertremap[i]; // not onseam
72                 if (j >= 0)
73                 {
74                         VectorCopy(v[i].v, out[j].v);
75                         out[j].lightnormalindex = v[i].lightnormalindex;
76                         if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
77                         {
78                                 invalidnormals++;
79                                 out[j].lightnormalindex = 0;
80                         }
81                 }
82                 j = vertremap[i+inverts]; // onseam
83                 if (j >= 0)
84                 {
85                         VectorCopy(v[i].v, out[j].v);
86                         out[j].lightnormalindex = v[i].lightnormalindex;
87                         if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
88                         {
89                                 invalidnormals++;
90                                 out[j].lightnormalindex = 0;
91                         }
92                 }
93         }
94         if (invalidnormals)
95                 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
96 }
97
98 static void Mod_MDL_LoadFrames (long datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate)
99 {
100         daliasframetype_t       *pframetype;
101         daliasframe_t           *pinframe;
102         daliasgroup_t           *group;
103         daliasinterval_t        *intervals;
104         int                                     i, f, pose, groupframes;
105         float                           interval;
106         animscene_t                     *scene;
107         pose = 0;
108         scene = loadmodel->animscenes;
109         for (f = 0;f < loadmodel->numframes;f++)
110         {
111                 pframetype = (daliasframetype_t *)datapointer;
112                 datapointer += sizeof(daliasframetype_t);
113                 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
114                 {
115                         // a single frame is still treated as a group
116                         interval = 0.1f;
117                         groupframes = 1;
118                 }
119                 else
120                 {
121                         // read group header
122                         group = (daliasgroup_t *)datapointer;
123                         datapointer += sizeof(daliasgroup_t);
124                         groupframes = LittleLong (group->numframes);
125
126                         // intervals (time per frame)
127                         intervals = (daliasinterval_t *)datapointer;
128                         datapointer += sizeof(daliasinterval_t) * groupframes;
129
130                         interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
131                         if (interval < 0.01f)
132                                 Host_Error("Mod_LoadAliasGroup: invalid interval");
133                 }
134
135                 // get scene name from first frame
136                 pinframe = (daliasframe_t *)datapointer;
137
138                 strcpy(scene->name, pinframe->name);
139                 scene->firstframe = pose;
140                 scene->framecount = groupframes;
141                 scene->framerate = 1.0f / interval;
142                 scene->loop = true;
143                 scene++;
144
145                 // read frames
146                 for (i = 0;i < groupframes;i++)
147                 {
148                         pinframe = (daliasframe_t *)datapointer;
149                         datapointer += sizeof(daliasframe_t);
150
151                         // convert to MD2 frame headers
152                         strcpy(loadmodel->mdlmd2data_frames[pose].name, pinframe->name);
153                         VectorCopy(scale, loadmodel->mdlmd2data_frames[pose].scale);
154                         VectorCopy(translate, loadmodel->mdlmd2data_frames[pose].translate);
155
156                         Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->mdlmd2data_pose + pose * outverts);
157                         datapointer += sizeof(trivertx_t) * inverts;
158                         pose++;
159                 }
160         }
161 }
162
163 static rtexture_t *GL_SkinSplitShirt(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
164 {
165         int i, pixels, passed;
166         qbyte pixeltest[16];
167         for (i = 0;i < 16;i++)
168                 pixeltest[i] = (bits & (1 << i)) != 0;
169         pixels = width*height;
170         passed = 0;
171         while(pixels--)
172         {
173                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
174                 {
175                         passed++;
176                         // turn to white while copying
177                         if (*in >= 128 && *in < 224) // backwards ranges
178                                 *out = (*in & 15) ^ 15;
179                         else
180                                 *out = *in & 15;
181                 }
182                 else
183                         *out = 0;
184                 in++;
185                 out++;
186         }
187         if (passed)
188                 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
189         else
190                 return NULL;
191 }
192
193 static rtexture_t *GL_SkinSplit(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
194 {
195         int i, pixels, passed;
196         qbyte pixeltest[16];
197         for (i = 0;i < 16;i++)
198                 pixeltest[i] = (bits & (1 << i)) != 0;
199         pixels = width*height;
200         passed = 0;
201         while(pixels--)
202         {
203                 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
204                 {
205                         passed++;
206                         *out = *in;
207                 }
208                 else
209                         *out = 0;
210                 in++;
211                 out++;
212         }
213         if (passed)
214                 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
215         else
216                 return NULL;
217 }
218
219 static void Mod_LoadSkin (char *basename, qbyte *skindata, qbyte *skintemp, int width, int height, skinframe_t *skinframe, int precache)
220 {
221         skinframe->base   = loadtextureimagewithmask(loadmodel->texturepool, va("%s_normal", basename), 0, 0, false, r_mipskins.integer, precache);
222         skinframe->fog    = image_masktex;
223         skinframe->pants  = NULL;
224         skinframe->shirt  = NULL;
225         skinframe->glow   = loadtextureimage(loadmodel->texturepool, va("%s_glow"  , basename), 0, 0, false, r_mipskins.integer, precache);
226         skinframe->merged = NULL;
227         if (skinframe->base)
228         {
229                 skinframe->pants  = loadtextureimage(loadmodel->texturepool, va("%s_pants" , basename), 0, 0, false, r_mipskins.integer, precache);
230                 skinframe->shirt  = loadtextureimage(loadmodel->texturepool, va("%s_shirt" , basename), 0, 0, false, r_mipskins.integer, precache);
231         }
232         else
233         {
234                 skinframe->base   = loadtextureimagewithmask(loadmodel->texturepool, basename, 0, 0, false, true, true);
235                 skinframe->fog    = image_masktex;
236                 if (!skinframe->base)
237                 {
238                         skinframe->pants  = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
239                         skinframe->shirt  = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
240                         skinframe->glow   = GL_SkinSplit     (skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), precache); // glow
241                         if (skinframe->pants || skinframe->shirt)
242                         {
243                                 skinframe->base   = GL_SkinSplit (skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
244                                 skinframe->merged = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), precache); // body (normal + pants + shirt, but not glow)
245                         }
246                         else
247                                 skinframe->base   = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), precache); // no special colors
248                         // quake model skins don't have alpha
249                         skinframe->fog = NULL;
250                 }
251         }
252 }
253
254 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)\n", loadmodel->name, VALUE, MIN, MAX);
255 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%f exceeds %f - %f)\n", loadmodel->name, VALUE, MIN, MAX);
256 void Mod_LoadAliasModel (model_t *mod, void *buffer)
257 {
258         int                                             i, j, version, numverts, totalposes, totalskins, skinwidth, skinheight, totalverts, groupframes, groupskins;
259         mdl_t                                   *pinmodel;
260         stvert_t                                *pinstverts;
261         dtriangle_t                             *pintriangles;
262         daliasskintype_t                *pinskintype;
263         daliasskingroup_t               *pinskingroup;
264         daliasskininterval_t    *pinskinintervals;
265         daliasframetype_t               *pinframetype;
266         daliasgroup_t                   *pinframegroup;
267         float                                   scales, scalet, scale[3], translate[3], interval;
268         long                                    datapointer, startframes, startskins;
269         char                                    name[MAX_QPATH];
270         qbyte                                   *skintemp = NULL;
271         modelyawradius = 0;
272         modelradius = 0;
273
274         datapointer = (long) buffer;
275         pinmodel = (mdl_t *)datapointer;
276         datapointer += sizeof(mdl_t);
277
278         version = LittleLong (pinmodel->version);
279         if (version != ALIAS_VERSION)
280                 Host_Error ("%s has wrong version number (%i should be %i)",
281                                  loadmodel->name, version, ALIAS_VERSION);
282
283         loadmodel->type = mod_alias;
284         loadmodel->aliastype = ALIASTYPE_MDLMD2;
285
286         loadmodel->numskins = LittleLong(pinmodel->numskins);
287         BOUNDI(loadmodel->numskins,0,256);
288         skinwidth = LittleLong (pinmodel->skinwidth);
289         BOUNDI(skinwidth,0,4096);
290         skinheight = LittleLong (pinmodel->skinheight);
291         BOUNDI(skinheight,0,4096);
292         loadmodel->numverts = numverts = LittleLong(pinmodel->numverts);
293         BOUNDI(loadmodel->numverts,0,MAXALIASVERTS);
294         loadmodel->numtris = LittleLong(pinmodel->numtris);
295         BOUNDI(loadmodel->numtris,0,MAXALIASTRIS);
296         loadmodel->numframes = LittleLong(pinmodel->numframes);
297         BOUNDI(loadmodel->numframes,0,65536);
298         loadmodel->synctype = LittleLong (pinmodel->synctype);
299         BOUNDI(loadmodel->synctype,0,2);
300         loadmodel->flags = LittleLong (pinmodel->flags);
301
302         for (i = 0;i < 3;i++)
303         {
304                 scale[i] = LittleFloat (pinmodel->scale[i]);
305                 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
306         }
307
308         startskins = datapointer;
309         totalskins = 0;
310         for (i = 0;i < loadmodel->numskins;i++)
311         {
312                 pinskintype = (daliasskintype_t *)datapointer;
313                 datapointer += sizeof(daliasskintype_t);
314                 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
315                         groupskins = 1;
316                 else
317                 {
318                         pinskingroup = (daliasskingroup_t *)datapointer;
319                         datapointer += sizeof(daliasskingroup_t);
320                         groupskins = LittleLong(pinskingroup->numskins);
321                         datapointer += sizeof(daliasskininterval_t) * groupskins;
322                 }
323
324                 for (j = 0;j < groupskins;j++)
325                 {
326                         datapointer += skinwidth * skinheight;
327                         totalskins++;
328                 }
329         }
330
331         pinstverts = (stvert_t *)datapointer;
332         datapointer += sizeof(stvert_t) * numverts;
333
334         pintriangles = (dtriangle_t *)datapointer;
335         datapointer += sizeof(dtriangle_t) * loadmodel->numtris;
336
337         startframes = datapointer;
338         totalposes = 0;
339         for (i = 0;i < loadmodel->numframes;i++)
340         {
341                 pinframetype = (daliasframetype_t *)datapointer;
342                 datapointer += sizeof(daliasframetype_t);
343                 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
344                         groupframes = 1;
345                 else
346                 {
347                         pinframegroup = (daliasgroup_t *)datapointer;
348                         datapointer += sizeof(daliasgroup_t);
349                         groupframes = LittleLong(pinframegroup->numframes);
350                         datapointer += sizeof(daliasinterval_t) * groupframes;
351                 }
352
353                 for (j = 0;j < groupframes;j++)
354                 {
355                         datapointer += sizeof(daliasframe_t);
356                         datapointer += sizeof(trivertx_t) * numverts;
357                         totalposes++;
358                 }
359         }
360
361         // load the skins
362         skintemp = Mem_Alloc(tempmempool, skinwidth * skinheight);
363         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
364         loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
365         totalskins = 0;
366         datapointer = startskins;
367         for (i = 0;i < loadmodel->numskins;i++)
368         {
369                 pinskintype = (daliasskintype_t *)datapointer;
370                 datapointer += sizeof(daliasskintype_t);
371
372                 if (pinskintype->type == ALIAS_SKIN_SINGLE)
373                 {
374                         groupskins = 1;
375                         interval = 0.1f;
376                 }
377                 else
378                 {
379                         pinskingroup = (daliasskingroup_t *)datapointer;
380                         datapointer += sizeof(daliasskingroup_t);
381
382                         groupskins = LittleLong (pinskingroup->numskins);
383
384                         pinskinintervals = (daliasskininterval_t *)datapointer;
385                         datapointer += sizeof(daliasskininterval_t) * groupskins;
386
387                         interval = LittleFloat(pinskinintervals[0].interval);
388                         if (interval < 0.01f)
389                                 Host_Error("Mod_LoadAliasModel: invalid interval\n");
390                 }
391
392                 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
393                 loadmodel->skinscenes[i].firstframe = totalskins;
394                 loadmodel->skinscenes[i].framecount = groupskins;
395                 loadmodel->skinscenes[i].framerate = 1.0f / interval;
396                 loadmodel->skinscenes[i].loop = true;
397
398                 for (j = 0;j < groupskins;j++)
399                 {
400                         if (groupskins > 1)
401                                 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
402                         else
403                                 sprintf (name, "%s_%i", loadmodel->name, i);
404                         Mod_LoadSkin(name, (qbyte *)datapointer, skintemp, skinwidth, skinheight, loadmodel->skinframes + totalskins, i == 0);
405                         datapointer += skinwidth * skinheight;
406                         totalskins++;
407                 }
408         }
409         Mem_Free(skintemp);
410
411         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
412         scales = 1.0 / skinwidth;
413         scalet = 1.0 / skinheight;
414         for (i = 0;i < numverts;i++)
415         {
416                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
417                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
418                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
419                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
420                 vertst[i+numverts][1] = vertst[i][1];
421                 vertusage[i] = 0;
422                 vertusage[i+numverts] = 0;
423         }
424
425 // load triangle data
426         loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->numtris);
427
428         // count the vertices used
429         for (i = 0;i < numverts*2;i++)
430                 vertusage[i] = 0;
431         for (i = 0;i < loadmodel->numtris;i++)
432         {
433                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
434                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
435                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
436                 if (!LittleLong(pintriangles[i].facesfront)) // backface
437                 {
438                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
439                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
440                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
441                 }
442                 vertusage[temptris[i][0]]++;
443                 vertusage[temptris[i][1]]++;
444                 vertusage[temptris[i][2]]++;
445         }
446         // build remapping table and compact array
447         totalverts = 0;
448         for (i = 0;i < numverts*2;i++)
449         {
450                 if (vertusage[i])
451                 {
452                         vertremap[i] = totalverts;
453                         vertst[totalverts][0] = vertst[i][0];
454                         vertst[totalverts][1] = vertst[i][1];
455                         totalverts++;
456                 }
457                 else
458                         vertremap[i] = -1; // not used at all
459         }
460         loadmodel->numverts = totalverts;
461         // remap the triangle references
462         for (i = 0;i < loadmodel->numtris;i++)
463         {
464                 loadmodel->mdlmd2data_indices[i*3+0] = vertremap[temptris[i][0]];
465                 loadmodel->mdlmd2data_indices[i*3+1] = vertremap[temptris[i][1]];
466                 loadmodel->mdlmd2data_indices[i*3+2] = vertremap[temptris[i][2]];
467         }
468         // store the texture coordinates
469         loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
470         for (i = 0;i < totalverts;i++)
471         {
472                 loadmodel->mdlmd2data_texcoords[i*2+0] = vertst[i][0];
473                 loadmodel->mdlmd2data_texcoords[i*2+1] = vertst[i][1];
474         }
475
476 // load the frames
477         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
478         loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, sizeof(md2frame_t) * totalposes);
479         loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * totalposes * totalverts);
480
481         // LordHavoc: doing proper bbox for model
482         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
483         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
484
485         Mod_MDL_LoadFrames (startframes, numverts, totalverts, scale, translate);
486
487         // LordHavoc: fixed model bbox - was //FIXME: do this right
488         //loadmodel->mins[0] = loadmodel->mins[1] = loadmodel->mins[2] = -16;
489         //loadmodel->maxs[0] = loadmodel->maxs[1] = loadmodel->maxs[2] = 16;
490         modelyawradius = sqrt(modelyawradius);
491         modelradius = sqrt(modelradius);
492 //      loadmodel->modelradius = modelradius;
493         for (j = 0;j < 3;j++)
494         {
495                 loadmodel->normalmins[j] = aliasbboxmin[j];
496                 loadmodel->normalmaxs[j] = aliasbboxmax[j];
497                 loadmodel->rotatedmins[j] = -modelradius;
498                 loadmodel->rotatedmaxs[j] = modelradius;
499         }
500         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
501         loadmodel->yawmins[2] = loadmodel->normalmins[2];
502         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
503
504         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
505         loadmodel->Draw = R_DrawAliasModel;
506         loadmodel->DrawSky = NULL;
507         loadmodel->DrawShadow = NULL;
508 }
509
510 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out, int *vertremap)
511 {
512         int i, invalidnormals = 0;
513         float dist;
514         trivertx_t *in;
515         vec3_t temp;
516         for (i = 0;i < loadmodel->numverts;i++)
517         {
518                 in = v + vertremap[i];
519                 VectorCopy(in->v, out[i].v);
520                 temp[0] = in->v[0] * scale[0] + translate[0];
521                 temp[1] = in->v[1] * scale[1] + translate[1];
522                 temp[2] = in->v[2] * scale[2] + translate[2];
523                 // update bounding box
524                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
525                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
526                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
527                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
528                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
529                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
530                 dist = temp[0]*temp[0]+temp[1]*temp[1];
531                 if (modelyawradius < dist)
532                         modelyawradius = dist;
533                 dist += temp[2]*temp[2];
534                 if (modelradius < dist)
535                         modelradius = dist;
536                 out[i].lightnormalindex = in->lightnormalindex;
537                 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
538                 {
539                         invalidnormals++;
540                         out[i].lightnormalindex = 0;
541                 }
542         }
543         if (invalidnormals)
544                 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
545 }
546
547 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
548 {
549         int *vertremap;
550         md2_t *pinmodel;
551         long base;
552         int version, end;
553         int i, j, k, hashindex, num, numxyz, numst, xyz, st;
554         float *stverts, s, t;
555         struct md2verthash_s
556         {
557                 struct md2verthash_s *next;
558                 int xyz;
559                 float st[2];
560         }
561         *hash, **md2verthash, *md2verthashdata;
562         long datapointer;
563         md2frame_t *pinframe;
564         char *inskin;
565         md2triangle_t *intri;
566         unsigned short *inst;
567         int skinwidth, skinheight;
568
569         pinmodel = buffer;
570         base = (long) buffer;
571
572         version = LittleLong (pinmodel->version);
573         if (version != MD2ALIAS_VERSION)
574                 Host_Error ("%s has wrong version number (%i should be %i)",
575                         loadmodel->name, version, MD2ALIAS_VERSION);
576
577         loadmodel->type = mod_alias;
578         loadmodel->aliastype = ALIASTYPE_MDLMD2;
579         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
580         loadmodel->Draw = R_DrawAliasModel;
581         loadmodel->DrawSky = NULL;
582         loadmodel->DrawShadow = NULL;
583
584         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
585                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
586         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
587                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
588         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
589                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
590         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
591                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
592
593         end = LittleLong(pinmodel->ofs_end);
594         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
595                 Host_Error ("%s is not a valid model", loadmodel->name);
596         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
597                 Host_Error ("%s is not a valid model", loadmodel->name);
598         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
599                 Host_Error ("%s is not a valid model", loadmodel->name);
600         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
601                 Host_Error ("%s is not a valid model", loadmodel->name);
602         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
603                 Host_Error ("%s is not a valid model", loadmodel->name);
604
605         loadmodel->numskins = LittleLong(pinmodel->num_skins);
606         numxyz = LittleLong(pinmodel->num_xyz);
607         numst = LittleLong(pinmodel->num_st);
608         loadmodel->numtris = LittleLong(pinmodel->num_tris);
609         loadmodel->numframes = LittleLong(pinmodel->num_frames);
610
611         loadmodel->flags = 0; // there are no MD2 flags
612         loadmodel->synctype = ST_RAND;
613
614         // load the skins
615         inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
616         if (loadmodel->numskins)
617         {
618                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
619                 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
620                 for (i = 0;i < loadmodel->numskins;i++)
621                 {
622                         loadmodel->skinscenes[i].firstframe = i;
623                         loadmodel->skinscenes[i].framecount = 1;
624                         loadmodel->skinscenes[i].loop = true;
625                         loadmodel->skinscenes[i].framerate = 10;
626                         loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, inskin, 0, 0, true, r_mipskins.integer, true);
627                         loadmodel->skinframes[i].fog = image_masktex;
628                         loadmodel->skinframes[i].pants = NULL;
629                         loadmodel->skinframes[i].shirt = NULL;
630                         loadmodel->skinframes[i].glow = NULL;
631                         loadmodel->skinframes[i].merged = NULL;
632                         inskin += MD2MAX_SKINNAME;
633                 }
634         }
635
636         // load the triangles and stvert data
637         inst = (void*)(base + LittleLong(pinmodel->ofs_st));
638         intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
639         skinwidth = LittleLong(pinmodel->skinwidth);
640         skinheight = LittleLong(pinmodel->skinheight);
641
642         stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
643         s = 1.0f / skinwidth;
644         t = 1.0f / skinheight;
645         for (i = 0;i < numst;i++)
646         {
647                 j = (unsigned short) LittleShort(inst[i*2+0]);
648                 k = (unsigned short) LittleShort(inst[i*2+1]);
649                 if (j >= skinwidth || k >= skinheight)
650                 {
651                         Mem_Free(stverts);
652                         Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
653                 }
654                 stverts[i*2+0] = j * s;
655                 stverts[i*2+1] = k * t;
656         }
657
658         md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
659         md2verthashdata = Mem_Alloc(tempmempool, loadmodel->numtris * 3 * sizeof(*hash));
660         // swap the triangle list
661         num = 0;
662         loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(int[3]));
663         for (i = 0;i < loadmodel->numtris;i++)
664         {
665                 for (j = 0;j < 3;j++)
666                 {
667                         xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
668                         st = (unsigned short) LittleShort (intri[i].index_st[j]);
669                         if (xyz >= numxyz || st >= numst)
670                         {
671                                 Mem_Free(md2verthash);
672                                 Mem_Free(md2verthashdata);
673                                 Mem_Free(stverts);
674                                 if (xyz >= numxyz)
675                                         Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
676                                 if (st >= numst)
677                                         Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
678                         }
679                         s = stverts[st*2+0];
680                         t = stverts[st*2+1];
681                         hashindex = (xyz * 17 + st) & 255;
682                         for (hash = md2verthash[hashindex];hash;hash = hash->next)
683                                 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
684                                         break;
685                         if (hash == NULL)
686                         {
687                                 hash = md2verthashdata + num++;
688                                 hash->xyz = xyz;
689                                 hash->st[0] = s;
690                                 hash->st[1] = t;
691                                 hash->next = md2verthash[hashindex];
692                                 md2verthash[hashindex] = hash;
693                         }
694                         loadmodel->mdlmd2data_indices[i*3+j] = (hash - md2verthashdata);
695                 }
696         }
697
698         Mem_Free(stverts);
699
700         loadmodel->numverts = num;
701         vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
702         loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
703         for (i = 0;i < num;i++)
704         {
705                 hash = md2verthashdata + i;
706                 vertremap[i] = hash->xyz;
707                 loadmodel->mdlmd2data_texcoords[i*2+0] = hash->st[0];
708                 loadmodel->mdlmd2data_texcoords[i*2+1] = hash->st[1];
709         }
710
711         Mem_Free(md2verthash);
712         Mem_Free(md2verthashdata);
713
714         // load frames
715         // LordHavoc: doing proper bbox for model
716         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
717         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
718         modelyawradius = 0;
719         modelradius = 0;
720
721         datapointer = (base + LittleLong(pinmodel->ofs_frames));
722         // load the frames
723         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
724         loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
725         loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
726
727         for (i = 0;i < loadmodel->numframes;i++)
728         {
729                 pinframe = (md2frame_t *)datapointer;
730                 datapointer += sizeof(md2frame_t);
731                 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
732                 for (j = 0;j < 3;j++)
733                 {
734                         loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
735                         loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
736                 }
737                 Mod_MD2_ConvertVerts (loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts], vertremap);
738                 datapointer += numxyz * sizeof(trivertx_t);
739
740                 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
741                 loadmodel->animscenes[i].firstframe = i;
742                 loadmodel->animscenes[i].framecount = 1;
743                 loadmodel->animscenes[i].framerate = 10;
744                 loadmodel->animscenes[i].loop = true;
745         }
746
747         Mem_Free(vertremap);
748
749         // LordHavoc: model bbox
750         modelyawradius = sqrt(modelyawradius);
751         modelradius = sqrt(modelradius);
752 //      loadmodel->modelradius = modelradius;
753         for (j = 0;j < 3;j++)
754         {
755                 loadmodel->normalmins[j] = aliasbboxmin[j];
756                 loadmodel->normalmaxs[j] = aliasbboxmax[j];
757                 loadmodel->rotatedmins[j] = -modelradius;
758                 loadmodel->rotatedmaxs[j] = modelradius;
759         }
760         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
761         loadmodel->yawmins[2] = loadmodel->normalmins[2];
762         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
763 }
764
765 static void zymswapintblock(int *m, int size)
766 {
767         size /= 4;
768         while(size--)
769         {
770                 *m = BigLong(*m);
771                 m++;
772         }
773 }
774
775 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
776 {
777         int i, pbase, *bonecount;
778         unsigned int count, a, b, c, *renderlist, *renderlistend;
779         rtexture_t **texture;
780         char *shadername;
781         zymtype1header_t *pinmodel, *pheader;
782         zymscene_t *scene;
783         zymbone_t *bone;
784         float corner[2], modelradius;
785
786         pinmodel = (void *)buffer;
787
788         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
789                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
790
791         if (BigLong(pinmodel->type) != 1)
792                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
793
794         loadmodel->type = mod_alias;
795         loadmodel->aliastype = ALIASTYPE_ZYM;
796
797         loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
798
799         pbase = (int) pheader;
800
801         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
802
803         // byteswap header
804 #define SWAPLONG(var) var = BigLong(var)
805 #define SWAPFLOAT(var) var = BigFloat(var)
806 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
807         SWAPLONG(pheader->type);
808         SWAPLONG(pheader->filesize);
809         SWAPVEC(pheader->mins);
810         SWAPVEC(pheader->maxs);
811         SWAPFLOAT(pheader->radius);
812         SWAPLONG(pheader->numverts);
813         SWAPLONG(pheader->numtris);
814         SWAPLONG(pheader->numshaders);
815         SWAPLONG(pheader->numbones);
816         SWAPLONG(pheader->numscenes);
817
818 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
819         SWAPLUMPINFO(scenes);
820         SWAPLUMPINFO(poses);
821         SWAPLUMPINFO(bones);
822         SWAPLUMPINFO(vertbonecounts);
823         SWAPLUMPINFO(verts);
824         SWAPLUMPINFO(texcoords);
825         SWAPLUMPINFO(render);
826         SWAPLUMPINFO(shaders);
827         SWAPLUMPINFO(trizone);
828
829         loadmodel->flags = 0; // there are no flags
830         loadmodel->numframes = pheader->numscenes;
831         loadmodel->synctype = ST_SYNC;
832         loadmodel->numtris = pheader->numtris;
833
834         // FIXME: add skin support and texturing and shaders and...
835         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
836         loadmodel->skinscenes[0].firstframe = 0;
837         loadmodel->skinscenes[0].framecount = 1;
838         loadmodel->skinscenes[0].loop = true;
839         loadmodel->skinscenes[0].framerate = 10;
840         loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
841         loadmodel->skinframes->base = NULL;
842         loadmodel->skinframes->fog = NULL;
843         loadmodel->skinframes->pants = NULL;
844         loadmodel->skinframes->shirt = NULL;
845         loadmodel->skinframes->glow = NULL;
846         loadmodel->skinframes->merged = NULL;
847         loadmodel->numskins = 1;
848
849         // go through the lumps, swapping things
850
851 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
852         scene = (void *) (pheader->lump_scenes.start + pbase);
853         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
854         for (i = 0;i < pheader->numscenes;i++)
855         {
856                 SWAPVEC(scene->mins);
857                 SWAPVEC(scene->maxs);
858                 SWAPFLOAT(scene->radius);
859                 SWAPFLOAT(scene->framerate);
860                 SWAPLONG(scene->flags);
861                 SWAPLONG(scene->start);
862                 SWAPLONG(scene->length);
863
864                 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
865                 loadmodel->animscenes[i].firstframe = scene->start;
866                 loadmodel->animscenes[i].framecount = scene->length;
867                 loadmodel->animscenes[i].framerate = scene->framerate;
868                 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
869
870                 scene++;
871         }
872
873 //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
874         zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
875
876 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
877         bone = (void *) (pheader->lump_bones.start + pbase);
878         for (i = 0;i < pheader->numbones;i++)
879         {
880                 SWAPLONG(bone[i].flags);
881                 SWAPLONG(bone[i].parent);
882                 if (bone[i].parent >= i)
883                         Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
884         }
885
886 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
887         zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
888         bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
889         for (i = 0;i < pheader->numbones;i++)
890                 if (bonecount[i] < 1)
891                         Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
892
893 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
894         zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
895
896 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
897         zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
898
899 //      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)
900         zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
901         // validate renderlist and swap winding order of tris
902         renderlist = (void *) (pheader->lump_render.start + pbase);
903         renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
904         i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
905         if (pheader->lump_render.length != i)
906                 Host_Error("Mod_LoadZymoticModel: renderlist is wrong size in %s (is %i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, i);
907         for (i = 0;i < pheader->numshaders;i++)
908         {
909                 if (renderlist >= renderlistend)
910                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
911                 count = *renderlist++;
912                 if (renderlist + count * 3 > renderlistend)
913                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
914                 while (count--)
915                 {
916                         a = renderlist[0];
917                         b = renderlist[1];
918                         c = renderlist[2];
919                         if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
920                                 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
921                         renderlist[0] = c;
922                         renderlist[1] = b;
923                         renderlist[2] = a;
924                         renderlist += 3;
925                 }
926         }
927
928 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
929         shadername = (void *) (pheader->lump_shaders.start + pbase);
930         texture = (void *) shadername;
931         for (i = 0;i < pheader->numshaders;i++)
932         {
933                 rtexture_t *rt;
934                 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
935                 shadername += 32;
936                 *texture++ = rt; // reuse shader name list for texture pointers
937         }
938
939 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
940         zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
941
942         // model bbox
943         modelradius = pheader->radius;
944 //      loadmodel->modelradius = pheader->radius;
945         for (i = 0;i < 3;i++)
946         {
947                 loadmodel->normalmins[i] = pheader->mins[i];
948                 loadmodel->normalmaxs[i] = pheader->maxs[i];
949                 loadmodel->rotatedmins[i] = -modelradius;
950                 loadmodel->rotatedmaxs[i] = modelradius;
951         }
952         corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
953         corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
954         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
955         if (loadmodel->yawmaxs[0] > modelradius)
956                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
957         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
958         loadmodel->yawmins[2] = loadmodel->normalmins[2];
959         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
960
961         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
962         loadmodel->Draw = R_DrawAliasModel;
963         loadmodel->DrawSky = NULL;
964         loadmodel->DrawShadow = NULL;
965 }