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