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