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