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