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