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 void Mod_Alias_GetMesh_Vertex3f(const model_t *model, const frameblend_t *frameblend, const aliasmesh_t *mesh, float *out3f)
31 if (mesh->num_vertexboneweights)
34 aliasvertexboneweight_t *v;
35 float *out, *matrix, m[12], bonepose[256][12];
36 // vertex weighted skeletal
37 // interpolate matrices and concatenate them to their parents
38 for (i = 0;i < model->alias.aliasnum_bones;i++)
40 for (k = 0;k < 12;k++)
42 for (blends = 0;blends < 4 && frameblend[blends].lerp > 0;blends++)
44 matrix = model->alias.aliasdata_poses + (frameblend[blends].frame * model->alias.aliasnum_bones + i) * 12;
45 for (k = 0;k < 12;k++)
46 m[k] += matrix[k] * frameblend[blends].lerp;
48 if (model->alias.aliasdata_bones[i].parent >= 0)
49 R_ConcatTransforms(bonepose[model->alias.aliasdata_bones[i].parent], m, bonepose[i]);
51 for (k = 0;k < 12;k++)
52 bonepose[i][k] = m[k];
54 // blend the vertex bone weights
55 memset(out3f, 0, mesh->num_vertices * sizeof(float[3]));
56 v = mesh->data_vertexboneweights;
57 for (i = 0;i < mesh->num_vertexboneweights;i++, v++)
59 out = out3f + v->vertexindex * 3;
60 matrix = bonepose[v->boneindex];
61 // FIXME: this can very easily be optimized with SSE or 3DNow
62 out[0] += v->origin[0] * matrix[0] + v->origin[1] * matrix[1] + v->origin[2] * matrix[ 2] + v->origin[3] * matrix[ 3];
63 out[1] += v->origin[0] * matrix[4] + v->origin[1] * matrix[5] + v->origin[2] * matrix[ 6] + v->origin[3] * matrix[ 7];
64 out[2] += v->origin[0] * matrix[8] + v->origin[1] * matrix[9] + v->origin[2] * matrix[10] + v->origin[3] * matrix[11];
70 float lerp1, lerp2, lerp3, lerp4;
71 const float *vertsbase, *verts1, *verts2, *verts3, *verts4;
73 vertsbase = mesh->data_morphvertex3f;
74 vertcount = mesh->num_vertices;
75 verts1 = vertsbase + frameblend[0].frame * vertcount * 3;
76 lerp1 = frameblend[0].lerp;
77 if (frameblend[1].lerp)
79 verts2 = vertsbase + frameblend[1].frame * vertcount * 3;
80 lerp2 = frameblend[1].lerp;
81 if (frameblend[2].lerp)
83 verts3 = vertsbase + frameblend[2].frame * vertcount * 3;
84 lerp3 = frameblend[2].lerp;
85 if (frameblend[3].lerp)
87 verts4 = vertsbase + frameblend[3].frame * vertcount * 3;
88 lerp4 = frameblend[3].lerp;
89 for (i = 0;i < vertcount * 3;i++)
90 VectorMAMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, lerp4, verts4 + i, out3f + i);
93 for (i = 0;i < vertcount * 3;i++)
94 VectorMAMAM(lerp1, verts1 + i, lerp2, verts2 + i, lerp3, verts3 + i, out3f + i);
97 for (i = 0;i < vertcount * 3;i++)
98 VectorMAM(lerp1, verts1 + i, lerp2, verts2 + i, out3f + i);
101 memcpy(out3f, verts1, vertcount * sizeof(float[3]));
105 static void Mod_Alias_Mesh_CompileFrameZero(aliasmesh_t *mesh)
107 frameblend_t frameblend[4] = {{0, 1}, {0, 0}, {0, 0}, {0, 0}};
108 mesh->data_basevertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[3][4]));
109 mesh->data_basesvector3f = mesh->data_basevertex3f + mesh->num_vertices * 3;
110 mesh->data_basetvector3f = mesh->data_basevertex3f + mesh->num_vertices * 6;
111 mesh->data_basenormal3f = mesh->data_basevertex3f + mesh->num_vertices * 9;
112 Mod_Alias_GetMesh_Vertex3f(loadmodel, frameblend, mesh, mesh->data_basevertex3f);
113 Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, mesh->data_basevertex3f, mesh->data_texcoord2f, mesh->data_element3i, mesh->data_basesvector3f, mesh->data_basetvector3f, mesh->data_basenormal3f);
116 static void Mod_MDLMD2MD3_TraceBox(model_t *model, int frame, trace_t *trace, const vec3_t boxstartmins, const vec3_t boxstartmaxs, const vec3_t boxendmins, const vec3_t boxendmaxs, int hitsupercontentsmask)
119 float segmentmins[3], segmentmaxs[3];
120 colbrushf_t *thisbrush_start, *thisbrush_end;
121 matrix4x4_t startmatrix, endmatrix;
122 memset(trace, 0, sizeof(*trace));
124 trace->realfraction = 1;
125 trace->hitsupercontentsmask = hitsupercontentsmask;
126 segmentmins[0] = min(boxstartmins[0], boxendmins[0]);
127 segmentmins[1] = min(boxstartmins[1], boxendmins[1]);
128 segmentmins[2] = min(boxstartmins[2], boxendmins[2]);
129 segmentmaxs[0] = max(boxstartmaxs[0], boxendmaxs[0]);
130 segmentmaxs[1] = max(boxstartmaxs[1], boxendmaxs[1]);
131 segmentmaxs[2] = max(boxstartmaxs[2], boxendmaxs[2]);
132 if (VectorCompare(boxstartmins, boxstartmaxs) && VectorCompare(boxendmins, boxendmaxs))
135 for (i = 0;i < model->alias.aliasnum_meshes;i++)
138 if (framenum < 0 || framenum > model->alias.aliasdata_meshes[i].num_morphframes)
140 if (model->alias.aliasdata_meshes[i].data_morphvertex3f)
141 Collision_TraceLineTriangleMeshFloat(trace, boxstartmins, boxendmins, model->alias.aliasdata_meshes[i].num_triangles, model->alias.aliasdata_meshes[i].data_element3i, model->alias.aliasdata_meshes[i].data_morphvertex3f + framenum * model->alias.aliasdata_meshes[i].num_vertices * 3, SUPERCONTENTS_SOLID, segmentmins, segmentmaxs);
143 // FIXME!!! this needs to handle skeletal!
148 // box trace, performed as brush trace
149 Matrix4x4_CreateIdentity(&startmatrix);
150 Matrix4x4_CreateIdentity(&endmatrix);
151 thisbrush_start = Collision_BrushForBox(&startmatrix, boxstartmins, boxstartmaxs);
152 thisbrush_end = Collision_BrushForBox(&endmatrix, boxendmins, boxendmaxs);
153 for (i = 0;i < model->alias.aliasnum_meshes;i++)
156 if (framenum < 0 || framenum > model->alias.aliasdata_meshes[i].num_morphframes)
158 if (model->alias.aliasdata_meshes[i].data_morphvertex3f)
159 Collision_TraceBrushTriangleMeshFloat(trace, thisbrush_start, thisbrush_end, model->alias.aliasdata_meshes[i].num_triangles, model->alias.aliasdata_meshes[i].data_element3i, model->alias.aliasdata_meshes[i].data_morphvertex3f + framenum * model->alias.aliasdata_meshes[i].num_vertices * 3, SUPERCONTENTS_SOLID, segmentmins, segmentmaxs);
161 // FIXME!!! this needs to handle skeletal!
166 static void Mod_CalcAliasModelBBoxes (void)
169 float dist, yawradius, radius;
172 VectorClear(loadmodel->normalmins);
173 VectorClear(loadmodel->normalmaxs);
176 for (meshnum = 0, mesh = loadmodel->alias.aliasdata_meshes;meshnum < loadmodel->alias.aliasnum_meshes;meshnum++, mesh++)
178 for (vnum = 0, v = mesh->data_morphvertex3f;vnum < mesh->num_vertices * mesh->num_morphframes;vnum++, v += 3)
180 if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
181 if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
182 if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
183 if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
184 if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
185 if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
186 dist = v[0] * v[0] + v[1] * v[1];
187 if (yawradius < dist)
194 radius = sqrt(radius);
195 yawradius = sqrt(yawradius);
196 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
197 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = yawradius;
198 loadmodel->yawmins[2] = loadmodel->normalmins[2];
199 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
200 loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
201 loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
202 loadmodel->radius = radius;
203 loadmodel->radius2 = radius * radius;
206 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
210 for (i = 0;i < inverts;i++)
212 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
214 temp[0] = v[i].v[0] * scale[0] + translate[0];
215 temp[1] = v[i].v[1] * scale[1] + translate[1];
216 temp[2] = v[i].v[2] * scale[2] + translate[2];
217 j = vertremap[i]; // not onseam
219 VectorCopy(temp, out3f + j * 3);
220 j = vertremap[i+inverts]; // onseam
222 VectorCopy(temp, out3f + j * 3);
226 static void Mod_MDL_LoadFrames (qbyte* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
228 int i, f, pose, groupframes;
230 daliasframetype_t *pframetype;
231 daliasframe_t *pinframe;
232 daliasgroup_t *group;
233 daliasinterval_t *intervals;
236 scene = loadmodel->animscenes;
237 for (f = 0;f < loadmodel->numframes;f++)
239 pframetype = (daliasframetype_t *)datapointer;
240 datapointer += sizeof(daliasframetype_t);
241 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
243 // a single frame is still treated as a group
250 group = (daliasgroup_t *)datapointer;
251 datapointer += sizeof(daliasgroup_t);
252 groupframes = LittleLong (group->numframes);
254 // intervals (time per frame)
255 intervals = (daliasinterval_t *)datapointer;
256 datapointer += sizeof(daliasinterval_t) * groupframes;
258 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
259 if (interval < 0.01f)
261 Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
266 // get scene name from first frame
267 pinframe = (daliasframe_t *)datapointer;
269 strcpy(scene->name, pinframe->name);
270 scene->firstframe = pose;
271 scene->framecount = groupframes;
272 scene->framerate = 1.0f / interval;
277 for (i = 0;i < groupframes;i++)
279 pinframe = (daliasframe_t *)datapointer;
280 datapointer += sizeof(daliasframe_t);
281 Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->alias.aliasdata_meshes->data_morphvertex3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, vertremap);
282 datapointer += sizeof(trivertx_t) * inverts;
288 static skinframe_t missingskinframe;
289 aliaslayer_t mod_alias_layersbuffer[16]; // 7 currently used
290 void Mod_BuildAliasSkinFromSkinFrame(aliasskin_t *skin, skinframe_t *skinframe)
295 if (skinframe == NULL)
297 skinframe = &missingskinframe;
298 memset(skinframe, 0, sizeof(*skinframe));
299 skinframe->base = r_notexture;
302 memset(&mod_alias_layersbuffer, 0, sizeof(mod_alias_layersbuffer));
303 layer = mod_alias_layersbuffer;
304 layer->flags = ALIASLAYER_SPECULAR;
305 layer->texture = skinframe->gloss;
306 layer->nmap = skinframe->nmap;
308 if (skinframe->merged != NULL)
310 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_COLORMAPPED;
311 layer->texture = skinframe->merged;
312 layer->nmap = skinframe->nmap;
315 if (skinframe->base != NULL)
317 layer->flags = ALIASLAYER_DIFFUSE;
318 if (skinframe->merged != NULL)
319 layer->flags |= ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED;
320 layer->texture = skinframe->base;
321 layer->nmap = skinframe->nmap;
324 if (skinframe->pants != NULL)
326 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_PANTS;
327 layer->texture = skinframe->pants;
328 layer->nmap = skinframe->nmap;
331 if (skinframe->shirt != NULL)
333 layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_SHIRT;
334 layer->texture = skinframe->shirt;
335 layer->nmap = skinframe->nmap;
339 if (skinframe->glow != NULL)
342 layer->texture = skinframe->glow;
346 layer->flags = ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS;
347 layer->texture = skinframe->fog;
351 // fog texture only exists if some pixels are transparent...
352 if (skinframe->fog != NULL)
353 skin->flags |= ALIASSKIN_TRANSPARENT;
355 skin->num_layers = layer - mod_alias_layersbuffer;
356 skin->data_layers = Mem_Alloc(loadmodel->mempool, skin->num_layers * sizeof(aliaslayer_t));
357 memcpy(skin->data_layers, mod_alias_layersbuffer, skin->num_layers * sizeof(aliaslayer_t));
360 void Mod_BuildAliasSkinsFromSkinFiles(aliasskin_t *skin, skinfile_t *skinfile, char *meshname, char *shadername)
363 skinfileitem_t *skinfileitem;
364 skinframe_t tempskinframe;
367 for (i = 0;skinfile;skinfile = skinfile->next, i++, skin++)
369 memset(skin, 0, sizeof(*skin));
370 for (skinfileitem = skinfile->items;skinfileitem;skinfileitem = skinfileitem->next)
372 // leave the skin unitialized (nodraw) if the replacement is "common/nodraw" or "textures/common/nodraw"
373 if (!strcmp(skinfileitem->name, meshname) && strcmp(skinfileitem->replacement, "common/nodraw") && strcmp(skinfileitem->replacement, "textures/common/nodraw"))
375 memset(&tempskinframe, 0, sizeof(tempskinframe));
376 if (Mod_LoadSkinFrame(&tempskinframe, skinfileitem->replacement, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
377 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
380 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);
381 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
382 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
385 Con_Printf("failed to load skin \"%s\"\n", shadername);
386 Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
395 memset(&tempskinframe, 0, sizeof(tempskinframe));
396 if (Mod_LoadSkinFrame(&tempskinframe, shadername, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
397 Mod_BuildAliasSkinFromSkinFrame(skin, &tempskinframe);
400 Con_Printf("failed to load mesh \"%s\" shader \"%s\"\n", meshname, shadername);
401 Mod_BuildAliasSkinFromSkinFrame(skin, NULL);
406 #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);
407 #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);
408 extern void R_Model_Alias_Draw(entity_render_t *ent);
409 extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist, const vec3_t lightmins, const vec3_t lightmaxs);
410 extern void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist);
411 void Mod_IDP0_Load(model_t *mod, void *buffer)
413 int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
414 float scales, scalet, scale[3], translate[3], interval;
416 stvert_t *pinstverts;
417 dtriangle_t *pintriangles;
418 daliasskintype_t *pinskintype;
419 daliasskingroup_t *pinskingroup;
420 daliasskininterval_t *pinskinintervals;
421 daliasframetype_t *pinframetype;
422 daliasgroup_t *pinframegroup;
423 qbyte *datapointer, *startframes, *startskins;
424 char name[MAX_QPATH];
425 skinframe_t tempskinframe;
426 animscene_t *tempskinscenes;
427 aliasskin_t *tempaliasskins;
429 int *vertonseam, *vertremap;
430 skinfile_t *skinfiles;
432 datapointer = buffer;
433 pinmodel = (mdl_t *)datapointer;
434 datapointer += sizeof(mdl_t);
436 version = LittleLong (pinmodel->version);
437 if (version != ALIAS_VERSION)
438 Host_Error ("%s has wrong version number (%i should be %i)",
439 loadmodel->name, version, ALIAS_VERSION);
441 loadmodel->type = mod_alias;
442 loadmodel->DrawSky = NULL;
443 loadmodel->Draw = R_Model_Alias_Draw;
444 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
445 loadmodel->DrawLight = R_Model_Alias_DrawLight;
446 loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
448 loadmodel->alias.aliasnum_meshes = 1;
449 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
451 loadmodel->numskins = LittleLong(pinmodel->numskins);
452 BOUNDI(loadmodel->numskins,0,65536);
453 skinwidth = LittleLong (pinmodel->skinwidth);
454 BOUNDI(skinwidth,0,65536);
455 skinheight = LittleLong (pinmodel->skinheight);
456 BOUNDI(skinheight,0,65536);
457 numverts = LittleLong(pinmodel->numverts);
458 BOUNDI(numverts,0,65536);
459 loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
460 BOUNDI(loadmodel->alias.aliasdata_meshes->num_triangles,0,65536);
461 loadmodel->numframes = LittleLong(pinmodel->numframes);
462 BOUNDI(loadmodel->numframes,0,65536);
463 loadmodel->synctype = LittleLong (pinmodel->synctype);
464 BOUNDI(loadmodel->synctype,0,2);
465 loadmodel->flags = LittleLong (pinmodel->flags);
467 for (i = 0;i < 3;i++)
469 scale[i] = LittleFloat (pinmodel->scale[i]);
470 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
473 startskins = datapointer;
475 for (i = 0;i < loadmodel->numskins;i++)
477 pinskintype = (daliasskintype_t *)datapointer;
478 datapointer += sizeof(daliasskintype_t);
479 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
483 pinskingroup = (daliasskingroup_t *)datapointer;
484 datapointer += sizeof(daliasskingroup_t);
485 groupskins = LittleLong(pinskingroup->numskins);
486 datapointer += sizeof(daliasskininterval_t) * groupskins;
489 for (j = 0;j < groupskins;j++)
491 datapointer += skinwidth * skinheight;
496 pinstverts = (stvert_t *)datapointer;
497 datapointer += sizeof(stvert_t) * numverts;
499 pintriangles = (dtriangle_t *)datapointer;
500 datapointer += sizeof(dtriangle_t) * loadmodel->alias.aliasdata_meshes->num_triangles;
502 startframes = datapointer;
503 loadmodel->alias.aliasdata_meshes->num_morphframes = 0;
504 for (i = 0;i < loadmodel->numframes;i++)
506 pinframetype = (daliasframetype_t *)datapointer;
507 datapointer += sizeof(daliasframetype_t);
508 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
512 pinframegroup = (daliasgroup_t *)datapointer;
513 datapointer += sizeof(daliasgroup_t);
514 groupframes = LittleLong(pinframegroup->numframes);
515 datapointer += sizeof(daliasinterval_t) * groupframes;
518 for (j = 0;j < groupframes;j++)
520 datapointer += sizeof(daliasframe_t);
521 datapointer += sizeof(trivertx_t) * numverts;
522 loadmodel->alias.aliasdata_meshes->num_morphframes++;
526 // store texture coordinates into temporary array, they will be stored
527 // after usage is determined (triangle data)
528 vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
529 vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
530 vertonseam = vertremap + numverts * 2;
532 scales = 1.0 / skinwidth;
533 scalet = 1.0 / skinheight;
534 for (i = 0;i < numverts;i++)
536 vertonseam[i] = LittleLong(pinstverts[i].onseam);
537 vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
538 vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
539 vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
540 vertst[(i+numverts)*2+1] = vertst[i*2+1];
543 // load triangle data
544 loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->alias.aliasdata_meshes->num_triangles);
546 // read the triangle elements
547 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
548 for (j = 0;j < 3;j++)
549 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
550 // validate (note numverts is used because this is the original data)
551 Mod_ValidateElements(loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
552 // now butcher the elements according to vertonseam and tri->facesfront
553 // and then compact the vertex set to remove duplicates
554 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
555 if (!LittleLong(pintriangles[i].facesfront)) // backface
556 for (j = 0;j < 3;j++)
557 if (vertonseam[loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j]])
558 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] += numverts;
560 // (this uses vertremap to count usage to save some memory)
561 for (i = 0;i < numverts*2;i++)
563 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles*3;i++)
564 vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]]++;
565 // build remapping table and compact array
566 loadmodel->alias.aliasdata_meshes->num_vertices = 0;
567 for (i = 0;i < numverts*2;i++)
571 vertremap[i] = loadmodel->alias.aliasdata_meshes->num_vertices;
572 vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
573 vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
574 loadmodel->alias.aliasdata_meshes->num_vertices++;
577 vertremap[i] = -1; // not used at all
579 // remap the elements to the new vertex set
580 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles * 3;i++)
581 loadmodel->alias.aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]];
582 // store the texture coordinates
583 loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->alias.aliasdata_meshes->num_vertices);
584 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_vertices;i++)
586 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
587 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
591 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
592 loadmodel->alias.aliasdata_meshes->data_morphvertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[3]) * loadmodel->alias.aliasdata_meshes->num_morphframes * loadmodel->alias.aliasdata_meshes->num_vertices);
593 loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
594 Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
595 Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
596 Mod_CalcAliasModelBBoxes();
597 Mod_Alias_Mesh_CompileFrameZero(loadmodel->alias.aliasdata_meshes);
603 if ((skinfiles = Mod_LoadSkinFiles()))
605 loadmodel->alias.aliasdata_meshes->num_skins = totalskins = loadmodel->numskins;
606 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
607 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
608 Mod_FreeSkinFiles(skinfiles);
609 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
610 for (i = 0;i < loadmodel->numskins;i++)
612 loadmodel->skinscenes[i].firstframe = i;
613 loadmodel->skinscenes[i].framecount = 1;
614 loadmodel->skinscenes[i].loop = true;
615 loadmodel->skinscenes[i].framerate = 10;
620 loadmodel->alias.aliasdata_meshes->num_skins = totalskins;
621 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
622 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
624 datapointer = startskins;
625 for (i = 0;i < loadmodel->numskins;i++)
627 pinskintype = (daliasskintype_t *)datapointer;
628 datapointer += sizeof(daliasskintype_t);
630 if (pinskintype->type == ALIAS_SKIN_SINGLE)
637 pinskingroup = (daliasskingroup_t *)datapointer;
638 datapointer += sizeof(daliasskingroup_t);
640 groupskins = LittleLong (pinskingroup->numskins);
642 pinskinintervals = (daliasskininterval_t *)datapointer;
643 datapointer += sizeof(daliasskininterval_t) * groupskins;
645 interval = LittleFloat(pinskinintervals[0].interval);
646 if (interval < 0.01f)
648 Con_Printf("%s has an invalid interval %f, changing to 0.1\n", loadmodel->name, interval);
653 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
654 loadmodel->skinscenes[i].firstframe = totalskins;
655 loadmodel->skinscenes[i].framecount = groupskins;
656 loadmodel->skinscenes[i].framerate = 1.0f / interval;
657 loadmodel->skinscenes[i].loop = true;
659 for (j = 0;j < groupskins;j++)
662 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
664 sprintf (name, "%s_%i", loadmodel->name, i);
665 if (!Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, true))
666 Mod_LoadSkinFrame_Internal(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, r_fullbrights.integer, (qbyte *)datapointer, skinwidth, skinheight);
667 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
668 datapointer += skinwidth * skinheight;
672 // check for skins that don't exist in the model, but do exist as external images
673 // (this was added because yummyluv kept pestering me about support for it)
674 while (Mod_LoadSkinFrame(&tempskinframe, va("%s_%i", loadmodel->name, loadmodel->numskins), (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA | TEXF_PICMIP, true, false, true))
676 // expand the arrays to make room
677 tempskinscenes = loadmodel->skinscenes;
678 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
679 memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
680 Mem_Free(tempskinscenes);
682 tempaliasskins = loadmodel->alias.aliasdata_meshes->data_skins;
683 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(aliasskin_t));
684 memcpy(loadmodel->alias.aliasdata_meshes->data_skins, tempaliasskins, totalskins * sizeof(aliasskin_t));
685 Mem_Free(tempaliasskins);
687 // store the info about the new skin
688 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + totalskins, &tempskinframe);
689 strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
690 loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
691 loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
692 loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
693 loadmodel->skinscenes[loadmodel->numskins].loop = true;
695 //increase skin counts
696 loadmodel->alias.aliasdata_meshes->num_skins++;
697 loadmodel->numskins++;
703 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
707 for (i = 0;i < numverts;i++, out3f += 3)
709 in = v + vertremap[i];
710 out3f[0] = in->v[0] * scale[0] + translate[0];
711 out3f[1] = in->v[1] * scale[1] + translate[1];
712 out3f[2] = in->v[2] * scale[2] + translate[2];
716 void Mod_IDP2_Load(model_t *mod, void *buffer)
718 int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
719 float *stverts, s, t, scale[3], translate[3];
721 qbyte *base, *datapointer;
722 md2frame_t *pinframe;
724 md2triangle_t *intri;
725 unsigned short *inst;
728 struct md2verthash_s *next;
732 *hash, **md2verthash, *md2verthashdata;
733 skinframe_t tempskinframe;
734 skinfile_t *skinfiles;
739 version = LittleLong (pinmodel->version);
740 if (version != MD2ALIAS_VERSION)
741 Host_Error ("%s has wrong version number (%i should be %i)",
742 loadmodel->name, version, MD2ALIAS_VERSION);
744 loadmodel->type = mod_alias;
745 loadmodel->DrawSky = NULL;
746 loadmodel->Draw = R_Model_Alias_Draw;
747 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
748 loadmodel->DrawLight = R_Model_Alias_DrawLight;
749 loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
751 if (LittleLong(pinmodel->num_tris) < 1 || LittleLong(pinmodel->num_tris) > 65536)
752 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
753 if (LittleLong(pinmodel->num_xyz) < 1 || LittleLong(pinmodel->num_xyz) > 65536)
754 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
755 if (LittleLong(pinmodel->num_frames) < 1 || LittleLong(pinmodel->num_frames) > 65536)
756 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
757 if (LittleLong(pinmodel->num_skins) < 0 || LittleLong(pinmodel->num_skins) > 256)
758 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
760 end = LittleLong(pinmodel->ofs_end);
761 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins) <= 0 || LittleLong(pinmodel->ofs_skins) >= end))
762 Host_Error ("%s is not a valid model", loadmodel->name);
763 if (LittleLong(pinmodel->ofs_st) <= 0 || LittleLong(pinmodel->ofs_st) >= end)
764 Host_Error ("%s is not a valid model", loadmodel->name);
765 if (LittleLong(pinmodel->ofs_tris) <= 0 || LittleLong(pinmodel->ofs_tris) >= end)
766 Host_Error ("%s is not a valid model", loadmodel->name);
767 if (LittleLong(pinmodel->ofs_frames) <= 0 || LittleLong(pinmodel->ofs_frames) >= end)
768 Host_Error ("%s is not a valid model", loadmodel->name);
769 if (LittleLong(pinmodel->ofs_glcmds) <= 0 || LittleLong(pinmodel->ofs_glcmds) >= end)
770 Host_Error ("%s is not a valid model", loadmodel->name);
772 loadmodel->alias.aliasnum_meshes = 1;
773 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
775 loadmodel->numskins = LittleLong(pinmodel->num_skins);
776 numxyz = LittleLong(pinmodel->num_xyz);
777 numst = LittleLong(pinmodel->num_st);
778 loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
779 loadmodel->numframes = LittleLong(pinmodel->num_frames);
780 loadmodel->alias.aliasdata_meshes->num_morphframes = loadmodel->numframes;
781 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
783 loadmodel->flags = 0; // there are no MD2 flags
784 loadmodel->synctype = ST_RAND;
787 inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
788 if ((skinfiles = Mod_LoadSkinFiles()))
790 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
791 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
792 Mod_BuildAliasSkinsFromSkinFiles(loadmodel->alias.aliasdata_meshes->data_skins, skinfiles, "default", "");
793 Mod_FreeSkinFiles(skinfiles);
795 else if (loadmodel->numskins)
797 // skins found (most likely not a player model)
798 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
799 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
800 for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
802 if (Mod_LoadSkinFrame(&tempskinframe, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE | TEXF_PICMIP, true, false, true))
803 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, &tempskinframe);
806 Con_Printf("%s is missing skin \"%s\"\n", loadmodel->name, inskin);
807 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, NULL);
813 // no skins (most likely a player model)
814 loadmodel->numskins = 1;
815 loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
816 loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
817 Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins, NULL);
820 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
821 for (i = 0;i < loadmodel->numskins;i++)
823 loadmodel->skinscenes[i].firstframe = i;
824 loadmodel->skinscenes[i].framecount = 1;
825 loadmodel->skinscenes[i].loop = true;
826 loadmodel->skinscenes[i].framerate = 10;
829 // load the triangles and stvert data
830 inst = (void*)(base + LittleLong(pinmodel->ofs_st));
831 intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
832 skinwidth = LittleLong(pinmodel->skinwidth);
833 skinheight = LittleLong(pinmodel->skinheight);
835 stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
836 s = 1.0f / skinwidth;
837 t = 1.0f / skinheight;
838 for (i = 0;i < numst;i++)
840 j = (unsigned short) LittleShort(inst[i*2+0]);
841 k = (unsigned short) LittleShort(inst[i*2+1]);
842 if (j >= skinwidth || k >= skinheight)
844 Con_Printf("%s has an invalid skin coordinate (%i %i) on vert %i, changing to 0 0\n", loadmodel->name, j, k, i);
848 stverts[i*2+0] = j * s;
849 stverts[i*2+1] = k * t;
852 md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
853 md2verthashdata = Mem_Alloc(tempmempool, loadmodel->alias.aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
854 // swap the triangle list
856 loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
857 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
859 for (j = 0;j < 3;j++)
861 xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
862 st = (unsigned short) LittleShort (intri[i].index_st[j]);
865 Con_Printf("%s has an invalid xyz index (%i) on triangle %i, resetting to 0\n", loadmodel->name, xyz, i);
870 Con_Printf("%s has an invalid st index (%i) on triangle %i, resetting to 0\n", loadmodel->name, st, i);
875 hashindex = (xyz * 17 + st) & 255;
876 for (hash = md2verthash[hashindex];hash;hash = hash->next)
877 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
881 hash = md2verthashdata + num++;
885 hash->next = md2verthash[hashindex];
886 md2verthash[hashindex] = hash;
888 loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
895 loadmodel->alias.aliasdata_meshes->num_vertices = numverts;
896 vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
897 loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
898 for (i = 0;i < num;i++)
900 hash = md2verthashdata + i;
901 vertremap[i] = hash->xyz;
902 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
903 loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
906 Mem_Free(md2verthash);
907 Mem_Free(md2verthashdata);
910 datapointer = (base + LittleLong(pinmodel->ofs_frames));
911 loadmodel->alias.aliasdata_meshes->data_morphvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->alias.aliasdata_meshes->num_morphframes * sizeof(float[3]));
912 for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_morphframes;i++)
914 pinframe = (md2frame_t *)datapointer;
915 datapointer += sizeof(md2frame_t);
916 for (j = 0;j < 3;j++)
918 scale[j] = LittleFloat(pinframe->scale[j]);
919 translate[j] = LittleFloat(pinframe->translate[j]);
921 Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->alias.aliasdata_meshes->data_morphvertex3f + i * numverts * 3, numverts, vertremap);
922 datapointer += numxyz * sizeof(trivertx_t);
924 strcpy(loadmodel->animscenes[i].name, pinframe->name);
925 loadmodel->animscenes[i].firstframe = i;
926 loadmodel->animscenes[i].framecount = 1;
927 loadmodel->animscenes[i].framerate = 10;
928 loadmodel->animscenes[i].loop = true;
933 loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
934 Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
935 Mod_CalcAliasModelBBoxes();
936 Mod_Alias_Mesh_CompileFrameZero(loadmodel->alias.aliasdata_meshes);
939 void Mod_IDP3_Load(model_t *mod, void *buffer)
941 int i, j, k, version;
942 md3modelheader_t *pinmodel;
943 md3frameinfo_t *pinframe;
947 skinfile_t *skinfiles;
951 if (memcmp(pinmodel->identifier, "IDP3", 4))
952 Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
953 version = LittleLong (pinmodel->version);
954 if (version != MD3VERSION)
955 Host_Error ("%s has wrong version number (%i should be %i)",
956 loadmodel->name, version, MD3VERSION);
958 skinfiles = Mod_LoadSkinFiles();
959 if (loadmodel->numskins < 1)
960 loadmodel->numskins = 1;
962 loadmodel->type = mod_alias;
963 loadmodel->DrawSky = NULL;
964 loadmodel->Draw = R_Model_Alias_Draw;
965 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
966 loadmodel->DrawLight = R_Model_Alias_DrawLight;
967 loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox;
968 loadmodel->flags = LittleLong(pinmodel->flags);
969 loadmodel->synctype = ST_RAND;
971 // set up some global info about the model
972 loadmodel->numframes = LittleLong(pinmodel->num_frames);
973 loadmodel->alias.aliasnum_meshes = LittleLong(pinmodel->num_meshes);
975 // make skinscenes for the skins (no groups)
976 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
977 for (i = 0;i < loadmodel->numskins;i++)
979 loadmodel->skinscenes[i].firstframe = i;
980 loadmodel->skinscenes[i].framecount = 1;
981 loadmodel->skinscenes[i].loop = true;
982 loadmodel->skinscenes[i].framerate = 10;
986 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
987 for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
989 strcpy(loadmodel->animscenes[i].name, pinframe->name);
990 loadmodel->animscenes[i].firstframe = i;
991 loadmodel->animscenes[i].framecount = 1;
992 loadmodel->animscenes[i].framerate = 10;
993 loadmodel->animscenes[i].loop = true;
997 loadmodel->alias.aliasnum_tagframes = loadmodel->numframes;
998 loadmodel->alias.aliasnum_tags = LittleLong(pinmodel->num_tags);
999 loadmodel->alias.aliasdata_tags = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags * sizeof(aliastag_t));
1000 for (i = 0, pintag = (md3tag_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_tags));i < loadmodel->alias.aliasnum_tagframes * loadmodel->alias.aliasnum_tags;i++, pintag++)
1002 strcpy(loadmodel->alias.aliasdata_tags[i].name, pintag->name);
1003 Matrix4x4_CreateIdentity(&loadmodel->alias.aliasdata_tags[i].matrix);
1004 for (j = 0;j < 3;j++)
1006 for (k = 0;k < 3;k++)
1007 loadmodel->alias.aliasdata_tags[i].matrix.m[j][k] = LittleFloat(pintag->rotationmatrix[k * 3 + j]);
1008 loadmodel->alias.aliasdata_tags[i].matrix.m[j][3] = LittleFloat(pintag->origin[j]);
1010 //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);
1014 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
1015 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)))
1017 if (memcmp(pinmesh->identifier, "IDP3", 4))
1018 Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)\n");
1019 mesh = loadmodel->alias.aliasdata_meshes + i;
1020 mesh->num_skins = loadmodel->numskins;
1021 mesh->num_morphframes = LittleLong(pinmesh->num_frames);
1022 mesh->num_vertices = LittleLong(pinmesh->num_vertices);
1023 mesh->num_triangles = LittleLong(pinmesh->num_triangles);
1024 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
1025 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
1026 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
1027 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
1028 mesh->data_morphvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_morphframes * sizeof(float[3]));
1029 for (j = 0;j < mesh->num_triangles * 3;j++)
1030 mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_elements)))[j]);
1031 for (j = 0;j < mesh->num_vertices;j++)
1033 mesh->data_texcoord2f[j * 2 + 0] = LittleFloat(((float *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_texcoords)))[j * 2 + 0]);
1034 mesh->data_texcoord2f[j * 2 + 1] = LittleFloat(((float *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_texcoords)))[j * 2 + 1]);
1036 for (j = 0;j < mesh->num_vertices * mesh->num_morphframes;j++)
1038 mesh->data_morphvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_framevertices)))[j * 4 + 0]) * (1.0f / 64.0f);
1039 mesh->data_morphvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_framevertices)))[j * 4 + 1]) * (1.0f / 64.0f);
1040 mesh->data_morphvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_framevertices)))[j * 4 + 2]) * (1.0f / 64.0f);
1043 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
1044 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
1045 Mod_Alias_Mesh_CompileFrameZero(mesh);
1047 if (LittleLong(pinmesh->num_shaders) >= 1)
1048 Mod_BuildAliasSkinsFromSkinFiles(mesh->data_skins, skinfiles, pinmesh->name, ((md3shader_t *)((qbyte *) pinmesh + LittleLong(pinmesh->lump_shaders)))->name);
1050 for (j = 0;j < mesh->num_skins;j++)
1051 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins + j, NULL);
1053 Mod_CalcAliasModelBBoxes();
1054 Mod_FreeSkinFiles(skinfiles);
1057 void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer)
1059 zymtype1header_t *pinmodel, *pheader;
1061 int i, j, k, l, numposes, *bonecount, *vertbonecounts, count, *renderlist, *renderlistend, *outelements, *remapvertices;
1062 float modelradius, corner[2], *poses, *intexcoord2f, *outtexcoord2f;
1063 zymvertex_t *verts, *vertdata;
1067 skinfile_t *skinfiles;
1070 pinmodel = (void *)buffer;
1072 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
1073 Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model\n");
1074 if (BigLong(pinmodel->type) != 1)
1075 Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
1077 loadmodel->type = mod_alias;
1078 loadmodel->DrawSky = NULL;
1079 loadmodel->Draw = R_Model_Alias_Draw;
1080 loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
1081 loadmodel->DrawLight = R_Model_Alias_DrawLight;
1082 //loadmodel->TraceBox = Mod_MDLMD2MD3_TraceBox; // FIXME: implement collisions
1083 loadmodel->flags = 0; // there are no flags on zym models
1084 loadmodel->synctype = ST_RAND;
1088 pheader->type = BigLong(pinmodel->type);
1089 pheader->filesize = BigLong(pinmodel->filesize);
1090 pheader->mins[0] = BigFloat(pinmodel->mins[0]);
1091 pheader->mins[1] = BigFloat(pinmodel->mins[1]);
1092 pheader->mins[2] = BigFloat(pinmodel->mins[2]);
1093 pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
1094 pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
1095 pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
1096 pheader->radius = BigFloat(pinmodel->radius);
1097 pheader->numverts = BigLong(pinmodel->numverts);
1098 pheader->numtris = BigLong(pinmodel->numtris);
1099 pheader->numshaders = BigLong(pinmodel->numshaders);
1100 pheader->numbones = BigLong(pinmodel->numbones);
1101 pheader->numscenes = BigLong(pinmodel->numscenes);
1102 pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
1103 pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
1104 pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
1105 pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
1106 pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
1107 pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
1108 pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
1109 pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
1110 pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
1111 pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
1112 pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
1113 pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
1114 pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
1115 pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
1116 pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
1117 pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
1118 pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
1119 pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
1121 loadmodel->numframes = pheader->numscenes;
1122 loadmodel->alias.aliasnum_meshes = pheader->numshaders;
1124 skinfiles = Mod_LoadSkinFiles();
1125 if (loadmodel->numskins < 1)
1126 loadmodel->numskins = 1;
1128 // make skinscenes for the skins (no groups)
1129 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
1130 for (i = 0;i < loadmodel->numskins;i++)
1132 loadmodel->skinscenes[i].firstframe = i;
1133 loadmodel->skinscenes[i].framecount = 1;
1134 loadmodel->skinscenes[i].loop = true;
1135 loadmodel->skinscenes[i].framerate = 10;
1139 modelradius = pheader->radius;
1140 for (i = 0;i < 3;i++)
1142 loadmodel->normalmins[i] = pheader->mins[i];
1143 loadmodel->normalmaxs[i] = pheader->maxs[i];
1144 loadmodel->rotatedmins[i] = -modelradius;
1145 loadmodel->rotatedmaxs[i] = modelradius;
1147 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
1148 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
1149 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1150 if (loadmodel->yawmaxs[0] > modelradius)
1151 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
1152 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
1153 loadmodel->yawmins[2] = loadmodel->normalmins[2];
1154 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
1155 loadmodel->radius = modelradius;
1156 loadmodel->radius2 = modelradius * modelradius;
1158 // go through the lumps, swapping things
1160 //zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
1161 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
1162 scene = (void *) (pheader->lump_scenes.start + pbase);
1163 numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
1164 for (i = 0;i < pheader->numscenes;i++)
1166 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
1167 loadmodel->animscenes[i].firstframe = BigLong(scene->start);
1168 loadmodel->animscenes[i].framecount = BigLong(scene->length);
1169 loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
1170 loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
1171 if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
1172 Host_Error("%s scene->firstframe (%i) >= numposes (%i)\n", loadmodel->name, loadmodel->animscenes[i].firstframe, numposes);
1173 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
1174 Host_Error("%s scene->firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->name, loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
1175 if (loadmodel->animscenes[i].framerate < 0)
1176 Host_Error("%s scene->framerate (%f) < 0\n", loadmodel->name, loadmodel->animscenes[i].framerate);
1180 //zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
1181 loadmodel->alias.aliasnum_poses = pheader->lump_poses.length / sizeof(float[3][4]);
1182 loadmodel->alias.aliasdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
1183 poses = (void *) (pheader->lump_poses.start + pbase);
1184 for (i = 0;i < pheader->lump_poses.length / 4;i++)
1185 loadmodel->alias.aliasdata_poses[i] = BigFloat(poses[i]);
1187 //zymlump_t lump_bones; // zymbone_t bone[numbones];
1188 loadmodel->alias.aliasnum_bones = pheader->numbones;
1189 loadmodel->alias.aliasdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(aliasbone_t));
1190 bone = (void *) (pheader->lump_bones.start + pbase);
1191 for (i = 0;i < pheader->numbones;i++)
1193 memcpy(loadmodel->alias.aliasdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
1194 loadmodel->alias.aliasdata_bones[i].flags = BigLong(bone[i].flags);
1195 loadmodel->alias.aliasdata_bones[i].parent = BigLong(bone[i].parent);
1196 if (loadmodel->alias.aliasdata_bones[i].parent >= i)
1197 Host_Error("%s bone[%i].parent >= %i\n", loadmodel->name, i, i);
1200 //zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1201 vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1202 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
1203 for (i = 0;i < pheader->numverts;i++)
1205 vertbonecounts[i] = BigLong(bonecount[i]);
1206 if (vertbonecounts[i] < 1)
1207 Host_Error("%s bonecount[%i] < 1\n", loadmodel->name, i);
1210 //zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1211 verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
1212 vertdata = (void *) (pheader->lump_verts.start + pbase);
1213 for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
1215 verts[i].bonenum = BigLong(vertdata[i].bonenum);
1216 verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
1217 verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
1218 verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
1221 //zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1222 outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
1223 intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
1224 for (i = 0;i < pheader->numverts;i++)
1226 outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
1227 // flip T coordinate for OpenGL
1228 outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
1231 //zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1232 //loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
1233 //memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);
1235 loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
1237 //zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1238 //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)
1239 // byteswap, validate, and swap winding order of tris
1240 count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
1241 if (pheader->lump_render.length != count)
1242 Host_Error("%s renderlist is wrong size (%i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, count);
1243 renderlist = (void *) (pheader->lump_render.start + pbase);
1244 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
1245 for (i = 0;i < loadmodel->alias.aliasnum_meshes;i++)
1247 if (renderlist >= renderlistend)
1248 Host_Error("%s corrupt renderlist (wrong size)\n", loadmodel->name);
1249 count = BigLong(*renderlist);renderlist++;
1250 if (renderlist + count * 3 > renderlistend || (i == pheader->numshaders - 1 && renderlist + count * 3 != renderlistend))
1251 Host_Error("%s corrupt renderlist (wrong size)\n", loadmodel->name);
1252 mesh = loadmodel->alias.aliasdata_meshes + i;
1253 mesh->num_skins = loadmodel->numskins;
1254 mesh->num_triangles = count;
1255 mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
1256 mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
1257 mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
1258 outelements = mesh->data_element3i;
1259 for (j = 0;j < mesh->num_triangles;j++)
1261 outelements[2] = BigLong(renderlist[0]);
1262 outelements[1] = BigLong(renderlist[1]);
1263 outelements[0] = BigLong(renderlist[2]);
1264 if ((unsigned int)outelements[0] >= (unsigned int)pheader->numverts
1265 || (unsigned int)outelements[1] >= (unsigned int)pheader->numverts
1266 || (unsigned int)outelements[2] >= (unsigned int)pheader->numverts)
1267 Host_Error("%s corrupt renderlist (out of bounds index)\n", loadmodel->name);
1271 remapvertices = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
1272 mesh->num_vertices = Mod_BuildVertexRemapTableFromElements(mesh->num_triangles * 3, mesh->data_element3i, pheader->numverts, remapvertices);
1273 for (j = 0;j < mesh->num_triangles * 3;j++)
1274 mesh->data_element3i[j] = remapvertices[mesh->data_element3i[j]];
1275 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
1276 mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
1277 for (j = 0;j < pheader->numverts;j++)
1279 if (remapvertices[j] >= 0)
1281 mesh->data_texcoord2f[remapvertices[j]*2+0] = outtexcoord2f[j*2+0];
1282 mesh->data_texcoord2f[remapvertices[j]*2+1] = outtexcoord2f[j*2+1];
1285 mesh->num_vertexboneweights = 0;
1286 for (j = 0;j < mesh->num_vertices;j++)
1287 if (remapvertices[j] >= 0)
1288 mesh->num_vertexboneweights += vertbonecounts[remapvertices[j]];
1289 mesh->data_vertexboneweights = Mem_Alloc(loadmodel->mempool, mesh->num_vertexboneweights * sizeof(aliasvertexboneweight_t));
1290 mesh->num_vertexboneweights = 0;
1291 // note this vertexboneweight ordering requires that the remapvertices array is sequential numbers (separated by -1 values for omitted vertices)
1293 for (j = 0;j < mesh->num_vertices;j++)
1295 if (remapvertices[j] < 0)
1297 l += vertbonecounts[j];
1300 for (k = 0;k < vertbonecounts[j];k++)
1302 // this format really should have had a per vertexweight weight value...
1303 mesh->data_vertexboneweights[mesh->num_vertexboneweights].vertexindex = remapvertices[j];
1304 mesh->data_vertexboneweights[mesh->num_vertexboneweights].boneindex = verts[l].bonenum;
1305 mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[3] = 1.0f / vertbonecounts[j];
1306 mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[0] = verts[l].origin[0] * mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[3];
1307 mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[1] = verts[l].origin[1] * mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[3];
1308 mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[2] = verts[l].origin[2] * mesh->data_vertexboneweights[mesh->num_vertexboneweights].origin[3];
1309 mesh->num_vertexboneweights++;
1314 Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
1315 Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
1316 Mod_Alias_Mesh_CompileFrameZero(mesh);
1318 // since zym models do not have named sections, reuse their shader
1319 // name as the section name
1320 shadername = (char *) (pheader->lump_shaders.start + pbase) + i * 32;
1322 Mod_BuildAliasSkinsFromSkinFiles(mesh->data_skins, skinfiles, shadername, shadername);
1324 for (j = 0;j < mesh->num_skins;j++)
1325 Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins + j, NULL);
1328 Mem_Free(vertbonecounts);
1330 Mem_Free(outtexcoord2f);