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