rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[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)
220 {
221         skinframe->base   = loadtextureimagewithmask(loadmodel->texturepool, va("%s_normal", basename), 0, 0, false, r_mipskins.integer, true);
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, true);
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, true);
230                 skinframe->shirt  = loadtextureimage(loadmodel->texturepool, va("%s_shirt" , basename), 0, 0, false, r_mipskins.integer, true);
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), true); // 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), true); // body (normal + pants + shirt, but not glow)
245                         }
246                         else
247                                 skinframe->base   = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // 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);
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         rtexture_t **texture;
729         char *shadername;
730         zymtype1header_t *pinmodel, *pheader;
731         zymscene_t *scene;
732         zymbone_t *bone;
733         float corner[2], modelradius;
734
735         pinmodel = (void *)buffer;
736
737         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
738                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
739
740         if (BigLong(pinmodel->type) != 1)
741                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
742
743         loadmodel->type = mod_alias;
744         loadmodel->aliastype = ALIASTYPE_ZYM;
745
746         loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
747
748         pbase = (int) pheader;
749
750         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
751
752         // byteswap header
753 #define SWAPLONG(var) var = BigLong(var)
754 #define SWAPFLOAT(var) var = BigFloat(var)
755 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
756         SWAPLONG(pheader->type);
757         SWAPLONG(pheader->filesize);
758         SWAPVEC(pheader->mins);
759         SWAPVEC(pheader->maxs);
760         SWAPFLOAT(pheader->radius);
761         SWAPLONG(pheader->numverts);
762         SWAPLONG(pheader->numtris);
763         SWAPLONG(pheader->numshaders);
764         SWAPLONG(pheader->numbones);
765         SWAPLONG(pheader->numscenes);
766
767 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
768         SWAPLUMPINFO(scenes);
769         SWAPLUMPINFO(poses);
770         SWAPLUMPINFO(bones);
771         SWAPLUMPINFO(vertbonecounts);
772         SWAPLUMPINFO(verts);
773         SWAPLUMPINFO(texcoords);
774         SWAPLUMPINFO(render);
775         SWAPLUMPINFO(shaders);
776         SWAPLUMPINFO(trizone);
777
778         loadmodel->flags = 0; // there are no flags
779         loadmodel->numframes = pheader->numscenes;
780         loadmodel->synctype = ST_SYNC;
781         loadmodel->numtris = pheader->numtris;
782
783         // FIXME: add skin support and texturing and shaders and...
784         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
785         loadmodel->skinscenes[0].firstframe = 0;
786         loadmodel->skinscenes[0].framecount = 1;
787         loadmodel->skinscenes[0].loop = true;
788         loadmodel->skinscenes[0].framerate = 10;
789         loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
790         loadmodel->skinframes->base = NULL;
791         loadmodel->skinframes->fog = NULL;
792         loadmodel->skinframes->pants = NULL;
793         loadmodel->skinframes->shirt = NULL;
794         loadmodel->skinframes->glow = NULL;
795         loadmodel->skinframes->merged = NULL;
796         loadmodel->numskins = 1;
797
798         // go through the lumps, swapping things
799
800 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
801         scene = (void *) (pheader->lump_scenes.start + pbase);
802         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
803         for (i = 0;i < pheader->numscenes;i++)
804         {
805                 SWAPVEC(scene->mins);
806                 SWAPVEC(scene->maxs);
807                 SWAPFLOAT(scene->radius);
808                 SWAPFLOAT(scene->framerate);
809                 SWAPLONG(scene->flags);
810                 SWAPLONG(scene->start);
811                 SWAPLONG(scene->length);
812
813                 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
814                 loadmodel->animscenes[i].firstframe = scene->start;
815                 loadmodel->animscenes[i].framecount = scene->length;
816                 loadmodel->animscenes[i].framerate = scene->framerate;
817                 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
818
819                 scene++;
820         }
821
822 //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
823         zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
824
825 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
826         bone = (void *) (pheader->lump_bones.start + pbase);
827         for (i = 0;i < pheader->numbones;i++)
828         {
829                 SWAPLONG(bone[i].flags);
830                 SWAPLONG(bone[i].parent);
831                 if (bone[i].parent >= i)
832                         Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i\n");
833         }
834
835 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
836         zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
837         bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
838         for (i = 0;i < pheader->numbones;i++)
839                 if (bonecount[i] < 1)
840                         Host_Error("Mod_LoadZymoticModel: bone vertex count < 1\n");
841
842 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
843         zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
844
845 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
846         zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
847
848 //      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)
849         zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
850
851 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
852         shadername = (void *) (pheader->lump_shaders.start + pbase);
853         texture = (void *) shadername;
854         for (i = 0;i < pheader->numshaders;i++)
855         {
856                 rtexture_t *rt;
857                 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
858                 shadername += 32;
859                 *texture++ = rt; // reuse shader name list for texture pointers
860         }
861
862 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
863         zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
864
865         // model bbox
866         modelradius = pheader->radius;
867 //      loadmodel->modelradius = pheader->radius;
868         for (i = 0;i < 3;i++)
869         {
870                 loadmodel->normalmins[i] = pheader->mins[i];
871                 loadmodel->normalmaxs[i] = pheader->maxs[i];
872                 loadmodel->rotatedmins[i] = -modelradius;
873                 loadmodel->rotatedmaxs[i] = modelradius;
874         }
875         corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
876         corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
877         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
878         if (loadmodel->yawmaxs[0] > modelradius)
879                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
880         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
881         loadmodel->yawmins[2] = loadmodel->normalmins[2];
882         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
883
884         loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
885         loadmodel->Draw = R_DrawAliasModel;
886         loadmodel->DrawSky = NULL;
887         loadmodel->DrawShadow = NULL;
888 }