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