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