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