]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_alias.c
validate more when loading zymotic models, also reverse triangles so they don't rende...
[xonotic/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(byte *in, byte *out, int width, int height, int bits, char *name, int precache)
164 {
165         int i, pixels, passed;
166         byte 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(byte *in, byte *out, int width, int height, int bits, char *name, int precache)
194 {
195         int i, pixels, passed;
196         byte 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, byte *skindata, byte *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         byte                                    *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_MDL;
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, (byte *)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->mdldata_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->mdldata_indices[i*3+0] = vertremap[temptris[i][0]];
465                 loadmodel->mdldata_indices[i*3+1] = vertremap[temptris[i][1]];
466                 loadmodel->mdldata_indices[i*3+2] = vertremap[temptris[i][2]];
467         }
468         // store the texture coordinates
469         loadmodel->mdldata_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
470         for (i = 0;i < totalverts;i++)
471         {
472                 loadmodel->mdldata_texcoords[i*2+0] = vertst[i][0];
473                 loadmodel->mdldata_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 (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
511 {
512         int i, invalidnormals = 0;
513         float dist;
514         vec3_t temp;
515         for (i = 0;i < numverts;i++)
516         {
517                 VectorCopy(v[i].v, out[i].v);
518                 temp[0] = v[i].v[0] * scale[0] + translate[0];
519                 temp[1] = v[i].v[1] * scale[1] + translate[1];
520                 temp[2] = v[i].v[2] * scale[2] + translate[2];
521                 // update bounding box
522                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
523                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
524                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
525                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
526                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
527                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
528                 dist = temp[0]*temp[0]+temp[1]*temp[1];
529                 if (modelyawradius < dist)
530                         modelyawradius = dist;
531                 dist += temp[2]*temp[2];
532                 if (modelradius < dist)
533                         modelradius = dist;
534                 out[i].lightnormalindex = v[i].lightnormalindex;
535                 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
536                 {
537                         invalidnormals++;
538                         out[i].lightnormalindex = 0;
539                 }
540         }
541         if (invalidnormals)
542                 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
543 }
544
545 void Mod_MD2_ReadHeader(md2_t *in, int *numglcmds)
546 {
547         int version, end;
548
549         version = LittleLong (in->version);
550         if (version != MD2ALIAS_VERSION)
551                 Host_Error ("%s has wrong version number (%i should be %i)",
552                         loadmodel->name, version, MD2ALIAS_VERSION);
553
554         loadmodel->type = mod_alias;
555         loadmodel->aliastype = ALIASTYPE_MD2;
556         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
557         loadmodel->Draw = R_DrawAliasModel;
558         loadmodel->DrawSky = NULL;
559         loadmodel->DrawShadow = NULL;
560
561         if (LittleLong(in->num_tris < 1) || LittleLong(in->num_tris) > MD2MAX_TRIANGLES)
562                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(in->num_tris));
563         if (LittleLong(in->num_xyz < 1) || LittleLong(in->num_xyz) > MD2MAX_VERTS)
564                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(in->num_xyz));
565         if (LittleLong(in->num_frames < 1) || LittleLong(in->num_frames) > MD2MAX_FRAMES)
566                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(in->num_frames));
567         if (LittleLong(in->num_skins < 0) || LittleLong(in->num_skins) > MAX_SKINS)
568                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(in->num_skins));
569
570         end = LittleLong(in->ofs_end);
571         if (LittleLong(in->num_skins) >= 1 && (LittleLong(in->ofs_skins <= 0) || LittleLong(in->ofs_skins) >= end))
572                 Host_Error ("%s is not a valid model", loadmodel->name);
573         if (LittleLong(in->ofs_st <= 0) || LittleLong(in->ofs_st) >= end)
574                 Host_Error ("%s is not a valid model", loadmodel->name);
575         if (LittleLong(in->ofs_tris <= 0) || LittleLong(in->ofs_tris) >= end)
576                 Host_Error ("%s is not a valid model", loadmodel->name);
577         if (LittleLong(in->ofs_frames <= 0) || LittleLong(in->ofs_frames) >= end)
578                 Host_Error ("%s is not a valid model", loadmodel->name);
579         if (LittleLong(in->ofs_glcmds <= 0) || LittleLong(in->ofs_glcmds) >= end)
580                 Host_Error ("%s is not a valid model", loadmodel->name);
581
582         loadmodel->numskins = LittleLong(in->num_skins);
583         loadmodel->numverts = LittleLong(in->num_xyz);
584 //      loadmodel->md2num_st = LittleLong(in->num_st);
585         loadmodel->numtris = LittleLong(in->num_tris);
586         loadmodel->numframes = LittleLong(in->num_frames);
587         *numglcmds = LittleLong(in->num_glcmds);
588
589         loadmodel->flags = 0; // there are no MD2 flags
590         loadmodel->synctype = ST_RAND;
591 }
592
593 void Mod_MD2_LoadSkins(char *in)
594 {
595         int i;
596 // load the skins
597         if (loadmodel->numskins)
598         {
599                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
600                 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
601                 for (i = 0;i < loadmodel->numskins;i++)
602                 {
603                         loadmodel->skinscenes[i].firstframe = i;
604                         loadmodel->skinscenes[i].framecount = 1;
605                         loadmodel->skinscenes[i].loop = true;
606                         loadmodel->skinscenes[i].framerate = 10;
607                         loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, in, 0, 0, true, r_mipskins.integer, true);
608                         loadmodel->skinframes[i].fog = image_masktex;
609                         loadmodel->skinframes[i].pants = NULL;
610                         loadmodel->skinframes[i].shirt = NULL;
611                         loadmodel->skinframes[i].glow = NULL;
612                         loadmodel->skinframes[i].merged = NULL;
613                         in += MD2MAX_SKINNAME;
614                 }
615         }
616 }
617
618 /*
619 void Mod_MD2_LoadTriangles(md2triangle_t *in)
620 {
621         int i, j;
622         loadmodel->md2data_tris = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(md2triangle_t));
623         // swap the triangle list
624         for (i = 0;i < loadmodel->numtris;i++)
625         {
626                 for (j = 0;j < 3;j++)
627                 {
628                         loadmodel->md2data_tris[i].index_xyz[j] = LittleShort (in[i].index_xyz[j]);
629                         loadmodel->md2data_tris[i].index_st[j] = LittleShort (in[i].index_st[j]);
630                         if (loadmodel->md2data_tris[i].index_xyz[j] >= loadmodel->md2num_xyz)
631                                 Host_Error ("%s has invalid vertex indices", loadmodel->name);
632                         if (loadmodel->md2data_tris[i].index_st[j] >= loadmodel->md2num_st)
633                                 Host_Error ("%s has invalid vertex indices", loadmodel->name);
634                 }
635         }
636 }
637 */
638
639 void Mod_MD2_LoadFrames(void *start)
640 {
641         int i, j;
642         long datapointer;
643         md2frame_t *pinframe;
644         // LordHavoc: doing proper bbox for model
645         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
646         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
647         modelyawradius = 0;
648         modelradius = 0;
649
650         datapointer = (long) start;
651 // load the frames
652         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
653         loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
654         loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
655
656         for (i = 0;i < loadmodel->numframes;i++)
657         {
658                 pinframe = (md2frame_t *)datapointer;
659                 datapointer += sizeof(md2frame_t);
660                 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
661                 for (j = 0;j < 3;j++)
662                 {
663                         loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
664                         loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
665                 }
666                 Mod_MD2_ConvertVerts (loadmodel->numverts, loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts]);
667                 datapointer += loadmodel->numverts * sizeof(trivertx_t);
668
669                 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
670                 loadmodel->animscenes[i].firstframe = i;
671                 loadmodel->animscenes[i].framecount = 1;
672                 loadmodel->animscenes[i].framerate = 10;
673                 loadmodel->animscenes[i].loop = true;
674         }
675
676         // LordHavoc: model bbox
677         modelyawradius = sqrt(modelyawradius);
678         modelradius = sqrt(modelradius);
679 //      loadmodel->modelradius = modelradius;
680         for (j = 0;j < 3;j++)
681         {
682                 loadmodel->normalmins[j] = aliasbboxmin[j];
683                 loadmodel->normalmaxs[j] = aliasbboxmax[j];
684                 loadmodel->rotatedmins[j] = -modelradius;
685                 loadmodel->rotatedmaxs[j] = modelradius;
686         }
687         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
688         loadmodel->yawmins[2] = loadmodel->normalmins[2];
689         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
690 }
691
692 void Mod_MD2_LoadGLCmds(int *in, int numglcmds)
693 {
694         int i;
695         // load the draw list
696         loadmodel->md2data_glcmds = Mem_Alloc(loadmodel->mempool, numglcmds * sizeof(int));
697         for (i = 0;i < numglcmds;i++)
698                 loadmodel->md2data_glcmds[i] = LittleLong(in[i]);
699 }
700
701 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
702 {
703         md2_t *pinmodel;
704         int numglcmds;
705         long base;
706         pinmodel = buffer;
707         base = (long) buffer;
708         Mod_MD2_ReadHeader(pinmodel, &numglcmds);
709         Mod_MD2_LoadSkins((void*)(base + LittleLong(pinmodel->ofs_skins)));
710 //      Mod_MD2_LoadTriangles((void*)(base + LittleLong(pinmodel->ofs_tris)));
711         Mod_MD2_LoadFrames((void*)(base + LittleLong(pinmodel->ofs_frames)));
712         Mod_MD2_LoadGLCmds((void*)(base + LittleLong(pinmodel->ofs_glcmds)), numglcmds);
713 }
714
715 static void zymswapintblock(int *m, int size)
716 {
717         size /= 4;
718         while(size--)
719         {
720                 *m = BigLong(*m);
721                 m++;
722         }
723 }
724
725 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
726 {
727         int i, pbase, *bonecount;
728         unsigned int count, a, b, c, *renderlist, *renderlistend;
729         rtexture_t **texture;
730         char *shadername;
731         zymtype1header_t *pinmodel, *pheader;
732         zymscene_t *scene;
733         zymbone_t *bone;
734         float corner[2], modelradius;
735
736         pinmodel = (void *)buffer;
737
738         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
739                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
740
741         if (BigLong(pinmodel->type) != 1)
742                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
743
744         loadmodel->type = mod_alias;
745         loadmodel->aliastype = ALIASTYPE_ZYM;
746
747         loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
748
749         pbase = (int) pheader;
750
751         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
752
753         // byteswap header
754 #define SWAPLONG(var) var = BigLong(var)
755 #define SWAPFLOAT(var) var = BigFloat(var)
756 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
757         SWAPLONG(pheader->type);
758         SWAPLONG(pheader->filesize);
759         SWAPVEC(pheader->mins);
760         SWAPVEC(pheader->maxs);
761         SWAPFLOAT(pheader->radius);
762         SWAPLONG(pheader->numverts);
763         SWAPLONG(pheader->numtris);
764         SWAPLONG(pheader->numshaders);
765         SWAPLONG(pheader->numbones);
766         SWAPLONG(pheader->numscenes);
767
768 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
769         SWAPLUMPINFO(scenes);
770         SWAPLUMPINFO(poses);
771         SWAPLUMPINFO(bones);
772         SWAPLUMPINFO(vertbonecounts);
773         SWAPLUMPINFO(verts);
774         SWAPLUMPINFO(texcoords);
775         SWAPLUMPINFO(render);
776         SWAPLUMPINFO(shaders);
777         SWAPLUMPINFO(trizone);
778
779         loadmodel->flags = 0; // there are no flags
780         loadmodel->numframes = pheader->numscenes;
781         loadmodel->synctype = ST_SYNC;
782         loadmodel->numtris = pheader->numtris;
783
784         // FIXME: add skin support and texturing and shaders and...
785         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
786         loadmodel->skinscenes[0].firstframe = 0;
787         loadmodel->skinscenes[0].framecount = 1;
788         loadmodel->skinscenes[0].loop = true;
789         loadmodel->skinscenes[0].framerate = 10;
790         loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
791         loadmodel->skinframes->base = NULL;
792         loadmodel->skinframes->fog = NULL;
793         loadmodel->skinframes->pants = NULL;
794         loadmodel->skinframes->shirt = NULL;
795         loadmodel->skinframes->glow = NULL;
796         loadmodel->skinframes->merged = NULL;
797         loadmodel->numskins = 1;
798
799         // go through the lumps, swapping things
800
801 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
802         scene = (void *) (pheader->lump_scenes.start + pbase);
803         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
804         for (i = 0;i < pheader->numscenes;i++)
805         {
806                 SWAPVEC(scene->mins);
807                 SWAPVEC(scene->maxs);
808                 SWAPFLOAT(scene->radius);
809                 SWAPFLOAT(scene->framerate);
810                 SWAPLONG(scene->flags);
811                 SWAPLONG(scene->start);
812                 SWAPLONG(scene->length);
813
814                 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
815                 loadmodel->animscenes[i].firstframe = scene->start;
816                 loadmodel->animscenes[i].framecount = scene->length;
817                 loadmodel->animscenes[i].framerate = scene->framerate;
818                 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
819
820                 scene++;
821         }
822
823 //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
824         zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
825
826 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
827         bone = (void *) (pheader->lump_bones.start + pbase);
828         for (i = 0;i < pheader->numbones;i++)
829         {
830                 SWAPLONG(bone[i].flags);
831                 SWAPLONG(bone[i].parent);
832                 if (bone[i].parent >= i)
833                         Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
834         }
835
836 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
837         zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
838         bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
839         for (i = 0;i < pheader->numbones;i++)
840                 if (bonecount[i] < 1)
841                         Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
842
843 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
844         zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
845
846 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
847         zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
848
849 //      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)
850         zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
851         // validate renderlist and swap winding order of tris
852         renderlist = (void *) (pheader->lump_render.start + pbase);
853         renderlistend = (void *) ((byte *) renderlist + pheader->lump_render.length);
854         i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
855         if (pheader->lump_render.length != i)
856                 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);
857         for (i = 0;i < pheader->numshaders;i++)
858         {
859                 if (renderlist >= renderlistend)
860                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
861                 count = *renderlist++;
862                 if (renderlist + count * 3 > renderlistend)
863                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
864                 while (count--)
865                 {
866                         a = renderlist[0];
867                         b = renderlist[1];
868                         c = renderlist[2];
869                         if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
870                                 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
871                         renderlist[0] = c;
872                         renderlist[1] = b;
873                         renderlist[2] = a;
874                         renderlist += 3;
875                 }
876         }
877
878 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
879         shadername = (void *) (pheader->lump_shaders.start + pbase);
880         texture = (void *) shadername;
881         for (i = 0;i < pheader->numshaders;i++)
882         {
883                 rtexture_t *rt;
884                 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
885                 shadername += 32;
886                 *texture++ = rt; // reuse shader name list for texture pointers
887         }
888
889 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
890         zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
891
892         // model bbox
893         modelradius = pheader->radius;
894 //      loadmodel->modelradius = pheader->radius;
895         for (i = 0;i < 3;i++)
896         {
897                 loadmodel->normalmins[i] = pheader->mins[i];
898                 loadmodel->normalmaxs[i] = pheader->maxs[i];
899                 loadmodel->rotatedmins[i] = -modelradius;
900                 loadmodel->rotatedmaxs[i] = modelradius;
901         }
902         corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
903         corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
904         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
905         if (loadmodel->yawmaxs[0] > modelradius)
906                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
907         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
908         loadmodel->yawmins[2] = loadmodel->normalmins[2];
909         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
910
911         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
912         loadmodel->Draw = R_DrawAliasModel;
913         loadmodel->DrawSky = NULL;
914         loadmodel->DrawShadow = NULL;
915 }