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.
23 static cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0"};
25 void Mod_AliasInit (void)
27 Cvar_RegisterVariable(&r_mipskins);
30 // LordHavoc: proper bounding box considerations
31 static float aliasbboxmin[3], aliasbboxmax[3], modelyawradius, modelradius;
33 static float vertst[MAXALIASVERTS][2];
34 static int vertusage[MAXALIASVERTS];
35 static int vertonseam[MAXALIASVERTS];
36 static int vertremap[MAXALIASVERTS];
37 static int temptris[MAXALIASTRIS][3];
39 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
41 int i, j, invalidnormals = 0;
44 for (i = 0;i < inverts;i++)
46 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
49 temp[0] = v[i].v[0] * scale[0] + translate[0];
50 temp[1] = v[i].v[1] * scale[1] + translate[1];
51 temp[2] = v[i].v[2] * scale[2] + translate[2];
52 // update bounding box
53 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
54 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
55 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
56 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
57 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
58 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
59 dist = temp[0]*temp[0]+temp[1]*temp[1];
60 if (modelyawradius < dist)
61 modelyawradius = dist;
62 dist += temp[2]*temp[2];
63 if (modelradius < dist)
66 j = vertremap[i]; // not onseam
69 VectorCopy(v[i].v, out[j].v);
70 out[j].lightnormalindex = v[i].lightnormalindex;
71 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
74 out[j].lightnormalindex = 0;
77 j = vertremap[i+inverts]; // onseam
80 VectorCopy(v[i].v, out[j].v);
81 out[j].lightnormalindex = v[i].lightnormalindex;
82 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
85 out[j].lightnormalindex = 0;
90 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
93 static void Mod_MDL_LoadFrames (qbyte * datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate)
95 daliasframetype_t *pframetype;
96 daliasframe_t *pinframe;
98 daliasinterval_t *intervals;
99 int i, f, pose, groupframes;
103 scene = loadmodel->animscenes;
104 for (f = 0;f < loadmodel->numframes;f++)
106 pframetype = (daliasframetype_t *)datapointer;
107 datapointer += sizeof(daliasframetype_t);
108 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
110 // a single frame is still treated as a group
117 group = (daliasgroup_t *)datapointer;
118 datapointer += sizeof(daliasgroup_t);
119 groupframes = LittleLong (group->numframes);
121 // intervals (time per frame)
122 intervals = (daliasinterval_t *)datapointer;
123 datapointer += sizeof(daliasinterval_t) * groupframes;
125 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
126 if (interval < 0.01f)
127 Host_Error("Mod_LoadAliasGroup: invalid interval");
130 // get scene name from first frame
131 pinframe = (daliasframe_t *)datapointer;
133 strcpy(scene->name, pinframe->name);
134 scene->firstframe = pose;
135 scene->framecount = groupframes;
136 scene->framerate = 1.0f / interval;
141 for (i = 0;i < groupframes;i++)
143 pinframe = (daliasframe_t *)datapointer;
144 datapointer += sizeof(daliasframe_t);
146 // convert to MD2 frame headers
147 strcpy(loadmodel->mdlmd2data_frames[pose].name, pinframe->name);
148 VectorCopy(scale, loadmodel->mdlmd2data_frames[pose].scale);
149 VectorCopy(translate, loadmodel->mdlmd2data_frames[pose].translate);
151 Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->mdlmd2data_pose + pose * outverts);
152 datapointer += sizeof(trivertx_t) * inverts;
158 static rtexture_t *GL_SkinSplitShirt(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
160 int i, pixels, passed;
162 for (i = 0;i < 16;i++)
163 pixeltest[i] = (bits & (1 << i)) != 0;
164 pixels = width*height;
168 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
171 // turn to white while copying
172 if (*in >= 128 && *in < 224) // backwards ranges
173 *out = (*in & 15) ^ 15;
183 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
188 static rtexture_t *GL_SkinSplit(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
190 int i, pixels, passed;
192 for (i = 0;i < 16;i++)
193 pixeltest[i] = (bits & (1 << i)) != 0;
194 pixels = width*height;
198 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
209 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
214 static int Mod_LoadExternalSkin (char *basename, skinframe_t *skinframe, int precache)
216 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, va("%s_normal", basename), 0, 0, false, r_mipskins.integer, precache);
217 skinframe->fog = image_masktex;
218 if (!skinframe->base)
220 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, basename, 0, 0, false, r_mipskins.integer, precache);
221 skinframe->fog = image_masktex;
223 skinframe->pants = loadtextureimage(loadmodel->texturepool, va("%s_pants" , basename), 0, 0, false, r_mipskins.integer, precache);
224 skinframe->shirt = loadtextureimage(loadmodel->texturepool, va("%s_shirt" , basename), 0, 0, false, r_mipskins.integer, precache);
225 skinframe->glow = loadtextureimage(loadmodel->texturepool, va("%s_glow" , basename), 0, 0, false, r_mipskins.integer, precache);
226 skinframe->merged = NULL;
227 return skinframe->base != NULL || skinframe->pants != NULL || skinframe->shirt != NULL || skinframe->glow != NULL;
230 static int Mod_LoadInternalSkin (char *basename, qbyte *skindata, qbyte *skintemp, int width, int height, skinframe_t *skinframe, int precache)
232 if (!skindata || !skintemp)
234 skinframe->pants = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
235 skinframe->shirt = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
236 skinframe->glow = GL_SkinSplit (skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), precache); // glow
237 if (skinframe->pants || skinframe->shirt)
239 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
240 skinframe->merged = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), precache); // body (normal + pants + shirt, but not glow)
243 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), precache); // no special colors
244 // quake model skins don't have alpha
245 skinframe->fog = NULL;
249 #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);
250 #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);
251 void Mod_LoadAliasModel (model_t *mod, void *buffer)
253 int i, j, version, numverts, totalposes, totalskins, skinwidth, skinheight, totalverts, groupframes, groupskins;
255 stvert_t *pinstverts;
256 dtriangle_t *pintriangles;
257 daliasskintype_t *pinskintype;
258 daliasskingroup_t *pinskingroup;
259 daliasskininterval_t *pinskinintervals;
260 daliasframetype_t *pinframetype;
261 daliasgroup_t *pinframegroup;
262 float scales, scalet, scale[3], translate[3], interval;
263 qbyte *datapointer, *startframes, *startskins;
264 char name[MAX_QPATH];
265 qbyte *skintemp = NULL;
266 skinframe_t tempskinframe;
267 animscene_t *tempskinscenes;
268 skinframe_t *tempskinframes;
272 datapointer = buffer;
273 pinmodel = (mdl_t *)datapointer;
274 datapointer += sizeof(mdl_t);
276 version = LittleLong (pinmodel->version);
277 if (version != ALIAS_VERSION)
278 Host_Error ("%s has wrong version number (%i should be %i)",
279 loadmodel->name, version, ALIAS_VERSION);
281 loadmodel->type = mod_alias;
282 loadmodel->aliastype = ALIASTYPE_MDLMD2;
284 loadmodel->numskins = LittleLong(pinmodel->numskins);
285 BOUNDI(loadmodel->numskins,0,256);
286 skinwidth = LittleLong (pinmodel->skinwidth);
287 BOUNDI(skinwidth,0,4096);
288 skinheight = LittleLong (pinmodel->skinheight);
289 BOUNDI(skinheight,0,4096);
290 loadmodel->numverts = numverts = LittleLong(pinmodel->numverts);
291 BOUNDI(loadmodel->numverts,0,MAXALIASVERTS);
292 loadmodel->numtris = LittleLong(pinmodel->numtris);
293 BOUNDI(loadmodel->numtris,0,MAXALIASTRIS);
294 loadmodel->numframes = LittleLong(pinmodel->numframes);
295 BOUNDI(loadmodel->numframes,0,65536);
296 loadmodel->synctype = LittleLong (pinmodel->synctype);
297 BOUNDI(loadmodel->synctype,0,2);
298 loadmodel->flags = LittleLong (pinmodel->flags);
300 for (i = 0;i < 3;i++)
302 scale[i] = LittleFloat (pinmodel->scale[i]);
303 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
306 startskins = datapointer;
308 for (i = 0;i < loadmodel->numskins;i++)
310 pinskintype = (daliasskintype_t *)datapointer;
311 datapointer += sizeof(daliasskintype_t);
312 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
316 pinskingroup = (daliasskingroup_t *)datapointer;
317 datapointer += sizeof(daliasskingroup_t);
318 groupskins = LittleLong(pinskingroup->numskins);
319 datapointer += sizeof(daliasskininterval_t) * groupskins;
322 for (j = 0;j < groupskins;j++)
324 datapointer += skinwidth * skinheight;
329 pinstverts = (stvert_t *)datapointer;
330 datapointer += sizeof(stvert_t) * numverts;
332 pintriangles = (dtriangle_t *)datapointer;
333 datapointer += sizeof(dtriangle_t) * loadmodel->numtris;
335 startframes = datapointer;
337 for (i = 0;i < loadmodel->numframes;i++)
339 pinframetype = (daliasframetype_t *)datapointer;
340 datapointer += sizeof(daliasframetype_t);
341 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
345 pinframegroup = (daliasgroup_t *)datapointer;
346 datapointer += sizeof(daliasgroup_t);
347 groupframes = LittleLong(pinframegroup->numframes);
348 datapointer += sizeof(daliasinterval_t) * groupframes;
351 for (j = 0;j < groupframes;j++)
353 datapointer += sizeof(daliasframe_t);
354 datapointer += sizeof(trivertx_t) * numverts;
360 skintemp = Mem_Alloc(tempmempool, skinwidth * skinheight);
361 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
362 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
364 datapointer = startskins;
365 for (i = 0;i < loadmodel->numskins;i++)
367 pinskintype = (daliasskintype_t *)datapointer;
368 datapointer += sizeof(daliasskintype_t);
370 if (pinskintype->type == ALIAS_SKIN_SINGLE)
377 pinskingroup = (daliasskingroup_t *)datapointer;
378 datapointer += sizeof(daliasskingroup_t);
380 groupskins = LittleLong (pinskingroup->numskins);
382 pinskinintervals = (daliasskininterval_t *)datapointer;
383 datapointer += sizeof(daliasskininterval_t) * groupskins;
385 interval = LittleFloat(pinskinintervals[0].interval);
386 if (interval < 0.01f)
387 Host_Error("Mod_LoadAliasModel: invalid interval\n");
390 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
391 loadmodel->skinscenes[i].firstframe = totalskins;
392 loadmodel->skinscenes[i].framecount = groupskins;
393 loadmodel->skinscenes[i].framerate = 1.0f / interval;
394 loadmodel->skinscenes[i].loop = true;
396 for (j = 0;j < groupskins;j++)
399 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
401 sprintf (name, "%s_%i", loadmodel->name, i);
402 if (!Mod_LoadExternalSkin(name, loadmodel->skinframes + totalskins, i == 0))
403 Mod_LoadInternalSkin(name, (qbyte *)datapointer, skintemp, skinwidth, skinheight, loadmodel->skinframes + totalskins, i == 0);
404 datapointer += skinwidth * skinheight;
409 // check for skins that don't exist in the model, but do exist as external images
410 // (this was added because yummyluv kept pestering me about support for it)
413 sprintf (name, "%s_%i", loadmodel->name, loadmodel->numskins);
414 if (Mod_LoadExternalSkin(name, &tempskinframe, loadmodel->numskins == 0))
416 // expand the arrays to make room
417 tempskinscenes = loadmodel->skinscenes;
418 tempskinframes = loadmodel->skinframes;
419 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
420 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(skinframe_t));
421 memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
422 memcpy(loadmodel->skinframes, tempskinframes, totalskins * sizeof(skinframe_t));
423 Mem_Free(tempskinscenes);
424 Mem_Free(tempskinframes);
425 // store the info about the new skin
426 strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
427 loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
428 loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
429 loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
430 loadmodel->skinscenes[loadmodel->numskins].loop = true;
431 loadmodel->skinframes[totalskins] = tempskinframe;
432 loadmodel->numskins++;
439 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
440 scales = 1.0 / skinwidth;
441 scalet = 1.0 / skinheight;
442 for (i = 0;i < numverts;i++)
444 vertonseam[i] = LittleLong(pinstverts[i].onseam);
445 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
446 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
447 vertst[i+numverts][0] = vertst[i][0] + 0.5;
448 vertst[i+numverts][1] = vertst[i][1];
450 vertusage[i+numverts] = 0;
453 // load triangle data
454 loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->numtris);
456 // count the vertices used
457 for (i = 0;i < numverts*2;i++)
459 for (i = 0;i < loadmodel->numtris;i++)
461 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
462 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
463 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
464 if (!LittleLong(pintriangles[i].facesfront)) // backface
466 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
467 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
468 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
470 vertusage[temptris[i][0]]++;
471 vertusage[temptris[i][1]]++;
472 vertusage[temptris[i][2]]++;
474 // build remapping table and compact array
476 for (i = 0;i < numverts*2;i++)
480 vertremap[i] = totalverts;
481 vertst[totalverts][0] = vertst[i][0];
482 vertst[totalverts][1] = vertst[i][1];
486 vertremap[i] = -1; // not used at all
488 loadmodel->numverts = totalverts;
489 // remap the triangle references
490 for (i = 0;i < loadmodel->numtris;i++)
492 loadmodel->mdlmd2data_indices[i*3+0] = vertremap[temptris[i][0]];
493 loadmodel->mdlmd2data_indices[i*3+1] = vertremap[temptris[i][1]];
494 loadmodel->mdlmd2data_indices[i*3+2] = vertremap[temptris[i][2]];
496 // store the texture coordinates
497 loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
498 for (i = 0;i < totalverts;i++)
500 loadmodel->mdlmd2data_texcoords[i*2+0] = vertst[i][0];
501 loadmodel->mdlmd2data_texcoords[i*2+1] = vertst[i][1];
505 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
506 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, sizeof(md2frame_t) * totalposes);
507 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * totalposes * totalverts);
509 // LordHavoc: doing proper bbox for model
510 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
511 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
513 Mod_MDL_LoadFrames (startframes, numverts, totalverts, scale, translate);
515 modelyawradius = sqrt(modelyawradius);
516 modelradius = sqrt(modelradius);
517 for (j = 0;j < 3;j++)
519 loadmodel->normalmins[j] = aliasbboxmin[j];
520 loadmodel->normalmaxs[j] = aliasbboxmax[j];
521 loadmodel->rotatedmins[j] = -modelradius;
522 loadmodel->rotatedmaxs[j] = modelradius;
524 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
525 loadmodel->yawmins[2] = loadmodel->normalmins[2];
526 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
528 loadmodel->Draw = R_DrawQ1Q2AliasModel;
529 loadmodel->DrawSky = NULL;
530 loadmodel->DrawShadow = NULL;
533 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out, int *vertremap)
535 int i, invalidnormals = 0;
539 for (i = 0;i < loadmodel->numverts;i++)
541 in = v + vertremap[i];
542 VectorCopy(in->v, out[i].v);
543 temp[0] = in->v[0] * scale[0] + translate[0];
544 temp[1] = in->v[1] * scale[1] + translate[1];
545 temp[2] = in->v[2] * scale[2] + translate[2];
546 // update bounding box
547 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
548 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
549 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
550 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
551 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
552 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
553 dist = temp[0]*temp[0]+temp[1]*temp[1];
554 if (modelyawradius < dist)
555 modelyawradius = dist;
556 dist += temp[2]*temp[2];
557 if (modelradius < dist)
559 out[i].lightnormalindex = in->lightnormalindex;
560 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
563 out[i].lightnormalindex = 0;
567 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
570 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
576 int i, j, k, hashindex, num, numxyz, numst, xyz, st;
577 float *stverts, s, t;
580 struct md2verthash_s *next;
584 *hash, **md2verthash, *md2verthashdata;
586 md2frame_t *pinframe;
588 md2triangle_t *intri;
589 unsigned short *inst;
590 int skinwidth, skinheight;
595 version = LittleLong (pinmodel->version);
596 if (version != MD2ALIAS_VERSION)
597 Host_Error ("%s has wrong version number (%i should be %i)",
598 loadmodel->name, version, MD2ALIAS_VERSION);
600 loadmodel->type = mod_alias;
601 loadmodel->aliastype = ALIASTYPE_MDLMD2;
602 loadmodel->Draw = R_DrawQ1Q2AliasModel;
603 loadmodel->DrawSky = NULL;
604 loadmodel->DrawShadow = NULL;
606 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
607 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
608 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
609 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
610 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
611 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
612 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
613 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
615 end = LittleLong(pinmodel->ofs_end);
616 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
617 Host_Error ("%s is not a valid model", loadmodel->name);
618 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
619 Host_Error ("%s is not a valid model", loadmodel->name);
620 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
621 Host_Error ("%s is not a valid model", loadmodel->name);
622 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
623 Host_Error ("%s is not a valid model", loadmodel->name);
624 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
625 Host_Error ("%s is not a valid model", loadmodel->name);
627 loadmodel->numskins = LittleLong(pinmodel->num_skins);
628 numxyz = LittleLong(pinmodel->num_xyz);
629 numst = LittleLong(pinmodel->num_st);
630 loadmodel->numtris = LittleLong(pinmodel->num_tris);
631 loadmodel->numframes = LittleLong(pinmodel->num_frames);
633 loadmodel->flags = 0; // there are no MD2 flags
634 loadmodel->synctype = ST_RAND;
637 inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
638 if (loadmodel->numskins)
640 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
641 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
642 for (i = 0;i < loadmodel->numskins;i++)
644 loadmodel->skinscenes[i].firstframe = i;
645 loadmodel->skinscenes[i].framecount = 1;
646 loadmodel->skinscenes[i].loop = true;
647 loadmodel->skinscenes[i].framerate = 10;
648 loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, inskin, 0, 0, true, r_mipskins.integer, true);
649 loadmodel->skinframes[i].fog = image_masktex;
650 loadmodel->skinframes[i].pants = NULL;
651 loadmodel->skinframes[i].shirt = NULL;
652 loadmodel->skinframes[i].glow = NULL;
653 loadmodel->skinframes[i].merged = NULL;
654 inskin += MD2MAX_SKINNAME;
658 // load the triangles and stvert data
659 inst = (void*)(base + LittleLong(pinmodel->ofs_st));
660 intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
661 skinwidth = LittleLong(pinmodel->skinwidth);
662 skinheight = LittleLong(pinmodel->skinheight);
664 stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
665 s = 1.0f / skinwidth;
666 t = 1.0f / skinheight;
667 for (i = 0;i < numst;i++)
669 j = (unsigned short) LittleShort(inst[i*2+0]);
670 k = (unsigned short) LittleShort(inst[i*2+1]);
671 if (j >= skinwidth || k >= skinheight)
674 Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
676 stverts[i*2+0] = j * s;
677 stverts[i*2+1] = k * t;
680 md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
681 md2verthashdata = Mem_Alloc(tempmempool, loadmodel->numtris * 3 * sizeof(*hash));
682 // swap the triangle list
684 loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(int[3]));
685 for (i = 0;i < loadmodel->numtris;i++)
687 for (j = 0;j < 3;j++)
689 xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
690 st = (unsigned short) LittleShort (intri[i].index_st[j]);
691 if (xyz >= numxyz || st >= numst)
693 Mem_Free(md2verthash);
694 Mem_Free(md2verthashdata);
697 Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
699 Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
703 hashindex = (xyz * 17 + st) & 255;
704 for (hash = md2verthash[hashindex];hash;hash = hash->next)
705 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
709 hash = md2verthashdata + num++;
713 hash->next = md2verthash[hashindex];
714 md2verthash[hashindex] = hash;
716 loadmodel->mdlmd2data_indices[i*3+j] = (hash - md2verthashdata);
722 loadmodel->numverts = num;
723 vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
724 loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
725 for (i = 0;i < num;i++)
727 hash = md2verthashdata + i;
728 vertremap[i] = hash->xyz;
729 loadmodel->mdlmd2data_texcoords[i*2+0] = hash->st[0];
730 loadmodel->mdlmd2data_texcoords[i*2+1] = hash->st[1];
733 Mem_Free(md2verthash);
734 Mem_Free(md2verthashdata);
737 // LordHavoc: doing proper bbox for model
738 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
739 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
743 datapointer = (base + LittleLong(pinmodel->ofs_frames));
745 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
746 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
747 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
749 for (i = 0;i < loadmodel->numframes;i++)
751 pinframe = (md2frame_t *)datapointer;
752 datapointer += sizeof(md2frame_t);
753 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
754 for (j = 0;j < 3;j++)
756 loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
757 loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
759 Mod_MD2_ConvertVerts (loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts], vertremap);
760 datapointer += numxyz * sizeof(trivertx_t);
762 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
763 loadmodel->animscenes[i].firstframe = i;
764 loadmodel->animscenes[i].framecount = 1;
765 loadmodel->animscenes[i].framerate = 10;
766 loadmodel->animscenes[i].loop = true;
771 // LordHavoc: model bbox
772 modelyawradius = sqrt(modelyawradius);
773 modelradius = sqrt(modelradius);
774 for (j = 0;j < 3;j++)
776 loadmodel->normalmins[j] = aliasbboxmin[j];
777 loadmodel->normalmaxs[j] = aliasbboxmax[j];
778 loadmodel->rotatedmins[j] = -modelradius;
779 loadmodel->rotatedmaxs[j] = modelradius;
781 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
782 loadmodel->yawmins[2] = loadmodel->normalmins[2];
783 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
786 static void zymswapintblock(int *m, int size)
796 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
798 int i, pbase, *bonecount, numposes;
799 unsigned int count, a, b, c, *renderlist, *renderlistend;
800 rtexture_t **texture;
802 zymtype1header_t *pinmodel, *pheader;
805 float corner[2], modelradius;
807 pinmodel = (void *)buffer;
809 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
810 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
812 if (BigLong(pinmodel->type) != 1)
813 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
815 loadmodel->type = mod_alias;
816 loadmodel->aliastype = ALIASTYPE_ZYM;
818 loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
820 pbase = (int) pheader;
822 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
825 #define SWAPLONG(var) var = BigLong(var)
826 #define SWAPFLOAT(var) var = BigFloat(var)
827 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
828 SWAPLONG(pheader->type);
829 SWAPLONG(pheader->filesize);
830 SWAPVEC(pheader->mins);
831 SWAPVEC(pheader->maxs);
832 SWAPFLOAT(pheader->radius);
833 SWAPLONG(pheader->numverts);
834 SWAPLONG(pheader->numtris);
835 SWAPLONG(pheader->numshaders);
836 SWAPLONG(pheader->numbones);
837 SWAPLONG(pheader->numscenes);
839 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
840 SWAPLUMPINFO(scenes);
843 SWAPLUMPINFO(vertbonecounts);
845 SWAPLUMPINFO(texcoords);
846 SWAPLUMPINFO(render);
847 SWAPLUMPINFO(shaders);
848 SWAPLUMPINFO(trizone);
850 loadmodel->flags = 0; // there are no flags
851 loadmodel->numframes = pheader->numscenes;
852 loadmodel->synctype = ST_SYNC;
853 loadmodel->numtris = pheader->numtris;
855 // FIXME: add skin support and texturing and shaders and...
856 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
857 loadmodel->skinscenes[0].firstframe = 0;
858 loadmodel->skinscenes[0].framecount = 1;
859 loadmodel->skinscenes[0].loop = true;
860 loadmodel->skinscenes[0].framerate = 10;
861 loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
862 loadmodel->skinframes->base = NULL;
863 loadmodel->skinframes->fog = NULL;
864 loadmodel->skinframes->pants = NULL;
865 loadmodel->skinframes->shirt = NULL;
866 loadmodel->skinframes->glow = NULL;
867 loadmodel->skinframes->merged = NULL;
868 loadmodel->numskins = 1;
869 numposes = pheader->lump_poses.length / sizeof(float[3][4]) / pheader->numbones;
871 // go through the lumps, swapping things
873 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
874 scene = (void *) (pheader->lump_scenes.start + pbase);
875 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
876 for (i = 0;i < pheader->numscenes;i++)
878 SWAPVEC(scene->mins);
879 SWAPVEC(scene->maxs);
880 SWAPFLOAT(scene->radius);
881 SWAPFLOAT(scene->framerate);
882 SWAPLONG(scene->flags);
883 SWAPLONG(scene->start);
884 SWAPLONG(scene->length);
886 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
887 loadmodel->animscenes[i].firstframe = scene->start;
888 loadmodel->animscenes[i].framecount = scene->length;
889 loadmodel->animscenes[i].framerate = scene->framerate;
890 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
892 if ((unsigned int) loadmodel->animscenes[i].firstframe >= numposes)
893 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
894 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > numposes)
895 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
896 if (loadmodel->animscenes[i].framerate < 0)
897 Host_Error("Mod_LoadZymoticModel: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
901 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
902 zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
904 // zymlump_t lump_bones; // zymbone_t bone[numbones];
905 bone = (void *) (pheader->lump_bones.start + pbase);
906 for (i = 0;i < pheader->numbones;i++)
908 SWAPLONG(bone[i].flags);
909 SWAPLONG(bone[i].parent);
910 if (bone[i].parent >= i)
911 Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
914 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
915 zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
916 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
917 for (i = 0;i < pheader->numbones;i++)
918 if (bonecount[i] < 1)
919 Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
921 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
922 zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
924 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
925 zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
927 // 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)
928 zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
929 // validate renderlist and swap winding order of tris
930 renderlist = (void *) (pheader->lump_render.start + pbase);
931 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
932 i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
933 if (pheader->lump_render.length != i)
934 Host_Error("Mod_LoadZymoticModel: renderlist is wrong size in %s (is %i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, i);
935 for (i = 0;i < pheader->numshaders;i++)
937 if (renderlist >= renderlistend)
938 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
939 count = *renderlist++;
940 if (renderlist + count * 3 > renderlistend)
941 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
947 if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
948 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
956 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
957 shadername = (void *) (pheader->lump_shaders.start + pbase);
958 texture = (void *) shadername;
959 for (i = 0;i < pheader->numshaders;i++)
962 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
964 *texture++ = rt; // reuse shader name list for texture pointers
967 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
968 zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
971 modelradius = pheader->radius;
972 for (i = 0;i < 3;i++)
974 loadmodel->normalmins[i] = pheader->mins[i];
975 loadmodel->normalmaxs[i] = pheader->maxs[i];
976 loadmodel->rotatedmins[i] = -modelradius;
977 loadmodel->rotatedmaxs[i] = modelradius;
979 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
980 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
981 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
982 if (loadmodel->yawmaxs[0] > modelradius)
983 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
984 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
985 loadmodel->yawmins[2] = loadmodel->normalmins[2];
986 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
988 loadmodel->Draw = R_DrawZymoticModel;
989 loadmodel->DrawSky = NULL;
990 loadmodel->DrawShadow = NULL;