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