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