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