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