I hope this fixes a md3 tag bug, but don't know
[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_DrawFakeShadow(entity_render_t *ent);
271 extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
272 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);
273 void Mod_IDP0_Load(model_t *mod, void *buffer)
274 {
275         int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
276         float scales, scalet, scale[3], translate[3], interval;
277         mdl_t *pinmodel;
278         stvert_t *pinstverts;
279         dtriangle_t *pintriangles;
280         daliasskintype_t *pinskintype;
281         daliasskingroup_t *pinskingroup;
282         daliasskininterval_t *pinskinintervals;
283         daliasframetype_t *pinframetype;
284         daliasgroup_t *pinframegroup;
285         qbyte *datapointer, *startframes, *startskins;
286         char name[MAX_QPATH];
287         skinframe_t tempskinframe;
288         animscene_t *tempskinscenes;
289         aliasskin_t *tempaliasskins;
290         float *vertst;
291         int *vertonseam, *vertremap;
292         skinfile_t *skinfiles;
293
294         datapointer = buffer;
295         pinmodel = (mdl_t *)datapointer;
296         datapointer += sizeof(mdl_t);
297
298         version = LittleLong (pinmodel->version);
299         if (version != ALIAS_VERSION)
300                 Host_Error ("%s has wrong version number (%i should be %i)",
301                                  loadmodel->name, version, ALIAS_VERSION);
302
303         loadmodel->type = mod_alias;
304         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
305         loadmodel->DrawSky = NULL;
306         loadmodel->Draw = R_Model_Alias_Draw;
307         loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
308         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
309         loadmodel->DrawLight = R_Model_Alias_DrawLight;
310
311         loadmodel->alias.aliasnum_meshes = 1;
312         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
313
314         loadmodel->numskins = LittleLong(pinmodel->numskins);
315         BOUNDI(loadmodel->numskins,0,256);
316         skinwidth = LittleLong (pinmodel->skinwidth);
317         BOUNDI(skinwidth,0,4096);
318         skinheight = LittleLong (pinmodel->skinheight);
319         BOUNDI(skinheight,0,4096);
320         numverts = LittleLong(pinmodel->numverts);
321         BOUNDI(numverts,0,65536);
322         loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
323         BOUNDI(loadmodel->alias.aliasdata_meshes->num_triangles,0,65536 / 3); // max elements limit, rather than max triangles limit
324         loadmodel->numframes = LittleLong(pinmodel->numframes);
325         BOUNDI(loadmodel->numframes,0,65536);
326         loadmodel->synctype = LittleLong (pinmodel->synctype);
327         BOUNDI(loadmodel->synctype,0,2);
328         loadmodel->flags = LittleLong (pinmodel->flags);
329
330         for (i = 0;i < 3;i++)
331         {
332                 scale[i] = LittleFloat (pinmodel->scale[i]);
333                 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
334         }
335
336         startskins = datapointer;
337         totalskins = 0;
338         for (i = 0;i < loadmodel->numskins;i++)
339         {
340                 pinskintype = (daliasskintype_t *)datapointer;
341                 datapointer += sizeof(daliasskintype_t);
342                 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
343                         groupskins = 1;
344                 else
345                 {
346                         pinskingroup = (daliasskingroup_t *)datapointer;
347                         datapointer += sizeof(daliasskingroup_t);
348                         groupskins = LittleLong(pinskingroup->numskins);
349                         datapointer += sizeof(daliasskininterval_t) * groupskins;
350                 }
351
352                 for (j = 0;j < groupskins;j++)
353                 {
354                         datapointer += skinwidth * skinheight;
355                         totalskins++;
356                 }
357         }
358
359         pinstverts = (stvert_t *)datapointer;
360         datapointer += sizeof(stvert_t) * numverts;
361
362         pintriangles = (dtriangle_t *)datapointer;
363         datapointer += sizeof(dtriangle_t) * loadmodel->alias.aliasdata_meshes->num_triangles;
364
365         startframes = datapointer;
366         loadmodel->alias.aliasdata_meshes->num_frames = 0;
367         for (i = 0;i < loadmodel->numframes;i++)
368         {
369                 pinframetype = (daliasframetype_t *)datapointer;
370                 datapointer += sizeof(daliasframetype_t);
371                 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
372                         groupframes = 1;
373                 else
374                 {
375                         pinframegroup = (daliasgroup_t *)datapointer;
376                         datapointer += sizeof(daliasgroup_t);
377                         groupframes = LittleLong(pinframegroup->numframes);
378                         datapointer += sizeof(daliasinterval_t) * groupframes;
379                 }
380
381                 for (j = 0;j < groupframes;j++)
382                 {
383                         datapointer += sizeof(daliasframe_t);
384                         datapointer += sizeof(trivertx_t) * numverts;
385                         loadmodel->alias.aliasdata_meshes->num_frames++;
386                 }
387         }
388
389         // store texture coordinates into temporary array, they will be stored
390         // after usage is determined (triangle data)
391         vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
392         vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
393         vertonseam = vertremap + numverts * 2;
394
395         scales = 1.0 / skinwidth;
396         scalet = 1.0 / skinheight;
397         for (i = 0;i < numverts;i++)
398         {
399                 vertonseam[i] = LittleLong(pinstverts[i].onseam);
400                 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
401                 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
402                 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
403                 vertst[(i+numverts)*2+1] = vertst[i*2+1];
404         }
405
406 // load triangle data
407         loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->alias.aliasdata_meshes->num_triangles);
408
409         // read the triangle elements
410         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
411                 for (j = 0;j < 3;j++)
412                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
413         // validate (note numverts is used because this is the original data)
414         Mod_ValidateElements(loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
415         // now butcher the elements according to vertonseam and tri->facesfront
416         // and then compact the vertex set to remove duplicates
417         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
418                 if (!LittleLong(pintriangles[i].facesfront)) // backface
419                         for (j = 0;j < 3;j++)
420                                 if (vertonseam[loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j]])
421                                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] += numverts;
422         // count the usage
423         // (this uses vertremap to count usage to save some memory)
424         for (i = 0;i < numverts*2;i++)
425                 vertremap[i] = 0;
426         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles*3;i++)
427                 vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]]++;
428         // build remapping table and compact array
429         loadmodel->alias.aliasdata_meshes->num_vertices = 0;
430         for (i = 0;i < numverts*2;i++)
431         {
432                 if (vertremap[i])
433                 {
434                         vertremap[i] = loadmodel->alias.aliasdata_meshes->num_vertices;
435                         vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
436                         vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
437                         loadmodel->alias.aliasdata_meshes->num_vertices++;
438                 }
439                 else
440                         vertremap[i] = -1; // not used at all
441         }
442         // remap the elements to the new vertex set
443         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles * 3;i++)
444                 loadmodel->alias.aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]];
445         // store the texture coordinates
446         loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->alias.aliasdata_meshes->num_vertices);
447         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_vertices;i++)
448         {
449                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
450                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
451         }
452
453 // load the frames
454         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
455         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);
456         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;
457         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;
458         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;
459         Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
460         loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
461         Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
462         Mod_CalcAliasModelBBoxes();
463
464         Mem_Free(vertst);
465         Mem_Free(vertremap);
466
467         // load the skins
468         if ((skinfiles = Mod_LoadSkinFiles()))
469         {
470                 loadmodel->alias.aliasdata_meshes->num_skins = totalskins = loadmodel->numskins;
471                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
472                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
473                 Mod_FreeSkinFiles(skinfiles);
474                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
475                 for (i = 0;i < loadmodel->numskins;i++)
476                 {
477                         loadmodel->skinscenes[i].firstframe = i;
478                         loadmodel->skinscenes[i].framecount = 1;
479                         loadmodel->skinscenes[i].loop = true;
480                         loadmodel->skinscenes[i].framerate = 10;
481                 }
482         }
483         else
484         {
485                 loadmodel->alias.aliasdata_meshes->num_skins = totalskins;
486                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
487                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
488                 totalskins = 0;
489                 datapointer = startskins;
490                 for (i = 0;i < loadmodel->numskins;i++)
491                 {
492                         pinskintype = (daliasskintype_t *)datapointer;
493                         datapointer += sizeof(daliasskintype_t);
494
495                         if (pinskintype->type == ALIAS_SKIN_SINGLE)
496                         {
497                                 groupskins = 1;
498                                 interval = 0.1f;
499                         }
500                         else
501                         {
502                                 pinskingroup = (daliasskingroup_t *)datapointer;
503                                 datapointer += sizeof(daliasskingroup_t);
504
505                                 groupskins = LittleLong (pinskingroup->numskins);
506
507                                 pinskinintervals = (daliasskininterval_t *)datapointer;
508                                 datapointer += sizeof(daliasskininterval_t) * groupskins;
509
510                                 interval = LittleFloat(pinskinintervals[0].interval);
511                                 if (interval < 0.01f)
512                                         Host_Error("Mod_IDP0_Load: invalid interval\n");
513                         }
514
515                         sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
516                         loadmodel->skinscenes[i].firstframe = totalskins;
517                         loadmodel->skinscenes[i].framecount = groupskins;
518                         loadmodel->skinscenes[i].framerate = 1.0f / interval;
519                         loadmodel->skinscenes[i].loop = true;
520
521                         for (j = 0;j < groupskins;j++)
522                         {
523                                 if (groupskins > 1)
524                                         sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
525                                 else
526                                         sprintf (name, "%s_%i", loadmodel->name, i);
527                                 if (!Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
528                                         Mod_LoadSkinFrame_Internal(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true, (qbyte *)datapointer, skinwidth, skinheight);
529                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
530                                 datapointer += skinwidth * skinheight;
531                                 totalskins++;
532                         }
533                 }
534                 // check for skins that don't exist in the model, but do exist as external images
535                 // (this was added because yummyluv kept pestering me about support for it)
536                 while (Mod_LoadSkinFrame(&tempskinframe, va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
537                 {
538                         // expand the arrays to make room
539                         tempskinscenes = loadmodel->skinscenes;
540                         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
541                         memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
542                         Mem_Free(tempskinscenes);
543
544                         tempaliasskins = loadmodel->alias.aliasdata_meshes->data_skins;
545                         loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(aliasskin_t));
546                         memcpy(loadmodel->alias.aliasdata_meshes->data_skins, tempaliasskins, totalskins * sizeof(aliasskin_t));
547                         Mem_Free(tempaliasskins);
548
549                         // store the info about the new skin
550                         Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
551                         strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
552                         loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
553                         loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
554                         loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
555                         loadmodel->skinscenes[loadmodel->numskins].loop = true;
556
557                         //increase skin counts
558                         loadmodel->alias.aliasdata_meshes->num_skins++;
559                         loadmodel->numskins++;
560                         totalskins++;
561                 }
562         }
563 }
564
565 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
566 {
567         int i;
568         trivertx_t *in;
569         for (i = 0;i < numverts;i++, out3f += 3)
570         {
571                 in = v + vertremap[i];
572                 out3f[0] = in->v[0] * scale[0] + translate[0];
573                 out3f[1] = in->v[1] * scale[1] + translate[1];
574                 out3f[2] = in->v[2] * scale[2] + translate[2];
575         }
576 }
577
578 void Mod_IDP2_Load(model_t *mod, void *buffer)
579 {
580         int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
581         float *stverts, s, t, scale[3], translate[3];
582         md2_t *pinmodel;
583         qbyte *base, *datapointer;
584         md2frame_t *pinframe;
585         char *inskin;
586         md2triangle_t *intri;
587         unsigned short *inst;
588         struct md2verthash_s
589         {
590                 struct md2verthash_s *next;
591                 int xyz;
592                 float st[2];
593         }
594         *hash, **md2verthash, *md2verthashdata;
595         skinframe_t tempskinframe;
596         skinfile_t *skinfiles;
597
598         pinmodel = buffer;
599         base = buffer;
600
601         version = LittleLong (pinmodel->version);
602         if (version != MD2ALIAS_VERSION)
603                 Host_Error ("%s has wrong version number (%i should be %i)",
604                         loadmodel->name, version, MD2ALIAS_VERSION);
605
606         loadmodel->type = mod_alias;
607         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
608         loadmodel->DrawSky = NULL;
609         loadmodel->Draw = R_Model_Alias_Draw;
610         loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
611         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
612         loadmodel->DrawLight = R_Model_Alias_DrawLight;
613
614         if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > (65536 / 3))
615                 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
616         if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > 65536)
617                 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
618         if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > 65536)
619                 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
620         if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > 256)
621                 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
622
623         end = LittleLong(pinmodel->ofs_end);
624         if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
625                 Host_Error ("%s is not a valid model", loadmodel->name);
626         if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
627                 Host_Error ("%s is not a valid model", loadmodel->name);
628         if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
629                 Host_Error ("%s is not a valid model", loadmodel->name);
630         if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
631                 Host_Error ("%s is not a valid model", loadmodel->name);
632         if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
633                 Host_Error ("%s is not a valid model", loadmodel->name);
634
635         loadmodel->alias.aliasnum_meshes = 1;
636         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
637
638         loadmodel->numskins = LittleLong(pinmodel->num_skins);
639         numxyz = LittleLong(pinmodel->num_xyz);
640         numst = LittleLong(pinmodel->num_st);
641         loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
642         loadmodel->numframes = LittleLong(pinmodel->num_frames);
643         loadmodel->alias.aliasdata_meshes->num_frames = loadmodel->numframes;
644         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
645
646         loadmodel->flags = 0; // there are no MD2 flags
647         loadmodel->synctype = ST_RAND;
648
649         // load the skins
650         inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
651         if ((skinfiles = Mod_LoadSkinFiles()))
652         {
653                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
654                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
655                 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
656                 Mod_FreeSkinFiles(skinfiles);
657         }
658         else if (loadmodel->numskins)
659         {
660                 // skins found (most likely not a player model)
661                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
662                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
663                 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
664                 {
665                         if (Mod_LoadSkinFrame(&tempskinframe, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true))
666                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, &tempskinframe);
667                         else
668                         {
669                                 Con_Printf("Mod_IDP2_Load: missing skin \"%s\"\n", inskin);
670                                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, NULL);
671                         }
672                 }
673         }
674         else
675         {
676                 // no skins (most likely a player model)
677                 loadmodel->numskins = 1;
678                 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
679                 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
680                 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins, NULL);
681         }
682
683         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
684         for (i = 0;i < loadmodel->numskins;i++)
685         {
686                 loadmodel->skinscenes[i].firstframe = i;
687                 loadmodel->skinscenes[i].framecount = 1;
688                 loadmodel->skinscenes[i].loop = true;
689                 loadmodel->skinscenes[i].framerate = 10;
690         }
691
692         // load the triangles and stvert data
693         inst = (void*)(base + LittleLong(pinmodel->ofs_st));
694         intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
695         skinwidth = LittleLong(pinmodel->skinwidth);
696         skinheight = LittleLong(pinmodel->skinheight);
697
698         stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
699         s = 1.0f / skinwidth;
700         t = 1.0f / skinheight;
701         for (i = 0;i < numst;i++)
702         {
703                 j = (unsigned short) LittleShort(inst[i*2+0]);
704                 k = (unsigned short) LittleShort(inst[i*2+1]);
705                 if (j >= skinwidth || k >= skinheight)
706                 {
707                         Mem_Free(stverts);
708                         Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
709                 }
710                 stverts[i*2+0] = j * s;
711                 stverts[i*2+1] = k * t;
712         }
713
714         md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
715         md2verthashdata = Mem_Alloc(tempmempool, loadmodel->alias.aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
716         // swap the triangle list
717         num = 0;
718         loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
719         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
720         {
721                 for (j = 0;j < 3;j++)
722                 {
723                         xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
724                         st = (unsigned short) LittleShort (intri[i].index_st[j]);
725                         if (xyz >= numxyz || st >= numst)
726                         {
727                                 Mem_Free(md2verthash);
728                                 Mem_Free(md2verthashdata);
729                                 Mem_Free(stverts);
730                                 if (xyz >= numxyz)
731                                         Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
732                                 if (st >= numst)
733                                         Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
734                         }
735                         s = stverts[st*2+0];
736                         t = stverts[st*2+1];
737                         hashindex = (xyz * 17 + st) & 255;
738                         for (hash = md2verthash[hashindex];hash;hash = hash->next)
739                                 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
740                                         break;
741                         if (hash == NULL)
742                         {
743                                 hash = md2verthashdata + num++;
744                                 hash->xyz = xyz;
745                                 hash->st[0] = s;
746                                 hash->st[1] = t;
747                                 hash->next = md2verthash[hashindex];
748                                 md2verthash[hashindex] = hash;
749                         }
750                         loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
751                 }
752         }
753
754         Mem_Free(stverts);
755
756         numverts = num;
757         loadmodel->alias.aliasdata_meshes->num_vertices = numverts;
758         vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
759         loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
760         for (i = 0;i < num;i++)
761         {
762                 hash = md2verthashdata + i;
763                 vertremap[i] = hash->xyz;
764                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
765                 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
766         }
767
768         Mem_Free(md2verthash);
769         Mem_Free(md2verthashdata);
770
771         // load the frames
772         datapointer = (base + LittleLong(pinmodel->ofs_frames));
773         loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->alias.aliasdata_meshes->num_frames * sizeof(float[4][3]));
774         loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 1;
775         loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 2;
776         loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 3;
777         for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_frames;i++)
778         {
779                 pinframe = (md2frame_t *)datapointer;
780                 datapointer += sizeof(md2frame_t);
781                 for (j = 0;j < 3;j++)
782                 {
783                         scale[j] = LittleFloat(pinframe->scale[j]);
784                         translate[j] = LittleFloat(pinframe->translate[j]);
785                 }
786                 Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * numverts * 3, numverts, vertremap);
787                 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);
788                 datapointer += numxyz * sizeof(trivertx_t);
789
790                 strcpy(loadmodel->animscenes[i].name, pinframe->name);
791                 loadmodel->animscenes[i].firstframe = i;
792                 loadmodel->animscenes[i].framecount = 1;
793                 loadmodel->animscenes[i].framerate = 10;
794                 loadmodel->animscenes[i].loop = true;
795         }
796
797         Mem_Free(vertremap);
798
799         loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
800         Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
801         Mod_CalcAliasModelBBoxes();
802 }
803
804 void Mod_IDP3_Load(model_t *mod, void *buffer)
805 {
806         int i, j, k, version;
807         md3modelheader_t *pinmodel;
808         md3frameinfo_t *pinframe;
809         md3mesh_t *pinmesh;
810         md3tag_t *pintag;
811         aliasmesh_t *mesh;
812         skinfile_t *skinfiles;
813
814         pinmodel = buffer;
815
816         if (memcmp(pinmodel->identifier, "IDP3", 4))
817                 Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
818         version = LittleLong (pinmodel->version);
819         if (version != MD3VERSION)
820                 Host_Error ("%s has wrong version number (%i should be %i)",
821                         loadmodel->name, version, MD3VERSION);
822
823         skinfiles = Mod_LoadSkinFiles();
824         if (loadmodel->numskins < 1)
825                 loadmodel->numskins = 1;
826
827         loadmodel->type = mod_alias;
828         loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
829         loadmodel->DrawSky = NULL;
830         loadmodel->Draw = R_Model_Alias_Draw;
831         loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
832         loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
833         loadmodel->DrawLight = R_Model_Alias_DrawLight;
834         loadmodel->flags = 0;
835         loadmodel->synctype = ST_RAND;
836
837         // set up some global info about the model
838         loadmodel->numframes = LittleLong(pinmodel->num_frames);
839         loadmodel->alias.aliasnum_meshes = LittleLong(pinmodel->num_meshes);
840
841         // make skinscenes for the skins (no groups)
842         loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
843         for (i = 0;i < loadmodel->numskins;i++)
844         {
845                 loadmodel->skinscenes[i].firstframe = i;
846                 loadmodel->skinscenes[i].framecount = 1;
847                 loadmodel->skinscenes[i].loop = true;
848                 loadmodel->skinscenes[i].framerate = 10;
849         }
850
851         // load frameinfo
852         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
853         for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
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
862         // load tags
863         loadmodel->alias.aliasnum_tagframes = loadmodel->numframes;
864         loadmodel->alias.aliasnum_tags = LittleLong(pinmodel->num_tags);
865         loadmodel->alias.aliasdata_tags = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags * sizeof(aliastag_t));
866         for (i = 0, pintag = (md3tag_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_tags));i < loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags;i++, pintag++)
867         {
868                 strcpy(loadmodel->alias.aliasdata_tags[i].name, pintag->name);
869                 Matrix4x4_CreateIdentity(&loadmodel->alias.aliasdata_tags[i].matrix);
870                 for (j = 0;j < 3;j++)
871                 {
872                         for (k = 0;k < 3;k++)
873                                 loadmodel->alias.aliasdata_tags[i].matrix.m[j][k] = LittleFloat(pintag->rotationmatrix[k * 3 + j]);
874                         loadmodel->alias.aliasdata_tags[i].matrix.m[j][3] = LittleFloat(pintag->origin[j]);
875                 }
876                 //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);
877         }
878
879         // load meshes
880         loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
881         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)))
882         {
883                 if (memcmp(pinmesh->identifier, "IDP3", 4))
884                         Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)\n");
885                 mesh = loadmodel->alias.aliasdata_meshes + i;
886                 mesh->num_skins = loadmodel->numskins;
887                 mesh->num_frames = LittleLong(pinmesh->num_frames);
888                 mesh->num_vertices = LittleLong(pinmesh->num_vertices);
889                 mesh->num_triangles = LittleLong(pinmesh->num_triangles);
890                 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
891                 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
892                 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
893                 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
894                 mesh->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_frames * sizeof(float[4][3]));
895                 mesh->data_aliassvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 1;
896                 mesh->data_aliastvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 2;
897                 mesh->data_aliasnormal3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 3;
898                 for (j = 0;j < mesh->num_triangles * 3;j++)
899                         mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + pinmesh->lump_elements))[j]);
900                 for (j = 0;j < mesh->num_vertices;j++)
901                 {
902                         mesh->data_texcoord2f[j * 2 + 0] = LittleFloat(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 0]);
903                         mesh->data_texcoord2f[j * 2 + 1] = LittleFloat(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 1]);
904                 }
905                 for (j = 0;j < mesh->num_vertices * mesh->num_frames;j++)
906                 {
907                         mesh->data_aliasvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 0]) * (1.0f / 64.0f);
908                         mesh->data_aliasvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 1]) * (1.0f / 64.0f);
909                         mesh->data_aliasvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 2]) * (1.0f / 64.0f);
910                 }
911                 for (j = 0;j < mesh->num_frames;j++)
912                         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);
913
914                 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
915                 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
916
917                 if (LittleLong(pinmesh->num_shaders) >= 1 && ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name[0])
918                         Mod_BuildAliasSkinsFromSkinFiles(mesh->data_skins, skinfiles, pinmesh->name, ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name);
919                 else
920                         for (j = 0;j < mesh->num_skins;j++)
921                                 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins + j, NULL);
922         }
923         Mod_CalcAliasModelBBoxes();
924         Mod_FreeSkinFiles(skinfiles);
925 }
926
927 extern void R_Model_Zymotic_DrawSky(entity_render_t *ent);
928 extern void R_Model_Zymotic_Draw(entity_render_t *ent);
929 extern void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent);
930 extern void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
931 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);
932 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer)
933 {
934         zymtype1header_t *pinmodel, *pheader;
935         qbyte *pbase;
936
937         pinmodel = (void *)buffer;
938         pbase = buffer;
939         if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
940                 Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model\n");
941         if (BigLong(pinmodel->type) != 1)
942                 Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
943
944         loadmodel->type = mod_alias;
945         loadmodel->alias.aliastype = ALIASTYPE_ZYM;
946         loadmodel->DrawSky = NULL;
947         loadmodel->Draw = R_Model_Zymotic_Draw;
948         loadmodel->DrawFakeShadow = NULL;//R_Model_Zymotic_DrawFakeShadow;
949         loadmodel->DrawShadowVolume = NULL;//R_Model_Zymotic_DrawShadowVolume;
950         loadmodel->DrawLight = NULL;//R_Model_Zymotic_DrawLight;
951
952         // byteswap header
953         pheader = pinmodel;
954         pheader->type = BigLong(pinmodel->type);
955         pheader->filesize = BigLong(pinmodel->filesize);
956         pheader->mins[0] = BigFloat(pinmodel->mins[0]);
957         pheader->mins[1] = BigFloat(pinmodel->mins[1]);
958         pheader->mins[2] = BigFloat(pinmodel->mins[2]);
959         pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
960         pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
961         pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
962         pheader->radius = BigFloat(pinmodel->radius);
963         pheader->numverts = loadmodel->alias.zymnum_verts = BigLong(pinmodel->numverts);
964         pheader->numtris = loadmodel->alias.zymnum_tris = BigLong(pinmodel->numtris);
965         pheader->numshaders = loadmodel->alias.zymnum_shaders = BigLong(pinmodel->numshaders);
966         pheader->numbones = loadmodel->alias.zymnum_bones = BigLong(pinmodel->numbones);
967         pheader->numscenes = loadmodel->alias.zymnum_scenes = BigLong(pinmodel->numscenes);
968         pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
969         pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
970         pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
971         pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
972         pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
973         pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
974         pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
975         pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
976         pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
977         pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
978         pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
979         pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
980         pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
981         pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
982         pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
983         pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
984         pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
985         pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
986
987         loadmodel->flags = 0; // there are no flags
988         loadmodel->numframes = pheader->numscenes;
989         loadmodel->synctype = ST_SYNC;
990         //loadmodel->numtris = pheader->numtris;
991         //loadmodel->numverts = 0;
992
993         {
994                 unsigned int i;
995                 float modelradius, corner[2];
996                 // model bbox
997                 modelradius = pheader->radius;
998                 for (i = 0;i < 3;i++)
999                 {
1000                         loadmodel->normalmins[i] = pheader->mins[i];
1001                         loadmodel->normalmaxs[i] = pheader->maxs[i];
1002                         loadmodel->rotatedmins[i] = -modelradius;
1003                         loadmodel->rotatedmaxs[i] = modelradius;
1004                 }
1005                 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
1006                 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
1007                 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1008                 if (loadmodel->yawmaxs[0] > modelradius)
1009                         loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
1010                 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
1011                 loadmodel->yawmins[2] = loadmodel->normalmins[2];
1012                 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
1013                 loadmodel->radius = modelradius;
1014                 loadmodel->radius2 = modelradius * modelradius;
1015         }
1016
1017         {
1018                 // FIXME: add shaders, and make them switchable shader sets and...
1019                 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t));
1020                 loadmodel->skinscenes[0].firstframe = 0;
1021                 loadmodel->skinscenes[0].framecount = 1;
1022                 loadmodel->skinscenes[0].loop = true;
1023                 loadmodel->skinscenes[0].framerate = 10;
1024                 loadmodel->numskins = 1;
1025         }
1026
1027         // go through the lumps, swapping things
1028
1029         {
1030                 int i, numposes;
1031                 zymscene_t *scene;
1032         //      zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1033                 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
1034                 scene = (void *) (pheader->lump_scenes.start + pbase);
1035                 numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
1036                 for (i = 0;i < pheader->numscenes;i++)
1037                 {
1038                         memcpy(loadmodel->animscenes[i].name, scene->name, 32);
1039                         loadmodel->animscenes[i].firstframe = BigLong(scene->start);
1040                         loadmodel->animscenes[i].framecount = BigLong(scene->length);
1041                         loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
1042                         loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
1043                         if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
1044                                 Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
1045                         if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
1046                                 Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
1047                         if (loadmodel->animscenes[i].framerate < 0)
1048                                 Host_Error("Mod_ZYMOTICMODEL_Load: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
1049                         scene++;
1050                 }
1051         }
1052
1053         {
1054                 int i;
1055                 float *poses;
1056         //      zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
1057                 loadmodel->alias.zymdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
1058                 poses = (void *) (pheader->lump_poses.start + pbase);
1059                 for (i = 0;i < pheader->lump_poses.length / 4;i++)
1060                         loadmodel->alias.zymdata_poses[i] = BigFloat(poses[i]);
1061         }
1062
1063         {
1064                 int i;
1065                 zymbone_t *bone;
1066         //      zymlump_t lump_bones; // zymbone_t bone[numbones];
1067                 loadmodel->alias.zymdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(zymbone_t));
1068                 bone = (void *) (pheader->lump_bones.start + pbase);
1069                 for (i = 0;i < pheader->numbones;i++)
1070                 {
1071                         memcpy(loadmodel->alias.zymdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
1072                         loadmodel->alias.zymdata_bones[i].flags = BigLong(bone[i].flags);
1073                         loadmodel->alias.zymdata_bones[i].parent = BigLong(bone[i].parent);
1074                         if (loadmodel->alias.zymdata_bones[i].parent >= i)
1075                                 Host_Error("Mod_ZYMOTICMODEL_Load: bone[%i].parent >= %i in %s\n", i, i, loadmodel->name);
1076                 }
1077         }
1078
1079         {
1080                 int i, *bonecount;
1081         //      zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1082                 loadmodel->alias.zymdata_vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1083                 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
1084                 for (i = 0;i < pheader->numverts;i++)
1085                 {
1086                         loadmodel->alias.zymdata_vertbonecounts[i] = BigLong(bonecount[i]);
1087                         if (loadmodel->alias.zymdata_vertbonecounts[i] < 1)
1088                                 Host_Error("Mod_ZYMOTICMODEL_Load: bone vertex count < 1 in %s\n", loadmodel->name);
1089                 }
1090         }
1091
1092         {
1093                 int i;
1094                 zymvertex_t *vertdata;
1095         //      zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1096                 loadmodel->alias.zymdata_verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
1097                 vertdata = (void *) (pheader->lump_verts.start + pbase);
1098                 for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
1099                 {
1100                         loadmodel->alias.zymdata_verts[i].bonenum = BigLong(vertdata[i].bonenum);
1101                         loadmodel->alias.zymdata_verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
1102                         loadmodel->alias.zymdata_verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
1103                         loadmodel->alias.zymdata_verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
1104                 }
1105         }
1106
1107         {
1108                 int i;
1109                 float *intexcoord2f, *outtexcoord2f;
1110         //      zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1111                 loadmodel->alias.zymdata_texcoords = outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
1112                 intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
1113                 for (i = 0;i < pheader->numverts;i++)
1114                 {
1115                         outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1116                         // flip T coordinate for OpenGL
1117                         outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1118                 }
1119         }
1120
1121         {
1122                 int i, count, *renderlist, *renderlistend, *outrenderlist;
1123         //      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)
1124                 loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, pheader->lump_render.length);
1125                 // byteswap, validate, and swap winding order of tris
1126                 count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1127                 if (pheader->lump_render.length != count)
1128                         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);
1129                 outrenderlist = loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, count);
1130                 renderlist = (void *) (pheader->lump_render.start + pbase);
1131                 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
1132                 for (i = 0;i < pheader->numshaders;i++)
1133                 {
1134                         if (renderlist >= renderlistend)
1135                                 Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1136                         count = BigLong(*renderlist);renderlist++;
1137                         if (renderlist + count * 3 > renderlistend)
1138                                 Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1139                         *outrenderlist++ = count;
1140                         while (count--)
1141                         {
1142                                 outrenderlist[2] = BigLong(renderlist[0]);
1143                                 outrenderlist[1] = BigLong(renderlist[1]);
1144                                 outrenderlist[0] = BigLong(renderlist[2]);
1145                                 if ((unsigned int)outrenderlist[0] >= (unsigned int)pheader->numverts
1146                                  || (unsigned int)outrenderlist[1] >= (unsigned int)pheader->numverts
1147                                  || (unsigned int)outrenderlist[2] >= (unsigned int)pheader->numverts)
1148                                         Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
1149                                 renderlist += 3;
1150                                 outrenderlist += 3;
1151                         }
1152                 }
1153         }
1154
1155         {
1156                 int i;
1157                 char *shadername;
1158         //      zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1159                 loadmodel->alias.zymdata_textures = Mem_Alloc(loadmodel->mempool, pheader->numshaders * sizeof(rtexture_t *));
1160                 shadername = (void *) (pheader->lump_shaders.start + pbase);
1161                 for (i = 0;i < pheader->numshaders;i++)
1162                         loadmodel->alias.zymdata_textures[i] = loadtextureimage(loadmodel->texturepool, shadername + i * 32, 0, 0, true, TEXF_ALPHA | TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0));
1163         }
1164
1165         {
1166         //      zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1167                 loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1168                 memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);
1169         }
1170 }
1171