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