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