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