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