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