calculate bounding box properly for psk models, and a cleanup of bounding box generat...
[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 cvar_t r_skeletal_debugbone = {0, "r_skeletal_debugbone", "-1", "development cvar for testing skeletal model code"};
26 cvar_t r_skeletal_debugbonecomponent = {0, "r_skeletal_debugbonecomponent", "3", "development cvar for testing skeletal model code"};
27 cvar_t r_skeletal_debugbonevalue = {0, "r_skeletal_debugbonevalue", "100", "development cvar for testing skeletal model code"};
28 cvar_t r_skeletal_debugtranslatex = {0, "r_skeletal_debugtranslatex", "1", "development cvar for testing skeletal model code"};
29 cvar_t r_skeletal_debugtranslatey = {0, "r_skeletal_debugtranslatey", "1", "development cvar for testing skeletal model code"};
30 cvar_t r_skeletal_debugtranslatez = {0, "r_skeletal_debugtranslatez", "1", "development cvar for testing skeletal model code"};
31
32 void Mod_AliasInit (void)
33 {
34         Cvar_RegisterVariable(&r_skeletal_debugbone);
35         Cvar_RegisterVariable(&r_skeletal_debugbonecomponent);
36         Cvar_RegisterVariable(&r_skeletal_debugbonevalue);
37         Cvar_RegisterVariable(&r_skeletal_debugtranslatex);
38         Cvar_RegisterVariable(&r_skeletal_debugtranslatey);
39         Cvar_RegisterVariable(&r_skeletal_debugtranslatez);
40 }
41
42 void Mod_Alias_GetMesh_Vertices(const model_t *model, const frameblend_t *frameblend, float *vertex3f, float *normal3f, float *svector3f, float *tvector3f)
43 {
44         if (model->surfmesh.data_vertexweightindex4i)
45         {
46                 int i, k, blends;
47                 const float *v = model->surfmesh.data_vertex3f;
48                 const float *n = model->surfmesh.data_normal3f;
49                 const float *sv = model->surfmesh.data_svector3f;
50                 const float *tv = model->surfmesh.data_tvector3f;
51                 const int *wi = model->surfmesh.data_vertexweightindex4i;
52                 const float *wf = model->surfmesh.data_vertexweightinfluence4f;
53                 float *matrix, m[12], bonepose[256][12], boneposerelative[256][12];
54                 // vertex weighted skeletal
55                 memset(vertex3f, 0, model->surfmesh.num_vertices * sizeof(float[3]));
56                 if (normal3f)
57                         memset(normal3f, 0, model->surfmesh.num_vertices * sizeof(float[3]));
58                 if (svector3f)
59                 {
60                         memset(svector3f, 0, model->surfmesh.num_vertices * sizeof(float[3]));
61                         memset(tvector3f, 0, model->surfmesh.num_vertices * sizeof(float[3]));
62                 }
63                 // interpolate matrices and concatenate them to their parents
64                 for (i = 0;i < model->num_bones;i++)
65                 {
66                         for (k = 0;k < 12;k++)
67                                 m[k] = 0;
68                         for (blends = 0;blends < 4 && frameblend[blends].lerp > 0;blends++)
69                         {
70                                 matrix = model->data_poses + (frameblend[blends].frame * model->num_bones + i) * 12;
71                                 for (k = 0;k < 12;k++)
72                                         m[k] += matrix[k] * frameblend[blends].lerp;
73                         }
74                         if (i == r_skeletal_debugbone.integer)
75                                 m[r_skeletal_debugbonecomponent.integer % 12] += r_skeletal_debugbonevalue.value;
76                         m[3] *= r_skeletal_debugtranslatex.value;
77                         m[7] *= r_skeletal_debugtranslatey.value;
78                         m[11] *= r_skeletal_debugtranslatez.value;
79                         if (model->data_bones[i].parent >= 0)
80                                 R_ConcatTransforms(bonepose[model->data_bones[i].parent], m, bonepose[i]);
81                         else
82                                 for (k = 0;k < 12;k++)
83                                         bonepose[i][k] = m[k];
84                         // create a relative deformation matrix to describe displacement
85                         // from the base mesh, which is used by the actual weighting
86                         R_ConcatTransforms(bonepose[i], model->data_baseboneposeinverse + i * 12, boneposerelative[i]);
87                 }
88                 // blend the vertex bone weights
89                 if (svector3f)
90                 {
91                         for (i = 0;i < model->surfmesh.num_vertices;i++, v += 3, n += 3, sv += 3, tv += 3, wi += 4, wf += 4, vertex3f += 3, normal3f += 3, svector3f += 3, tvector3f += 3)
92                         {
93                                 for (k = 0;k < 4 && wf[k];k++)
94                                 {
95                                         const float *m = boneposerelative[wi[k]];
96                                         float f = wf[k];
97                                         vertex3f[0] += f * (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
98                                         vertex3f[1] += f * (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
99                                         vertex3f[2] += f * (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
100                                         normal3f[0] += f * (n[0] * m[0] + n[1] * m[1] + n[2] * m[ 2]);
101                                         normal3f[1] += f * (n[0] * m[4] + n[1] * m[5] + n[2] * m[ 6]);
102                                         normal3f[2] += f * (n[0] * m[8] + n[1] * m[9] + n[2] * m[10]);
103                                         svector3f[0] += f * (sv[0] * m[0] + sv[1] * m[1] + sv[2] * m[ 2]);
104                                         svector3f[1] += f * (sv[0] * m[4] + sv[1] * m[5] + sv[2] * m[ 6]);
105                                         svector3f[2] += f * (sv[0] * m[8] + sv[1] * m[9] + sv[2] * m[10]);
106                                         tvector3f[0] += f * (tv[0] * m[0] + tv[1] * m[1] + tv[2] * m[ 2]);
107                                         tvector3f[1] += f * (tv[0] * m[4] + tv[1] * m[5] + tv[2] * m[ 6]);
108                                         tvector3f[2] += f * (tv[0] * m[8] + tv[1] * m[9] + tv[2] * m[10]);
109                                 }
110                         }
111                 }
112                 else if (normal3f)
113                 {
114                         for (i = 0;i < model->surfmesh.num_vertices;i++, v += 3, n += 3, wi += 4, wf += 4, vertex3f += 3, normal3f += 3)
115                         {
116                                 for (k = 0;k < 4 && wf[k];k++)
117                                 {
118                                         const float *m = boneposerelative[wi[k]];
119                                         float f = wf[k];
120                                         vertex3f[0] += f * (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
121                                         vertex3f[1] += f * (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
122                                         vertex3f[2] += f * (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
123                                         normal3f[0] += f * (n[0] * m[0] + n[1] * m[1] + n[2] * m[ 2]);
124                                         normal3f[1] += f * (n[0] * m[4] + n[1] * m[5] + n[2] * m[ 6]);
125                                         normal3f[2] += f * (n[0] * m[8] + n[1] * m[9] + n[2] * m[10]);
126                                 }
127                         }
128                 }
129                 else
130                 {
131                         for (i = 0;i < model->surfmesh.num_vertices;i++, v += 3, wi += 4, wf += 4, vertex3f += 3)
132                         {
133                                 for (k = 0;k < 4 && wf[k];k++)
134                                 {
135                                         const float *m = boneposerelative[wi[k]];
136                                         float f = wf[k];
137                                         vertex3f[0] += f * (v[0] * m[0] + v[1] * m[1] + v[2] * m[ 2] + m[ 3]);
138                                         vertex3f[1] += f * (v[0] * m[4] + v[1] * m[5] + v[2] * m[ 6] + m[ 7]);
139                                         vertex3f[2] += f * (v[0] * m[8] + v[1] * m[9] + v[2] * m[10] + m[11]);
140                                 }
141                         }
142                 }
143         }
144         else if (model->surfmesh.data_morphvertex3f)
145         {
146                 // vertex morph
147                 int numverts = model->surfmesh.num_vertices;
148                 const float *vertsbase = model->surfmesh.data_morphvertex3f;
149                 const float *verts1 = vertsbase + numverts * 3 * frameblend[0].frame;
150                 if (frameblend[1].lerp)
151                 {
152                         int i;
153                         float lerp1 = frameblend[0].lerp;
154                         const float *verts2 = vertsbase + numverts * 3 * frameblend[1].frame;
155                         float lerp2 = frameblend[1].lerp;
156                         if (frameblend[2].lerp)
157                         {
158                                 const float *verts3 = vertsbase + numverts * 3 * frameblend[2].frame;
159                                 float lerp3 = frameblend[2].lerp;
160                                 if (frameblend[3].lerp)
161                                 {
162                                         const float *verts4 = vertsbase + numverts * 3 * frameblend[3].frame;
163                                         float lerp4 = frameblend[3].lerp;
164                                         for (i = 0;i < numverts * 3;i++)
165                                                 vertex3f[i] = lerp1 * verts1[i] + lerp2 * verts2[i] + lerp3 * verts3[i] + lerp4 * verts4[i];
166                                 }
167                                 else
168                                         for (i = 0;i < numverts * 3;i++)
169                                                 vertex3f[i] = lerp1 * verts1[i] + lerp2 * verts2[i] + lerp3 * verts3[i];
170                         }
171                         else
172                                 for (i = 0;i < numverts * 3;i++)
173                                         vertex3f[i] = lerp1 * verts1[i] + lerp2 * verts2[i];
174                 }
175                 else
176                         memcpy(vertex3f, verts1, numverts * 3 * sizeof(float));
177                 if (normal3f)
178                 {
179                         Mod_BuildNormals(0, model->surfmesh.num_vertices, model->surfmesh.num_triangles, vertex3f, model->surfmesh.data_element3i, normal3f, r_smoothnormals_areaweighting.integer);
180                         if (svector3f)
181                                 Mod_BuildTextureVectorsFromNormals(0, model->surfmesh.num_vertices, model->surfmesh.num_triangles, vertex3f, model->surfmesh.data_texcoordtexture2f, normal3f, model->surfmesh.data_element3i, svector3f, tvector3f, r_smoothnormals_areaweighting.integer);
182                 }
183         }
184         else
185                 Host_Error("model %s has no skeletal or vertex morph animation data", model->name);
186 }
187
188 int Mod_Alias_GetTagMatrix(const model_t *model, int poseframe, int tagindex, matrix4x4_t *outmatrix)
189 {
190         const float *boneframe;
191         float tempbonematrix[12], bonematrix[12];
192         *outmatrix = identitymatrix;
193         if (model->num_bones)
194         {
195                 if (tagindex < 0 || tagindex >= model->num_bones)
196                         return 4;
197                 if (poseframe >= model->num_poses)
198                         return 6;
199                 boneframe = model->data_poses + poseframe * model->num_bones * 12;
200                 memcpy(bonematrix, boneframe + tagindex * 12, sizeof(float[12]));
201                 while (model->data_bones[tagindex].parent >= 0)
202                 {
203                         memcpy(tempbonematrix, bonematrix, sizeof(float[12]));
204                         R_ConcatTransforms(boneframe + model->data_bones[tagindex].parent * 12, tempbonematrix, bonematrix);
205                         tagindex = model->data_bones[tagindex].parent;
206                 }
207                 outmatrix->m[0][0] = bonematrix[0];
208                 outmatrix->m[0][1] = bonematrix[1];
209                 outmatrix->m[0][2] = bonematrix[2];
210                 outmatrix->m[0][3] = bonematrix[3];
211                 outmatrix->m[1][0] = bonematrix[4];
212                 outmatrix->m[1][1] = bonematrix[5];
213                 outmatrix->m[1][2] = bonematrix[6];
214                 outmatrix->m[1][3] = bonematrix[7];
215                 outmatrix->m[2][0] = bonematrix[8];
216                 outmatrix->m[2][1] = bonematrix[9];
217                 outmatrix->m[2][2] = bonematrix[10];
218                 outmatrix->m[2][3] = bonematrix[11];
219                 outmatrix->m[3][0] = 0;
220                 outmatrix->m[3][1] = 0;
221                 outmatrix->m[3][2] = 0;
222                 outmatrix->m[3][3] = 1;
223         }
224         else if (model->num_tags)
225         {
226                 if (tagindex < 0 || tagindex >= model->num_tags)
227                         return 4;
228                 if (poseframe >= model->num_tagframes)
229                         return 6;
230                 *outmatrix = model->data_tags[poseframe * model->num_tags + tagindex].matrix;
231         }
232         return 0;
233 }
234
235 int Mod_Alias_GetTagIndexForName(const model_t *model, unsigned int skin, const char *tagname)
236 {
237         int i;
238         if (model->data_overridetagnamesforskin && skin < (unsigned int)model->numskins && model->data_overridetagnamesforskin[(unsigned int)skin].num_overridetagnames)
239                 for (i = 0;i < model->data_overridetagnamesforskin[skin].num_overridetagnames;i++)
240                         if (!strcasecmp(tagname, model->data_overridetagnamesforskin[skin].data_overridetagnames[i].name))
241                                 return i + 1;
242         if (model->num_bones)
243                 for (i = 0;i < model->num_bones;i++)
244                         if (!strcasecmp(tagname, model->data_bones[i].name))
245                                 return i + 1;
246         if (model->num_tags)
247                 for (i = 0;i < model->num_tags;i++)
248                         if (!strcasecmp(tagname, model->data_tags[i].name))
249                                 return i + 1;
250         return 0;
251 }
252
253 static void Mod_BuildBaseBonePoses(void)
254 {
255         int i, k;
256         double scale;
257         float *in12f = loadmodel->data_poses;
258         float *out12f = loadmodel->data_basebonepose;
259         float *outinv12f = loadmodel->data_baseboneposeinverse;
260         for (i = 0;i < loadmodel->num_bones;i++, in12f += 12, out12f += 12, outinv12f += 12)
261         {
262                 if (loadmodel->data_bones[i].parent >= 0)
263                         R_ConcatTransforms(loadmodel->data_basebonepose + 12 * loadmodel->data_bones[i].parent, in12f, out12f);
264                 else
265                         for (k = 0;k < 12;k++)
266                                 out12f[k] = in12f[k];
267
268                 // invert The Matrix
269
270                 // we only support uniform scaling, so assume the first row is enough
271                 // (note the lack of sqrt here, because we're trying to undo the scaling,
272                 // this means multiplying by the inverse scale twice - squaring it, which
273                 // makes the sqrt a waste of time)
274                 scale = 1.0 / (out12f[ 0] * out12f[ 0] + out12f[ 1] * out12f[ 1] + out12f[ 2] * out12f[ 2]);
275
276                 // invert the rotation by transposing and multiplying by the squared
277                 // recipricol of the input matrix scale as described above
278                 outinv12f[ 0] = (float)(out12f[ 0] * scale);
279                 outinv12f[ 1] = (float)(out12f[ 4] * scale);
280                 outinv12f[ 2] = (float)(out12f[ 8] * scale);
281                 outinv12f[ 4] = (float)(out12f[ 1] * scale);
282                 outinv12f[ 5] = (float)(out12f[ 5] * scale);
283                 outinv12f[ 6] = (float)(out12f[ 9] * scale);
284                 outinv12f[ 8] = (float)(out12f[ 2] * scale);
285                 outinv12f[ 9] = (float)(out12f[ 6] * scale);
286                 outinv12f[10] = (float)(out12f[10] * scale);
287
288                 // invert the translate
289                 outinv12f[ 3] = -(out12f[ 3] * outinv12f[ 0] + out12f[ 7] * outinv12f[ 1] + out12f[11] * outinv12f[ 2]);
290                 outinv12f[ 7] = -(out12f[ 3] * outinv12f[ 4] + out12f[ 7] * outinv12f[ 5] + out12f[11] * outinv12f[ 6]);
291                 outinv12f[11] = -(out12f[ 3] * outinv12f[ 8] + out12f[ 7] * outinv12f[ 9] + out12f[11] * outinv12f[10]);
292         }
293 }
294
295 static void Mod_Alias_CalculateBoundingBox(void)
296 {
297         int i, j;
298         int vnum;
299         qboolean firstvertex = true;
300         float dist, yawradius, radius;
301         float *v;
302         float *vertex3f;
303         frameblend_t frameblend[4];
304         memset(frameblend, 0, sizeof(frameblend));
305         frameblend[0].lerp = 1;
306         vertex3f = Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(float[3]));
307         VectorClear(loadmodel->normalmins);
308         VectorClear(loadmodel->normalmaxs);
309         yawradius = 0;
310         radius = 0;
311         for (i = 0;i < loadmodel->numframes;i++)
312         {
313                 for (j = 0, frameblend[0].frame = loadmodel->animscenes[i].firstframe;j < loadmodel->animscenes[i].framecount;j++, frameblend[0].frame++)
314                 {
315                         Mod_Alias_GetMesh_Vertices(loadmodel, frameblend, vertex3f, NULL, NULL, NULL);
316                         for (vnum = 0, v = vertex3f;vnum < loadmodel->surfmesh.num_vertices;vnum++, v += 3)
317                         {
318                                 if (firstvertex)
319                                 {
320                                         firstvertex = false;
321                                         VectorCopy(v, loadmodel->normalmins);
322                                         VectorCopy(v, loadmodel->normalmaxs);
323                                 }
324                                 else
325                                 {
326                                         if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
327                                         if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
328                                         if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
329                                         if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
330                                         if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
331                                         if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
332                                 }
333                                 dist = v[0] * v[0] + v[1] * v[1];
334                                 if (yawradius < dist)
335                                         yawradius = dist;
336                                 dist += v[2] * v[2];
337                                 if (radius < dist)
338                                         radius = dist;
339                         }
340                 }
341         }
342         Mem_Free(vertex3f);
343         radius = sqrt(radius);
344         yawradius = sqrt(yawradius);
345         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
346         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = yawradius;
347         loadmodel->yawmins[2] = loadmodel->normalmins[2];
348         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
349         loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
350         loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
351         loadmodel->radius = radius;
352         loadmodel->radius2 = radius * radius;
353 }
354
355 static void Mod_Alias_Mesh_CompileFrameZero(void)
356 {
357         frameblend_t frameblend[4] = {{0, 1}, {0, 0}, {0, 0}, {0, 0}};
358         loadmodel->surfmesh.data_vertex3f = (float *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_vertices * sizeof(float[3][4]));
359         loadmodel->surfmesh.data_svector3f = loadmodel->surfmesh.data_vertex3f + loadmodel->surfmesh.num_vertices * 3;
360         loadmodel->surfmesh.data_tvector3f = loadmodel->surfmesh.data_vertex3f + loadmodel->surfmesh.num_vertices * 6;
361         loadmodel->surfmesh.data_normal3f = loadmodel->surfmesh.data_vertex3f + loadmodel->surfmesh.num_vertices * 9;
362         Mod_Alias_GetMesh_Vertices(loadmodel, frameblend, loadmodel->surfmesh.data_vertex3f, NULL, NULL, NULL);
363         Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, true);
364         Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
365 }
366
367 static void Mod_MDLMD2MD3_TraceBox(model_t *model, int frame, trace_t *trace, const vec3_t start, const vec3_t boxmins, const vec3_t boxmaxs, const vec3_t end, int hitsupercontentsmask)
368 {
369         int i;
370         float segmentmins[3], segmentmaxs[3];
371         frameblend_t frameblend[4];
372         msurface_t *surface;
373         static int maxvertices = 0;
374         static float *vertex3f = NULL;
375         memset(trace, 0, sizeof(*trace));
376         trace->fraction = 1;
377         trace->realfraction = 1;
378         trace->hitsupercontentsmask = hitsupercontentsmask;
379         memset(frameblend, 0, sizeof(frameblend));
380         frameblend[0].frame = frame;
381         frameblend[0].lerp = 1;
382         if (maxvertices < model->surfmesh.num_vertices)
383         {
384                 if (vertex3f)
385                         Z_Free(vertex3f);
386                 maxvertices = (model->surfmesh.num_vertices + 255) & ~255;
387                 vertex3f = (float *)Z_Malloc(maxvertices * sizeof(float[3]));
388         }
389         if (VectorLength2(boxmins) + VectorLength2(boxmaxs) == 0)
390         {
391                 // line trace
392                 segmentmins[0] = min(start[0], end[0]) - 1;
393                 segmentmins[1] = min(start[1], end[1]) - 1;
394                 segmentmins[2] = min(start[2], end[2]) - 1;
395                 segmentmaxs[0] = max(start[0], end[0]) + 1;
396                 segmentmaxs[1] = max(start[1], end[1]) + 1;
397                 segmentmaxs[2] = max(start[2], end[2]) + 1;
398                 for (i = 0, surface = model->data_surfaces;i < model->num_surfaces;i++, surface++)
399                 {
400                         Mod_Alias_GetMesh_Vertices(model, frameblend, vertex3f, NULL, NULL, NULL);
401                         Collision_TraceLineTriangleMeshFloat(trace, start, end, model->surfmesh.num_triangles, model->surfmesh.data_element3i, vertex3f, SUPERCONTENTS_SOLID, 0, surface->texture, segmentmins, segmentmaxs);
402                 }
403         }
404         else
405         {
406                 // box trace, performed as brush trace
407                 colbrushf_t *thisbrush_start, *thisbrush_end;
408                 vec3_t boxstartmins, boxstartmaxs, boxendmins, boxendmaxs;
409                 segmentmins[0] = min(start[0], end[0]) + boxmins[0] - 1;
410                 segmentmins[1] = min(start[1], end[1]) + boxmins[1] - 1;
411                 segmentmins[2] = min(start[2], end[2]) + boxmins[2] - 1;
412                 segmentmaxs[0] = max(start[0], end[0]) + boxmaxs[0] + 1;
413                 segmentmaxs[1] = max(start[1], end[1]) + boxmaxs[1] + 1;
414                 segmentmaxs[2] = max(start[2], end[2]) + boxmaxs[2] + 1;
415                 VectorAdd(start, boxmins, boxstartmins);
416                 VectorAdd(start, boxmaxs, boxstartmaxs);
417                 VectorAdd(end, boxmins, boxendmins);
418                 VectorAdd(end, boxmaxs, boxendmaxs);
419                 thisbrush_start = Collision_BrushForBox(&identitymatrix, boxstartmins, boxstartmaxs, 0, 0, NULL);
420                 thisbrush_end = Collision_BrushForBox(&identitymatrix, boxendmins, boxendmaxs, 0, 0, NULL);
421                 for (i = 0, surface = model->data_surfaces;i < model->num_surfaces;i++, surface++)
422                 {
423                         if (maxvertices < model->surfmesh.num_vertices)
424                         {
425                                 if (vertex3f)
426                                         Z_Free(vertex3f);
427                                 maxvertices = (model->surfmesh.num_vertices + 255) & ~255;
428                                 vertex3f = (float *)Z_Malloc(maxvertices * sizeof(float[3]));
429                         }
430                         Mod_Alias_GetMesh_Vertices(model, frameblend, vertex3f, NULL, NULL, NULL);
431                         Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, model->surfmesh.num_triangles, model->surfmesh.data_element3i, vertex3f, SUPERCONTENTS_SOLID, 0, surface->texture, segmentmins, segmentmaxs);
432                 }
433         }
434 }
435
436 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
437 {
438         int i, j;
439         vec3_t temp;
440         for (i = 0;i < inverts;i++)
441         {
442                 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
443                         continue;
444                 temp[0] = v[i].v[0] * scale[0] + translate[0];
445                 temp[1] = v[i].v[1] * scale[1] + translate[1];
446                 temp[2] = v[i].v[2] * scale[2] + translate[2];
447                 j = vertremap[i]; // not onseam
448                 if (j >= 0)
449                         VectorCopy(temp, out3f + j * 3);
450                 j = vertremap[i+inverts]; // onseam
451                 if (j >= 0)
452                         VectorCopy(temp, out3f + j * 3);
453         }
454 }
455
456 static void Mod_MDL_LoadFrames (unsigned char* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
457 {
458         int i, f, pose, groupframes;
459         float interval;
460         daliasframetype_t *pframetype;
461         daliasframe_t *pinframe;
462         daliasgroup_t *group;
463         daliasinterval_t *intervals;
464         animscene_t *scene;
465         pose = 0;
466         scene = loadmodel->animscenes;
467         for (f = 0;f < loadmodel->numframes;f++)
468         {
469                 pframetype = (daliasframetype_t *)datapointer;
470                 datapointer += sizeof(daliasframetype_t);
471                 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
472                 {
473                         // a single frame is still treated as a group
474                         interval = 0.1f;
475                         groupframes = 1;
476                 }
477                 else
478                 {
479                         // read group header
480                         group = (daliasgroup_t *)datapointer;
481                         datapointer += sizeof(daliasgroup_t);
482                         groupframes = LittleLong (group->numframes);
483
484                         // intervals (time per frame)
485                         intervals = (daliasinterval_t *)datapointer;
486                         datapointer += sizeof(daliasinterval_t) * groupframes;
487
488                         interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
489                         if (interval < 0.01f)
490                         {
491                                 Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
492                                 interval = 0.1f;
493                         }
494                 }
495
496                 // get scene name from first frame
497                 pinframe = (daliasframe_t *)datapointer;
498
499                 strcpy(scene->name, pinframe->name);
500                 scene->firstframe = pose;
501                 scene->framecount = groupframes;
502                 scene->framerate = 1.0f / interval;
503                 scene->loop = true;
504                 scene++;
505
506                 // read frames
507                 for (i = 0;i < groupframes;i++)
508                 {
509                         pinframe = (daliasframe_t *)datapointer;
510                         datapointer += sizeof(daliasframe_t);
511                         Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->surfmesh.data_morphvertex3f + pose * loadmodel->surfmesh.num_vertices * 3, vertremap);
512                         datapointer += sizeof(trivertx_t) * inverts;
513                         pose++;
514                 }
515         }
516 }
517
518 static skinframe_t missingskinframe;
519 static void Mod_BuildAliasSkinFromSkinFrame(texture_t *skin, skinframe_t *skinframe)
520 {
521         // hack
522         if (skinframe == NULL)
523         {
524                 skinframe = &missingskinframe;
525                 memset(skinframe, 0, sizeof(*skinframe));
526                 skinframe->base = r_texture_notexture;
527         }
528
529         skin->skin = *skinframe;
530         skin->currentframe = skin;
531         skin->basematerialflags = MATERIALFLAG_WALL;
532         if (skin->skin.fog)
533                 skin->basematerialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_TRANSPARENT;
534         skin->currentmaterialflags = skin->basematerialflags;
535 }
536
537 static void Mod_BuildAliasSkinsFromSkinFiles(texture_t *skin, skinfile_t *skinfile, char *meshname, char *shadername)
538 {
539         int i;
540         skinfileitem_t *skinfileitem;
541         skinframe_t tempskinframe;
542         if (skinfile)
543         {
544                 // the skin += loadmodel->num_surfaces part of this is because data_textures on alias models is arranged as [numskins][numsurfaces]
545                 for (i = 0;skinfile;skinfile = skinfile->next, i++, skin += loadmodel->num_surfaces)
546                 {
547                         memset(skin, 0, sizeof(*skin));
548                         for (skinfileitem = skinfile->items;skinfileitem;skinfileitem = skinfileitem->next)
549                         {
550                                 // leave the skin unitialized (nodraw) if the replacement is "common/nodraw" or "textures/common/nodraw"
551                                 if (!strcmp(skinfileitem->name, meshname) && strcmp(skinfileitem->replacement, "common/nodraw") && strcmp(skinfileitem->replacement, "textures/common/nodraw"))
552                                 {
553                                         memset(&tempskinframe, 0, sizeof(tempskinframe));
554                                         if (Mod_LoadSkinFrame(&tempskinframe, skinfileitem->replacement, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, true, true))
555                                                 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
556                                         else
557                                         {
558                                                 if (cls.state != ca_dedicated)
559                                                         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);
560                                                 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, true, true))
561                                                         Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
562                                                 else
563                                                 {
564                                                         if (cls.state != ca_dedicated)
565                                                                 Con_Printf("failed to load skin \"%s\"\n", shadername);
566                                                         Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
567                                                 }
568                                         }
569                                 }
570                         }
571                 }
572         }
573         else
574         {
575                 memset(&tempskinframe, 0, sizeof(tempskinframe));
576                 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, true, true))
577                         Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
578                 else
579                 {
580                         if (cls.state != ca_dedicated)
581                                 Con_Printf("Can't find texture \"%s\" for mesh \"%s\", using grey checkerboard\n", shadername, meshname);
582                         Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
583                 }
584         }
585 }
586
587 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)", loadmodel->name, VALUE, MIN, MAX);
588 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%f exceeds %f - %f)", loadmodel->name, VALUE, MIN, MAX);
589 void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend)
590 {
591         int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
592         float scales, scalet, scale[3], translate[3], interval;
593         msurface_t *surface;
594         unsigned char *data;
595         mdl_t *pinmodel;
596         stvert_t *pinstverts;
597         dtriangle_t *pintriangles;
598         daliasskintype_t *pinskintype;
599         daliasskingroup_t *pinskingroup;
600         daliasskininterval_t *pinskinintervals;
601         daliasframetype_t *pinframetype;
602         daliasgroup_t *pinframegroup;
603         unsigned char *datapointer, *startframes, *startskins;
604         char name[MAX_QPATH];
605         skinframe_t tempskinframe;
606         animscene_t *tempskinscenes;
607         texture_t *tempaliasskins;
608         float *vertst;
609         int *vertonseam, *vertremap;
610         skinfile_t *skinfiles;
611
612         datapointer = (unsigned char *)buffer;
613         pinmodel = (mdl_t *)datapointer;
614         datapointer += sizeof(mdl_t);
615
616         version = LittleLong (pinmodel->version);
617         if (version != ALIAS_VERSION)
618                 Host_Error ("%s has wrong version number (%i should be %i)",
619                                  loadmodel->name, version, ALIAS_VERSION);
620
621         loadmodel->type = mod_alias;
622         loadmodel->DrawSky = NULL;
623         loadmodel->Draw = R_Q1BSP_Draw;
624         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
625         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
626         loadmodel->DrawLight = R_Q1BSP_DrawLight;
627         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
628
629         loadmodel->num_surfaces = 1;
630         loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
631         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int));
632         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
633         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
634         loadmodel->surfacelist[0] = 0;
635
636         loadmodel->numskins = LittleLong(pinmodel->numskins);
637         BOUNDI(loadmodel->numskins,0,65536);
638         skinwidth = LittleLong (pinmodel->skinwidth);
639         BOUNDI(skinwidth,0,65536);
640         skinheight = LittleLong (pinmodel->skinheight);
641         BOUNDI(skinheight,0,65536);
642         numverts = LittleLong(pinmodel->numverts);
643         BOUNDI(numverts,0,65536);
644         loadmodel->surfmesh.num_triangles = LittleLong(pinmodel->numtris);
645         BOUNDI(loadmodel->surfmesh.num_triangles,0,65536);
646         loadmodel->numframes = LittleLong(pinmodel->numframes);
647         BOUNDI(loadmodel->numframes,0,65536);
648         loadmodel->synctype = (synctype_t)LittleLong (pinmodel->synctype);
649         BOUNDI(loadmodel->synctype,0,2);
650         loadmodel->flags = LittleLong (pinmodel->flags);
651
652         for (i = 0;i < 3;i++)
653         {
654                 scale[i] = LittleFloat (pinmodel->scale[i]);
655                 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
656         }
657
658         startskins = datapointer;
659         totalskins = 0;
660         for (i = 0;i < loadmodel->numskins;i++)
661         {
662                 pinskintype = (daliasskintype_t *)datapointer;
663                 datapointer += sizeof(daliasskintype_t);
664                 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
665                         groupskins = 1;
666                 else
667                 {
668                         pinskingroup = (daliasskingroup_t *)datapointer;
669                         datapointer += sizeof(daliasskingroup_t);
670                         groupskins = LittleLong(pinskingroup->numskins);
671                         datapointer += sizeof(daliasskininterval_t) * groupskins;
672                 }
673
674                 for (j = 0;j < groupskins;j++)
675                 {
676                         datapointer += skinwidth * skinheight;
677                         totalskins++;
678                 }
679         }
680
681         pinstverts = (stvert_t *)datapointer;
682         datapointer += sizeof(stvert_t) * numverts;
683
684         pintriangles = (dtriangle_t *)datapointer;
685         datapointer += sizeof(dtriangle_t) * loadmodel->surfmesh.num_triangles;
686
687         startframes = datapointer;
688         loadmodel->surfmesh.num_morphframes = 0;
689         for (i = 0;i < loadmodel->numframes;i++)
690         {
691                 pinframetype = (daliasframetype_t *)datapointer;
692                 datapointer += sizeof(daliasframetype_t);
693                 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
694                         groupframes = 1;
695                 else
696                 {
697                         pinframegroup = (daliasgroup_t *)datapointer;
698                         datapointer += sizeof(daliasgroup_t);
699                         groupframes = LittleLong(pinframegroup->numframes);
700                         datapointer += sizeof(daliasinterval_t) * groupframes;
701                 }
702
703                 for (j = 0;j < groupframes;j++)
704                 {
705                         datapointer += sizeof(daliasframe_t);
706                         datapointer += sizeof(trivertx_t) * numverts;
707                         loadmodel->surfmesh.num_morphframes++;
708                 }
709         }
710
711         // store texture coordinates into temporary array, they will be stored
712         // after usage is determined (triangle data)
713         vertst = (float *)Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
714         vertremap = (int *)Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
715         vertonseam = vertremap + numverts * 2;
716
717         scales = 1.0 / skinwidth;
718         scalet = 1.0 / skinheight;
719         for (i = 0;i < numverts;i++)
720         {
721                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
722                 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
723                 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
724                 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
725                 vertst[(i+numverts)*2+1] = vertst[i*2+1];
726         }
727
728 // load triangle data
729         loadmodel->surfmesh.data_element3i = (int *)Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->surfmesh.num_triangles);
730
731         // read the triangle elements
732         for (i = 0;i < loadmodel->surfmesh.num_triangles;i++)
733                 for (j = 0;j < 3;j++)
734                         loadmodel->surfmesh.data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
735         // validate (note numverts is used because this is the original data)
736         Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, numverts, __FILE__, __LINE__);
737         // now butcher the elements according to vertonseam and tri->facesfront
738         // and then compact the vertex set to remove duplicates
739         for (i = 0;i < loadmodel->surfmesh.num_triangles;i++)
740                 if (!LittleLong(pintriangles[i].facesfront)) // backface
741                         for (j = 0;j < 3;j++)
742                                 if (vertonseam[loadmodel->surfmesh.data_element3i[i*3+j]])
743                                         loadmodel->surfmesh.data_element3i[i*3+j] += numverts;
744         // count the usage
745         // (this uses vertremap to count usage to save some memory)
746         for (i = 0;i < numverts*2;i++)
747                 vertremap[i] = 0;
748         for (i = 0;i < loadmodel->surfmesh.num_triangles*3;i++)
749                 vertremap[loadmodel->surfmesh.data_element3i[i]]++;
750         // build remapping table and compact array
751         loadmodel->surfmesh.num_vertices = 0;
752         for (i = 0;i < numverts*2;i++)
753         {
754                 if (vertremap[i])
755                 {
756                         vertremap[i] = loadmodel->surfmesh.num_vertices;
757                         vertst[loadmodel->surfmesh.num_vertices*2+0] = vertst[i*2+0];
758                         vertst[loadmodel->surfmesh.num_vertices*2+1] = vertst[i*2+1];
759                         loadmodel->surfmesh.num_vertices++;
760                 }
761                 else
762                         vertremap[i] = -1; // not used at all
763         }
764         // remap the elements to the new vertex set
765         for (i = 0;i < loadmodel->surfmesh.num_triangles * 3;i++)
766                 loadmodel->surfmesh.data_element3i[i] = vertremap[loadmodel->surfmesh.data_element3i[i]];
767         // store the texture coordinates
768         loadmodel->surfmesh.data_texcoordtexture2f = (float *)Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->surfmesh.num_vertices);
769         for (i = 0;i < loadmodel->surfmesh.num_vertices;i++)
770         {
771                 loadmodel->surfmesh.data_texcoordtexture2f[i*2+0] = vertst[i*2+0];
772                 loadmodel->surfmesh.data_texcoordtexture2f[i*2+1] = vertst[i*2+1];
773         }
774
775 // load the frames
776         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
777         loadmodel->surfmesh.data_morphvertex3f = (float *)Mem_Alloc(loadmodel->mempool, sizeof(float[3]) * loadmodel->surfmesh.num_morphframes * loadmodel->surfmesh.num_vertices);
778         loadmodel->surfmesh.data_neighbor3i = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
779         Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
780         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
781         Mod_Alias_CalculateBoundingBox();
782         Mod_Alias_Mesh_CompileFrameZero();
783
784         Mem_Free(vertst);
785         Mem_Free(vertremap);
786
787         // load the skins
788         skinfiles = Mod_LoadSkinFiles();
789         loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
790         loadmodel->num_textures = loadmodel->num_surfaces;
791         loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * totalskins * sizeof(texture_t));
792         if (skinfiles)
793         {
794                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures, skinfiles, "default", "");
795                 Mod_FreeSkinFiles(skinfiles);
796                 for (i = 0;i < loadmodel->numskins;i++)
797                 {
798                         loadmodel->skinscenes[i].firstframe = i;
799                         loadmodel->skinscenes[i].framecount = 1;
800                         loadmodel->skinscenes[i].loop = true;
801                         loadmodel->skinscenes[i].framerate = 10;
802                 }
803         }
804         else
805         {
806                 totalskins = 0;
807                 datapointer = startskins;
808                 for (i = 0;i < loadmodel->numskins;i++)
809                 {
810                         pinskintype = (daliasskintype_t *)datapointer;
811                         datapointer += sizeof(daliasskintype_t);
812
813                         if (pinskintype->type == ALIAS_SKIN_SINGLE)
814                         {
815                                 groupskins = 1;
816                                 interval = 0.1f;
817                         }
818                         else
819                         {
820                                 pinskingroup = (daliasskingroup_t *)datapointer;
821                                 datapointer += sizeof(daliasskingroup_t);
822
823                                 groupskins = LittleLong (pinskingroup->numskins);
824
825                                 pinskinintervals = (daliasskininterval_t *)datapointer;
826                                 datapointer += sizeof(daliasskininterval_t) * groupskins;
827
828                                 interval = LittleFloat(pinskinintervals[0].interval);
829                                 if (interval < 0.01f)
830                                 {
831                                         Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
832                                         interval = 0.1f;
833                                 }
834                         }
835
836                         sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
837                         loadmodel->skinscenes[i].firstframe = totalskins;
838                         loadmodel->skinscenes[i].framecount = groupskins;
839                         loadmodel->skinscenes[i].framerate = 1.0f / interval;
840                         loadmodel->skinscenes[i].loop = true;
841
842                         for (j = 0;j < groupskins;j++)
843                         {
844                                 if (groupskins > 1)
845                                         sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
846                                 else
847                                         sprintf (name, "%s_%i", loadmodel->name, i);
848                                 if (!Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP, true, true))
849                                         Mod_LoadSkinFrame_Internal(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP, true, r_fullbrights.integer, (unsigned char *)datapointer, skinwidth, skinheight, 8, NULL, NULL);
850                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + totalskins * loadmodel->num_surfaces, &tempskinframe);
851                                 datapointer += skinwidth * skinheight;
852                                 totalskins++;
853                         }
854                 }
855                 // check for skins that don't exist in the model, but do exist as external images
856                 // (this was added because yummyluv kept pestering me about support for it)
857                 while (Mod_LoadSkinFrame(&tempskinframe, va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PICMIP, true, true))
858                 {
859                         // expand the arrays to make room
860                         tempskinscenes = loadmodel->skinscenes;
861                         loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
862                         memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
863                         Mem_Free(tempskinscenes);
864
865                         tempaliasskins = loadmodel->data_textures;
866                         loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * (totalskins + 1) * sizeof(texture_t));
867                         memcpy(loadmodel->data_textures, tempaliasskins, loadmodel->num_surfaces * totalskins * sizeof(texture_t));
868                         Mem_Free(tempaliasskins);
869
870                         // store the info about the new skin
871                         Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + totalskins * loadmodel->num_surfaces, &tempskinframe);
872                         strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
873                         loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
874                         loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
875                         loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
876                         loadmodel->skinscenes[loadmodel->numskins].loop = true;
877
878                         //increase skin counts
879                         loadmodel->numskins++;
880                         totalskins++;
881                 }
882         }
883
884         surface = loadmodel->data_surfaces;
885         surface->texture = loadmodel->data_textures;
886         surface->num_firsttriangle = 0;
887         surface->num_triangles = loadmodel->surfmesh.num_triangles;
888         surface->num_firstvertex = 0;
889         surface->num_vertices = loadmodel->surfmesh.num_vertices;
890 }
891
892 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
893 {
894         int i;
895         trivertx_t *in;
896         for (i = 0;i < numverts;i++, out3f += 3)
897         {
898                 in = v + vertremap[i];
899                 out3f[0] = in->v[0] * scale[0] + translate[0];
900                 out3f[1] = in->v[1] * scale[1] + translate[1];
901                 out3f[2] = in->v[2] * scale[2] + translate[2];
902         }
903 }
904
905 void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend)
906 {
907         int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
908         float *stverts, s, t, scale[3], translate[3];
909         unsigned char *data;
910         msurface_t *surface;
911         md2_t *pinmodel;
912         unsigned char *base, *datapointer;
913         md2frame_t *pinframe;
914         char *inskin;
915         md2triangle_t *intri;
916         unsigned short *inst;
917         struct md2verthash_s
918         {
919                 struct md2verthash_s *next;
920                 int xyz;
921                 float st[2];
922         }
923         *hash, **md2verthash, *md2verthashdata;
924         skinframe_t tempskinframe;
925         skinfile_t *skinfiles;
926
927         pinmodel = (md2_t *)buffer;
928         base = (unsigned char *)buffer;
929
930         version = LittleLong (pinmodel->version);
931         if (version != MD2ALIAS_VERSION)
932                 Host_Error ("%s has wrong version number (%i should be %i)",
933                         loadmodel->name, version, MD2ALIAS_VERSION);
934
935         loadmodel->type = mod_alias;
936         loadmodel->DrawSky = NULL;
937         loadmodel->Draw = R_Q1BSP_Draw;
938         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
939         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
940         loadmodel->DrawLight = R_Q1BSP_DrawLight;
941         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
942
943         if (LittleLong(pinmodel->num_tris) < 1 || LittleLong(pinmodel->num_tris) > 65536)
944                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
945         if (LittleLong(pinmodel->num_xyz) < 1 || LittleLong(pinmodel->num_xyz) > 65536)
946                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
947         if (LittleLong(pinmodel->num_frames) < 1 || LittleLong(pinmodel->num_frames) > 65536)
948                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
949         if (LittleLong(pinmodel->num_skins) < 0 || LittleLong(pinmodel->num_skins) > 256)
950                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
951
952         end = LittleLong(pinmodel->ofs_end);
953         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins) <= 0 || LittleLong(pinmodel->ofs_skins) >= end))
954                 Host_Error ("%s is not a valid model", loadmodel->name);
955         if (LittleLong(pinmodel->ofs_st) <= 0 || LittleLong(pinmodel->ofs_st) >= end)
956                 Host_Error ("%s is not a valid model", loadmodel->name);
957         if (LittleLong(pinmodel->ofs_tris) <= 0 || LittleLong(pinmodel->ofs_tris) >= end)
958                 Host_Error ("%s is not a valid model", loadmodel->name);
959         if (LittleLong(pinmodel->ofs_frames) <= 0 || LittleLong(pinmodel->ofs_frames) >= end)
960                 Host_Error ("%s is not a valid model", loadmodel->name);
961         if (LittleLong(pinmodel->ofs_glcmds) <= 0 || LittleLong(pinmodel->ofs_glcmds) >= end)
962                 Host_Error ("%s is not a valid model", loadmodel->name);
963
964         loadmodel->num_surfaces = 1;
965         loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
966         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int));
967         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
968         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
969         loadmodel->surfacelist[0] = 0;
970
971         loadmodel->numskins = LittleLong(pinmodel->num_skins);
972         numxyz = LittleLong(pinmodel->num_xyz);
973         numst = LittleLong(pinmodel->num_st);
974         loadmodel->surfmesh.num_triangles = LittleLong(pinmodel->num_tris);
975         loadmodel->numframes = LittleLong(pinmodel->num_frames);
976         loadmodel->surfmesh.num_morphframes = loadmodel->numframes;
977         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
978
979         loadmodel->flags = 0; // there are no MD2 flags
980         loadmodel->synctype = ST_RAND;
981
982         // load the skins
983         inskin = (char *)(base + LittleLong(pinmodel->ofs_skins));
984         skinfiles = Mod_LoadSkinFiles();
985         if (skinfiles)
986         {
987                 loadmodel->num_textures = loadmodel->num_surfaces;
988                 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t));
989                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures, skinfiles, "default", "");
990                 Mod_FreeSkinFiles(skinfiles);
991         }
992         else if (loadmodel->numskins)
993         {
994                 // skins found (most likely not a player model)
995                 loadmodel->num_textures = loadmodel->num_surfaces;
996                 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t));
997                 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
998                 {
999                         if (Mod_LoadSkinFrame(&tempskinframe, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_PRECACHE | TEXF_PICMIP, true, true))
1000                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + i * loadmodel->num_surfaces, &tempskinframe);
1001                         else
1002                         {
1003                                 Con_Printf("%s is missing skin \"%s\"\n", loadmodel->name, inskin);
1004                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + i * loadmodel->num_surfaces, NULL);
1005                         }
1006                 }
1007         }
1008         else
1009         {
1010                 // no skins (most likely a player model)
1011                 loadmodel->numskins = 1;
1012                 loadmodel->num_textures = loadmodel->num_surfaces;
1013                 loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t));
1014                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures, NULL);
1015         }
1016
1017         loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
1018         for (i = 0;i < loadmodel->numskins;i++)
1019         {
1020                 loadmodel->skinscenes[i].firstframe = i;
1021                 loadmodel->skinscenes[i].framecount = 1;
1022                 loadmodel->skinscenes[i].loop = true;
1023                 loadmodel->skinscenes[i].framerate = 10;
1024         }
1025
1026         // load the triangles and stvert data
1027         inst = (unsigned short *)(base + LittleLong(pinmodel->ofs_st));
1028         intri = (md2triangle_t *)(base + LittleLong(pinmodel->ofs_tris));
1029         skinwidth = LittleLong(pinmodel->skinwidth);
1030         skinheight = LittleLong(pinmodel->skinheight);
1031
1032         stverts = (float *)Mem_Alloc(tempmempool, numst * sizeof(float[2]));
1033         s = 1.0f / skinwidth;
1034         t = 1.0f / skinheight;
1035         for (i = 0;i < numst;i++)
1036         {
1037                 j = (unsigned short) LittleShort(inst[i*2+0]);
1038                 k = (unsigned short) LittleShort(inst[i*2+1]);
1039                 if (j >= skinwidth || k >= skinheight)
1040                 {
1041                         Con_Printf("%s has an invalid skin coordinate (%i %i) on vert %i, changing to 0 0\n", loadmodel->name, j, k, i);
1042                         j = 0;
1043                         k = 0;
1044                 }
1045                 stverts[i*2+0] = j * s;
1046                 stverts[i*2+1] = k * t;
1047         }
1048
1049         md2verthash = (struct md2verthash_s **)Mem_Alloc(tempmempool, 256 * sizeof(hash));
1050         md2verthashdata = (struct md2verthash_s *)Mem_Alloc(tempmempool, loadmodel->surfmesh.num_triangles * 3 * sizeof(*hash));
1051         // swap the triangle list
1052         num = 0;
1053         loadmodel->surfmesh.data_element3i = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
1054         for (i = 0;i < loadmodel->surfmesh.num_triangles;i++)
1055         {
1056                 for (j = 0;j < 3;j++)
1057                 {
1058                         xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
1059                         st = (unsigned short) LittleShort (intri[i].index_st[j]);
1060                         if (xyz >= numxyz)
1061                         {
1062                                 Con_Printf("%s has an invalid xyz index (%i) on triangle %i, resetting to 0\n", loadmodel->name, xyz, i);
1063                                 xyz = 0;
1064                         }
1065                         if (st >= numst)
1066                         {
1067                                 Con_Printf("%s has an invalid st index (%i) on triangle %i, resetting to 0\n", loadmodel->name, st, i);
1068                                 st = 0;
1069                         }
1070                         s = stverts[st*2+0];
1071                         t = stverts[st*2+1];
1072                         hashindex = (xyz * 17 + st) & 255;
1073                         for (hash = md2verthash[hashindex];hash;hash = hash->next)
1074                                 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
1075                                         break;
1076                         if (hash == NULL)
1077                         {
1078                                 hash = md2verthashdata + num++;
1079                                 hash->xyz = xyz;
1080                                 hash->st[0] = s;
1081                                 hash->st[1] = t;
1082                                 hash->next = md2verthash[hashindex];
1083                                 md2verthash[hashindex] = hash;
1084                         }
1085                         loadmodel->surfmesh.data_element3i[i*3+j] = (hash - md2verthashdata);
1086                 }
1087         }
1088
1089         Mem_Free(stverts);
1090
1091         numverts = num;
1092         loadmodel->surfmesh.num_vertices = numverts;
1093         vertremap = (int *)Mem_Alloc(loadmodel->mempool, num * sizeof(int));
1094         loadmodel->surfmesh.data_texcoordtexture2f = (float *)Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
1095         for (i = 0;i < num;i++)
1096         {
1097                 hash = md2verthashdata + i;
1098                 vertremap[i] = hash->xyz;
1099                 loadmodel->surfmesh.data_texcoordtexture2f[i*2+0] = hash->st[0];
1100                 loadmodel->surfmesh.data_texcoordtexture2f[i*2+1] = hash->st[1];
1101         }
1102
1103         Mem_Free(md2verthash);
1104         Mem_Free(md2verthashdata);
1105
1106         // load the frames
1107         datapointer = (base + LittleLong(pinmodel->ofs_frames));
1108         loadmodel->surfmesh.data_morphvertex3f = (float *)Mem_Alloc(loadmodel->mempool, numverts * loadmodel->surfmesh.num_morphframes * sizeof(float[3]));
1109         for (i = 0;i < loadmodel->surfmesh.num_morphframes;i++)
1110         {
1111                 pinframe = (md2frame_t *)datapointer;
1112                 datapointer += sizeof(md2frame_t);
1113                 for (j = 0;j < 3;j++)
1114                 {
1115                         scale[j] = LittleFloat(pinframe->scale[j]);
1116                         translate[j] = LittleFloat(pinframe->translate[j]);
1117                 }
1118                 Mod_MD2_ConvertVerts(scale, translate, (trivertx_t *)datapointer, loadmodel->surfmesh.data_morphvertex3f + i * numverts * 3, numverts, vertremap);
1119                 datapointer += numxyz * sizeof(trivertx_t);
1120
1121                 strcpy(loadmodel->animscenes[i].name, pinframe->name);
1122                 loadmodel->animscenes[i].firstframe = i;
1123                 loadmodel->animscenes[i].framecount = 1;
1124                 loadmodel->animscenes[i].framerate = 10;
1125                 loadmodel->animscenes[i].loop = true;
1126         }
1127
1128         Mem_Free(vertremap);
1129
1130         loadmodel->surfmesh.data_neighbor3i = (int *)Mem_Alloc(loadmodel->mempool, loadmodel->surfmesh.num_triangles * sizeof(int[3]));
1131         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
1132         Mod_Alias_CalculateBoundingBox();
1133         Mod_Alias_Mesh_CompileFrameZero();
1134
1135         surface = loadmodel->data_surfaces;
1136         surface->texture = loadmodel->data_textures;
1137         surface->num_firsttriangle = 0;
1138         surface->num_triangles = loadmodel->surfmesh.num_triangles;
1139         surface->num_firstvertex = 0;
1140         surface->num_vertices = loadmodel->surfmesh.num_vertices;
1141 }
1142
1143 void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend)
1144 {
1145         int i, j, k, version, meshvertices, meshtriangles;
1146         unsigned char *data;
1147         msurface_t *surface;
1148         md3modelheader_t *pinmodel;
1149         md3frameinfo_t *pinframe;
1150         md3mesh_t *pinmesh;
1151         md3tag_t *pintag;
1152         skinfile_t *skinfiles;
1153
1154         pinmodel = (md3modelheader_t *)buffer;
1155
1156         if (memcmp(pinmodel->identifier, "IDP3", 4))
1157                 Host_Error ("%s is not a MD3 (IDP3) file", loadmodel->name);
1158         version = LittleLong (pinmodel->version);
1159         if (version != MD3VERSION)
1160                 Host_Error ("%s has wrong version number (%i should be %i)",
1161                         loadmodel->name, version, MD3VERSION);
1162
1163         skinfiles = Mod_LoadSkinFiles();
1164         if (loadmodel->numskins < 1)
1165                 loadmodel->numskins = 1;
1166
1167         loadmodel->type = mod_alias;
1168         loadmodel->DrawSky = NULL;
1169         loadmodel->Draw = R_Q1BSP_Draw;
1170         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
1171         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
1172         loadmodel->DrawLight = R_Q1BSP_DrawLight;
1173         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
1174         loadmodel->flags = LittleLong(pinmodel->flags);
1175         loadmodel->synctype = ST_RAND;
1176
1177         // set up some global info about the model
1178         loadmodel->numframes = LittleLong(pinmodel->num_frames);
1179         loadmodel->num_surfaces = LittleLong(pinmodel->num_meshes);
1180
1181         // make skinscenes for the skins (no groups)
1182         loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
1183         for (i = 0;i < loadmodel->numskins;i++)
1184         {
1185                 loadmodel->skinscenes[i].firstframe = i;
1186                 loadmodel->skinscenes[i].framecount = 1;
1187                 loadmodel->skinscenes[i].loop = true;
1188                 loadmodel->skinscenes[i].framerate = 10;
1189         }
1190
1191         // load frameinfo
1192         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
1193         for (i = 0, pinframe = (md3frameinfo_t *)((unsigned char *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
1194         {
1195                 strcpy(loadmodel->animscenes[i].name, pinframe->name);
1196                 loadmodel->animscenes[i].firstframe = i;
1197                 loadmodel->animscenes[i].framecount = 1;
1198                 loadmodel->animscenes[i].framerate = 10;
1199                 loadmodel->animscenes[i].loop = true;
1200         }
1201
1202         // load tags
1203         loadmodel->num_tagframes = loadmodel->numframes;
1204         loadmodel->num_tags = LittleLong(pinmodel->num_tags);
1205         loadmodel->data_tags = (aliastag_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_tagframes * loadmodel->num_tags * sizeof(aliastag_t));
1206         for (i = 0, pintag = (md3tag_t *)((unsigned char *)pinmodel + LittleLong(pinmodel->lump_tags));i < loadmodel->num_tagframes * loadmodel->num_tags;i++, pintag++)
1207         {
1208                 strcpy(loadmodel->data_tags[i].name, pintag->name);
1209                 loadmodel->data_tags[i].matrix = identitymatrix;
1210                 for (j = 0;j < 3;j++)
1211                 {
1212                         for (k = 0;k < 3;k++)
1213                                 loadmodel->data_tags[i].matrix.m[j][k] = LittleFloat(pintag->rotationmatrix[k * 3 + j]);
1214                         loadmodel->data_tags[i].matrix.m[j][3] = LittleFloat(pintag->origin[j]);
1215                 }
1216                 //Con_Printf("model \"%s\" frame #%i tag #%i \"%s\"\n", loadmodel->name, i / loadmodel->num_tags, i % loadmodel->num_tags, loadmodel->data_tags[i].name);
1217         }
1218
1219         // load meshes
1220         meshvertices = 0;
1221         meshtriangles = 0;
1222         for (i = 0, pinmesh = (md3mesh_t *)((unsigned char *)pinmodel + LittleLong(pinmodel->lump_meshes));i < loadmodel->num_surfaces;i++, pinmesh = (md3mesh_t *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_end)))
1223         {
1224                 if (memcmp(pinmesh->identifier, "IDP3", 4))
1225                         Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)");
1226                 if (LittleLong(pinmesh->num_frames) != loadmodel->numframes)
1227                         Host_Error("Mod_IDP3_Load: mesh numframes differs from header");
1228                 meshvertices += LittleLong(pinmesh->num_vertices);
1229                 meshtriangles += LittleLong(pinmesh->num_triangles);
1230         }
1231
1232         loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
1233         loadmodel->num_textures = loadmodel->num_surfaces;
1234         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * sizeof(float[2]) + meshvertices * loadmodel->numframes * sizeof(float[3]));
1235         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
1236         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
1237         loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
1238         loadmodel->surfmesh.num_vertices = meshvertices;
1239         loadmodel->surfmesh.num_triangles = meshtriangles;
1240         loadmodel->surfmesh.num_morphframes = loadmodel->numframes; // TODO: remove?
1241         loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1242         loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1243         loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
1244         loadmodel->surfmesh.data_morphvertex3f = (float *)data;data += meshvertices * loadmodel->numframes * sizeof(float[3]);
1245
1246         meshvertices = 0;
1247         meshtriangles = 0;
1248         for (i = 0, pinmesh = (md3mesh_t *)((unsigned char *)pinmodel + LittleLong(pinmodel->lump_meshes));i < loadmodel->num_surfaces;i++, pinmesh = (md3mesh_t *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_end)))
1249         {
1250                 if (memcmp(pinmesh->identifier, "IDP3", 4))
1251                         Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)");
1252                 loadmodel->surfacelist[i] = i;
1253                 surface = loadmodel->data_surfaces + i;
1254                 surface->texture = loadmodel->data_textures + i;
1255                 surface->num_firsttriangle = meshtriangles;
1256                 surface->num_triangles = LittleLong(pinmesh->num_triangles);
1257                 surface->num_firstvertex = meshvertices;
1258                 surface->num_vertices = LittleLong(pinmesh->num_vertices);
1259                 meshvertices += surface->num_vertices;
1260                 meshtriangles += surface->num_triangles;
1261
1262                 for (j = 0;j < surface->num_triangles * 3;j++)
1263                         loadmodel->surfmesh.data_element3i[j + surface->num_firsttriangle * 3] = surface->num_firstvertex + LittleLong(((int *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_elements)))[j]);
1264                 for (j = 0;j < surface->num_vertices;j++)
1265                 {
1266                         loadmodel->surfmesh.data_texcoordtexture2f[(j + surface->num_firstvertex) * 2 + 0] = LittleFloat(((float *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_texcoords)))[j * 2 + 0]);
1267                         loadmodel->surfmesh.data_texcoordtexture2f[(j + surface->num_firstvertex) * 2 + 1] = LittleFloat(((float *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_texcoords)))[j * 2 + 1]);
1268                 }
1269                 for (j = 0;j < loadmodel->numframes;j++)
1270                 {
1271                         const short *in4s = (short *)((unsigned char *)pinmesh + LittleLong(pinmesh->lump_framevertices)) + j * surface->num_vertices * 4;
1272                         float *out3f = loadmodel->surfmesh.data_morphvertex3f + 3 * (j * loadmodel->surfmesh.num_vertices + surface->num_firstvertex);
1273                         for (k = 0;k < surface->num_vertices;k++, in4s += 4, out3f += 3)
1274                         {
1275                                 out3f[0] = LittleShort(in4s[0]) * (1.0f / 64.0f);
1276                                 out3f[1] = LittleShort(in4s[1]) * (1.0f / 64.0f);
1277                                 out3f[2] = LittleShort(in4s[2]) * (1.0f / 64.0f);
1278                         }
1279                 }
1280
1281                 if (LittleLong(pinmesh->num_shaders) >= 1)
1282                         Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + i, skinfiles, pinmesh->name, ((md3shader_t *)((unsigned char *) pinmesh + LittleLong(pinmesh->lump_shaders)))->name);
1283                 else
1284                         for (j = 0;j < loadmodel->numskins;j++)
1285                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + i + j * loadmodel->num_surfaces, NULL);
1286
1287                 Mod_ValidateElements(loadmodel->surfmesh.data_element3i + surface->num_firsttriangle * 3, surface->num_triangles, surface->num_firstvertex, surface->num_vertices, __FILE__, __LINE__);
1288         }
1289         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
1290         Mod_Alias_Mesh_CompileFrameZero();
1291         Mod_Alias_CalculateBoundingBox();
1292         Mod_FreeSkinFiles(skinfiles);
1293 }
1294
1295 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend)
1296 {
1297         zymtype1header_t *pinmodel, *pheader;
1298         unsigned char *pbase;
1299         int i, j, k, numposes, meshvertices, meshtriangles, *bonecount, *vertbonecounts, count, *renderlist, *renderlistend, *outelements;
1300         float modelradius, corner[2], *poses, *intexcoord2f, *outtexcoord2f, *bonepose;
1301         zymvertex_t *verts, *vertdata;
1302         zymscene_t *scene;
1303         zymbone_t *bone;
1304         char *shadername;
1305         skinfile_t *skinfiles;
1306         unsigned char *data;
1307         msurface_t *surface;
1308
1309         pinmodel = (zymtype1header_t *)buffer;
1310         pbase = (unsigned char *)buffer;
1311         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
1312                 Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model");
1313         if (BigLong(pinmodel->type) != 1)
1314                 Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)", loadmodel->name);
1315
1316         loadmodel->type = mod_alias;
1317         loadmodel->flags = 0; // there are no flags on zym models
1318         loadmodel->synctype = ST_RAND;
1319
1320         // byteswap header
1321         pheader = pinmodel;
1322         pheader->type = BigLong(pinmodel->type);
1323         pheader->filesize = BigLong(pinmodel->filesize);
1324         pheader->mins[0] = BigFloat(pinmodel->mins[0]);
1325         pheader->mins[1] = BigFloat(pinmodel->mins[1]);
1326         pheader->mins[2] = BigFloat(pinmodel->mins[2]);
1327         pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
1328         pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
1329         pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
1330         pheader->radius = BigFloat(pinmodel->radius);
1331         pheader->numverts = BigLong(pinmodel->numverts);
1332         pheader->numtris = BigLong(pinmodel->numtris);
1333         pheader->numshaders = BigLong(pinmodel->numshaders);
1334         pheader->numbones = BigLong(pinmodel->numbones);
1335         pheader->numscenes = BigLong(pinmodel->numscenes);
1336         pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
1337         pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
1338         pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
1339         pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
1340         pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
1341         pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
1342         pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
1343         pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
1344         pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
1345         pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
1346         pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
1347         pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
1348         pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
1349         pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
1350         pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
1351         pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
1352         pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
1353         pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
1354
1355         if (pheader->numtris < 1 || pheader->numverts < 3 || pheader->numshaders < 1)
1356         {
1357                 Con_Printf("%s has no geometry\n");
1358                 return;
1359         }
1360         if (pheader->numscenes < 1 || pheader->lump_poses.length < (int)sizeof(float[3][4]))
1361         {
1362                 Con_Printf("%s has no animations\n");
1363                 return;
1364         }
1365
1366         loadmodel->DrawSky = NULL;
1367         loadmodel->Draw = R_Q1BSP_Draw;
1368         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
1369         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
1370         loadmodel->DrawLight = R_Q1BSP_DrawLight;
1371         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
1372
1373         loadmodel->numframes = pheader->numscenes;
1374         loadmodel->num_surfaces = pheader->numshaders;
1375
1376         skinfiles = Mod_LoadSkinFiles();
1377         if (loadmodel->numskins < 1)
1378                 loadmodel->numskins = 1;
1379
1380         // make skinscenes for the skins (no groups)
1381         loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
1382         for (i = 0;i < loadmodel->numskins;i++)
1383         {
1384                 loadmodel->skinscenes[i].firstframe = i;
1385                 loadmodel->skinscenes[i].framecount = 1;
1386                 loadmodel->skinscenes[i].loop = true;
1387                 loadmodel->skinscenes[i].framerate = 10;
1388         }
1389
1390         // model bbox
1391         modelradius = pheader->radius;
1392         for (i = 0;i < 3;i++)
1393         {
1394                 loadmodel->normalmins[i] = pheader->mins[i];
1395                 loadmodel->normalmaxs[i] = pheader->maxs[i];
1396                 loadmodel->rotatedmins[i] = -modelradius;
1397                 loadmodel->rotatedmaxs[i] = modelradius;
1398         }
1399         corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
1400         corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
1401         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1402         if (loadmodel->yawmaxs[0] > modelradius)
1403                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
1404         loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
1405         loadmodel->yawmins[2] = loadmodel->normalmins[2];
1406         loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
1407         loadmodel->radius = modelradius;
1408         loadmodel->radius2 = modelradius * modelradius;
1409
1410         // go through the lumps, swapping things
1411
1412         //zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1413         loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
1414         scene = (zymscene_t *) (pheader->lump_scenes.start + pbase);
1415         numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
1416         for (i = 0;i < pheader->numscenes;i++)
1417         {
1418                 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
1419                 loadmodel->animscenes[i].firstframe = BigLong(scene->start);
1420                 loadmodel->animscenes[i].framecount = BigLong(scene->length);
1421                 loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
1422                 loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
1423                 if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
1424                         Host_Error("%s scene->firstframe (%i) >= numposes (%i)", loadmodel->name, loadmodel->animscenes[i].firstframe, numposes);
1425                 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
1426                         Host_Error("%s scene->firstframe (%i) + framecount (%i) >= numposes (%i)", loadmodel->name, loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
1427                 if (loadmodel->animscenes[i].framerate < 0)
1428                         Host_Error("%s scene->framerate (%f) < 0", loadmodel->name, loadmodel->animscenes[i].framerate);
1429                 scene++;
1430         }
1431
1432         //zymlump_t lump_bones; // zymbone_t bone[numbones];
1433         loadmodel->num_bones = pheader->numbones;
1434         loadmodel->data_bones = (aliasbone_t *)Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(aliasbone_t));
1435         bone = (zymbone_t *) (pheader->lump_bones.start + pbase);
1436         for (i = 0;i < pheader->numbones;i++)
1437         {
1438                 memcpy(loadmodel->data_bones[i].name, bone[i].name, sizeof(bone[i].name));
1439                 loadmodel->data_bones[i].flags = BigLong(bone[i].flags);
1440                 loadmodel->data_bones[i].parent = BigLong(bone[i].parent);
1441                 if (loadmodel->data_bones[i].parent >= i)
1442                         Host_Error("%s bone[%i].parent >= %i", loadmodel->name, i, i);
1443         }
1444
1445         //zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1446         vertbonecounts = (int *)Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1447         bonecount = (int *) (pheader->lump_vertbonecounts.start + pbase);
1448         for (i = 0;i < pheader->numverts;i++)
1449         {
1450                 vertbonecounts[i] = BigLong(bonecount[i]);
1451                 if (vertbonecounts[i] != 1)
1452                         Host_Error("%s bonecount[%i] != 1 (vertex weight support is impossible in this format)", loadmodel->name, i);
1453         }
1454
1455         loadmodel->num_poses = pheader->lump_poses.length / sizeof(float[3][4]);
1456
1457         meshvertices = pheader->numverts;
1458         meshtriangles = pheader->numtris;
1459
1460         loadmodel->nummodelsurfaces = loadmodel->num_surfaces;
1461         loadmodel->num_textures = loadmodel->num_surfaces;
1462         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * sizeof(float[14]) + meshvertices * sizeof(int[4]) + meshvertices * sizeof(float[4]) + loadmodel->num_poses * sizeof(float[36]));
1463         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
1464         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
1465         loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
1466         loadmodel->surfmesh.num_vertices = meshvertices;
1467         loadmodel->surfmesh.num_triangles = meshtriangles;
1468         loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1469         loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1470         loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
1471         loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
1472         loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
1473         loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
1474         loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
1475         loadmodel->surfmesh.data_vertexweightindex4i = (int *)data;data += meshvertices * sizeof(int[4]);
1476         loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += meshvertices * sizeof(float[4]);
1477         loadmodel->data_poses = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1478         loadmodel->data_basebonepose = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1479         loadmodel->data_baseboneposeinverse = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1480
1481         //zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
1482         poses = (float *) (pheader->lump_poses.start + pbase);
1483         for (i = 0;i < pheader->lump_poses.length / 4;i++)
1484                 loadmodel->data_poses[i] = BigFloat(poses[i]);
1485
1486         //zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1487         verts = (zymvertex_t *)Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
1488         vertdata = (zymvertex_t *) (pheader->lump_verts.start + pbase);
1489         // reconstruct frame 0 matrices to allow reconstruction of the base mesh
1490         // (converting from weight-blending skeletal animation to
1491         //  deformation-based skeletal animation)
1492         bonepose = (float *)Z_Malloc(loadmodel->num_bones * sizeof(float[12]));
1493         for (i = 0;i < loadmodel->num_bones;i++)
1494         {
1495                 const float *m = loadmodel->data_poses + i * 12;
1496                 if (loadmodel->data_bones[i].parent >= 0)
1497                         R_ConcatTransforms(bonepose + 12 * loadmodel->data_bones[i].parent, m, bonepose + 12 * i);
1498                 else
1499                         for (k = 0;k < 12;k++)
1500                                 bonepose[12*i+k] = m[k];
1501         }
1502         for (j = 0;j < pheader->numverts;j++)
1503         {
1504                 // this format really should have had a per vertexweight weight value...
1505                 // but since it does not, the weighting is completely ignored and
1506                 // only one weight is allowed per vertex
1507                 int boneindex = BigLong(vertdata[j].bonenum);
1508                 const float *m = bonepose + 12 * boneindex;
1509                 float relativeorigin[3];
1510                 relativeorigin[0] = BigFloat(vertdata[j].origin[0]);
1511                 relativeorigin[1] = BigFloat(vertdata[j].origin[1]);
1512                 relativeorigin[2] = BigFloat(vertdata[j].origin[2]);
1513                 // transform the vertex bone weight into the base mesh
1514                 loadmodel->surfmesh.data_vertex3f[j*3+0] = relativeorigin[0] * m[0] + relativeorigin[1] * m[1] + relativeorigin[2] * m[ 2] + m[ 3];
1515                 loadmodel->surfmesh.data_vertex3f[j*3+1] = relativeorigin[0] * m[4] + relativeorigin[1] * m[5] + relativeorigin[2] * m[ 6] + m[ 7];
1516                 loadmodel->surfmesh.data_vertex3f[j*3+2] = relativeorigin[0] * m[8] + relativeorigin[1] * m[9] + relativeorigin[2] * m[10] + m[11];
1517                 // store the weight as the primary weight on this vertex
1518                 loadmodel->surfmesh.data_vertexweightindex4i[j*4+0] = boneindex;
1519                 loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+0] = 1;
1520         }
1521         Z_Free(bonepose);
1522         // normals and tangents are calculated after elements are loaded
1523
1524         //zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1525         outtexcoord2f = loadmodel->surfmesh.data_texcoordtexture2f;
1526         intexcoord2f = (float *) (pheader->lump_texcoords.start + pbase);
1527         for (i = 0;i < pheader->numverts;i++)
1528         {
1529                 outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1530                 // flip T coordinate for OpenGL
1531                 outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1532         }
1533
1534         //zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1535         //loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1536         //memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);
1537
1538         //zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1539         //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)
1540         // byteswap, validate, and swap winding order of tris
1541         count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1542         if (pheader->lump_render.length != count)
1543                 Host_Error("%s renderlist is wrong size (%i bytes, should be %i bytes)", loadmodel->name, pheader->lump_render.length, count);
1544         renderlist = (int *) (pheader->lump_render.start + pbase);
1545         renderlistend = (int *) ((unsigned char *) renderlist + pheader->lump_render.length);
1546         meshtriangles = 0;
1547         for (i = 0;i < loadmodel->num_surfaces;i++)
1548         {
1549                 int firstvertex, lastvertex;
1550                 if (renderlist >= renderlistend)
1551                         Host_Error("%s corrupt renderlist (wrong size)", loadmodel->name);
1552                 count = BigLong(*renderlist);renderlist++;
1553                 if (renderlist + count * 3 > renderlistend || (i == pheader->numshaders - 1 && renderlist + count * 3 != renderlistend))
1554                         Host_Error("%s corrupt renderlist (wrong size)", loadmodel->name);
1555
1556                 loadmodel->surfacelist[i] = i;
1557                 surface = loadmodel->data_surfaces + i;
1558                 surface->texture = loadmodel->data_textures + i;
1559                 surface->num_firsttriangle = meshtriangles;
1560                 surface->num_triangles = count;
1561                 meshtriangles += surface->num_triangles;
1562
1563                 // load the elements
1564                 outelements = loadmodel->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1565                 for (j = 0;j < surface->num_triangles;j++, renderlist += 3)
1566                 {
1567                         outelements[j*3+2] = BigLong(renderlist[0]);
1568                         outelements[j*3+1] = BigLong(renderlist[1]);
1569                         outelements[j*3+0] = BigLong(renderlist[2]);
1570                 }
1571                 // validate the elements and find the used vertex range
1572                 firstvertex = meshvertices;
1573                 lastvertex = 0;
1574                 for (j = 0;j < surface->num_triangles * 3;j++)
1575                 {
1576                         if ((unsigned int)outelements[j] >= (unsigned int)meshvertices)
1577                                 Host_Error("%s corrupt renderlist (out of bounds index)", loadmodel->name);
1578                         firstvertex = min(firstvertex, outelements[j]);
1579                         lastvertex = max(lastvertex, outelements[j]);
1580                 }
1581                 surface->num_firstvertex = firstvertex;
1582                 surface->num_vertices = lastvertex + 1 - firstvertex;
1583
1584                 // since zym models do not have named sections, reuse their shader
1585                 // name as the section name
1586                 shadername = (char *) (pheader->lump_shaders.start + pbase) + i * 32;
1587                 if (shadername[0])
1588                         Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + i, skinfiles, shadername, shadername);
1589                 else
1590                         for (j = 0;j < loadmodel->numskins;j++)
1591                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + i + j * loadmodel->num_surfaces, NULL);
1592         }
1593         Mod_FreeSkinFiles(skinfiles);
1594         Mem_Free(vertbonecounts);
1595         Mem_Free(verts);
1596
1597         // compute all the mesh information that was not loaded from the file
1598         Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, loadmodel->surfmesh.num_vertices, __FILE__, __LINE__);
1599         Mod_BuildBaseBonePoses();
1600         Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, true);
1601         Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
1602         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
1603 }
1604
1605 void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend)
1606 {
1607         dpmheader_t *pheader;
1608         dpmframe_t *frame;
1609         dpmbone_t *bone;
1610         dpmmesh_t *dpmmesh;
1611         unsigned char *pbase;
1612         int i, j, k, meshvertices, meshtriangles;
1613         skinfile_t *skinfiles;
1614         unsigned char *data;
1615         float *bonepose;
1616
1617         pheader = (dpmheader_t *)buffer;
1618         pbase = (unsigned char *)buffer;
1619         if (memcmp(pheader->id, "DARKPLACESMODEL\0", 16))
1620                 Host_Error ("Mod_DARKPLACESMODEL_Load: %s is not a darkplaces model");
1621         if (BigLong(pheader->type) != 2)
1622                 Host_Error ("Mod_DARKPLACESMODEL_Load: only type 2 (hierarchical skeletal pose) models are currently supported (name = %s)", loadmodel->name);
1623
1624         loadmodel->type = mod_alias;
1625         loadmodel->flags = 0; // there are no flags on zym models
1626         loadmodel->synctype = ST_RAND;
1627
1628         // byteswap header
1629         pheader->type = BigLong(pheader->type);
1630         pheader->filesize = BigLong(pheader->filesize);
1631         pheader->mins[0] = BigFloat(pheader->mins[0]);
1632         pheader->mins[1] = BigFloat(pheader->mins[1]);
1633         pheader->mins[2] = BigFloat(pheader->mins[2]);
1634         pheader->maxs[0] = BigFloat(pheader->maxs[0]);
1635         pheader->maxs[1] = BigFloat(pheader->maxs[1]);
1636         pheader->maxs[2] = BigFloat(pheader->maxs[2]);
1637         pheader->yawradius = BigFloat(pheader->yawradius);
1638         pheader->allradius = BigFloat(pheader->allradius);
1639         pheader->num_bones = BigLong(pheader->num_bones);
1640         pheader->num_meshs = BigLong(pheader->num_meshs);
1641         pheader->num_frames = BigLong(pheader->num_frames);
1642         pheader->ofs_bones = BigLong(pheader->ofs_bones);
1643         pheader->ofs_meshs = BigLong(pheader->ofs_meshs);
1644         pheader->ofs_frames = BigLong(pheader->ofs_frames);
1645
1646         if (pheader->num_bones < 1 || pheader->num_meshs < 1)
1647         {
1648                 Con_Printf("%s has no geometry\n");
1649                 return;
1650         }
1651         if (pheader->num_frames < 1)
1652         {
1653                 Con_Printf("%s has no frames\n");
1654                 return;
1655         }
1656
1657         loadmodel->DrawSky = NULL;
1658         loadmodel->Draw = R_Q1BSP_Draw;
1659         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
1660         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
1661         loadmodel->DrawLight = R_Q1BSP_DrawLight;
1662         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
1663
1664         // model bbox
1665         for (i = 0;i < 3;i++)
1666         {
1667                 loadmodel->normalmins[i] = pheader->mins[i];
1668                 loadmodel->normalmaxs[i] = pheader->maxs[i];
1669                 loadmodel->yawmins[i] = i != 2 ? -pheader->yawradius : pheader->mins[i];
1670                 loadmodel->yawmaxs[i] = i != 2 ? pheader->yawradius : pheader->maxs[i];
1671                 loadmodel->rotatedmins[i] = -pheader->allradius;
1672                 loadmodel->rotatedmaxs[i] = pheader->allradius;
1673         }
1674         loadmodel->radius = pheader->allradius;
1675         loadmodel->radius2 = pheader->allradius * pheader->allradius;
1676
1677         // load external .skin files if present
1678         skinfiles = Mod_LoadSkinFiles();
1679         if (loadmodel->numskins < 1)
1680                 loadmodel->numskins = 1;
1681
1682         meshvertices = 0;
1683         meshtriangles = 0;
1684
1685         // gather combined statistics from the meshes
1686         dpmmesh = (dpmmesh_t *) (pbase + pheader->ofs_meshs);
1687         for (i = 0;i < (int)pheader->num_meshs;i++)
1688         {
1689                 int numverts = BigLong(dpmmesh->num_verts);
1690                 meshvertices += numverts;;
1691                 meshtriangles += BigLong(dpmmesh->num_tris);
1692                 dpmmesh++;
1693         }
1694
1695         loadmodel->numframes = pheader->num_frames;
1696         loadmodel->num_bones = pheader->num_bones;
1697         loadmodel->num_poses = loadmodel->num_bones * loadmodel->numframes;
1698         loadmodel->num_textures = loadmodel->nummodelsurfaces = loadmodel->num_surfaces = pheader->num_meshs;
1699         // do most allocations as one merged chunk
1700         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * (sizeof(float[14]) + sizeof(int[4]) + sizeof(float[4])) + loadmodel->num_poses * sizeof(float[36]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t));
1701         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
1702         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
1703         loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
1704         loadmodel->surfmesh.num_vertices = meshvertices;
1705         loadmodel->surfmesh.num_triangles = meshtriangles;
1706         loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1707         loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
1708         loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
1709         loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
1710         loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
1711         loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
1712         loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
1713         loadmodel->surfmesh.data_vertexweightindex4i = (int *)data;data += meshvertices * sizeof(int[4]);
1714         loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += meshvertices * sizeof(float[4]);
1715         loadmodel->data_poses = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1716         loadmodel->data_basebonepose = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1717         loadmodel->data_baseboneposeinverse = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
1718         loadmodel->skinscenes = (animscene_t *)data;data += loadmodel->numskins * sizeof(animscene_t);
1719         loadmodel->data_bones = (aliasbone_t *)data;data += loadmodel->num_bones * sizeof(aliasbone_t);
1720         loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t);
1721
1722         for (i = 0;i < loadmodel->numskins;i++)
1723         {
1724                 loadmodel->skinscenes[i].firstframe = i;
1725                 loadmodel->skinscenes[i].framecount = 1;
1726                 loadmodel->skinscenes[i].loop = true;
1727                 loadmodel->skinscenes[i].framerate = 10;
1728         }
1729
1730         // load the bone info
1731         bone = (dpmbone_t *) (pbase + pheader->ofs_bones);
1732         for (i = 0;i < loadmodel->num_bones;i++)
1733         {
1734                 memcpy(loadmodel->data_bones[i].name, bone[i].name, sizeof(bone[i].name));
1735                 loadmodel->data_bones[i].flags = BigLong(bone[i].flags);
1736                 loadmodel->data_bones[i].parent = BigLong(bone[i].parent);
1737                 if (loadmodel->data_bones[i].parent >= i)
1738                         Host_Error("%s bone[%i].parent >= %i", loadmodel->name, i, i);
1739         }
1740
1741         // load the frames
1742         frame = (dpmframe_t *) (pbase + pheader->ofs_frames);
1743         for (i = 0;i < loadmodel->numframes;i++)
1744         {
1745                 const float *poses;
1746                 memcpy(loadmodel->animscenes[i].name, frame->name, sizeof(frame->name));
1747                 loadmodel->animscenes[i].firstframe = i;
1748                 loadmodel->animscenes[i].framecount = 1;
1749                 loadmodel->animscenes[i].loop = true;
1750                 loadmodel->animscenes[i].framerate = 10;
1751                 // load the bone poses for this frame
1752                 poses = (float *) (pbase + BigLong(frame->ofs_bonepositions));
1753                 for (j = 0;j < loadmodel->num_bones*12;j++)
1754                         loadmodel->data_poses[i * loadmodel->num_bones*12 + j] = BigFloat(poses[j]);
1755                 // stuff not processed here: mins, maxs, yawradius, allradius
1756                 frame++;
1757         }
1758
1759         // load the meshes now
1760         dpmmesh = (dpmmesh_t *) (pbase + pheader->ofs_meshs);
1761         meshvertices = 0;
1762         meshtriangles = 0;
1763         // reconstruct frame 0 matrices to allow reconstruction of the base mesh
1764         // (converting from weight-blending skeletal animation to
1765         //  deformation-based skeletal animation)
1766         bonepose = (float *)Z_Malloc(loadmodel->num_bones * sizeof(float[12]));
1767         for (i = 0;i < loadmodel->num_bones;i++)
1768         {
1769                 const float *m = loadmodel->data_poses + i * 12;
1770                 if (loadmodel->data_bones[i].parent >= 0)
1771                         R_ConcatTransforms(bonepose + 12 * loadmodel->data_bones[i].parent, m, bonepose + 12 * i);
1772                 else
1773                         for (k = 0;k < 12;k++)
1774                                 bonepose[12*i+k] = m[k];
1775         }
1776         for (i = 0;i < loadmodel->num_surfaces;i++, dpmmesh++)
1777         {
1778                 const int *inelements;
1779                 int *outelements;
1780                 const float *intexcoord;
1781                 msurface_t *surface;
1782
1783                 loadmodel->surfacelist[i] = i;
1784                 surface = loadmodel->data_surfaces + i;
1785                 surface->texture = loadmodel->data_textures + i;
1786                 surface->num_firsttriangle = meshtriangles;
1787                 surface->num_triangles = BigLong(dpmmesh->num_tris);
1788                 surface->num_firstvertex = meshvertices;
1789                 surface->num_vertices = BigLong(dpmmesh->num_verts);
1790                 meshvertices += surface->num_vertices;
1791                 meshtriangles += surface->num_triangles;
1792
1793                 inelements = (int *) (pbase + BigLong(dpmmesh->ofs_indices));
1794                 outelements = loadmodel->surfmesh.data_element3i + surface->num_firsttriangle * 3;
1795                 for (j = 0;j < surface->num_triangles;j++)
1796                 {
1797                         // swap element order to flip triangles, because Quake uses clockwise (rare) and dpm uses counterclockwise (standard)
1798                         outelements[0] = surface->num_firstvertex + BigLong(inelements[2]);
1799                         outelements[1] = surface->num_firstvertex + BigLong(inelements[1]);
1800                         outelements[2] = surface->num_firstvertex + BigLong(inelements[0]);
1801                         inelements += 3;
1802                         outelements += 3;
1803                 }
1804
1805                 intexcoord = (float *) (pbase + BigLong(dpmmesh->ofs_texcoords));
1806                 for (j = 0;j < surface->num_vertices*2;j++)
1807                         loadmodel->surfmesh.data_texcoordtexture2f[j + surface->num_firstvertex * 2] = BigFloat(intexcoord[j]);
1808
1809                 data = (unsigned char *) (pbase + BigLong(dpmmesh->ofs_verts));
1810                 for (j = surface->num_firstvertex;j < surface->num_firstvertex + surface->num_vertices;j++)
1811                 {
1812                         float sum;
1813                         int l;
1814                         int numweights = BigLong(((dpmvertex_t *)data)->numbones);
1815                         data += sizeof(dpmvertex_t);
1816                         for (k = 0;k < numweights;k++)
1817                         {
1818                                 const dpmbonevert_t *vert = (dpmbonevert_t *) data;
1819                                 int boneindex = BigLong(vert->bonenum);
1820                                 const float *m = bonepose + 12 * boneindex;
1821                                 float influence = BigFloat(vert->influence);
1822                                 float relativeorigin[3], relativenormal[3];
1823                                 relativeorigin[0] = BigFloat(vert->origin[0]);
1824                                 relativeorigin[1] = BigFloat(vert->origin[1]);
1825                                 relativeorigin[2] = BigFloat(vert->origin[2]);
1826                                 relativenormal[0] = BigFloat(vert->normal[0]);
1827                                 relativenormal[1] = BigFloat(vert->normal[1]);
1828                                 relativenormal[2] = BigFloat(vert->normal[2]);
1829                                 // blend the vertex bone weights into the base mesh
1830                                 loadmodel->surfmesh.data_vertex3f[j*3+0] += relativeorigin[0] * m[0] + relativeorigin[1] * m[1] + relativeorigin[2] * m[ 2] + influence * m[ 3];
1831                                 loadmodel->surfmesh.data_vertex3f[j*3+1] += relativeorigin[0] * m[4] + relativeorigin[1] * m[5] + relativeorigin[2] * m[ 6] + influence * m[ 7];
1832                                 loadmodel->surfmesh.data_vertex3f[j*3+2] += relativeorigin[0] * m[8] + relativeorigin[1] * m[9] + relativeorigin[2] * m[10] + influence * m[11];
1833                                 loadmodel->surfmesh.data_normal3f[j*3+0] += relativenormal[0] * m[0] + relativenormal[1] * m[1] + relativenormal[2] * m[ 2];
1834                                 loadmodel->surfmesh.data_normal3f[j*3+1] += relativenormal[0] * m[4] + relativenormal[1] * m[5] + relativenormal[2] * m[ 6];
1835                                 loadmodel->surfmesh.data_normal3f[j*3+2] += relativenormal[0] * m[8] + relativenormal[1] * m[9] + relativenormal[2] * m[10];
1836                                 if (!k)
1837                                 {
1838                                         // store the first (and often only) weight
1839                                         loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+0] = influence;
1840                                         loadmodel->surfmesh.data_vertexweightindex4i[j*4+0] = boneindex;
1841                                 }
1842                                 else
1843                                 {
1844                                         // sort the new weight into this vertex's weight table
1845                                         // (which only accepts up to 4 bones per vertex)
1846                                         for (l = 0;l < 4;l++)
1847                                         {
1848                                                 if (loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] < influence)
1849                                                 {
1850                                                         // move weaker influence weights out of the way first
1851                                                         int l2;
1852                                                         for (l2 = 3;l2 > l;l2--)
1853                                                         {
1854                                                                 loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l2] = loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l2-1];
1855                                                                 loadmodel->surfmesh.data_vertexweightindex4i[j*4+l2] = loadmodel->surfmesh.data_vertexweightindex4i[j*4+l2-1];
1856                                                         }
1857                                                         // store the new weight
1858                                                         loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] = influence;
1859                                                         loadmodel->surfmesh.data_vertexweightindex4i[j*4+l] = boneindex;
1860                                                         break;
1861                                                 }
1862                                         }
1863                                 }
1864                                 data += sizeof(dpmbonevert_t);
1865                         }
1866                         sum = 0;
1867                         for (l = 0;l < 4;l++)
1868                                 sum += loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l];
1869                         if (sum && fabs(sum - 1) > (1.0f / 256.0f))
1870                         {
1871                                 float f = 1.0f / sum;
1872                                 for (l = 0;l < 4;l++)
1873                                         loadmodel->surfmesh.data_vertexweightinfluence4f[j*4+l] *= f;
1874                         }
1875                 }
1876
1877                 // since dpm models do not have named sections, reuse their shader name as the section name
1878                 if (dpmmesh->shadername[0])
1879                         Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + i, skinfiles, dpmmesh->shadername, dpmmesh->shadername);
1880                 else
1881                         for (j = 0;j < loadmodel->numskins;j++)
1882                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + i + j * loadmodel->num_surfaces, NULL);
1883
1884                 Mod_ValidateElements(loadmodel->surfmesh.data_element3i + surface->num_firsttriangle * 3, surface->num_triangles, surface->num_firstvertex, surface->num_vertices, __FILE__, __LINE__);
1885         }
1886         Z_Free(bonepose);
1887         Mod_FreeSkinFiles(skinfiles);
1888
1889         // compute all the mesh information that was not loaded from the file
1890         Mod_BuildBaseBonePoses();
1891         Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
1892         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
1893 }
1894
1895 static void Mod_PSKMODEL_AnimKeyToMatrix(float *origin, float *quat, matrix4x4_t *m)
1896 {
1897         float x = quat[0], y = quat[1], z = quat[2], w = quat[3];
1898         m->m[0][0]=1-2*(y*y+z*z);m->m[0][1]=  2*(x*y-z*w);m->m[0][2]=  2*(x*z+y*w);m->m[0][3]=origin[0];
1899         m->m[1][0]=  2*(x*y+z*w);m->m[1][1]=1-2*(x*x+z*z);m->m[1][2]=  2*(y*z-x*w);m->m[1][3]=origin[1];
1900         m->m[2][0]=  2*(x*z-y*w);m->m[2][1]=  2*(y*z+x*w);m->m[2][2]=1-2*(x*x+y*y);m->m[2][3]=origin[2];
1901         m->m[3][0]=  0          ;m->m[3][1]=  0          ;m->m[3][2]=  0          ;m->m[3][3]=1;
1902 }
1903
1904 // no idea why PSK/PSA files contain weird quaternions but they do...
1905 #define PSKQUATNEGATIONS
1906 void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend)
1907 {
1908         int i, j, index, version, recordsize, numrecords, meshvertices, meshtriangles;
1909         int numpnts, numvtxw, numfaces, nummatts, numbones, numrawweights, numanimbones, numanims, numanimkeys;
1910         fs_offset_t filesize;
1911         pskpnts_t *pnts;
1912         pskvtxw_t *vtxw;
1913         pskface_t *faces;
1914         pskmatt_t *matts;
1915         pskboneinfo_t *bones;
1916         pskrawweights_t *rawweights;
1917         pskboneinfo_t *animbones;
1918         pskaniminfo_t *anims;
1919         pskanimkeys_t *animkeys;
1920         void *animfilebuffer, *animbuffer, *animbufferend;
1921         unsigned char *data;
1922         pskchunk_t *pchunk;
1923         skinfile_t *skinfiles;
1924         char animname[MAX_QPATH];
1925
1926         pchunk = (pskchunk_t *)buffer;
1927         if (strcmp(pchunk->id, "ACTRHEAD"))
1928                 Host_Error ("Mod_PSKMODEL_Load: %s is not an Unreal Engine ActorX (.psk + .psa) model");
1929
1930         loadmodel->type = mod_alias;
1931         loadmodel->DrawSky = NULL;
1932         loadmodel->Draw = R_Q1BSP_Draw;
1933         loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
1934         loadmodel->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
1935         loadmodel->DrawLight = R_Q1BSP_DrawLight;
1936         loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
1937         loadmodel->flags = 0; // there are no flags on zym models
1938         loadmodel->synctype = ST_RAND;
1939
1940         FS_StripExtension(loadmodel->name, animname, sizeof(animname));
1941         strlcat(animname, ".psa", sizeof(animname));
1942         animbuffer = animfilebuffer = FS_LoadFile(animname, loadmodel->mempool, false, &filesize);
1943         animbufferend = (void *)((unsigned char*)animbuffer + (int)filesize);
1944         if (animbuffer == NULL)
1945                 Host_Error("%s: can't find .psa file (%s)", loadmodel->name, animname);
1946
1947         numpnts = 0;
1948         pnts = NULL;
1949         numvtxw = 0;
1950         vtxw = NULL;
1951         numfaces = 0;
1952         faces = NULL;
1953         nummatts = 0;
1954         matts = NULL;
1955         numbones = 0;
1956         bones = NULL;
1957         numrawweights = 0;
1958         rawweights = NULL;
1959         numanims = 0;
1960         anims = NULL;
1961         numanimkeys = 0;
1962         animkeys = NULL;
1963
1964         while (buffer < bufferend)
1965         {
1966                 pchunk = (pskchunk_t *)buffer;
1967                 buffer = (void *)((unsigned char *)buffer + sizeof(pskchunk_t));
1968                 version = LittleLong(pchunk->version);
1969                 recordsize = LittleLong(pchunk->recordsize);
1970                 numrecords = LittleLong(pchunk->numrecords);
1971                 if (developer.integer >= 100)
1972                         Con_Printf("%s: %s %x: %i * %i = %i\n", loadmodel->name, pchunk->id, version, recordsize, numrecords, recordsize * numrecords);
1973                 if (version != 0x1e83b9 && version != 0x1e9179 && version != 0x2e && version != 0x12f2bc && version != 0x12f2f0)
1974                         Con_Printf ("%s: chunk %s has unknown version %x (0x1e83b9, 0x1e9179, 0x2e, 0x12f2bc, 0x12f2f0 are currently supported), trying to load anyway!\n", loadmodel->name, pchunk->id, version);
1975                 if (!strcmp(pchunk->id, "ACTRHEAD"))
1976                 {
1977                         // nothing to do
1978                 }
1979                 else if (!strcmp(pchunk->id, "PNTS0000"))
1980                 {
1981                         pskpnts_t *p;
1982                         if (recordsize != sizeof(*p))
1983                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
1984                         // byteswap in place and keep the pointer
1985                         numpnts = numrecords;
1986                         pnts = (pskpnts_t *)buffer;
1987                         for (index = 0, p = (pskpnts_t *)buffer;index < numrecords;index++, p++)
1988                         {
1989                                 p->origin[0] = LittleFloat(p->origin[0]);
1990                                 p->origin[1] = LittleFloat(p->origin[1]);
1991                                 p->origin[2] = LittleFloat(p->origin[2]);
1992                         }
1993                         buffer = p;
1994                 }
1995                 else if (!strcmp(pchunk->id, "VTXW0000"))
1996                 {
1997                         pskvtxw_t *p;
1998                         if (recordsize != sizeof(*p))
1999                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
2000                         // byteswap in place and keep the pointer
2001                         numvtxw = numrecords;
2002                         vtxw = (pskvtxw_t *)buffer;
2003                         for (index = 0, p = (pskvtxw_t *)buffer;index < numrecords;index++, p++)
2004                         {
2005                                 p->pntsindex = LittleShort(p->pntsindex);
2006                                 p->texcoord[0] = LittleFloat(p->texcoord[0]);
2007                                 p->texcoord[1] = LittleFloat(p->texcoord[1]);
2008                                 if (p->pntsindex >= numpnts)
2009                                 {
2010                                         Con_Printf("%s: vtxw->pntsindex %i >= numpnts %i\n", loadmodel->name, p->pntsindex, numpnts);
2011                                         p->pntsindex = 0;
2012                                 }
2013                         }
2014                         buffer = p;
2015                 }
2016                 else if (!strcmp(pchunk->id, "FACE0000"))
2017                 {
2018                         pskface_t *p;
2019                         if (recordsize != sizeof(*p))
2020                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
2021                         // byteswap in place and keep the pointer
2022                         numfaces = numrecords;
2023                         faces = (pskface_t *)buffer;
2024                         for (index = 0, p = (pskface_t *)buffer;index < numrecords;index++, p++)
2025                         {
2026                                 p->vtxwindex[0] = LittleShort(p->vtxwindex[0]);
2027                                 p->vtxwindex[1] = LittleShort(p->vtxwindex[1]);
2028                                 p->vtxwindex[2] = LittleShort(p->vtxwindex[2]);
2029                                 p->group = LittleLong(p->group);
2030                                 if (p->vtxwindex[0] >= numvtxw)
2031                                 {
2032                                         Con_Printf("%s: face->vtxwindex[0] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[0], numvtxw);
2033                                         p->vtxwindex[0] = 0;
2034                                 }
2035                                 if (p->vtxwindex[1] >= numvtxw)
2036                                 {
2037                                         Con_Printf("%s: face->vtxwindex[1] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[1], numvtxw);
2038                                         p->vtxwindex[1] = 0;
2039                                 }
2040                                 if (p->vtxwindex[2] >= numvtxw)
2041                                 {
2042                                         Con_Printf("%s: face->vtxwindex[2] %i >= numvtxw %i\n", loadmodel->name, p->vtxwindex[2], numvtxw);
2043                                         p->vtxwindex[2] = 0;
2044                                 }
2045                         }
2046                         buffer = p;
2047                 }
2048                 else if (!strcmp(pchunk->id, "MATT0000"))
2049                 {
2050                         pskmatt_t *p;
2051                         if (recordsize != sizeof(*p))
2052                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
2053                         // byteswap in place and keep the pointer
2054                         nummatts = numrecords;
2055                         matts = (pskmatt_t *)buffer;
2056                         for (index = 0, p = (pskmatt_t *)buffer;index < numrecords;index++, p++)
2057                         {
2058                                 // nothing to do
2059                         }
2060                         buffer = p;
2061                 }
2062                 else if (!strcmp(pchunk->id, "REFSKELT"))
2063                 {
2064                         pskboneinfo_t *p;
2065                         if (recordsize != sizeof(*p))
2066                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
2067                         // byteswap in place and keep the pointer
2068                         numbones = numrecords;
2069                         bones = (pskboneinfo_t *)buffer;
2070                         for (index = 0, p = (pskboneinfo_t *)buffer;index < numrecords;index++, p++)
2071                         {
2072                                 p->numchildren = LittleLong(p->numchildren);
2073                                 p->parent = LittleLong(p->parent);
2074                                 p->basepose.quat[0] = LittleFloat(p->basepose.quat[0]);
2075                                 p->basepose.quat[1] = LittleFloat(p->basepose.quat[1]);
2076                                 p->basepose.quat[2] = LittleFloat(p->basepose.quat[2]);
2077                                 p->basepose.quat[3] = LittleFloat(p->basepose.quat[3]);
2078                                 p->basepose.origin[0] = LittleFloat(p->basepose.origin[0]);
2079                                 p->basepose.origin[1] = LittleFloat(p->basepose.origin[1]);
2080                                 p->basepose.origin[2] = LittleFloat(p->basepose.origin[2]);
2081                                 p->basepose.unknown = LittleFloat(p->basepose.unknown);
2082                                 p->basepose.size[0] = LittleFloat(p->basepose.size[0]);
2083                                 p->basepose.size[1] = LittleFloat(p->basepose.size[1]);
2084                                 p->basepose.size[2] = LittleFloat(p->basepose.size[2]);
2085 #ifdef PSKQUATNEGATIONS
2086                                 if (index)
2087                                 {
2088                                         p->basepose.quat[0] *= -1;
2089                                         p->basepose.quat[1] *= -1;
2090                                         p->basepose.quat[2] *= -1;
2091                                 }
2092                                 else
2093                                 {
2094                                         p->basepose.quat[0] *=  1;
2095                                         p->basepose.quat[1] *= -1;
2096                                         p->basepose.quat[2] *=  1;
2097                                 }
2098 #endif
2099                                 if (p->parent < 0 || p->parent >= numbones)
2100                                 {
2101                                         Con_Printf("%s: bone->parent %i >= numbones %i\n", loadmodel->name, p->parent, numbones);
2102                                         p->parent = 0;
2103                                 }
2104                         }
2105                         buffer = p;
2106                 }
2107                 else if (!strcmp(pchunk->id, "RAWWEIGHTS"))
2108                 {
2109                         pskrawweights_t *p;
2110                         if (recordsize != sizeof(*p))
2111                                 Host_Error("%s: %s has unsupported recordsize", loadmodel->name, pchunk->id);
2112                         // byteswap in place and keep the pointer
2113                         numrawweights = numrecords;
2114                         rawweights = (pskrawweights_t *)buffer;
2115                         for (index = 0, p = (pskrawweights_t *)buffer;index < numrecords;index++, p++)
2116                         {
2117                                 p->weight = LittleFloat(p->weight);
2118                                 p->pntsindex = LittleLong(p->pntsindex);
2119                                 p->boneindex = LittleLong(p->boneindex);
2120                                 if (p->pntsindex < 0 || p->pntsindex >= numpnts)
2121                                 {
2122                                         Con_Printf("%s: weight->pntsindex %i >= numpnts %i\n", loadmodel->name, p->pntsindex, numpnts);
2123                                         p->pntsindex = 0;
2124                                 }
2125                                 if (p->boneindex < 0 || p->boneindex >= numbones)
2126                                 {
2127                                         Con_Printf("%s: weight->boneindex %i >= numbones %i\n", loadmodel->name, p->boneindex, numbones);
2128                                         p->boneindex = 0;
2129                                 }
2130                         }
2131                         buffer = p;
2132                 }
2133         }
2134
2135         while (animbuffer < animbufferend)
2136         {
2137                 pchunk = (pskchunk_t *)animbuffer;
2138                 animbuffer = (void *)((unsigned char *)animbuffer + sizeof(pskchunk_t));
2139                 version = LittleLong(pchunk->version);
2140                 recordsize = LittleLong(pchunk->recordsize);
2141                 numrecords = LittleLong(pchunk->numrecords);
2142                 if (developer.integer >= 100)
2143                         Con_Printf("%s: %s %x: %i * %i = %i\n", animname, pchunk->id, version, recordsize, numrecords, recordsize * numrecords);
2144                 if (version != 0x1e83b9 && version != 0x1e9179 && version != 0x2e && version != 0x12f2bc && version != 0x12f2f0)
2145                         Con_Printf ("%s: chunk %s has unknown version %x (0x1e83b9, 0x1e9179, 0x2e, 0x12f2bc, 0x12f2f0 are currently supported), trying to load anyway!\n", animname, pchunk->id, version);
2146                 if (!strcmp(pchunk->id, "ANIMHEAD"))
2147                 {
2148                         // nothing to do
2149                 }
2150                 else if (!strcmp(pchunk->id, "BONENAMES"))
2151                 {
2152                         pskboneinfo_t *p;
2153                         if (recordsize != sizeof(*p))
2154                                 Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
2155                         // byteswap in place and keep the pointer
2156                         numanimbones = numrecords;
2157                         animbones = (pskboneinfo_t *)animbuffer;
2158                         // NOTE: supposedly psa does not need to match the psk model, the
2159                         // bones missing from the psa would simply use their base
2160                         // positions from the psk, but this is hard for me to implement
2161                         // and people can easily make animations that match.
2162                         if (numanimbones != numbones)
2163                                 Host_Error("%s: this loader only supports animations with the same bones as the mesh");
2164                         for (index = 0, p = (pskboneinfo_t *)animbuffer;index < numrecords;index++, p++)
2165                         {
2166                                 p->numchildren = LittleLong(p->numchildren);
2167                                 p->parent = LittleLong(p->parent);
2168                                 p->basepose.quat[0] = LittleFloat(p->basepose.quat[0]);
2169                                 p->basepose.quat[1] = LittleFloat(p->basepose.quat[1]);
2170                                 p->basepose.quat[2] = LittleFloat(p->basepose.quat[2]);
2171                                 p->basepose.quat[3] = LittleFloat(p->basepose.quat[3]);
2172                                 p->basepose.origin[0] = LittleFloat(p->basepose.origin[0]);
2173                                 p->basepose.origin[1] = LittleFloat(p->basepose.origin[1]);
2174                                 p->basepose.origin[2] = LittleFloat(p->basepose.origin[2]);
2175                                 p->basepose.unknown = LittleFloat(p->basepose.unknown);
2176                                 p->basepose.size[0] = LittleFloat(p->basepose.size[0]);
2177                                 p->basepose.size[1] = LittleFloat(p->basepose.size[1]);
2178                                 p->basepose.size[2] = LittleFloat(p->basepose.size[2]);
2179 #ifdef PSKQUATNEGATIONS
2180                                 if (index)
2181                                 {
2182                                         p->basepose.quat[0] *= -1;
2183                                         p->basepose.quat[1] *= -1;
2184                                         p->basepose.quat[2] *= -1;
2185                                 }
2186                                 else
2187                                 {
2188                                         p->basepose.quat[0] *=  1;
2189                                         p->basepose.quat[1] *= -1;
2190                                         p->basepose.quat[2] *=  1;
2191                                 }
2192 #endif
2193                                 if (p->parent < 0 || p->parent >= numanimbones)
2194                                 {
2195                                         Con_Printf("%s: bone->parent %i >= numanimbones %i\n", animname, p->parent, numanimbones);
2196                                         p->parent = 0;
2197                                 }
2198                                 // check that bones are the same as in the base
2199                                 if (strcmp(p->name, bones[index].name) || p->parent != bones[index].parent)
2200                                         Host_Error("%s: this loader only supports animations with the same bones as the mesh", animname);
2201                         }
2202                         animbuffer = p;
2203                 }
2204                 else if (!strcmp(pchunk->id, "ANIMINFO"))
2205                 {
2206                         pskaniminfo_t *p;
2207                         if (recordsize != sizeof(*p))
2208                                 Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
2209                         // byteswap in place and keep the pointer
2210                         numanims = numrecords;
2211                         anims = (pskaniminfo_t *)animbuffer;
2212                         for (index = 0, p = (pskaniminfo_t *)animbuffer;index < numrecords;index++, p++)
2213                         {
2214                                 p->numbones = LittleLong(p->numbones);
2215                                 p->playtime = LittleFloat(p->playtime);
2216                                 p->fps = LittleFloat(p->fps);
2217                                 p->firstframe = LittleLong(p->firstframe);
2218                                 p->numframes = LittleLong(p->numframes);
2219                                 if (p->numbones != numbones)
2220                                         Con_Printf("%s: animinfo->numbones != numbones, trying to load anyway!\n", animname);
2221                         }
2222                         animbuffer = p;
2223                 }
2224                 else if (!strcmp(pchunk->id, "ANIMKEYS"))
2225                 {
2226                         pskanimkeys_t *p;
2227                         if (recordsize != sizeof(*p))
2228                                 Host_Error("%s: %s has unsupported recordsize", animname, pchunk->id);
2229                         numanimkeys = numrecords;
2230                         animkeys = (pskanimkeys_t *)animbuffer;
2231                         for (index = 0, p = (pskanimkeys_t *)animbuffer;index < numrecords;index++, p++)
2232                         {
2233                                 p->origin[0] = LittleFloat(p->origin[0]);
2234                                 p->origin[1] = LittleFloat(p->origin[1]);
2235                                 p->origin[2] = LittleFloat(p->origin[2]);
2236                                 p->quat[0] = LittleFloat(p->quat[0]);
2237                                 p->quat[1] = LittleFloat(p->quat[1]);
2238                                 p->quat[2] = LittleFloat(p->quat[2]);
2239                                 p->quat[3] = LittleFloat(p->quat[3]);
2240                                 p->frametime = LittleFloat(p->frametime);
2241 #ifdef PSKQUATNEGATIONS
2242                                 if (index % numbones)
2243                                 {
2244                                         p->quat[0] *= -1;
2245                                         p->quat[1] *= -1;
2246                                         p->quat[2] *= -1;
2247                                 }
2248                                 else
2249                                 {
2250                                         p->quat[0] *=  1;
2251                                         p->quat[1] *= -1;
2252                                         p->quat[2] *=  1;
2253                                 }
2254 #endif
2255                         }
2256                         animbuffer = p;
2257                         // TODO: allocate bonepose stuff
2258                 }
2259                 else
2260                         Con_Printf("%s: unknown chunk ID \"%s\"\n", animname, pchunk->id);
2261         }
2262
2263         if (!numpnts || !pnts || !numvtxw || !vtxw || !numfaces || !faces || !nummatts || !matts || !numbones || !bones || !numrawweights || !rawweights || !numanims || !anims || !numanimkeys || !animkeys)
2264                 Host_Error("%s: missing required chunks", loadmodel->name);
2265
2266         loadmodel->numframes = 0;
2267         for (index = 0;index < numanims;index++)
2268                 loadmodel->numframes += anims[index].numframes;
2269
2270         if (numanimkeys != numbones * loadmodel->numframes)
2271                 Host_Error("%s: %s has incorrect number of animation keys", animname, pchunk->id);
2272
2273         meshvertices = numvtxw;
2274         meshtriangles = numfaces;
2275
2276         // load external .skin files if present
2277         skinfiles = Mod_LoadSkinFiles();
2278         if (loadmodel->numskins < 1)
2279                 loadmodel->numskins = 1;
2280         loadmodel->num_bones = numbones;
2281         loadmodel->num_poses = loadmodel->num_bones * loadmodel->numframes;
2282         loadmodel->num_textures = loadmodel->nummodelsurfaces = loadmodel->num_surfaces = nummatts;
2283         // do most allocations as one merged chunk
2284         data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * (sizeof(float[14]) + sizeof(int[4]) + sizeof(float[4])) + loadmodel->num_poses * sizeof(float[36]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t));
2285         loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t);
2286         loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int);
2287         loadmodel->data_textures = (texture_t *)data;data += loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t);
2288         loadmodel->surfmesh.num_vertices = meshvertices;
2289         loadmodel->surfmesh.num_triangles = meshtriangles;
2290         loadmodel->surfmesh.data_element3i = (int *)data;data += meshtriangles * sizeof(int[3]);
2291         loadmodel->surfmesh.data_neighbor3i = (int *)data;data += meshtriangles * sizeof(int[3]);
2292         loadmodel->surfmesh.data_vertex3f = (float *)data;data += meshvertices * sizeof(float[3]);
2293         loadmodel->surfmesh.data_svector3f = (float *)data;data += meshvertices * sizeof(float[3]);
2294         loadmodel->surfmesh.data_tvector3f = (float *)data;data += meshvertices * sizeof(float[3]);
2295         loadmodel->surfmesh.data_normal3f = (float *)data;data += meshvertices * sizeof(float[3]);
2296         loadmodel->surfmesh.data_texcoordtexture2f = (float *)data;data += meshvertices * sizeof(float[2]);
2297         loadmodel->surfmesh.data_vertexweightindex4i = (int *)data;data += meshvertices * sizeof(int[4]);
2298         loadmodel->surfmesh.data_vertexweightinfluence4f = (float *)data;data += meshvertices * sizeof(float[4]);
2299         loadmodel->data_poses = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
2300         loadmodel->data_basebonepose = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
2301         loadmodel->data_baseboneposeinverse = (float *)data;data += loadmodel->num_poses * sizeof(float[12]);
2302         loadmodel->skinscenes = (animscene_t *)data;data += loadmodel->numskins * sizeof(animscene_t);
2303         loadmodel->data_bones = (aliasbone_t *)data;data += loadmodel->num_bones * sizeof(aliasbone_t);
2304         loadmodel->animscenes = (animscene_t *)data;data += loadmodel->numframes * sizeof(animscene_t);
2305
2306         for (i = 0;i < loadmodel->numskins;i++)
2307         {
2308                 loadmodel->skinscenes[i].firstframe = i;
2309                 loadmodel->skinscenes[i].framecount = 1;
2310                 loadmodel->skinscenes[i].loop = true;
2311                 loadmodel->skinscenes[i].framerate = 10;
2312         }
2313
2314         // create surfaces
2315         for (index = 0, i = 0;index < nummatts;index++)
2316         {
2317                 // since psk models do not have named sections, reuse their shader name as the section name
2318                 if (matts[index].name[0])
2319                         Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures + index, skinfiles, matts[index].name, matts[index].name);
2320                 else
2321                         for (j = 0;j < loadmodel->numskins;j++)
2322                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures + index + j * loadmodel->num_surfaces, NULL);
2323                 loadmodel->surfacelist[index] = index;
2324                 loadmodel->data_surfaces[index].texture = loadmodel->data_textures + index;
2325                 loadmodel->data_surfaces[index].num_firstvertex = 0;
2326                 loadmodel->data_surfaces[index].num_vertices = loadmodel->surfmesh.num_vertices;
2327         }
2328
2329         // copy over the vertex locations and texcoords
2330         for (index = 0;index < numvtxw;index++)
2331         {
2332                 loadmodel->surfmesh.data_vertex3f[index*3+0] = pnts[vtxw[index].pntsindex].origin[0];
2333                 loadmodel->surfmesh.data_vertex3f[index*3+1] = pnts[vtxw[index].pntsindex].origin[1];
2334                 loadmodel->surfmesh.data_vertex3f[index*3+2] = pnts[vtxw[index].pntsindex].origin[2];
2335                 loadmodel->surfmesh.data_texcoordtexture2f[index*2+0] = vtxw[index].texcoord[0];
2336                 loadmodel->surfmesh.data_texcoordtexture2f[index*2+1] = vtxw[index].texcoord[1];
2337         }
2338
2339         // loading the faces is complicated because we need to sort them into surfaces by mattindex
2340         for (index = 0;index < numfaces;index++)
2341                 loadmodel->data_surfaces[faces[index].mattindex].num_triangles++;
2342         for (index = 0, i = 0;index < nummatts;index++)
2343         {
2344                 loadmodel->data_surfaces[index].num_firsttriangle = i;
2345                 i += loadmodel->data_surfaces[index].num_triangles;
2346                 loadmodel->data_surfaces[index].num_triangles = 0;
2347         }
2348         for (index = 0;index < numfaces;index++)
2349         {
2350                 i = (loadmodel->data_surfaces[faces[index].mattindex].num_firsttriangle + loadmodel->data_surfaces[faces[index].mattindex].num_triangles++)*3;
2351                 loadmodel->surfmesh.data_element3i[i+0] = faces[index].vtxwindex[0];
2352                 loadmodel->surfmesh.data_element3i[i+1] = faces[index].vtxwindex[1];
2353                 loadmodel->surfmesh.data_element3i[i+2] = faces[index].vtxwindex[2];
2354         }
2355
2356         // copy over the bones
2357         for (index = 0;index < numbones;index++)
2358         {
2359                 strlcpy(loadmodel->data_bones[index].name, bones[index].name, sizeof(loadmodel->data_bones[index].name));
2360                 loadmodel->data_bones[index].parent = (index || bones[index].parent > 0) ? bones[index].parent : -1;
2361                 if (loadmodel->data_bones[index].parent >= index)
2362                         Host_Error("%s bone[%i].parent >= %i", loadmodel->name, index, index);
2363         }
2364
2365         // sort the psk point weights into the vertex weight tables
2366         // (which only accept up to 4 bones per vertex)
2367         for (index = 0;index < numvtxw;index++)
2368         {
2369                 int l;
2370                 float sum;
2371                 for (j = 0;j < numrawweights;j++)
2372                 {
2373                         if (rawweights[j].pntsindex == vtxw[index].pntsindex)
2374                         {
2375                                 int boneindex = rawweights[j].boneindex;
2376                                 float influence = rawweights[j].weight;
2377                                 for (l = 0;l < 4;l++)
2378                                 {
2379                                         if (loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] < influence)
2380                                         {
2381                                                 // move lower influence weights out of the way first
2382                                                 int l2;
2383                                                 for (l2 = 3;l2 > l;l2--)
2384                                                 {
2385                                                         loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l2] = loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l2-1];
2386                                                         loadmodel->surfmesh.data_vertexweightindex4i[index*4+l2] = loadmodel->surfmesh.data_vertexweightindex4i[index*4+l2-1];
2387                                                 }
2388                                                 // store the new weight
2389                                                 loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] = influence;
2390                                                 loadmodel->surfmesh.data_vertexweightindex4i[index*4+l] = boneindex;
2391                                                 break;
2392                                         }
2393                                 }
2394                         }
2395                 }
2396                 sum = 0;
2397                 for (l = 0;l < 4;l++)
2398                         sum += loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l];
2399                 if (sum && fabs(sum - 1) > (1.0f / 256.0f))
2400                 {
2401                         float f = 1.0f / sum;
2402                         for (l = 0;l < 4;l++)
2403                                 loadmodel->surfmesh.data_vertexweightinfluence4f[index*4+l] *= f;
2404                 }
2405         }
2406
2407         // set up the animscenes based on the anims
2408         for (index = 0, i = 0;index < numanims;index++)
2409         {
2410                 for (j = 0;j < anims[index].numframes;j++, i++)
2411                 {
2412                         dpsnprintf(loadmodel->animscenes[i].name, sizeof(loadmodel->animscenes[i].name), "%s_%d", anims[index].name, j);
2413                         loadmodel->animscenes[i].firstframe = i;
2414                         loadmodel->animscenes[i].framecount = 1;
2415                         loadmodel->animscenes[i].loop = true;
2416                         loadmodel->animscenes[i].framerate = 10;
2417                 }
2418         }
2419
2420         // load the poses from the animkeys
2421         for (index = 0;index < numanimkeys;index++)
2422         {
2423                 matrix4x4_t matrix;
2424                 Mod_PSKMODEL_AnimKeyToMatrix(animkeys[index].origin, animkeys[index].quat, &matrix);
2425                 loadmodel->data_poses[index*12+0] = matrix.m[0][0];
2426                 loadmodel->data_poses[index*12+1] = matrix.m[0][1];
2427                 loadmodel->data_poses[index*12+2] = matrix.m[0][2];
2428                 loadmodel->data_poses[index*12+3] = matrix.m[0][3];
2429                 loadmodel->data_poses[index*12+4] = matrix.m[1][0];
2430                 loadmodel->data_poses[index*12+5] = matrix.m[1][1];
2431                 loadmodel->data_poses[index*12+6] = matrix.m[1][2];
2432                 loadmodel->data_poses[index*12+7] = matrix.m[1][3];
2433                 loadmodel->data_poses[index*12+8] = matrix.m[2][0];
2434                 loadmodel->data_poses[index*12+9] = matrix.m[2][1];
2435                 loadmodel->data_poses[index*12+10] = matrix.m[2][2];
2436                 loadmodel->data_poses[index*12+11] = matrix.m[2][3];
2437         }
2438         Mod_FreeSkinFiles(skinfiles);
2439         Mem_Free(animfilebuffer);
2440
2441         // compute all the mesh information that was not loaded from the file
2442         // TODO: honor smoothing groups somehow?
2443         Mod_ValidateElements(loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles, 0, loadmodel->surfmesh.num_vertices, __FILE__, __LINE__);
2444         Mod_BuildBaseBonePoses();
2445         Mod_BuildNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_normal3f, true);
2446         Mod_BuildTextureVectorsFromNormals(0, loadmodel->surfmesh.num_vertices, loadmodel->surfmesh.num_triangles, loadmodel->surfmesh.data_vertex3f, loadmodel->surfmesh.data_texcoordtexture2f, loadmodel->surfmesh.data_normal3f, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.data_svector3f, loadmodel->surfmesh.data_tvector3f, true);
2447         Mod_BuildTriangleNeighbors(loadmodel->surfmesh.data_neighbor3i, loadmodel->surfmesh.data_element3i, loadmodel->surfmesh.num_triangles);
2448         Mod_Alias_CalculateBoundingBox();
2449 }
2450