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