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