2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
25 void Mod_AliasInit (void)
29 static void Mod_CalcAliasModelBBoxes (void)
32 float dist, yawradius, radius;
35 VectorClear(loadmodel->normalmins);
36 VectorClear(loadmodel->normalmaxs);
39 for (meshnum = 0, mesh = loadmodel->alias.aliasdata_meshes;meshnum < loadmodel->alias.aliasnum_meshes;meshnum++, mesh++)
41 for (vnum = 0, v = mesh->data_aliasvertex3f;vnum < mesh->num_vertices * mesh->num_frames;vnum++, v += 3)
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];
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;
69 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
73 for (i = 0;i < inverts;i++)
75 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
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
82 VectorCopy(temp, out3f + j * 3);
83 j = vertremap[i+inverts]; // onseam
85 VectorCopy(temp, out3f + j * 3);
89 static void Mod_MDL_LoadFrames (qbyte* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
91 int i, f, pose, groupframes;
93 daliasframetype_t *pframetype;
94 daliasframe_t *pinframe;
96 daliasinterval_t *intervals;
99 scene = loadmodel->animscenes;
100 for (f = 0;f < loadmodel->numframes;f++)
102 pframetype = (daliasframetype_t *)datapointer;
103 datapointer += sizeof(daliasframetype_t);
104 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
106 // a single frame is still treated as a group
113 group = (daliasgroup_t *)datapointer;
114 datapointer += sizeof(daliasgroup_t);
115 groupframes = LittleLong (group->numframes);
117 // intervals (time per frame)
118 intervals = (daliasinterval_t *)datapointer;
119 datapointer += sizeof(daliasinterval_t) * groupframes;
121 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
122 if (interval < 0.01f)
123 Host_Error("Mod_MDL_LoadFrames: invalid interval");
126 // get scene name from first frame
127 pinframe = (daliasframe_t *)datapointer;
129 strcpy(scene->name, pinframe->name);
130 scene->firstframe = pose;
131 scene->framecount = groupframes;
132 scene->framerate = 1.0f / interval;
137 for (i = 0;i < groupframes;i++)
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;
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)
156 if (skinframe == NULL)
158 skinframe = &missingskinframe;
159 memset(skinframe, 0, sizeof(*skinframe));
160 skinframe->base = r_notexture;
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;
169 if (skinframe->merged != NULL)
171 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_COLORMAPPED;
172 layer->texture = skinframe->merged;
173 layer->nmap = skinframe->nmap;
176 if (skinframe->base != NULL)
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;
185 if (skinframe->pants != NULL)
187 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_PANTS;
188 layer->texture = skinframe->pants;
189 layer->nmap = skinframe->nmap;
192 if (skinframe->shirt != NULL)
194 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_SHIRT;
195 layer->texture = skinframe->shirt;
196 layer->nmap = skinframe->nmap;
200 if (skinframe->glow != NULL)
203 layer->texture = skinframe->glow;
207 layer->flags = ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS;
208 layer->texture = skinframe->fog;
212 // fog texture only exists if some pixels are transparent...
213 if (skinframe->fog != NULL)
214 skin->flags |= ALIASSKIN_TRANSPARENT;
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));
221 void Mod_BuildAliasSkinsFromSkinFiles(aliasskin_t *skin, skinfile_t *skinfile, char *meshname, char *shadername)
224 skinfileitem_t *skinfileitem;
225 skinframe_t tempskinframe;
228 for (i = 0;skinfile;skinfile = skinfile->next, i++, skin++)
230 memset(skin, 0, sizeof(*skin));
231 for (skinfileitem = skinfile->items;skinfileitem;skinfileitem = skinfileitem->next)
233 if (!strcmp(skinfileitem->name, meshname))
235 if (!strcmp(skinfileitem->replacement, "common/nodraw"))
240 memset(&tempskinframe, 0, sizeof(tempskinframe));
241 if (Mod_LoadSkinFrame(&tempskinframe, skinfileitem->replacement, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true))
242 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
245 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);
246 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true))
247 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
250 Con_Printf("failed to load skin \"%s\"\n", shadername);
251 Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
261 memset(&tempskinframe, 0, sizeof(tempskinframe));
262 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true))
263 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
266 Con_Printf("failed to load mesh \"%s\" shader \"%s\"\n", meshname, shadername);
267 Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
272 #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);
273 #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);
274 extern void R_Model_Alias_Draw(entity_render_t *ent);
275 extern void R_Model_Alias_DrawFakeShadow(entity_render_t *ent);
276 extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
277 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);
278 void Mod_IDP0_Load(model_t *mod, void *buffer)
280 int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
281 float scales, scalet, scale[3], translate[3], interval;
283 stvert_t *pinstverts;
284 dtriangle_t *pintriangles;
285 daliasskintype_t *pinskintype;
286 daliasskingroup_t *pinskingroup;
287 daliasskininterval_t *pinskinintervals;
288 daliasframetype_t *pinframetype;
289 daliasgroup_t *pinframegroup;
290 qbyte *datapointer, *startframes, *startskins;
291 char name[MAX_QPATH];
292 skinframe_t tempskinframe;
293 animscene_t *tempskinscenes;
294 aliasskin_t *tempaliasskins;
296 int *vertonseam, *vertremap;
297 skinfile_t *skinfiles;
299 datapointer = buffer;
300 pinmodel = (mdl_t *)datapointer;
301 datapointer += sizeof(mdl_t);
303 version = LittleLong (pinmodel->version);
304 if (version != ALIAS_VERSION)
305 Host_Error ("%s has wrong version number (%i should be %i)",
306 loadmodel->name, version, ALIAS_VERSION);
308 loadmodel->type = mod_alias;
309 loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
310 loadmodel->DrawSky = NULL;
311 loadmodel->Draw = R_Model_Alias_Draw;
312 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
313 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
314 loadmodel->DrawLight = R_Model_Alias_DrawLight;
316 loadmodel->alias.aliasnum_meshes = 1;
317 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
319 loadmodel->numskins = LittleLong(pinmodel->numskins);
320 BOUNDI(loadmodel->numskins,0,256);
321 skinwidth = LittleLong (pinmodel->skinwidth);
322 BOUNDI(skinwidth,0,4096);
323 skinheight = LittleLong (pinmodel->skinheight);
324 BOUNDI(skinheight,0,4096);
325 numverts = LittleLong(pinmodel->numverts);
326 BOUNDI(numverts,0,65536);
327 loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
328 BOUNDI(loadmodel->alias.aliasdata_meshes->num_triangles,0,65536 / 3); // max elements limit, rather than max triangles limit
329 loadmodel->numframes = LittleLong(pinmodel->numframes);
330 BOUNDI(loadmodel->numframes,0,65536);
331 loadmodel->synctype = LittleLong (pinmodel->synctype);
332 BOUNDI(loadmodel->synctype,0,2);
333 loadmodel->flags = LittleLong (pinmodel->flags);
335 for (i = 0;i < 3;i++)
337 scale[i] = LittleFloat (pinmodel->scale[i]);
338 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
341 startskins = datapointer;
343 for (i = 0;i < loadmodel->numskins;i++)
345 pinskintype = (daliasskintype_t *)datapointer;
346 datapointer += sizeof(daliasskintype_t);
347 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
351 pinskingroup = (daliasskingroup_t *)datapointer;
352 datapointer += sizeof(daliasskingroup_t);
353 groupskins = LittleLong(pinskingroup->numskins);
354 datapointer += sizeof(daliasskininterval_t) * groupskins;
357 for (j = 0;j < groupskins;j++)
359 datapointer += skinwidth * skinheight;
364 pinstverts = (stvert_t *)datapointer;
365 datapointer += sizeof(stvert_t) * numverts;
367 pintriangles = (dtriangle_t *)datapointer;
368 datapointer += sizeof(dtriangle_t) * loadmodel->alias.aliasdata_meshes->num_triangles;
370 startframes = datapointer;
371 loadmodel->alias.aliasdata_meshes->num_frames = 0;
372 for (i = 0;i < loadmodel->numframes;i++)
374 pinframetype = (daliasframetype_t *)datapointer;
375 datapointer += sizeof(daliasframetype_t);
376 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
380 pinframegroup = (daliasgroup_t *)datapointer;
381 datapointer += sizeof(daliasgroup_t);
382 groupframes = LittleLong(pinframegroup->numframes);
383 datapointer += sizeof(daliasinterval_t) * groupframes;
386 for (j = 0;j < groupframes;j++)
388 datapointer += sizeof(daliasframe_t);
389 datapointer += sizeof(trivertx_t) * numverts;
390 loadmodel->alias.aliasdata_meshes->num_frames++;
394 // store texture coordinates into temporary array, they will be stored
395 // after usage is determined (triangle data)
396 vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
397 vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
398 vertonseam = vertremap + numverts * 2;
400 scales = 1.0 / skinwidth;
401 scalet = 1.0 / skinheight;
402 for (i = 0;i < numverts;i++)
404 vertonseam[i] = LittleLong(pinstverts[i].onseam);
405 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
406 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
407 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
408 vertst[(i+numverts)*2+1] = vertst[i*2+1];
411 // load triangle data
412 loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->alias.aliasdata_meshes->num_triangles);
414 // read the triangle elements
415 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
416 for (j = 0;j < 3;j++)
417 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
418 // validate (note numverts is used because this is the original data)
419 Mod_ValidateElements(loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
420 // now butcher the elements according to vertonseam and tri->facesfront
421 // and then compact the vertex set to remove duplicates
422 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
423 if (!LittleLong(pintriangles[i].facesfront)) // backface
424 for (j = 0;j < 3;j++)
425 if (vertonseam[loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j]])
426 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] += numverts;
428 // (this uses vertremap to count usage to save some memory)
429 for (i = 0;i < numverts*2;i++)
431 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles*3;i++)
432 vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]]++;
433 // build remapping table and compact array
434 loadmodel->alias.aliasdata_meshes->num_vertices = 0;
435 for (i = 0;i < numverts*2;i++)
439 vertremap[i] = loadmodel->alias.aliasdata_meshes->num_vertices;
440 vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
441 vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
442 loadmodel->alias.aliasdata_meshes->num_vertices++;
445 vertremap[i] = -1; // not used at all
447 // remap the elements to the new vertex set
448 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles * 3;i++)
449 loadmodel->alias.aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]];
450 // store the texture coordinates
451 loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->alias.aliasdata_meshes->num_vertices);
452 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_vertices;i++)
454 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
455 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
459 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
460 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);
461 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;
462 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;
463 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;
464 Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
465 loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
466 Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
467 Mod_CalcAliasModelBBoxes();
473 if ((skinfiles = Mod_LoadSkinFiles()))
475 loadmodel->alias.aliasdata_meshes->num_skins = totalskins = loadmodel->numskins = Mod_CountSkinFiles(skinfiles);
476 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
477 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
478 Mod_FreeSkinFiles(skinfiles);
479 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
480 for (i = 0;i < loadmodel->numskins;i++)
482 loadmodel->skinscenes[i].firstframe = i;
483 loadmodel->skinscenes[i].framecount = 1;
484 loadmodel->skinscenes[i].loop = true;
485 loadmodel->skinscenes[i].framerate = 10;
490 loadmodel->alias.aliasdata_meshes->num_skins = totalskins;
491 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
492 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
494 datapointer = startskins;
495 for (i = 0;i < loadmodel->numskins;i++)
497 pinskintype = (daliasskintype_t *)datapointer;
498 datapointer += sizeof(daliasskintype_t);
500 if (pinskintype->type == ALIAS_SKIN_SINGLE)
507 pinskingroup = (daliasskingroup_t *)datapointer;
508 datapointer += sizeof(daliasskingroup_t);
510 groupskins = LittleLong (pinskingroup->numskins);
512 pinskinintervals = (daliasskininterval_t *)datapointer;
513 datapointer += sizeof(daliasskininterval_t) * groupskins;
515 interval = LittleFloat(pinskinintervals[0].interval);
516 if (interval < 0.01f)
517 Host_Error("Mod_IDP0_Load: invalid interval\n");
520 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
521 loadmodel->skinscenes[i].firstframe = totalskins;
522 loadmodel->skinscenes[i].framecount = groupskins;
523 loadmodel->skinscenes[i].framerate = 1.0f / interval;
524 loadmodel->skinscenes[i].loop = true;
526 for (j = 0;j < groupskins;j++)
529 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
531 sprintf (name, "%s_%i", loadmodel->name, i);
532 if (!Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
533 Mod_LoadSkinFrame_Internal(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true, (qbyte *)datapointer, skinwidth, skinheight);
534 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
535 datapointer += skinwidth * skinheight;
539 // check for skins that don't exist in the model, but do exist as external images
540 // (this was added because yummyluv kept pestering me about support for it)
541 while (Mod_LoadSkinFrame(&tempskinframe, va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
543 // expand the arrays to make room
544 tempskinscenes = loadmodel->skinscenes;
545 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
546 memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
547 Mem_Free(tempskinscenes);
549 tempaliasskins = loadmodel->alias.aliasdata_meshes->data_skins;
550 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(aliasskin_t));
551 memcpy(loadmodel->alias.aliasdata_meshes->data_skins, tempaliasskins, totalskins * sizeof(aliasskin_t));
552 Mem_Free(tempaliasskins);
554 // store the info about the new skin
555 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
556 strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
557 loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
558 loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
559 loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
560 loadmodel->skinscenes[loadmodel->numskins].loop = true;
562 //increase skin counts
563 loadmodel->alias.aliasdata_meshes->num_skins++;
564 loadmodel->numskins++;
570 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
574 for (i = 0;i < numverts;i++, out3f += 3)
576 in = v + vertremap[i];
577 out3f[0] = in->v[0] * scale[0] + translate[0];
578 out3f[1] = in->v[1] * scale[1] + translate[1];
579 out3f[2] = in->v[2] * scale[2] + translate[2];
583 void Mod_IDP2_Load(model_t *mod, void *buffer)
585 int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
586 float *stverts, s, t, scale[3], translate[3];
588 qbyte *base, *datapointer;
589 md2frame_t *pinframe;
591 md2triangle_t *intri;
592 unsigned short *inst;
595 struct md2verthash_s *next;
599 *hash, **md2verthash, *md2verthashdata;
600 skinframe_t tempskinframe;
601 skinfile_t *skinfiles;
606 version = LittleLong (pinmodel->version);
607 if (version != MD2ALIAS_VERSION)
608 Host_Error ("%s has wrong version number (%i should be %i)",
609 loadmodel->name, version, MD2ALIAS_VERSION);
611 loadmodel->type = mod_alias;
612 loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
613 loadmodel->DrawSky = NULL;
614 loadmodel->Draw = R_Model_Alias_Draw;
615 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
616 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
617 loadmodel->DrawLight = R_Model_Alias_DrawLight;
619 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > (65536 / 3))
620 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
621 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > 65536)
622 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
623 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > 65536)
624 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
625 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > 256)
626 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
628 end = LittleLong(pinmodel->ofs_end);
629 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
630 Host_Error ("%s is not a valid model", loadmodel->name);
631 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
632 Host_Error ("%s is not a valid model", loadmodel->name);
633 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
634 Host_Error ("%s is not a valid model", loadmodel->name);
635 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
636 Host_Error ("%s is not a valid model", loadmodel->name);
637 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
638 Host_Error ("%s is not a valid model", loadmodel->name);
640 loadmodel->alias.aliasnum_meshes = 1;
641 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
643 loadmodel->numskins = LittleLong(pinmodel->num_skins);
644 numxyz = LittleLong(pinmodel->num_xyz);
645 numst = LittleLong(pinmodel->num_st);
646 loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
647 loadmodel->numframes = LittleLong(pinmodel->num_frames);
648 loadmodel->alias.aliasdata_meshes->num_frames = loadmodel->numframes;
649 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
651 loadmodel->flags = 0; // there are no MD2 flags
652 loadmodel->synctype = ST_RAND;
655 inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
656 if ((skinfiles = Mod_LoadSkinFiles()))
658 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins = Mod_CountSkinFiles(skinfiles);
659 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
660 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
661 Mod_FreeSkinFiles(skinfiles);
663 else if (loadmodel->numskins)
665 // skins found (most likely not a player model)
666 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
667 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
668 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
670 if (Mod_LoadSkinFrame(&tempskinframe, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true))
671 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, &tempskinframe);
674 Con_Printf("Mod_IDP2_Load: missing skin \"%s\"\n", inskin);
675 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, NULL);
681 // no skins (most likely a player model)
682 loadmodel->numskins = 1;
683 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
684 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
685 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins, NULL);
688 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
689 for (i = 0;i < loadmodel->numskins;i++)
691 loadmodel->skinscenes[i].firstframe = i;
692 loadmodel->skinscenes[i].framecount = 1;
693 loadmodel->skinscenes[i].loop = true;
694 loadmodel->skinscenes[i].framerate = 10;
697 // load the triangles and stvert data
698 inst = (void*)(base + LittleLong(pinmodel->ofs_st));
699 intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
700 skinwidth = LittleLong(pinmodel->skinwidth);
701 skinheight = LittleLong(pinmodel->skinheight);
703 stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
704 s = 1.0f / skinwidth;
705 t = 1.0f / skinheight;
706 for (i = 0;i < numst;i++)
708 j = (unsigned short) LittleShort(inst[i*2+0]);
709 k = (unsigned short) LittleShort(inst[i*2+1]);
710 if (j >= skinwidth || k >= skinheight)
713 Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
715 stverts[i*2+0] = j * s;
716 stverts[i*2+1] = k * t;
719 md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
720 md2verthashdata = Mem_Alloc(tempmempool, loadmodel->alias.aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
721 // swap the triangle list
723 loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
724 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
726 for (j = 0;j < 3;j++)
728 xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
729 st = (unsigned short) LittleShort (intri[i].index_st[j]);
730 if (xyz >= numxyz || st >= numst)
732 Mem_Free(md2verthash);
733 Mem_Free(md2verthashdata);
736 Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
738 Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
742 hashindex = (xyz * 17 + st) & 255;
743 for (hash = md2verthash[hashindex];hash;hash = hash->next)
744 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
748 hash = md2verthashdata + num++;
752 hash->next = md2verthash[hashindex];
753 md2verthash[hashindex] = hash;
755 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
762 loadmodel->alias.aliasdata_meshes->num_vertices = numverts;
763 vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
764 loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
765 for (i = 0;i < num;i++)
767 hash = md2verthashdata + i;
768 vertremap[i] = hash->xyz;
769 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
770 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
773 Mem_Free(md2verthash);
774 Mem_Free(md2verthashdata);
777 datapointer = (base + LittleLong(pinmodel->ofs_frames));
778 loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->alias.aliasdata_meshes->num_frames * sizeof(float[4][3]));
779 loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 1;
780 loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 2;
781 loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 3;
782 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_frames;i++)
784 pinframe = (md2frame_t *)datapointer;
785 datapointer += sizeof(md2frame_t);
786 for (j = 0;j < 3;j++)
788 scale[j] = LittleFloat(pinframe->scale[j]);
789 translate[j] = LittleFloat(pinframe->translate[j]);
791 Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * numverts * 3, numverts, vertremap);
792 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);
793 datapointer += numxyz * sizeof(trivertx_t);
795 strcpy(loadmodel->animscenes[i].name, pinframe->name);
796 loadmodel->animscenes[i].firstframe = i;
797 loadmodel->animscenes[i].framecount = 1;
798 loadmodel->animscenes[i].framerate = 10;
799 loadmodel->animscenes[i].loop = true;
804 loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
805 Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
806 Mod_CalcAliasModelBBoxes();
809 void Mod_IDP3_Load(model_t *mod, void *buffer)
811 int i, j, k, version;
812 md3modelheader_t *pinmodel;
813 md3frameinfo_t *pinframe;
817 skinfile_t *skinfiles;
821 if (memcmp(pinmodel->identifier, "IDP3", 4))
822 Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
823 version = LittleLong (pinmodel->version);
824 if (version != MD3VERSION)
825 Host_Error ("%s has wrong version number (%i should be %i)",
826 loadmodel->name, version, MD3VERSION);
828 skinfiles = Mod_LoadSkinFiles();
829 loadmodel->numskins = Mod_CountSkinFiles(skinfiles);
830 if (loadmodel->numskins < 1)
831 loadmodel->numskins = 1;
833 loadmodel->type = mod_alias;
834 loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
835 loadmodel->DrawSky = NULL;
836 loadmodel->Draw = R_Model_Alias_Draw;
837 loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
838 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
839 loadmodel->DrawLight = R_Model_Alias_DrawLight;
840 loadmodel->flags = 0;
841 loadmodel->synctype = ST_RAND;
843 // set up some global info about the model
844 loadmodel->numframes = LittleLong(pinmodel->num_frames);
845 loadmodel->alias.aliasnum_meshes = LittleLong(pinmodel->num_meshes);
847 // make skinscenes for the skins (no groups)
848 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
849 for (i = 0;i < loadmodel->numskins;i++)
851 loadmodel->skinscenes[i].firstframe = i;
852 loadmodel->skinscenes[i].framecount = 1;
853 loadmodel->skinscenes[i].loop = true;
854 loadmodel->skinscenes[i].framerate = 10;
858 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
859 for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
861 strcpy(loadmodel->animscenes[i].name, pinframe->name);
862 loadmodel->animscenes[i].firstframe = i;
863 loadmodel->animscenes[i].framecount = 1;
864 loadmodel->animscenes[i].framerate = 10;
865 loadmodel->animscenes[i].loop = true;
869 loadmodel->alias.aliasnum_tagframes = loadmodel->numframes;
870 loadmodel->alias.aliasnum_tags = LittleLong(pinmodel->num_tags);
871 loadmodel->alias.aliasdata_tags = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags * sizeof(aliastag_t));
872 for (i = 0, pintag = (md3tag_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_tags));i < loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags;i++, pinframe++)
874 strcpy(loadmodel->alias.aliasdata_tags[i].name, pintag->name);
875 Matrix4x4_CreateIdentity(&loadmodel->alias.aliasdata_tags[i].matrix);
876 for (j = 0;j < 3;j++)
878 for (k = 0;k < 3;k++)
879 loadmodel->alias.aliasdata_tags[i].matrix.m[j][k] = LittleLong(pintag->rotationmatrix[j * 3 + k]);
880 loadmodel->alias.aliasdata_tags[i].matrix.m[j][3] = LittleLong(pintag->origin[j]);
885 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
886 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)))
888 if (memcmp(pinmesh->identifier, "IDP3", 4))
889 Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)\n");
890 mesh = loadmodel->alias.aliasdata_meshes + i;
891 mesh->num_skins = loadmodel->numskins;
892 mesh->num_frames = LittleLong(pinmesh->num_frames);
893 mesh->num_vertices = LittleLong(pinmesh->num_vertices);
894 mesh->num_triangles = LittleLong(pinmesh->num_triangles);
895 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
896 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
897 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
898 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
899 mesh->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_frames * sizeof(float[4][3]));
900 mesh->data_aliassvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 1;
901 mesh->data_aliastvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 2;
902 mesh->data_aliasnormal3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 3;
903 for (j = 0;j < mesh->num_triangles * 3;j++)
904 mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + pinmesh->lump_elements))[j]);
905 for (j = 0;j < mesh->num_vertices;j++)
907 mesh->data_texcoord2f[j * 2 + 0] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 0]);
908 mesh->data_texcoord2f[j * 2 + 1] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 1]);
910 for (j = 0;j < mesh->num_vertices * mesh->num_frames;j++)
912 mesh->data_aliasvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 0]) * (1.0f / 64.0f);
913 mesh->data_aliasvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 1]) * (1.0f / 64.0f);
914 mesh->data_aliasvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 2]) * (1.0f / 64.0f);
916 for (j = 0;j < mesh->num_frames;j++)
917 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);
919 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
920 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
922 if (LittleLong(pinmesh->num_shaders) >= 1 && ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name[0])
923 Mod_BuildAliasSkinsFromSkinFiles(mesh->data_skins, skinfiles, pinmesh->name, ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name);
926 for (j = 0;j < mesh->num_skins;j++)
927 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins + j, NULL);
930 Mod_CalcAliasModelBBoxes();
931 Mod_FreeSkinFiles(skinfiles);
934 extern void R_Model_Zymotic_DrawSky(entity_render_t *ent);
935 extern void R_Model_Zymotic_Draw(entity_render_t *ent);
936 extern void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent);
937 extern void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
938 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);
939 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer)
941 zymtype1header_t *pinmodel, *pheader;
944 pinmodel = (void *)buffer;
946 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
947 Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model\n");
948 if (BigLong(pinmodel->type) != 1)
949 Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
951 loadmodel->type = mod_alias;
952 loadmodel->alias.aliastype = ALIASTYPE_ZYM;
953 loadmodel->DrawSky = NULL;
954 loadmodel->Draw = R_Model_Zymotic_Draw;
955 loadmodel->DrawFakeShadow = NULL;//R_Model_Zymotic_DrawFakeShadow;
956 loadmodel->DrawShadowVolume = NULL;//R_Model_Zymotic_DrawShadowVolume;
957 loadmodel->DrawLight = NULL;//R_Model_Zymotic_DrawLight;
961 pheader->type = BigLong(pinmodel->type);
962 pheader->filesize = BigLong(pinmodel->filesize);
963 pheader->mins[0] = BigFloat(pinmodel->mins[0]);
964 pheader->mins[1] = BigFloat(pinmodel->mins[1]);
965 pheader->mins[2] = BigFloat(pinmodel->mins[2]);
966 pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
967 pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
968 pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
969 pheader->radius = BigFloat(pinmodel->radius);
970 pheader->numverts = loadmodel->alias.zymnum_verts = BigLong(pinmodel->numverts);
971 pheader->numtris = loadmodel->alias.zymnum_tris = BigLong(pinmodel->numtris);
972 pheader->numshaders = loadmodel->alias.zymnum_shaders = BigLong(pinmodel->numshaders);
973 pheader->numbones = loadmodel->alias.zymnum_bones = BigLong(pinmodel->numbones);
974 pheader->numscenes = loadmodel->alias.zymnum_scenes = BigLong(pinmodel->numscenes);
975 pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
976 pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
977 pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
978 pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
979 pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
980 pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
981 pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
982 pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
983 pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
984 pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
985 pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
986 pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
987 pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
988 pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
989 pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
990 pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
991 pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
992 pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
994 loadmodel->flags = 0; // there are no flags
995 loadmodel->numframes = pheader->numscenes;
996 loadmodel->synctype = ST_SYNC;
997 //loadmodel->numtris = pheader->numtris;
998 //loadmodel->numverts = 0;
1002 float modelradius, corner[2];
1004 modelradius = pheader->radius;
1005 for (i = 0;i < 3;i++)
1007 loadmodel->normalmins[i] = pheader->mins[i];
1008 loadmodel->normalmaxs[i] = pheader->maxs[i];
1009 loadmodel->rotatedmins[i] = -modelradius;
1010 loadmodel->rotatedmaxs[i] = modelradius;
1012 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
1013 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
1014 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1015 if (loadmodel->yawmaxs[0] > modelradius)
1016 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
1017 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
1018 loadmodel->yawmins[2] = loadmodel->normalmins[2];
1019 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
1020 loadmodel->radius = modelradius;
1021 loadmodel->radius2 = modelradius * modelradius;
1025 // FIXME: add shaders, and make them switchable shader sets and...
1026 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t));
1027 loadmodel->skinscenes[0].firstframe = 0;
1028 loadmodel->skinscenes[0].framecount = 1;
1029 loadmodel->skinscenes[0].loop = true;
1030 loadmodel->skinscenes[0].framerate = 10;
1031 loadmodel->numskins = 1;
1034 // go through the lumps, swapping things
1039 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1040 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
1041 scene = (void *) (pheader->lump_scenes.start + pbase);
1042 numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
1043 for (i = 0;i < pheader->numscenes;i++)
1045 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
1046 loadmodel->animscenes[i].firstframe = BigLong(scene->start);
1047 loadmodel->animscenes[i].framecount = BigLong(scene->length);
1048 loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
1049 loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
1050 if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
1051 Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
1052 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
1053 Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
1054 if (loadmodel->animscenes[i].framerate < 0)
1055 Host_Error("Mod_ZYMOTICMODEL_Load: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
1063 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
1064 loadmodel->alias.zymdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
1065 poses = (void *) (pheader->lump_poses.start + pbase);
1066 for (i = 0;i < pheader->lump_poses.length / 4;i++)
1067 loadmodel->alias.zymdata_poses[i] = BigFloat(poses[i]);
1073 // zymlump_t lump_bones; // zymbone_t bone[numbones];
1074 loadmodel->alias.zymdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(zymbone_t));
1075 bone = (void *) (pheader->lump_bones.start + pbase);
1076 for (i = 0;i < pheader->numbones;i++)
1078 memcpy(loadmodel->alias.zymdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
1079 loadmodel->alias.zymdata_bones[i].flags = BigLong(bone[i].flags);
1080 loadmodel->alias.zymdata_bones[i].parent = BigLong(bone[i].parent);
1081 if (loadmodel->alias.zymdata_bones[i].parent >= i)
1082 Host_Error("Mod_ZYMOTICMODEL_Load: bone[%i].parent >= %i in %s\n", i, i, loadmodel->name);
1088 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1089 loadmodel->alias.zymdata_vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1090 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
1091 for (i = 0;i < pheader->numverts;i++)
1093 loadmodel->alias.zymdata_vertbonecounts[i] = BigLong(bonecount[i]);
1094 if (loadmodel->alias.zymdata_vertbonecounts[i] < 1)
1095 Host_Error("Mod_ZYMOTICMODEL_Load: bone vertex count < 1 in %s\n", loadmodel->name);
1101 zymvertex_t *vertdata;
1102 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1103 loadmodel->alias.zymdata_verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
1104 vertdata = (void *) (pheader->lump_verts.start + pbase);
1105 for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
1107 loadmodel->alias.zymdata_verts[i].bonenum = BigLong(vertdata[i].bonenum);
1108 loadmodel->alias.zymdata_verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
1109 loadmodel->alias.zymdata_verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
1110 loadmodel->alias.zymdata_verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
1116 float *intexcoord2f, *outtexcoord2f;
1117 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1118 loadmodel->alias.zymdata_texcoords = outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
1119 intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
1120 for (i = 0;i < pheader->numverts;i++)
1122 outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1123 // flip T coordinate for OpenGL
1124 outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1129 int i, count, *renderlist, *renderlistend, *outrenderlist;
1130 // 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)
1131 loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, pheader->lump_render.length);
1132 // byteswap, validate, and swap winding order of tris
1133 count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1134 if (pheader->lump_render.length != count)
1135 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);
1136 outrenderlist = loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, count);
1137 renderlist = (void *) (pheader->lump_render.start + pbase);
1138 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
1139 for (i = 0;i < pheader->numshaders;i++)
1141 if (renderlist >= renderlistend)
1142 Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1143 count = BigLong(*renderlist);renderlist++;
1144 if (renderlist + count * 3 > renderlistend)
1145 Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
1146 *outrenderlist++ = count;
1149 outrenderlist[2] = BigLong(renderlist[0]);
1150 outrenderlist[1] = BigLong(renderlist[1]);
1151 outrenderlist[0] = BigLong(renderlist[2]);
1152 if ((unsigned int)outrenderlist[0] >= (unsigned int)pheader->numverts
1153 || (unsigned int)outrenderlist[1] >= (unsigned int)pheader->numverts
1154 || (unsigned int)outrenderlist[2] >= (unsigned int)pheader->numverts)
1155 Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
1165 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1166 loadmodel->alias.zymdata_textures = Mem_Alloc(loadmodel->mempool, pheader->numshaders * sizeof(rtexture_t *));
1167 shadername = (void *) (pheader->lump_shaders.start + pbase);
1168 for (i = 0;i < pheader->numshaders;i++)
1169 loadmodel->alias.zymdata_textures[i] = loadtextureimage(loadmodel->texturepool, shadername + i * 32, 0, 0, true, TEXF_ALPHA | TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0));
1173 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1174 loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1175 memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);