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