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