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