merged R_Shadow_Stage_LightWithShadows and R_Shadow_Stage_LightWithoutShadows to...
[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 #include "r_shadow.h"
24
25 void Mod_AliasInit (void)
26 {
27 }
28
29 static void Mod_MDLMD2MD3_TraceBox(model_t *model, int frame, trace_t *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs, int hitsupercontentsmask)
30 {
31         int i, framenum;
32         float segmentmins[3], segmentmaxs[3];
33         colbrushf_t *thisbrush_start, *thisbrush_end;
34         matrix4x4_t startmatrix, endmatrix;
35         memset(trace, 0, sizeof(*trace));
36         trace->fraction = 1;
37         trace->realfraction = 1;
38         trace->hitsupercontentsmask = hitsupercontentsmask;
39         segmentmins[0] = min(boxstartmins[0], boxendmins[0]);
40         segmentmins[1] = min(boxstartmins[1], boxendmins[1]);
41         segmentmins[2] = min(boxstartmins[2], boxendmins[2]);
42         segmentmaxs[0] = max(boxstartmaxs[0], boxendmaxs[0]);
43         segmentmaxs[1] = max(boxstartmaxs[1], boxendmaxs[1]);
44         segmentmaxs[2] = max(boxstartmaxs[2], boxendmaxs[2]);
45         if (VectorCompare(boxstartmins, boxstartmaxs) && VectorCompare(boxendmins, boxendmaxs))
46         {
47                 // line trace
48                 for (i = 0;i < model->alias.aliasnum_meshes;i++)
49                 {
50                         framenum = frame;
51                         if (framenum < 0 || framenum > model->alias.aliasdata_meshes[i].num_frames)
52                                 framenum = 0;
53                         Collision_TraceLineTriangleMeshFloat(trace, boxstartmins, boxendmins, model->alias.aliasdata_meshes[i].num_triangles, model->alias.aliasdata_meshes[i].data_element3i, model->alias.aliasdata_meshes[i].data_aliasvertex3f + framenum * model->alias.aliasdata_meshes[i].num_vertices * 3, SUPERCONTENTS_SOLID, segmentmins, segmentmaxs);
54                 }
55         }
56         else
57         {
58                 // box trace, performed as brush trace
59                 Matrix4x4_CreateIdentity(&startmatrix);
60                 Matrix4x4_CreateIdentity(&endmatrix);
61                 thisbrush_start = Collision_BrushForBox(&startmatrix, boxstartmins, boxstartmaxs);
62                 thisbrush_end = Collision_BrushForBox(&endmatrix, boxendmins, boxendmaxs);
63                 for (i = 0;i < model->alias.aliasnum_meshes;i++)
64                 {
65                         framenum = frame;
66                         if (framenum < 0 || framenum > model->alias.aliasdata_meshes[i].num_frames)
67                                 framenum = 0;
68                         Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, model->alias.aliasdata_meshes[i].num_triangles, model->alias.aliasdata_meshes[i].data_element3i, model->alias.aliasdata_meshes[i].data_aliasvertex3f + framenum * model->alias.aliasdata_meshes[i].num_vertices * 3, SUPERCONTENTS_SOLID, segmentmins, segmentmaxs);
69                 }
70         }
71 }
72
73 static void Mod_CalcAliasModelBBoxes (void)
74 {
75         int vnum, meshnum;
76         float dist, yawradius, radius;
77         aliasmesh_t *mesh;
78         float *v;
79         VectorClear(loadmodel->normalmins);
80         VectorClear(loadmodel->normalmaxs);
81         yawradius = 0;
82         radius = 0;
83         for (meshnum = 0, mesh = loadmodel->alias.aliasdata_meshes;meshnum < loadmodel->alias.aliasnum_meshes;meshnum++, mesh++)
84         {
85                 for (vnum = 0, v = mesh->data_aliasvertex3f;vnum < mesh->num_vertices * mesh->num_frames;vnum++, v += 3)
86                 {
87                         if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
88                         if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
89                         if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
90                         if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
91                         if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
92                         if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
93                         dist = v[0] * v[0] + v[1] * v[1];
94                         if (yawradius < dist)
95                                 yawradius = dist;
96                         dist += v[2] * v[2];
97                         if (radius < dist)
98                                 radius = dist;
99                 }
100         }
101         radius = sqrt(radius);
102         yawradius = sqrt(yawradius);
103         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
104         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = yawradius;
105         loadmodel->yawmins[2] = loadmodel->normalmins[2];
106         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
107         loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
108         loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
109         loadmodel->radius = radius;
110         loadmodel->radius2 = radius * radius;
111 }
112
113 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
114 {
115         int i, j;
116         vec3_t temp;
117         for (i = 0;i < inverts;i++)
118         {
119                 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
120                         continue;
121                 temp[0] = v[i].v[0] * scale[0] + translate[0];
122                 temp[1] = v[i].v[1] * scale[1] + translate[1];
123                 temp[2] = v[i].v[2] * scale[2] + translate[2];
124                 j = vertremap[i]; // not onseam
125                 if (j >= 0)
126                         VectorCopy(temp, out3f + j * 3);
127                 j = vertremap[i+inverts]; // onseam
128                 if (j >= 0)
129                         VectorCopy(temp, out3f + j * 3);
130         }
131 }
132
133 static void Mod_MDL_LoadFrames (qbyte* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
134 {
135         int i, f, pose, groupframes;
136         float interval;
137         daliasframetype_t *pframetype;
138         daliasframe_t *pinframe;
139         daliasgroup_t *group;
140         daliasinterval_t *intervals;
141         animscene_t *scene;
142         pose = 0;
143         scene = loadmodel->animscenes;
144         for (f = 0;f < loadmodel->numframes;f++)
145         {
146                 pframetype = (daliasframetype_t *)datapointer;
147                 datapointer += sizeof(daliasframetype_t);
148                 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
149                 {
150                         // a single frame is still treated as a group
151                         interval = 0.1f;
152                         groupframes = 1;
153                 }
154                 else
155                 {
156                         // read group header
157                         group = (daliasgroup_t *)datapointer;
158                         datapointer += sizeof(daliasgroup_t);
159                         groupframes = LittleLong (group->numframes);
160
161                         // intervals (time per frame)
162                         intervals = (daliasinterval_t *)datapointer;
163                         datapointer += sizeof(daliasinterval_t) * groupframes;
164
165                         interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
166                         if (interval < 0.01f)
167                         {
168                                 Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
169                                 interval = 0.1f;
170                         }
171                 }
172
173                 // get scene name from first frame
174                 pinframe = (daliasframe_t *)datapointer;
175
176                 strcpy(scene->name, pinframe->name);
177                 scene->firstframe = pose;
178                 scene->framecount = groupframes;
179                 scene->framerate = 1.0f / interval;
180                 scene->loop = true;
181                 scene++;
182
183                 // read frames
184                 for (i = 0;i < groupframes;i++)
185                 {
186                         pinframe = (daliasframe_t *)datapointer;
187                         datapointer += sizeof(daliasframe_t);
188                         Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, vertremap);
189                         Mod_BuildTextureVectorsAndNormals(loadmodel->alias.aliasdata_meshes->num_vertices, loadmodel->alias.aliasdata_meshes->num_triangles, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_texcoord2f, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->data_aliassvector3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliastvector3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliasnormal3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3);
190                         datapointer += sizeof(trivertx_t) * inverts;
191                         pose++;
192                 }
193         }
194 }
195
196 static skinframe_t missingskinframe;
197 aliaslayer_t mod_alias_layersbuffer[16]; // 7 currently used
198 void Mod_BuildAliasSkinFromSkinFrame(aliasskin_t *skin, skinframe_t *skinframe)
199 {
200         aliaslayer_t *layer;
201
202         // hack
203         if (skinframe == NULL)
204         {
205                 skinframe = &missingskinframe;
206                 memset(skinframe, 0, sizeof(*skinframe));
207                 skinframe->base = r_notexture;
208         }
209
210         memset(&mod_alias_layersbuffer, 0, sizeof(mod_alias_layersbuffer));
211         layer = mod_alias_layersbuffer;
212         layer->flags = ALIASLAYER_SPECULAR;
213         layer->texture = skinframe->gloss;
214         layer->nmap = skinframe->nmap;
215         layer++;
216         if (skinframe->merged != NULL)
217         {
218                 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_COLORMAPPED;
219                 layer->texture = skinframe->merged;
220                 layer->nmap = skinframe->nmap;
221                 layer++;
222         }
223         if (skinframe->base != NULL)
224         {
225                 layer->flags = ALIASLAYER_DIFFUSE;
226                 if (skinframe->merged != NULL)
227                         layer->flags |= ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED;
228                 layer->texture = skinframe->base;
229                 layer->nmap = skinframe->nmap;
230                 layer++;
231         }
232         if (skinframe->pants != NULL)
233         {
234                 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_PANTS;
235                 layer->texture = skinframe->pants;
236                 layer->nmap = skinframe->nmap;
237                 layer++;
238         }
239         if (skinframe->shirt != NULL)
240         {
241                 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_SHIRT;
242                 layer->texture = skinframe->shirt;
243                 layer->nmap = skinframe->nmap;
244                 layer++;
245         }
246
247         if (skinframe->glow != NULL)
248         {
249                 layer->flags = 0;
250                 layer->texture = skinframe->glow;
251                 layer++;
252         }
253
254         layer->flags = ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS;
255         layer->texture = skinframe->fog;
256         layer++;
257
258         skin->flags = 0;
259         // fog texture only exists if some pixels are transparent...
260         if (skinframe->fog != NULL)
261                 skin->flags |= ALIASSKIN_TRANSPARENT;
262
263         skin->num_layers = layer - mod_alias_layersbuffer;
264         skin->data_layers = Mem_Alloc(loadmodel->mempool, skin->num_layers * sizeof(aliaslayer_t));
265         memcpy(skin->data_layers, mod_alias_layersbuffer, skin->num_layers * sizeof(aliaslayer_t));
266 }
267
268 void Mod_BuildAliasSkinsFromSkinFiles(aliasskin_t *skin, skinfile_t *skinfile, char *meshname, char *shadername)
269 {
270         int i;
271         skinfileitem_t *skinfileitem;
272         skinframe_t tempskinframe;
273         if (skinfile)
274         {
275                 for (i = 0;skinfile;skinfile = skinfile->next, i++, skin++)
276                 {
277                         memset(skin, 0, sizeof(*skin));
278                         for (skinfileitem = skinfile->items;skinfileitem;skinfileitem = skinfileitem->next)
279                         {
280                                 // leave the skin unitialized (nodraw) if the replacement is "common/nodraw" or "textures/common/nodraw"
281                                 if (!strcmp(skinfileitem->name, meshname) && strcmp(skinfileitem->replacement, "common/nodraw") && strcmp(skinfileitem->replacement, "textures/common/nodraw"))
282                                 {
283                                         memset(&tempskinframe, 0, sizeof(tempskinframe));
284                                         if (Mod_LoadSkinFrame(&tempskinframe, skinfileitem->replacement, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
285                                                 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
286                                         else
287                                         {
288                                                 Con_Printf("mesh \"%s\": failed to load skin #%i \"%s\", falling back to mesh's internal shader name \"%s\"\n", meshname, i, skinfileitem->replacement, shadername);
289                                                 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
290                                                         Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
291                                                 else
292                                                 {
293                                                         Con_Printf("failed to load skin \"%s\"\n", shadername);
294                                                         Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
295                                                 }
296                                         }
297                                 }
298                         }
299                 }
300         }
301         else
302         {
303                 memset(&tempskinframe, 0, sizeof(tempskinframe));
304                 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
305                         Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
306                 else
307                 {
308                         Con_Printf("failed to load mesh \"%s\" shader \"%s\"\n", meshname, shadername);
309                         Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
310                 }
311         }
312 }
313
314 #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);
315 #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);
316 extern void R_Model_Alias_Draw(entity_render_t *ent);
317 extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist);
318 extern void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist);
319 void Mod_IDP0_Load(model_t *mod, void *buffer)
320 {
321         int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
322         float scales, scalet, scale[3], translate[3], interval;
323         mdl_t *pinmodel;
324         stvert_t *pinstverts;
325         dtriangle_t *pintriangles;
326         daliasskintype_t *pinskintype;
327         daliasskingroup_t *pinskingroup;
328         daliasskininterval_t *pinskinintervals;
329         daliasframetype_t *pinframetype;
330         daliasgroup_t *pinframegroup;
331         qbyte *datapointer, *startframes, *startskins;
332         char name[MAX_QPATH];
333         skinframe_t tempskinframe;
334         animscene_t *tempskinscenes;
335         aliasskin_t *tempaliasskins;
336         float *vertst;
337         int *vertonseam, *vertremap;
338         skinfile_t *skinfiles;
339
340         datapointer = buffer;
341         pinmodel = (mdl_t *)datapointer;
342         datapointer += sizeof(mdl_t);
343
344         version = LittleLong (pinmodel->version);
345         if (version != ALIAS_VERSION)
346                 Host_Error ("%s has wrong version number (%i should be %i)",
347                                  loadmodel->name, version, ALIAS_VERSION);
348
349         loadmodel->type = mod_alias;
350         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
351         loadmodel->DrawSky = NULL;
352         loadmodel->Draw = R_Model_Alias_Draw;
353         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
354         loadmodel->DrawLight = R_Model_Alias_DrawLight;
355         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
356
357         loadmodel->alias.aliasnum_meshes = 1;
358         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
359
360         loadmodel->numskins = LittleLong(pinmodel->numskins);
361         BOUNDI(loadmodel->numskins,0,65536);
362         skinwidth = LittleLong (pinmodel->skinwidth);
363         BOUNDI(skinwidth,0,65536);
364         skinheight = LittleLong (pinmodel->skinheight);
365         BOUNDI(skinheight,0,65536);
366         numverts = LittleLong(pinmodel->numverts);
367         BOUNDI(numverts,0,65536);
368         loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
369         BOUNDI(loadmodel->alias.aliasdata_meshes->num_triangles,0,65536);
370         loadmodel->numframes = LittleLong(pinmodel->numframes);
371         BOUNDI(loadmodel->numframes,0,65536);
372         loadmodel->synctype = LittleLong (pinmodel->synctype);
373         BOUNDI(loadmodel->synctype,0,2);
374         loadmodel->flags = LittleLong (pinmodel->flags);
375
376         for (i = 0;i < 3;i++)
377         {
378                 scale[i] = LittleFloat (pinmodel->scale[i]);
379                 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
380         }
381
382         startskins = datapointer;
383         totalskins = 0;
384         for (i = 0;i < loadmodel->numskins;i++)
385         {
386                 pinskintype = (daliasskintype_t *)datapointer;
387                 datapointer += sizeof(daliasskintype_t);
388                 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
389                         groupskins = 1;
390                 else
391                 {
392                         pinskingroup = (daliasskingroup_t *)datapointer;
393                         datapointer += sizeof(daliasskingroup_t);
394                         groupskins = LittleLong(pinskingroup->numskins);
395                         datapointer += sizeof(daliasskininterval_t) * groupskins;
396                 }
397
398                 for (j = 0;j < groupskins;j++)
399                 {
400                         datapointer += skinwidth * skinheight;
401                         totalskins++;
402                 }
403         }
404
405         pinstverts = (stvert_t *)datapointer;
406         datapointer += sizeof(stvert_t) * numverts;
407
408         pintriangles = (dtriangle_t *)datapointer;
409         datapointer += sizeof(dtriangle_t) * loadmodel->alias.aliasdata_meshes->num_triangles;
410
411         startframes = datapointer;
412         loadmodel->alias.aliasdata_meshes->num_frames = 0;
413         for (i = 0;i < loadmodel->numframes;i++)
414         {
415                 pinframetype = (daliasframetype_t *)datapointer;
416                 datapointer += sizeof(daliasframetype_t);
417                 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
418                         groupframes = 1;
419                 else
420                 {
421                         pinframegroup = (daliasgroup_t *)datapointer;
422                         datapointer += sizeof(daliasgroup_t);
423                         groupframes = LittleLong(pinframegroup->numframes);
424                         datapointer += sizeof(daliasinterval_t) * groupframes;
425                 }
426
427                 for (j = 0;j < groupframes;j++)
428                 {
429                         datapointer += sizeof(daliasframe_t);
430                         datapointer += sizeof(trivertx_t) * numverts;
431                         loadmodel->alias.aliasdata_meshes->num_frames++;
432                 }
433         }
434
435         // store texture coordinates into temporary array, they will be stored
436         // after usage is determined (triangle data)
437         vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
438         vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
439         vertonseam = vertremap + numverts * 2;
440
441         scales = 1.0 / skinwidth;
442         scalet = 1.0 / skinheight;
443         for (i = 0;i < numverts;i++)
444         {
445                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
446                 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
447                 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
448                 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
449                 vertst[(i+numverts)*2+1] = vertst[i*2+1];
450         }
451
452 // load triangle data
453         loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->alias.aliasdata_meshes->num_triangles);
454
455         // read the triangle elements
456         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
457                 for (j = 0;j < 3;j++)
458                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
459         // validate (note numverts is used because this is the original data)
460         Mod_ValidateElements(loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
461         // now butcher the elements according to vertonseam and tri->facesfront
462         // and then compact the vertex set to remove duplicates
463         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
464                 if (!LittleLong(pintriangles[i].facesfront)) // backface
465                         for (j = 0;j < 3;j++)
466                                 if (vertonseam[loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j]])
467                                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] += numverts;
468         // count the usage
469         // (this uses vertremap to count usage to save some memory)
470         for (i = 0;i < numverts*2;i++)
471                 vertremap[i] = 0;
472         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles*3;i++)
473                 vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]]++;
474         // build remapping table and compact array
475         loadmodel->alias.aliasdata_meshes->num_vertices = 0;
476         for (i = 0;i < numverts*2;i++)
477         {
478                 if (vertremap[i])
479                 {
480                         vertremap[i] = loadmodel->alias.aliasdata_meshes->num_vertices;
481                         vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
482                         vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
483                         loadmodel->alias.aliasdata_meshes->num_vertices++;
484                 }
485                 else
486                         vertremap[i] = -1; // not used at all
487         }
488         // remap the elements to the new vertex set
489         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles * 3;i++)
490                 loadmodel->alias.aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]];
491         // store the texture coordinates
492         loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->alias.aliasdata_meshes->num_vertices);
493         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_vertices;i++)
494         {
495                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
496                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
497         }
498
499 // load the frames
500         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
501         loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[4][3]) * loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices);
502         loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 1;
503         loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 2;
504         loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 3;
505         Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
506         loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
507         Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
508         Mod_CalcAliasModelBBoxes();
509
510         Mem_Free(vertst);
511         Mem_Free(vertremap);
512
513         // load the skins
514         if ((skinfiles = Mod_LoadSkinFiles()))
515         {
516                 loadmodel->alias.aliasdata_meshes->num_skins = totalskins = loadmodel->numskins;
517                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
518                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
519                 Mod_FreeSkinFiles(skinfiles);
520                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
521                 for (i = 0;i < loadmodel->numskins;i++)
522                 {
523                         loadmodel->skinscenes[i].firstframe = i;
524                         loadmodel->skinscenes[i].framecount = 1;
525                         loadmodel->skinscenes[i].loop = true;
526                         loadmodel->skinscenes[i].framerate = 10;
527                 }
528         }
529         else
530         {
531                 loadmodel->alias.aliasdata_meshes->num_skins = totalskins;
532                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
533                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
534                 totalskins = 0;
535                 datapointer = startskins;
536                 for (i = 0;i < loadmodel->numskins;i++)
537                 {
538                         pinskintype = (daliasskintype_t *)datapointer;
539                         datapointer += sizeof(daliasskintype_t);
540
541                         if (pinskintype->type == ALIAS_SKIN_SINGLE)
542                         {
543                                 groupskins = 1;
544                                 interval = 0.1f;
545                         }
546                         else
547                         {
548                                 pinskingroup = (daliasskingroup_t *)datapointer;
549                                 datapointer += sizeof(daliasskingroup_t);
550
551                                 groupskins = LittleLong (pinskingroup->numskins);
552
553                                 pinskinintervals = (daliasskininterval_t *)datapointer;
554                                 datapointer += sizeof(daliasskininterval_t) * groupskins;
555
556                                 interval = LittleFloat(pinskinintervals[0].interval);
557                                 if (interval < 0.01f)
558                                 {
559                                         Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
560                                         interval = 0.1f;
561                                 }
562                         }
563
564                         sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
565                         loadmodel->skinscenes[i].firstframe = totalskins;
566                         loadmodel->skinscenes[i].framecount = groupskins;
567                         loadmodel->skinscenes[i].framerate = 1.0f / interval;
568                         loadmodel->skinscenes[i].loop = true;
569
570                         for (j = 0;j < groupskins;j++)
571                         {
572                                 if (groupskins > 1)
573                                         sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
574                                 else
575                                         sprintf (name, "%s_%i", loadmodel->name, i);
576                                 if (!Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, true))
577                                         Mod_LoadSkinFrame_Internal(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, r_fullbrights.integer, (qbyte *)datapointer, skinwidth, skinheight);
578                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
579                                 datapointer += skinwidth * skinheight;
580                                 totalskins++;
581                         }
582                 }
583                 // check for skins that don't exist in the model, but do exist as external images
584                 // (this was added because yummyluv kept pestering me about support for it)
585                 while (Mod_LoadSkinFrame(&tempskinframe, va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, true))
586                 {
587                         // expand the arrays to make room
588                         tempskinscenes = loadmodel->skinscenes;
589                         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
590                         memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
591                         Mem_Free(tempskinscenes);
592
593                         tempaliasskins = loadmodel->alias.aliasdata_meshes->data_skins;
594                         loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(aliasskin_t));
595                         memcpy(loadmodel->alias.aliasdata_meshes->data_skins, tempaliasskins, totalskins * sizeof(aliasskin_t));
596                         Mem_Free(tempaliasskins);
597
598                         // store the info about the new skin
599                         Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
600                         strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
601                         loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
602                         loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
603                         loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
604                         loadmodel->skinscenes[loadmodel->numskins].loop = true;
605
606                         //increase skin counts
607                         loadmodel->alias.aliasdata_meshes->num_skins++;
608                         loadmodel->numskins++;
609                         totalskins++;
610                 }
611         }
612 }
613
614 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
615 {
616         int i;
617         trivertx_t *in;
618         for (i = 0;i < numverts;i++, out3f += 3)
619         {
620                 in = v + vertremap[i];
621                 out3f[0] = in->v[0] * scale[0] + translate[0];
622                 out3f[1] = in->v[1] * scale[1] + translate[1];
623                 out3f[2] = in->v[2] * scale[2] + translate[2];
624         }
625 }
626
627 void Mod_IDP2_Load(model_t *mod, void *buffer)
628 {
629         int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
630         float *stverts, s, t, scale[3], translate[3];
631         md2_t *pinmodel;
632         qbyte *base, *datapointer;
633         md2frame_t *pinframe;
634         char *inskin;
635         md2triangle_t *intri;
636         unsigned short *inst;
637         struct md2verthash_s
638         {
639                 struct md2verthash_s *next;
640                 int xyz;
641                 float st[2];
642         }
643         *hash, **md2verthash, *md2verthashdata;
644         skinframe_t tempskinframe;
645         skinfile_t *skinfiles;
646
647         pinmodel = buffer;
648         base = buffer;
649
650         version = LittleLong (pinmodel->version);
651         if (version != MD2ALIAS_VERSION)
652                 Host_Error ("%s has wrong version number (%i should be %i)",
653                         loadmodel->name, version, MD2ALIAS_VERSION);
654
655         loadmodel->type = mod_alias;
656         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
657         loadmodel->DrawSky = NULL;
658         loadmodel->Draw = R_Model_Alias_Draw;
659         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
660         loadmodel->DrawLight = R_Model_Alias_DrawLight;
661         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
662
663         if (LittleLong(pinmodel->num_tris) < 1 || LittleLong(pinmodel->num_tris) > 65536)
664                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
665         if (LittleLong(pinmodel->num_xyz) < 1 || LittleLong(pinmodel->num_xyz) > 65536)
666                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
667         if (LittleLong(pinmodel->num_frames) < 1 || LittleLong(pinmodel->num_frames) > 65536)
668                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
669         if (LittleLong(pinmodel->num_skins) < 0 || LittleLong(pinmodel->num_skins) > 256)
670                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
671
672         end = LittleLong(pinmodel->ofs_end);
673         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins) <= 0 || LittleLong(pinmodel->ofs_skins) >= end))
674                 Host_Error ("%s is not a valid model", loadmodel->name);
675         if (LittleLong(pinmodel->ofs_st) <= 0 || LittleLong(pinmodel->ofs_st) >= end)
676                 Host_Error ("%s is not a valid model", loadmodel->name);
677         if (LittleLong(pinmodel->ofs_tris) <= 0 || LittleLong(pinmodel->ofs_tris) >= end)
678                 Host_Error ("%s is not a valid model", loadmodel->name);
679         if (LittleLong(pinmodel->ofs_frames) <= 0 || LittleLong(pinmodel->ofs_frames) >= end)
680                 Host_Error ("%s is not a valid model", loadmodel->name);
681         if (LittleLong(pinmodel->ofs_glcmds) <= 0 || LittleLong(pinmodel->ofs_glcmds) >= end)
682                 Host_Error ("%s is not a valid model", loadmodel->name);
683
684         loadmodel->alias.aliasnum_meshes = 1;
685         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
686
687         loadmodel->numskins = LittleLong(pinmodel->num_skins);
688         numxyz = LittleLong(pinmodel->num_xyz);
689         numst = LittleLong(pinmodel->num_st);
690         loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
691         loadmodel->numframes = LittleLong(pinmodel->num_frames);
692         loadmodel->alias.aliasdata_meshes->num_frames = loadmodel->numframes;
693         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
694
695         loadmodel->flags = 0; // there are no MD2 flags
696         loadmodel->synctype = ST_RAND;
697
698         // load the skins
699         inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
700         if ((skinfiles = Mod_LoadSkinFiles()))
701         {
702                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
703                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
704                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
705                 Mod_FreeSkinFiles(skinfiles);
706         }
707         else if (loadmodel->numskins)
708         {
709                 // skins found (most likely not a player model)
710                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
711                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
712                 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
713                 {
714                         if (Mod_LoadSkinFrame(&tempskinframe, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
715                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, &tempskinframe);
716                         else
717                         {
718                                 Con_Printf("%s is missing skin \"%s\"\n", loadmodel->name, inskin);
719                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, NULL);
720                         }
721                 }
722         }
723         else
724         {
725                 // no skins (most likely a player model)
726                 loadmodel->numskins = 1;
727                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
728                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
729                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins, NULL);
730         }
731
732         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
733         for (i = 0;i < loadmodel->numskins;i++)
734         {
735                 loadmodel->skinscenes[i].firstframe = i;
736                 loadmodel->skinscenes[i].framecount = 1;
737                 loadmodel->skinscenes[i].loop = true;
738                 loadmodel->skinscenes[i].framerate = 10;
739         }
740
741         // load the triangles and stvert data
742         inst = (void*)(base + LittleLong(pinmodel->ofs_st));
743         intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
744         skinwidth = LittleLong(pinmodel->skinwidth);
745         skinheight = LittleLong(pinmodel->skinheight);
746
747         stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
748         s = 1.0f / skinwidth;
749         t = 1.0f / skinheight;
750         for (i = 0;i < numst;i++)
751         {
752                 j = (unsigned short) LittleShort(inst[i*2+0]);
753                 k = (unsigned short) LittleShort(inst[i*2+1]);
754                 if (j >= skinwidth || k >= skinheight)
755                 {
756                         Con_Printf("%s has an invalid skin coordinate (%i %i) on vert %i, changing to 0 0\n", loadmodel->name, j, k, i);
757                         j = 0;
758                         k = 0;
759                 }
760                 stverts[i*2+0] = j * s;
761                 stverts[i*2+1] = k * t;
762         }
763
764         md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
765         md2verthashdata = Mem_Alloc(tempmempool, loadmodel->alias.aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
766         // swap the triangle list
767         num = 0;
768         loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
769         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
770         {
771                 for (j = 0;j < 3;j++)
772                 {
773                         xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
774                         st = (unsigned short) LittleShort (intri[i].index_st[j]);
775                         if (xyz >= numxyz)
776                         {
777                                 Con_Printf("%s has an invalid xyz index (%i) on triangle %i, resetting to 0\n", loadmodel->name, xyz, i);
778                                 xyz = 0;
779                         }
780                         if (st >= numst)
781                         {
782                                 Con_Printf("%s has an invalid st index (%i) on triangle %i, resetting to 0\n", loadmodel->name, st, i);
783                                 st = 0;
784                         }
785                         s = stverts[st*2+0];
786                         t = stverts[st*2+1];
787                         hashindex = (xyz * 17 + st) & 255;
788                         for (hash = md2verthash[hashindex];hash;hash = hash->next)
789                                 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
790                                         break;
791                         if (hash == NULL)
792                         {
793                                 hash = md2verthashdata + num++;
794                                 hash->xyz = xyz;
795                                 hash->st[0] = s;
796                                 hash->st[1] = t;
797                                 hash->next = md2verthash[hashindex];
798                                 md2verthash[hashindex] = hash;
799                         }
800                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
801                 }
802         }
803
804         Mem_Free(stverts);
805
806         numverts = num;
807         loadmodel->alias.aliasdata_meshes->num_vertices = numverts;
808         vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
809         loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
810         for (i = 0;i < num;i++)
811         {
812                 hash = md2verthashdata + i;
813                 vertremap[i] = hash->xyz;
814                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
815                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
816         }
817
818         Mem_Free(md2verthash);
819         Mem_Free(md2verthashdata);
820
821         // load the frames
822         datapointer = (base + LittleLong(pinmodel->ofs_frames));
823         loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->alias.aliasdata_meshes->num_frames * sizeof(float[4][3]));
824         loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 1;
825         loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 2;
826         loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 3;
827         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_frames;i++)
828         {
829                 pinframe = (md2frame_t *)datapointer;
830                 datapointer += sizeof(md2frame_t);
831                 for (j = 0;j < 3;j++)
832                 {
833                         scale[j] = LittleFloat(pinframe->scale[j]);
834                         translate[j] = LittleFloat(pinframe->translate[j]);
835                 }
836                 Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * numverts * 3, numverts, vertremap);
837                 Mod_BuildTextureVectorsAndNormals(loadmodel->alias.aliasdata_meshes->num_vertices, loadmodel->alias.aliasdata_meshes->num_triangles, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_texcoord2f, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->data_aliassvector3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliastvector3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliasnormal3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3);
838                 datapointer += numxyz * sizeof(trivertx_t);
839
840                 strcpy(loadmodel->animscenes[i].name, pinframe->name);
841                 loadmodel->animscenes[i].firstframe = i;
842                 loadmodel->animscenes[i].framecount = 1;
843                 loadmodel->animscenes[i].framerate = 10;
844                 loadmodel->animscenes[i].loop = true;
845         }
846
847         Mem_Free(vertremap);
848
849         loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
850         Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
851         Mod_CalcAliasModelBBoxes();
852 }
853
854 void Mod_IDP3_Load(model_t *mod, void *buffer)
855 {
856         int i, j, k, version;
857         md3modelheader_t *pinmodel;
858         md3frameinfo_t *pinframe;
859         md3mesh_t *pinmesh;
860         md3tag_t *pintag;
861         aliasmesh_t *mesh;
862         skinfile_t *skinfiles;
863
864         pinmodel = buffer;
865
866         if (memcmp(pinmodel->identifier, "IDP3", 4))
867                 Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
868         version = LittleLong (pinmodel->version);
869         if (version != MD3VERSION)
870                 Host_Error ("%s has wrong version number (%i should be %i)",
871                         loadmodel->name, version, MD3VERSION);
872
873         skinfiles = Mod_LoadSkinFiles();
874         if (loadmodel->numskins < 1)
875                 loadmodel->numskins = 1;
876
877         loadmodel->type = mod_alias;
878         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
879         loadmodel->DrawSky = NULL;
880         loadmodel->Draw = R_Model_Alias_Draw;
881         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
882         loadmodel->DrawLight = R_Model_Alias_DrawLight;
883         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
884         loadmodel->flags = LittleLong(pinmodel->flags);
885         loadmodel->synctype = ST_RAND;
886
887         // set up some global info about the model
888         loadmodel->numframes = LittleLong(pinmodel->num_frames);
889         loadmodel->alias.aliasnum_meshes = LittleLong(pinmodel->num_meshes);
890
891         // make skinscenes for the skins (no groups)
892         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
893         for (i = 0;i < loadmodel->numskins;i++)
894         {
895                 loadmodel->skinscenes[i].firstframe = i;
896                 loadmodel->skinscenes[i].framecount = 1;
897                 loadmodel->skinscenes[i].loop = true;
898                 loadmodel->skinscenes[i].framerate = 10;
899         }
900
901         // load frameinfo
902         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
903         for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
904         {
905                 strcpy(loadmodel->animscenes[i].name, pinframe->name);
906                 loadmodel->animscenes[i].firstframe = i;
907                 loadmodel->animscenes[i].framecount = 1;
908                 loadmodel->animscenes[i].framerate = 10;
909                 loadmodel->animscenes[i].loop = true;
910         }
911
912         // load tags
913         loadmodel->alias.aliasnum_tagframes = loadmodel->numframes;
914         loadmodel->alias.aliasnum_tags = LittleLong(pinmodel->num_tags);
915         loadmodel->alias.aliasdata_tags = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags * sizeof(aliastag_t));
916         for (i = 0, pintag = (md3tag_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_tags));i < loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags;i++, pintag++)
917         {
918                 strcpy(loadmodel->alias.aliasdata_tags[i].name, pintag->name);
919                 Matrix4x4_CreateIdentity(&loadmodel->alias.aliasdata_tags[i].matrix);
920                 for (j = 0;j < 3;j++)
921                 {
922                         for (k = 0;k < 3;k++)
923                                 loadmodel->alias.aliasdata_tags[i].matrix.m[j][k] = LittleFloat(pintag->rotationmatrix[k * 3 + j]);
924                         loadmodel->alias.aliasdata_tags[i].matrix.m[j][3] = LittleFloat(pintag->origin[j]);
925                 }
926                 //Con_Printf("model \"%s\" frame #%i tag #%i \"%s\"\n", loadmodel->name, i / loadmodel->alias.aliasnum_tags, i % loadmodel->alias.aliasnum_tags, loadmodel->alias.aliasdata_tags[i].name);
927         }
928
929         // load meshes
930         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
931         for (i = 0, pinmesh = (md3mesh_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_meshes));i < loadmodel->alias.aliasnum_meshes;i++, pinmesh = (md3mesh_t *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_end)))
932         {
933                 if (memcmp(pinmesh->identifier, "IDP3", 4))
934                         Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)\n");
935                 mesh = loadmodel->alias.aliasdata_meshes + i;
936                 mesh->num_skins = loadmodel->numskins;
937                 mesh->num_frames = LittleLong(pinmesh->num_frames);
938                 mesh->num_vertices = LittleLong(pinmesh->num_vertices);
939                 mesh->num_triangles = LittleLong(pinmesh->num_triangles);
940                 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
941                 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
942                 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
943                 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
944                 mesh->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_frames * sizeof(float[4][3]));
945                 mesh->data_aliassvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 1;
946                 mesh->data_aliastvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 2;
947                 mesh->data_aliasnormal3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 3;
948                 for (j = 0;j < mesh->num_triangles * 3;j++)
949                         mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + pinmesh->lump_elements))[j]);
950                 for (j = 0;j < mesh->num_vertices;j++)
951                 {
952                         mesh->data_texcoord2f[j * 2 + 0] = LittleFloat(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 0]);
953                         mesh->data_texcoord2f[j * 2 + 1] = LittleFloat(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 1]);
954                 }
955                 for (j = 0;j < mesh->num_vertices * mesh->num_frames;j++)
956                 {
957                         mesh->data_aliasvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 0]) * (1.0f / 64.0f);
958                         mesh->data_aliasvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 1]) * (1.0f / 64.0f);
959                         mesh->data_aliasvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 2]) * (1.0f / 64.0f);
960                 }
961                 for (j = 0;j < mesh->num_frames;j++)
962                         Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, mesh->data_aliasvertex3f + j * mesh->num_vertices * 3, mesh->data_texcoord2f, mesh->data_element3i, mesh->data_aliassvector3f + j * mesh->num_vertices * 3, mesh->data_aliastvector3f + j * mesh->num_vertices * 3, mesh->data_aliasnormal3f + j * mesh->num_vertices * 3);
963
964                 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
965                 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
966
967                 if (LittleLong(pinmesh->num_shaders) >= 1 && ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name[0])
968                         Mod_BuildAliasSkinsFromSkinFiles(mesh->data_skins, skinfiles, pinmesh->name, ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name);
969                 else
970                         for (j = 0;j < mesh->num_skins;j++)
971                                 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins + j, NULL);
972         }
973         Mod_CalcAliasModelBBoxes();
974         Mod_FreeSkinFiles(skinfiles);
975 }
976
977 extern void R_Model_Zymotic_DrawSky(entity_render_t *ent);
978 extern void R_Model_Zymotic_Draw(entity_render_t *ent);
979 extern void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist);
980 extern void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist);
981 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer)
982 {
983         zymtype1header_t *pinmodel, *pheader;
984         qbyte *pbase;
985
986         pinmodel = (void *)buffer;
987         pbase = buffer;
988         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
989                 Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model\n");
990         if (BigLong(pinmodel->type) != 1)
991                 Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
992
993         loadmodel->type = mod_alias;
994         loadmodel->alias.aliastype = ALIASTYPE_ZYM;
995         loadmodel->DrawSky = NULL;
996         loadmodel->Draw = R_Model_Zymotic_Draw;
997         loadmodel->DrawShadowVolume = NULL;//R_Model_Zymotic_DrawShadowVolume;
998         loadmodel->DrawLight = NULL;//R_Model_Zymotic_DrawLight;
999
1000         // byteswap header
1001         pheader = pinmodel;
1002         pheader->type = BigLong(pinmodel->type);
1003         pheader->filesize = BigLong(pinmodel->filesize);
1004         pheader->mins[0] = BigFloat(pinmodel->mins[0]);
1005         pheader->mins[1] = BigFloat(pinmodel->mins[1]);
1006         pheader->mins[2] = BigFloat(pinmodel->mins[2]);
1007         pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
1008         pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
1009         pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
1010         pheader->radius = BigFloat(pinmodel->radius);
1011         pheader->numverts = loadmodel->alias.zymnum_verts = BigLong(pinmodel->numverts);
1012         pheader->numtris = loadmodel->alias.zymnum_tris = BigLong(pinmodel->numtris);
1013         pheader->numshaders = loadmodel->alias.zymnum_shaders = BigLong(pinmodel->numshaders);
1014         pheader->numbones = loadmodel->alias.zymnum_bones = BigLong(pinmodel->numbones);
1015         pheader->numscenes = loadmodel->alias.zymnum_scenes = BigLong(pinmodel->numscenes);
1016         pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
1017         pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
1018         pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
1019         pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
1020         pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
1021         pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
1022         pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
1023         pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
1024         pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
1025         pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
1026         pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
1027         pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
1028         pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
1029         pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
1030         pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
1031         pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
1032         pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
1033         pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
1034
1035         loadmodel->flags = 0; // there are no flags
1036         loadmodel->numframes = pheader->numscenes;
1037         loadmodel->synctype = ST_SYNC;
1038         //loadmodel->num_triangles = pheader->num_triangles;
1039         //loadmodel->num_vertices = 0;
1040
1041         {
1042                 unsigned int i;
1043                 float modelradius, corner[2];
1044                 // model bbox
1045                 modelradius = pheader->radius;
1046                 for (i = 0;i < 3;i++)
1047                 {
1048                         loadmodel->normalmins[i] = pheader->mins[i];
1049                         loadmodel->normalmaxs[i] = pheader->maxs[i];
1050                         loadmodel->rotatedmins[i] = -modelradius;
1051                         loadmodel->rotatedmaxs[i] = modelradius;
1052                 }
1053                 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
1054                 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
1055                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1056                 if (loadmodel->yawmaxs[0] > modelradius)
1057                         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
1058                 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
1059                 loadmodel->yawmins[2] = loadmodel->normalmins[2];
1060                 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
1061                 loadmodel->radius = modelradius;
1062                 loadmodel->radius2 = modelradius * modelradius;
1063         }
1064
1065         {
1066                 // FIXME: add shaders, and make them switchable shader sets and...
1067                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t));
1068                 loadmodel->skinscenes[0].firstframe = 0;
1069                 loadmodel->skinscenes[0].framecount = 1;
1070                 loadmodel->skinscenes[0].loop = true;
1071                 loadmodel->skinscenes[0].framerate = 10;
1072                 loadmodel->numskins = 1;
1073         }
1074
1075         // go through the lumps, swapping things
1076
1077         {
1078                 int i, numposes;
1079                 zymscene_t *scene;
1080         //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1081                 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
1082                 scene = (void *) (pheader->lump_scenes.start + pbase);
1083                 numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
1084                 for (i = 0;i < pheader->numscenes;i++)
1085                 {
1086                         memcpy(loadmodel->animscenes[i].name, scene->name, 32);
1087                         loadmodel->animscenes[i].firstframe = BigLong(scene->start);
1088                         loadmodel->animscenes[i].framecount = BigLong(scene->length);
1089                         loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
1090                         loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
1091                         if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
1092                                 Host_Error("%s scene->firstframe (%i) >= numposes (%i)\n", loadmodel->name, loadmodel->animscenes[i].firstframe, numposes);
1093                         if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
1094                                 Host_Error("%s scene->firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->name, loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
1095                         if (loadmodel->animscenes[i].framerate < 0)
1096                                 Host_Error("%s scene->framerate (%f) < 0\n", loadmodel->name, loadmodel->animscenes[i].framerate);
1097                         scene++;
1098                 }
1099         }
1100
1101         {
1102                 int i;
1103                 float *poses;
1104         //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
1105                 loadmodel->alias.zymdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
1106                 poses = (void *) (pheader->lump_poses.start + pbase);
1107                 for (i = 0;i < pheader->lump_poses.length / 4;i++)
1108                         loadmodel->alias.zymdata_poses[i] = BigFloat(poses[i]);
1109         }
1110
1111         {
1112                 int i;
1113                 zymbone_t *bone;
1114         //      zymlump_t lump_bones; // zymbone_t bone[numbones];
1115                 loadmodel->alias.zymdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(zymbone_t));
1116                 bone = (void *) (pheader->lump_bones.start + pbase);
1117                 for (i = 0;i < pheader->numbones;i++)
1118                 {
1119                         memcpy(loadmodel->alias.zymdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
1120                         loadmodel->alias.zymdata_bones[i].flags = BigLong(bone[i].flags);
1121                         loadmodel->alias.zymdata_bones[i].parent = BigLong(bone[i].parent);
1122                         if (loadmodel->alias.zymdata_bones[i].parent >= i)
1123                                 Host_Error("%s bone[%i].parent >= %i\n", loadmodel->name, i, i);
1124                 }
1125         }
1126
1127         {
1128                 int i, *bonecount;
1129         //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1130                 loadmodel->alias.zymdata_vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1131                 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
1132                 for (i = 0;i < pheader->numverts;i++)
1133                 {
1134                         loadmodel->alias.zymdata_vertbonecounts[i] = BigLong(bonecount[i]);
1135                         if (loadmodel->alias.zymdata_vertbonecounts[i] < 1)
1136                                 Host_Error("%s bonecount[%i] < 1\n", loadmodel->name, i);
1137                 }
1138         }
1139
1140         {
1141                 int i;
1142                 zymvertex_t *vertdata;
1143         //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1144                 loadmodel->alias.zymdata_verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
1145                 vertdata = (void *) (pheader->lump_verts.start + pbase);
1146                 for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
1147                 {
1148                         loadmodel->alias.zymdata_verts[i].bonenum = BigLong(vertdata[i].bonenum);
1149                         loadmodel->alias.zymdata_verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
1150                         loadmodel->alias.zymdata_verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
1151                         loadmodel->alias.zymdata_verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
1152                 }
1153         }
1154
1155         {
1156                 int i;
1157                 float *intexcoord2f, *outtexcoord2f;
1158         //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1159                 loadmodel->alias.zymdata_texcoords = outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
1160                 intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
1161                 for (i = 0;i < pheader->numverts;i++)
1162                 {
1163                         outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1164                         // flip T coordinate for OpenGL
1165                         outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1166                 }
1167         }
1168
1169         {
1170                 int i, count, *renderlist, *renderlistend, *outrenderlist;
1171         //      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)
1172                 loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, pheader->lump_render.length);
1173                 // byteswap, validate, and swap winding order of tris
1174                 count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1175                 if (pheader->lump_render.length != count)
1176                         Host_Error("%s renderlist is wrong size (%i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, count);
1177                 outrenderlist = loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, count);
1178                 renderlist = (void *) (pheader->lump_render.start + pbase);
1179                 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
1180                 for (i = 0;i < pheader->numshaders;i++)
1181                 {
1182                         if (renderlist >= renderlistend)
1183                                 Host_Error("%s corrupt renderlist (wrong size)\n", loadmodel->name);
1184                         count = BigLong(*renderlist);renderlist++;
1185                         if (renderlist + count * 3 > renderlistend)
1186                                 Host_Error("%s corrupt renderlist (wrong size)\n", loadmodel->name);
1187                         *outrenderlist++ = count;
1188                         while (count--)
1189                         {
1190                                 outrenderlist[2] = BigLong(renderlist[0]);
1191                                 outrenderlist[1] = BigLong(renderlist[1]);
1192                                 outrenderlist[0] = BigLong(renderlist[2]);
1193                                 if ((unsigned int)outrenderlist[0] >= (unsigned int)pheader->numverts
1194                                  || (unsigned int)outrenderlist[1] >= (unsigned int)pheader->numverts
1195                                  || (unsigned int)outrenderlist[2] >= (unsigned int)pheader->numverts)
1196                                         Host_Error("%s corrupt renderlist (out of bounds index)\n", loadmodel->name);
1197                                 renderlist += 3;
1198                                 outrenderlist += 3;
1199                         }
1200                 }
1201         }
1202
1203         {
1204                 int i;
1205                 char *shadername;
1206         //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1207                 loadmodel->alias.zymdata_textures = Mem_Alloc(loadmodel->mempool, pheader->numshaders * sizeof(rtexture_t *));
1208                 shadername = (void *) (pheader->lump_shaders.start + pbase);
1209                 for (i = 0;i < pheader->numshaders;i++)
1210                         loadmodel->alias.zymdata_textures[i] = loadtextureimage(loadmodel->texturepool, shadername + i * 32, 0, 0, true, TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP | (r_mipskins.integer ? TEXF_MIPMAP : 0));
1211         }
1212
1213         {
1214         //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1215                 loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1216                 memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);
1217         }
1218 }
1219