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