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