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