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