support for loading external skins that don't exist inside the model
[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 || skinframe->merged != NULL || skinframe->fog != 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 void Mod_LoadAliasModel (model_t *mod, void *buffer)
252 {
253         int                                             i, j, version, numverts, totalposes, totalskins, skinwidth, skinheight, totalverts, groupframes, groupskins;
254         mdl_t                                   *pinmodel;
255         stvert_t                                *pinstverts;
256         dtriangle_t                             *pintriangles;
257         daliasskintype_t                *pinskintype;
258         daliasskingroup_t               *pinskingroup;
259         daliasskininterval_t    *pinskinintervals;
260         daliasframetype_t               *pinframetype;
261         daliasgroup_t                   *pinframegroup;
262         float                                   scales, scalet, scale[3], translate[3], interval;
263         qbyte                                   *datapointer, *startframes, *startskins;
264         char                                    name[MAX_QPATH];
265         qbyte                                   *skintemp = NULL;
266         skinframe_t                             tempskinframe;
267         animscene_t                             *tempskinscenes;
268         skinframe_t                             *tempskinframes;
269         modelyawradius = 0;
270         modelradius = 0;
271
272         datapointer = buffer;
273         pinmodel = (mdl_t *)datapointer;
274         datapointer += sizeof(mdl_t);
275
276         version = LittleLong (pinmodel->version);
277         if (version != ALIAS_VERSION)
278                 Host_Error ("%s has wrong version number (%i should be %i)",
279                                  loadmodel->name, version, ALIAS_VERSION);
280
281         loadmodel->type = mod_alias;
282         loadmodel->aliastype = ALIASTYPE_MDLMD2;
283
284         loadmodel->numskins = LittleLong(pinmodel->numskins);
285         BOUNDI(loadmodel->numskins,0,256);
286         skinwidth = LittleLong (pinmodel->skinwidth);
287         BOUNDI(skinwidth,0,4096);
288         skinheight = LittleLong (pinmodel->skinheight);
289         BOUNDI(skinheight,0,4096);
290         loadmodel->numverts = numverts = LittleLong(pinmodel->numverts);
291         BOUNDI(loadmodel->numverts,0,MAXALIASVERTS);
292         loadmodel->numtris = LittleLong(pinmodel->numtris);
293         BOUNDI(loadmodel->numtris,0,MAXALIASTRIS);
294         loadmodel->numframes = LittleLong(pinmodel->numframes);
295         BOUNDI(loadmodel->numframes,0,65536);
296         loadmodel->synctype = LittleLong (pinmodel->synctype);
297         BOUNDI(loadmodel->synctype,0,2);
298         loadmodel->flags = LittleLong (pinmodel->flags);
299
300         for (i = 0;i < 3;i++)
301         {
302                 scale[i] = LittleFloat (pinmodel->scale[i]);
303                 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
304         }
305
306         startskins = datapointer;
307         totalskins = 0;
308         for (i = 0;i < loadmodel->numskins;i++)
309         {
310                 pinskintype = (daliasskintype_t *)datapointer;
311                 datapointer += sizeof(daliasskintype_t);
312                 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
313                         groupskins = 1;
314                 else
315                 {
316                         pinskingroup = (daliasskingroup_t *)datapointer;
317                         datapointer += sizeof(daliasskingroup_t);
318                         groupskins = LittleLong(pinskingroup->numskins);
319                         datapointer += sizeof(daliasskininterval_t) * groupskins;
320                 }
321
322                 for (j = 0;j < groupskins;j++)
323                 {
324                         datapointer += skinwidth * skinheight;
325                         totalskins++;
326                 }
327         }
328
329         pinstverts = (stvert_t *)datapointer;
330         datapointer += sizeof(stvert_t) * numverts;
331
332         pintriangles = (dtriangle_t *)datapointer;
333         datapointer += sizeof(dtriangle_t) * loadmodel->numtris;
334
335         startframes = datapointer;
336         totalposes = 0;
337         for (i = 0;i < loadmodel->numframes;i++)
338         {
339                 pinframetype = (daliasframetype_t *)datapointer;
340                 datapointer += sizeof(daliasframetype_t);
341                 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
342                         groupframes = 1;
343                 else
344                 {
345                         pinframegroup = (daliasgroup_t *)datapointer;
346                         datapointer += sizeof(daliasgroup_t);
347                         groupframes = LittleLong(pinframegroup->numframes);
348                         datapointer += sizeof(daliasinterval_t) * groupframes;
349                 }
350
351                 for (j = 0;j < groupframes;j++)
352                 {
353                         datapointer += sizeof(daliasframe_t);
354                         datapointer += sizeof(trivertx_t) * numverts;
355                         totalposes++;
356                 }
357         }
358
359         // load the skins
360         skintemp = Mem_Alloc(tempmempool, skinwidth * skinheight);
361         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
362         loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
363         totalskins = 0;
364         datapointer = startskins;
365         for (i = 0;i < loadmodel->numskins;i++)
366         {
367                 pinskintype = (daliasskintype_t *)datapointer;
368                 datapointer += sizeof(daliasskintype_t);
369
370                 if (pinskintype->type == ALIAS_SKIN_SINGLE)
371                 {
372                         groupskins = 1;
373                         interval = 0.1f;
374                 }
375                 else
376                 {
377                         pinskingroup = (daliasskingroup_t *)datapointer;
378                         datapointer += sizeof(daliasskingroup_t);
379
380                         groupskins = LittleLong (pinskingroup->numskins);
381
382                         pinskinintervals = (daliasskininterval_t *)datapointer;
383                         datapointer += sizeof(daliasskininterval_t) * groupskins;
384
385                         interval = LittleFloat(pinskinintervals[0].interval);
386                         if (interval < 0.01f)
387                                 Host_Error("Mod_LoadAliasModel: invalid interval\n");
388                 }
389
390                 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
391                 loadmodel->skinscenes[i].firstframe = totalskins;
392                 loadmodel->skinscenes[i].framecount = groupskins;
393                 loadmodel->skinscenes[i].framerate = 1.0f / interval;
394                 loadmodel->skinscenes[i].loop = true;
395
396                 for (j = 0;j < groupskins;j++)
397                 {
398                         if (groupskins > 1)
399                                 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
400                         else
401                                 sprintf (name, "%s_%i", loadmodel->name, i);
402                         if (!Mod_LoadExternalSkin(name, loadmodel->skinframes + totalskins, i == 0))
403                                 Mod_LoadInternalSkin(name, (qbyte *)datapointer, skintemp, skinwidth, skinheight, loadmodel->skinframes + totalskins, i == 0);
404                         datapointer += skinwidth * skinheight;
405                         totalskins++;
406                 }
407         }
408         Mem_Free(skintemp);
409         // check for skins that don't exist in the model, but do exist as external images
410         // (this was added because yummyluv kept pestering me about support for it)
411         for (;;)
412         {
413                 sprintf (name, "%s_%i", loadmodel->name, loadmodel->numskins);
414                 if (Mod_LoadExternalSkin(name, &tempskinframe, loadmodel->numskins == 0))
415                 {
416                         // expand the arrays to make room
417                         tempskinscenes = loadmodel->skinscenes;
418                         tempskinframes = loadmodel->skinframes;
419                         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
420                         loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(skinframe_t));
421                         memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
422                         memcpy(loadmodel->skinframes, tempskinframes, totalskins * sizeof(skinframe_t));
423                         Mem_Free(tempskinscenes);
424                         Mem_Free(tempskinframes);
425                         // store the info about the new skin
426                         strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
427                         loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
428                         loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
429                         loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
430                         loadmodel->skinscenes[loadmodel->numskins].loop = true;
431                         loadmodel->skinframes[totalskins] = tempskinframe;
432                         loadmodel->numskins++;
433                         totalskins++;
434                 }
435                 else
436                         break;
437         }
438
439         // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
440         scales = 1.0 / skinwidth;
441         scalet = 1.0 / skinheight;
442         for (i = 0;i < numverts;i++)
443         {
444                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
445                 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
446                 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
447                 vertst[i+numverts][0] = vertst[i][0] + 0.5;
448                 vertst[i+numverts][1] = vertst[i][1];
449                 vertusage[i] = 0;
450                 vertusage[i+numverts] = 0;
451         }
452
453 // load triangle data
454         loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->numtris);
455
456         // count the vertices used
457         for (i = 0;i < numverts*2;i++)
458                 vertusage[i] = 0;
459         for (i = 0;i < loadmodel->numtris;i++)
460         {
461                 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
462                 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
463                 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
464                 if (!LittleLong(pintriangles[i].facesfront)) // backface
465                 {
466                         if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
467                         if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
468                         if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
469                 }
470                 vertusage[temptris[i][0]]++;
471                 vertusage[temptris[i][1]]++;
472                 vertusage[temptris[i][2]]++;
473         }
474         // build remapping table and compact array
475         totalverts = 0;
476         for (i = 0;i < numverts*2;i++)
477         {
478                 if (vertusage[i])
479                 {
480                         vertremap[i] = totalverts;
481                         vertst[totalverts][0] = vertst[i][0];
482                         vertst[totalverts][1] = vertst[i][1];
483                         totalverts++;
484                 }
485                 else
486                         vertremap[i] = -1; // not used at all
487         }
488         loadmodel->numverts = totalverts;
489         // remap the triangle references
490         for (i = 0;i < loadmodel->numtris;i++)
491         {
492                 loadmodel->mdlmd2data_indices[i*3+0] = vertremap[temptris[i][0]];
493                 loadmodel->mdlmd2data_indices[i*3+1] = vertremap[temptris[i][1]];
494                 loadmodel->mdlmd2data_indices[i*3+2] = vertremap[temptris[i][2]];
495         }
496         // store the texture coordinates
497         loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
498         for (i = 0;i < totalverts;i++)
499         {
500                 loadmodel->mdlmd2data_texcoords[i*2+0] = vertst[i][0];
501                 loadmodel->mdlmd2data_texcoords[i*2+1] = vertst[i][1];
502         }
503
504 // load the frames
505         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
506         loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, sizeof(md2frame_t) * totalposes);
507         loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * totalposes * totalverts);
508
509         // LordHavoc: doing proper bbox for model
510         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
511         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
512
513         Mod_MDL_LoadFrames (startframes, numverts, totalverts, scale, translate);
514
515         modelyawradius = sqrt(modelyawradius);
516         modelradius = sqrt(modelradius);
517         for (j = 0;j < 3;j++)
518         {
519                 loadmodel->normalmins[j] = aliasbboxmin[j];
520                 loadmodel->normalmaxs[j] = aliasbboxmax[j];
521                 loadmodel->rotatedmins[j] = -modelradius;
522                 loadmodel->rotatedmaxs[j] = modelradius;
523         }
524         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
525         loadmodel->yawmins[2] = loadmodel->normalmins[2];
526         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
527
528         loadmodel->Draw = R_DrawQ1Q2AliasModel;
529         loadmodel->DrawSky = NULL;
530         loadmodel->DrawShadow = NULL;
531 }
532
533 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out, int *vertremap)
534 {
535         int i, invalidnormals = 0;
536         float dist;
537         trivertx_t *in;
538         vec3_t temp;
539         for (i = 0;i < loadmodel->numverts;i++)
540         {
541                 in = v + vertremap[i];
542                 VectorCopy(in->v, out[i].v);
543                 temp[0] = in->v[0] * scale[0] + translate[0];
544                 temp[1] = in->v[1] * scale[1] + translate[1];
545                 temp[2] = in->v[2] * scale[2] + translate[2];
546                 // update bounding box
547                 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
548                 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
549                 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
550                 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
551                 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
552                 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
553                 dist = temp[0]*temp[0]+temp[1]*temp[1];
554                 if (modelyawradius < dist)
555                         modelyawradius = dist;
556                 dist += temp[2]*temp[2];
557                 if (modelradius < dist)
558                         modelradius = dist;
559                 out[i].lightnormalindex = in->lightnormalindex;
560                 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
561                 {
562                         invalidnormals++;
563                         out[i].lightnormalindex = 0;
564                 }
565         }
566         if (invalidnormals)
567                 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
568 }
569
570 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
571 {
572         int *vertremap;
573         md2_t *pinmodel;
574         qbyte *base;
575         int version, end;
576         int i, j, k, hashindex, num, numxyz, numst, xyz, st;
577         float *stverts, s, t;
578         struct md2verthash_s
579         {
580                 struct md2verthash_s *next;
581                 int xyz;
582                 float st[2];
583         }
584         *hash, **md2verthash, *md2verthashdata;
585         qbyte *datapointer;
586         md2frame_t *pinframe;
587         char *inskin;
588         md2triangle_t *intri;
589         unsigned short *inst;
590         int skinwidth, skinheight;
591
592         pinmodel = buffer;
593         base = buffer;
594
595         version = LittleLong (pinmodel->version);
596         if (version != MD2ALIAS_VERSION)
597                 Host_Error ("%s has wrong version number (%i should be %i)",
598                         loadmodel->name, version, MD2ALIAS_VERSION);
599
600         loadmodel->type = mod_alias;
601         loadmodel->aliastype = ALIASTYPE_MDLMD2;
602         loadmodel->Draw = R_DrawQ1Q2AliasModel;
603         loadmodel->DrawSky = NULL;
604         loadmodel->DrawShadow = NULL;
605
606         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
607                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
608         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
609                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
610         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
611                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
612         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
613                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
614
615         end = LittleLong(pinmodel->ofs_end);
616         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
617                 Host_Error ("%s is not a valid model", loadmodel->name);
618         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
619                 Host_Error ("%s is not a valid model", loadmodel->name);
620         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
621                 Host_Error ("%s is not a valid model", loadmodel->name);
622         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
623                 Host_Error ("%s is not a valid model", loadmodel->name);
624         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
625                 Host_Error ("%s is not a valid model", loadmodel->name);
626
627         loadmodel->numskins = LittleLong(pinmodel->num_skins);
628         numxyz = LittleLong(pinmodel->num_xyz);
629         numst = LittleLong(pinmodel->num_st);
630         loadmodel->numtris = LittleLong(pinmodel->num_tris);
631         loadmodel->numframes = LittleLong(pinmodel->num_frames);
632
633         loadmodel->flags = 0; // there are no MD2 flags
634         loadmodel->synctype = ST_RAND;
635
636         // load the skins
637         inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
638         if (loadmodel->numskins)
639         {
640                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
641                 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
642                 for (i = 0;i < loadmodel->numskins;i++)
643                 {
644                         loadmodel->skinscenes[i].firstframe = i;
645                         loadmodel->skinscenes[i].framecount = 1;
646                         loadmodel->skinscenes[i].loop = true;
647                         loadmodel->skinscenes[i].framerate = 10;
648                         loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, inskin, 0, 0, true, r_mipskins.integer, true);
649                         loadmodel->skinframes[i].fog = image_masktex;
650                         loadmodel->skinframes[i].pants = NULL;
651                         loadmodel->skinframes[i].shirt = NULL;
652                         loadmodel->skinframes[i].glow = NULL;
653                         loadmodel->skinframes[i].merged = NULL;
654                         inskin += MD2MAX_SKINNAME;
655                 }
656         }
657
658         // load the triangles and stvert data
659         inst = (void*)(base + LittleLong(pinmodel->ofs_st));
660         intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
661         skinwidth = LittleLong(pinmodel->skinwidth);
662         skinheight = LittleLong(pinmodel->skinheight);
663
664         stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
665         s = 1.0f / skinwidth;
666         t = 1.0f / skinheight;
667         for (i = 0;i < numst;i++)
668         {
669                 j = (unsigned short) LittleShort(inst[i*2+0]);
670                 k = (unsigned short) LittleShort(inst[i*2+1]);
671                 if (j >= skinwidth || k >= skinheight)
672                 {
673                         Mem_Free(stverts);
674                         Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
675                 }
676                 stverts[i*2+0] = j * s;
677                 stverts[i*2+1] = k * t;
678         }
679
680         md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
681         md2verthashdata = Mem_Alloc(tempmempool, loadmodel->numtris * 3 * sizeof(*hash));
682         // swap the triangle list
683         num = 0;
684         loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(int[3]));
685         for (i = 0;i < loadmodel->numtris;i++)
686         {
687                 for (j = 0;j < 3;j++)
688                 {
689                         xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
690                         st = (unsigned short) LittleShort (intri[i].index_st[j]);
691                         if (xyz >= numxyz || st >= numst)
692                         {
693                                 Mem_Free(md2verthash);
694                                 Mem_Free(md2verthashdata);
695                                 Mem_Free(stverts);
696                                 if (xyz >= numxyz)
697                                         Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
698                                 if (st >= numst)
699                                         Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
700                         }
701                         s = stverts[st*2+0];
702                         t = stverts[st*2+1];
703                         hashindex = (xyz * 17 + st) & 255;
704                         for (hash = md2verthash[hashindex];hash;hash = hash->next)
705                                 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
706                                         break;
707                         if (hash == NULL)
708                         {
709                                 hash = md2verthashdata + num++;
710                                 hash->xyz = xyz;
711                                 hash->st[0] = s;
712                                 hash->st[1] = t;
713                                 hash->next = md2verthash[hashindex];
714                                 md2verthash[hashindex] = hash;
715                         }
716                         loadmodel->mdlmd2data_indices[i*3+j] = (hash - md2verthashdata);
717                 }
718         }
719
720         Mem_Free(stverts);
721
722         loadmodel->numverts = num;
723         vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
724         loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
725         for (i = 0;i < num;i++)
726         {
727                 hash = md2verthashdata + i;
728                 vertremap[i] = hash->xyz;
729                 loadmodel->mdlmd2data_texcoords[i*2+0] = hash->st[0];
730                 loadmodel->mdlmd2data_texcoords[i*2+1] = hash->st[1];
731         }
732
733         Mem_Free(md2verthash);
734         Mem_Free(md2verthashdata);
735
736         // load frames
737         // LordHavoc: doing proper bbox for model
738         aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
739         aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
740         modelyawradius = 0;
741         modelradius = 0;
742
743         datapointer = (base + LittleLong(pinmodel->ofs_frames));
744         // load the frames
745         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
746         loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
747         loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
748
749         for (i = 0;i < loadmodel->numframes;i++)
750         {
751                 pinframe = (md2frame_t *)datapointer;
752                 datapointer += sizeof(md2frame_t);
753                 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
754                 for (j = 0;j < 3;j++)
755                 {
756                         loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
757                         loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
758                 }
759                 Mod_MD2_ConvertVerts (loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts], vertremap);
760                 datapointer += numxyz * sizeof(trivertx_t);
761
762                 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
763                 loadmodel->animscenes[i].firstframe = i;
764                 loadmodel->animscenes[i].framecount = 1;
765                 loadmodel->animscenes[i].framerate = 10;
766                 loadmodel->animscenes[i].loop = true;
767         }
768
769         Mem_Free(vertremap);
770
771         // LordHavoc: model bbox
772         modelyawradius = sqrt(modelyawradius);
773         modelradius = sqrt(modelradius);
774         for (j = 0;j < 3;j++)
775         {
776                 loadmodel->normalmins[j] = aliasbboxmin[j];
777                 loadmodel->normalmaxs[j] = aliasbboxmax[j];
778                 loadmodel->rotatedmins[j] = -modelradius;
779                 loadmodel->rotatedmaxs[j] = modelradius;
780         }
781         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
782         loadmodel->yawmins[2] = loadmodel->normalmins[2];
783         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
784 }
785
786 static void zymswapintblock(int *m, int size)
787 {
788         size /= 4;
789         while(size--)
790         {
791                 *m = BigLong(*m);
792                 m++;
793         }
794 }
795
796 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
797 {
798         int i, pbase, *bonecount, numposes;
799         unsigned int count, a, b, c, *renderlist, *renderlistend;
800         rtexture_t **texture;
801         char *shadername;
802         zymtype1header_t *pinmodel, *pheader;
803         zymscene_t *scene;
804         zymbone_t *bone;
805         float corner[2], modelradius;
806
807         pinmodel = (void *)buffer;
808
809         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
810                 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
811
812         if (BigLong(pinmodel->type) != 1)
813                 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
814
815         loadmodel->type = mod_alias;
816         loadmodel->aliastype = ALIASTYPE_ZYM;
817
818         loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
819
820         pbase = (int) pheader;
821
822         memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
823
824         // byteswap header
825 #define SWAPLONG(var) var = BigLong(var)
826 #define SWAPFLOAT(var) var = BigFloat(var)
827 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
828         SWAPLONG(pheader->type);
829         SWAPLONG(pheader->filesize);
830         SWAPVEC(pheader->mins);
831         SWAPVEC(pheader->maxs);
832         SWAPFLOAT(pheader->radius);
833         SWAPLONG(pheader->numverts);
834         SWAPLONG(pheader->numtris);
835         SWAPLONG(pheader->numshaders);
836         SWAPLONG(pheader->numbones);
837         SWAPLONG(pheader->numscenes);
838
839 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
840         SWAPLUMPINFO(scenes);
841         SWAPLUMPINFO(poses);
842         SWAPLUMPINFO(bones);
843         SWAPLUMPINFO(vertbonecounts);
844         SWAPLUMPINFO(verts);
845         SWAPLUMPINFO(texcoords);
846         SWAPLUMPINFO(render);
847         SWAPLUMPINFO(shaders);
848         SWAPLUMPINFO(trizone);
849
850         loadmodel->flags = 0; // there are no flags
851         loadmodel->numframes = pheader->numscenes;
852         loadmodel->synctype = ST_SYNC;
853         loadmodel->numtris = pheader->numtris;
854
855         // FIXME: add skin support and texturing and shaders and...
856         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
857         loadmodel->skinscenes[0].firstframe = 0;
858         loadmodel->skinscenes[0].framecount = 1;
859         loadmodel->skinscenes[0].loop = true;
860         loadmodel->skinscenes[0].framerate = 10;
861         loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
862         loadmodel->skinframes->base = NULL;
863         loadmodel->skinframes->fog = NULL;
864         loadmodel->skinframes->pants = NULL;
865         loadmodel->skinframes->shirt = NULL;
866         loadmodel->skinframes->glow = NULL;
867         loadmodel->skinframes->merged = NULL;
868         loadmodel->numskins = 1;
869         numposes = pheader->lump_poses.length / sizeof(float[3][4]) / pheader->numbones;
870
871         // go through the lumps, swapping things
872
873 //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
874         scene = (void *) (pheader->lump_scenes.start + pbase);
875         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
876         for (i = 0;i < pheader->numscenes;i++)
877         {
878                 SWAPVEC(scene->mins);
879                 SWAPVEC(scene->maxs);
880                 SWAPFLOAT(scene->radius);
881                 SWAPFLOAT(scene->framerate);
882                 SWAPLONG(scene->flags);
883                 SWAPLONG(scene->start);
884                 SWAPLONG(scene->length);
885
886                 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
887                 loadmodel->animscenes[i].firstframe = scene->start;
888                 loadmodel->animscenes[i].framecount = scene->length;
889                 loadmodel->animscenes[i].framerate = scene->framerate;
890                 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
891
892                 if ((unsigned int) loadmodel->animscenes[i].firstframe >= numposes)
893                         Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
894                 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > numposes)
895                         Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
896                 if (loadmodel->animscenes[i].framerate < 0)
897                         Host_Error("Mod_LoadZymoticModel: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
898                 scene++;
899         }
900
901 //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
902         zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
903
904 //      zymlump_t lump_bones; // zymbone_t bone[numbones];
905         bone = (void *) (pheader->lump_bones.start + pbase);
906         for (i = 0;i < pheader->numbones;i++)
907         {
908                 SWAPLONG(bone[i].flags);
909                 SWAPLONG(bone[i].parent);
910                 if (bone[i].parent >= i)
911                         Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
912         }
913
914 //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
915         zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
916         bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
917         for (i = 0;i < pheader->numbones;i++)
918                 if (bonecount[i] < 1)
919                         Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
920
921 //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
922         zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
923
924 //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
925         zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
926
927 //      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)
928         zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
929         // validate renderlist and swap winding order of tris
930         renderlist = (void *) (pheader->lump_render.start + pbase);
931         renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
932         i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
933         if (pheader->lump_render.length != i)
934                 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);
935         for (i = 0;i < pheader->numshaders;i++)
936         {
937                 if (renderlist >= renderlistend)
938                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
939                 count = *renderlist++;
940                 if (renderlist + count * 3 > renderlistend)
941                         Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
942                 while (count--)
943                 {
944                         a = renderlist[0];
945                         b = renderlist[1];
946                         c = renderlist[2];
947                         if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
948                                 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
949                         renderlist[0] = c;
950                         renderlist[1] = b;
951                         renderlist[2] = a;
952                         renderlist += 3;
953                 }
954         }
955
956 //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
957         shadername = (void *) (pheader->lump_shaders.start + pbase);
958         texture = (void *) shadername;
959         for (i = 0;i < pheader->numshaders;i++)
960         {
961                 rtexture_t *rt;
962                 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
963                 shadername += 32;
964                 *texture++ = rt; // reuse shader name list for texture pointers
965         }
966
967 //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
968         zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
969
970         // model bbox
971         modelradius = pheader->radius;
972         for (i = 0;i < 3;i++)
973         {
974                 loadmodel->normalmins[i] = pheader->mins[i];
975                 loadmodel->normalmaxs[i] = pheader->maxs[i];
976                 loadmodel->rotatedmins[i] = -modelradius;
977                 loadmodel->rotatedmaxs[i] = modelradius;
978         }
979         corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
980         corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
981         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
982         if (loadmodel->yawmaxs[0] > modelradius)
983                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
984         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
985         loadmodel->yawmins[2] = loadmodel->normalmins[2];
986         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
987
988         loadmodel->Draw = R_DrawZymoticModel;
989         loadmodel->DrawSky = NULL;
990         loadmodel->DrawShadow = NULL;
991 }
992