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