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 static void Mod_Alias_SERAddEntity(void)
32 R_Clip_AddBox(currentrenderentity->mins, currentrenderentity->maxs, R_Entity_Callback, currentrenderentity, NULL);
35 // LordHavoc: proper bounding box considerations
36 static float aliasbboxmin[3], aliasbboxmax[3], modelyawradius, modelradius;
38 static float vertst[MAXALIASVERTS][2];
39 static int vertusage[MAXALIASVERTS];
40 static int vertonseam[MAXALIASVERTS];
41 static int vertremap[MAXALIASVERTS];
42 static int temptris[MAXALIASTRIS][3];
44 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
46 int i, j, invalidnormals = 0;
49 for (i = 0;i < inverts;i++)
51 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
54 temp[0] = v[i].v[0] * scale[0] + translate[0];
55 temp[1] = v[i].v[1] * scale[1] + translate[1];
56 temp[2] = v[i].v[2] * scale[2] + translate[2];
57 // update bounding box
58 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
59 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
60 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
61 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
62 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
63 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
64 dist = temp[0]*temp[0]+temp[1]*temp[1];
65 if (modelyawradius < dist)
66 modelyawradius = dist;
67 dist += temp[2]*temp[2];
68 if (modelradius < dist)
71 j = vertremap[i]; // not onseam
74 VectorCopy(v[i].v, out[j].v);
75 out[j].lightnormalindex = v[i].lightnormalindex;
76 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
79 out[j].lightnormalindex = 0;
82 j = vertremap[i+inverts]; // onseam
85 VectorCopy(v[i].v, out[j].v);
86 out[j].lightnormalindex = v[i].lightnormalindex;
87 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
90 out[j].lightnormalindex = 0;
95 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
98 static void Mod_MDL_LoadFrames (qbyte * datapointer, int inverts, int outverts, vec3_t scale, vec3_t translate)
100 daliasframetype_t *pframetype;
101 daliasframe_t *pinframe;
102 daliasgroup_t *group;
103 daliasinterval_t *intervals;
104 int i, f, pose, groupframes;
108 scene = loadmodel->animscenes;
109 for (f = 0;f < loadmodel->numframes;f++)
111 pframetype = (daliasframetype_t *)datapointer;
112 datapointer += sizeof(daliasframetype_t);
113 if (LittleLong (pframetype->type) == ALIAS_SINGLE)
115 // a single frame is still treated as a group
122 group = (daliasgroup_t *)datapointer;
123 datapointer += sizeof(daliasgroup_t);
124 groupframes = LittleLong (group->numframes);
126 // intervals (time per frame)
127 intervals = (daliasinterval_t *)datapointer;
128 datapointer += sizeof(daliasinterval_t) * groupframes;
130 interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
131 if (interval < 0.01f)
132 Host_Error("Mod_LoadAliasGroup: invalid interval");
135 // get scene name from first frame
136 pinframe = (daliasframe_t *)datapointer;
138 strcpy(scene->name, pinframe->name);
139 scene->firstframe = pose;
140 scene->framecount = groupframes;
141 scene->framerate = 1.0f / interval;
146 for (i = 0;i < groupframes;i++)
148 pinframe = (daliasframe_t *)datapointer;
149 datapointer += sizeof(daliasframe_t);
151 // convert to MD2 frame headers
152 strcpy(loadmodel->mdlmd2data_frames[pose].name, pinframe->name);
153 VectorCopy(scale, loadmodel->mdlmd2data_frames[pose].scale);
154 VectorCopy(translate, loadmodel->mdlmd2data_frames[pose].translate);
156 Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->mdlmd2data_pose + pose * outverts);
157 datapointer += sizeof(trivertx_t) * inverts;
163 static rtexture_t *GL_SkinSplitShirt(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
165 int i, pixels, passed;
167 for (i = 0;i < 16;i++)
168 pixeltest[i] = (bits & (1 << i)) != 0;
169 pixels = width*height;
173 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
176 // turn to white while copying
177 if (*in >= 128 && *in < 224) // backwards ranges
178 *out = (*in & 15) ^ 15;
188 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
193 static rtexture_t *GL_SkinSplit(qbyte *in, qbyte *out, int width, int height, int bits, char *name, int precache)
195 int i, pixels, passed;
197 for (i = 0;i < 16;i++)
198 pixeltest[i] = (bits & (1 << i)) != 0;
199 pixels = width*height;
203 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
214 return R_LoadTexture (loadmodel->texturepool, name, width, height, out - width*height, TEXTYPE_QPALETTE, (r_mipskins.integer ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
219 static void Mod_LoadSkin (char *basename, qbyte *skindata, qbyte *skintemp, int width, int height, skinframe_t *skinframe, int precache)
221 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, va("%s_normal", basename), 0, 0, false, r_mipskins.integer, precache);
222 skinframe->fog = image_masktex;
223 skinframe->pants = NULL;
224 skinframe->shirt = NULL;
225 skinframe->glow = loadtextureimage(loadmodel->texturepool, va("%s_glow" , basename), 0, 0, false, r_mipskins.integer, precache);
226 skinframe->merged = NULL;
229 skinframe->pants = loadtextureimage(loadmodel->texturepool, va("%s_pants" , basename), 0, 0, false, r_mipskins.integer, precache);
230 skinframe->shirt = loadtextureimage(loadmodel->texturepool, va("%s_shirt" , basename), 0, 0, false, r_mipskins.integer, precache);
234 skinframe->base = loadtextureimagewithmask(loadmodel->texturepool, basename, 0, 0, false, true, true);
235 skinframe->fog = image_masktex;
236 if (!skinframe->base)
238 skinframe->pants = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
239 skinframe->shirt = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
240 skinframe->glow = GL_SkinSplit (skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), precache); // glow
241 if (skinframe->pants || skinframe->shirt)
243 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
244 skinframe->merged = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), precache); // body (normal + pants + shirt, but not glow)
247 skinframe->base = GL_SkinSplit (skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), precache); // no special colors
248 // quake model skins don't have alpha
249 skinframe->fog = NULL;
254 #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);
255 #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);
256 void Mod_LoadAliasModel (model_t *mod, void *buffer)
258 int i, j, version, numverts, totalposes, totalskins, skinwidth, skinheight, totalverts, groupframes, groupskins;
260 stvert_t *pinstverts;
261 dtriangle_t *pintriangles;
262 daliasskintype_t *pinskintype;
263 daliasskingroup_t *pinskingroup;
264 daliasskininterval_t *pinskinintervals;
265 daliasframetype_t *pinframetype;
266 daliasgroup_t *pinframegroup;
267 float scales, scalet, scale[3], translate[3], interval;
268 qbyte *datapointer, *startframes, *startskins;
269 char name[MAX_QPATH];
270 qbyte *skintemp = NULL;
274 datapointer = buffer;
275 pinmodel = (mdl_t *)datapointer;
276 datapointer += sizeof(mdl_t);
278 version = LittleLong (pinmodel->version);
279 if (version != ALIAS_VERSION)
280 Host_Error ("%s has wrong version number (%i should be %i)",
281 loadmodel->name, version, ALIAS_VERSION);
283 loadmodel->type = mod_alias;
284 loadmodel->aliastype = ALIASTYPE_MDLMD2;
286 loadmodel->numskins = LittleLong(pinmodel->numskins);
287 BOUNDI(loadmodel->numskins,0,256);
288 skinwidth = LittleLong (pinmodel->skinwidth);
289 BOUNDI(skinwidth,0,4096);
290 skinheight = LittleLong (pinmodel->skinheight);
291 BOUNDI(skinheight,0,4096);
292 loadmodel->numverts = numverts = LittleLong(pinmodel->numverts);
293 BOUNDI(loadmodel->numverts,0,MAXALIASVERTS);
294 loadmodel->numtris = LittleLong(pinmodel->numtris);
295 BOUNDI(loadmodel->numtris,0,MAXALIASTRIS);
296 loadmodel->numframes = LittleLong(pinmodel->numframes);
297 BOUNDI(loadmodel->numframes,0,65536);
298 loadmodel->synctype = LittleLong (pinmodel->synctype);
299 BOUNDI(loadmodel->synctype,0,2);
300 loadmodel->flags = LittleLong (pinmodel->flags);
302 for (i = 0;i < 3;i++)
304 scale[i] = LittleFloat (pinmodel->scale[i]);
305 translate[i] = LittleFloat (pinmodel->scale_origin[i]);
308 startskins = datapointer;
310 for (i = 0;i < loadmodel->numskins;i++)
312 pinskintype = (daliasskintype_t *)datapointer;
313 datapointer += sizeof(daliasskintype_t);
314 if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
318 pinskingroup = (daliasskingroup_t *)datapointer;
319 datapointer += sizeof(daliasskingroup_t);
320 groupskins = LittleLong(pinskingroup->numskins);
321 datapointer += sizeof(daliasskininterval_t) * groupskins;
324 for (j = 0;j < groupskins;j++)
326 datapointer += skinwidth * skinheight;
331 pinstverts = (stvert_t *)datapointer;
332 datapointer += sizeof(stvert_t) * numverts;
334 pintriangles = (dtriangle_t *)datapointer;
335 datapointer += sizeof(dtriangle_t) * loadmodel->numtris;
337 startframes = datapointer;
339 for (i = 0;i < loadmodel->numframes;i++)
341 pinframetype = (daliasframetype_t *)datapointer;
342 datapointer += sizeof(daliasframetype_t);
343 if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
347 pinframegroup = (daliasgroup_t *)datapointer;
348 datapointer += sizeof(daliasgroup_t);
349 groupframes = LittleLong(pinframegroup->numframes);
350 datapointer += sizeof(daliasinterval_t) * groupframes;
353 for (j = 0;j < groupframes;j++)
355 datapointer += sizeof(daliasframe_t);
356 datapointer += sizeof(trivertx_t) * numverts;
362 skintemp = Mem_Alloc(tempmempool, skinwidth * skinheight);
363 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
364 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
366 datapointer = startskins;
367 for (i = 0;i < loadmodel->numskins;i++)
369 pinskintype = (daliasskintype_t *)datapointer;
370 datapointer += sizeof(daliasskintype_t);
372 if (pinskintype->type == ALIAS_SKIN_SINGLE)
379 pinskingroup = (daliasskingroup_t *)datapointer;
380 datapointer += sizeof(daliasskingroup_t);
382 groupskins = LittleLong (pinskingroup->numskins);
384 pinskinintervals = (daliasskininterval_t *)datapointer;
385 datapointer += sizeof(daliasskininterval_t) * groupskins;
387 interval = LittleFloat(pinskinintervals[0].interval);
388 if (interval < 0.01f)
389 Host_Error("Mod_LoadAliasModel: invalid interval\n");
392 sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
393 loadmodel->skinscenes[i].firstframe = totalskins;
394 loadmodel->skinscenes[i].framecount = groupskins;
395 loadmodel->skinscenes[i].framerate = 1.0f / interval;
396 loadmodel->skinscenes[i].loop = true;
398 for (j = 0;j < groupskins;j++)
401 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
403 sprintf (name, "%s_%i", loadmodel->name, i);
404 Mod_LoadSkin(name, (qbyte *)datapointer, skintemp, skinwidth, skinheight, loadmodel->skinframes + totalskins, i == 0);
405 datapointer += skinwidth * skinheight;
411 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
412 scales = 1.0 / skinwidth;
413 scalet = 1.0 / skinheight;
414 for (i = 0;i < numverts;i++)
416 vertonseam[i] = LittleLong(pinstverts[i].onseam);
417 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
418 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
419 vertst[i+numverts][0] = vertst[i][0] + 0.5;
420 vertst[i+numverts][1] = vertst[i][1];
422 vertusage[i+numverts] = 0;
425 // load triangle data
426 loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->numtris);
428 // count the vertices used
429 for (i = 0;i < numverts*2;i++)
431 for (i = 0;i < loadmodel->numtris;i++)
433 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
434 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
435 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
436 if (!LittleLong(pintriangles[i].facesfront)) // backface
438 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
439 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
440 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
442 vertusage[temptris[i][0]]++;
443 vertusage[temptris[i][1]]++;
444 vertusage[temptris[i][2]]++;
446 // build remapping table and compact array
448 for (i = 0;i < numverts*2;i++)
452 vertremap[i] = totalverts;
453 vertst[totalverts][0] = vertst[i][0];
454 vertst[totalverts][1] = vertst[i][1];
458 vertremap[i] = -1; // not used at all
460 loadmodel->numverts = totalverts;
461 // remap the triangle references
462 for (i = 0;i < loadmodel->numtris;i++)
464 loadmodel->mdlmd2data_indices[i*3+0] = vertremap[temptris[i][0]];
465 loadmodel->mdlmd2data_indices[i*3+1] = vertremap[temptris[i][1]];
466 loadmodel->mdlmd2data_indices[i*3+2] = vertremap[temptris[i][2]];
468 // store the texture coordinates
469 loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * totalverts);
470 for (i = 0;i < totalverts;i++)
472 loadmodel->mdlmd2data_texcoords[i*2+0] = vertst[i][0];
473 loadmodel->mdlmd2data_texcoords[i*2+1] = vertst[i][1];
477 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
478 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, sizeof(md2frame_t) * totalposes);
479 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, sizeof(trivertx_t) * totalposes * totalverts);
481 // LordHavoc: doing proper bbox for model
482 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
483 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
485 Mod_MDL_LoadFrames (startframes, numverts, totalverts, scale, translate);
487 modelyawradius = sqrt(modelyawradius);
488 modelradius = sqrt(modelradius);
489 for (j = 0;j < 3;j++)
491 loadmodel->normalmins[j] = aliasbboxmin[j];
492 loadmodel->normalmaxs[j] = aliasbboxmax[j];
493 loadmodel->rotatedmins[j] = -modelradius;
494 loadmodel->rotatedmaxs[j] = modelradius;
496 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
497 loadmodel->yawmins[2] = loadmodel->normalmins[2];
498 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
500 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
501 loadmodel->Draw = R_DrawAliasModel;
502 loadmodel->DrawSky = NULL;
503 loadmodel->DrawShadow = NULL;
506 static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out, int *vertremap)
508 int i, invalidnormals = 0;
512 for (i = 0;i < loadmodel->numverts;i++)
514 in = v + vertremap[i];
515 VectorCopy(in->v, out[i].v);
516 temp[0] = in->v[0] * scale[0] + translate[0];
517 temp[1] = in->v[1] * scale[1] + translate[1];
518 temp[2] = in->v[2] * scale[2] + translate[2];
519 // update bounding box
520 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
521 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
522 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
523 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
524 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
525 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
526 dist = temp[0]*temp[0]+temp[1]*temp[1];
527 if (modelyawradius < dist)
528 modelyawradius = dist;
529 dist += temp[2]*temp[2];
530 if (modelradius < dist)
532 out[i].lightnormalindex = in->lightnormalindex;
533 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
536 out[i].lightnormalindex = 0;
540 Con_Printf("Mod_MD2_ConvertVerts: \"%s\", %i invalid normal indices found\n", loadmodel->name, invalidnormals);
543 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
549 int i, j, k, hashindex, num, numxyz, numst, xyz, st;
550 float *stverts, s, t;
553 struct md2verthash_s *next;
557 *hash, **md2verthash, *md2verthashdata;
559 md2frame_t *pinframe;
561 md2triangle_t *intri;
562 unsigned short *inst;
563 int skinwidth, skinheight;
568 version = LittleLong (pinmodel->version);
569 if (version != MD2ALIAS_VERSION)
570 Host_Error ("%s has wrong version number (%i should be %i)",
571 loadmodel->name, version, MD2ALIAS_VERSION);
573 loadmodel->type = mod_alias;
574 loadmodel->aliastype = ALIASTYPE_MDLMD2;
575 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
576 loadmodel->Draw = R_DrawAliasModel;
577 loadmodel->DrawSky = NULL;
578 loadmodel->DrawShadow = NULL;
580 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
581 Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
582 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
583 Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
584 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
585 Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
586 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
587 Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
589 end = LittleLong(pinmodel->ofs_end);
590 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
591 Host_Error ("%s is not a valid model", loadmodel->name);
592 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
593 Host_Error ("%s is not a valid model", loadmodel->name);
594 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
595 Host_Error ("%s is not a valid model", loadmodel->name);
596 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
597 Host_Error ("%s is not a valid model", loadmodel->name);
598 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
599 Host_Error ("%s is not a valid model", loadmodel->name);
601 loadmodel->numskins = LittleLong(pinmodel->num_skins);
602 numxyz = LittleLong(pinmodel->num_xyz);
603 numst = LittleLong(pinmodel->num_st);
604 loadmodel->numtris = LittleLong(pinmodel->num_tris);
605 loadmodel->numframes = LittleLong(pinmodel->num_frames);
607 loadmodel->flags = 0; // there are no MD2 flags
608 loadmodel->synctype = ST_RAND;
611 inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
612 if (loadmodel->numskins)
614 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
615 loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
616 for (i = 0;i < loadmodel->numskins;i++)
618 loadmodel->skinscenes[i].firstframe = i;
619 loadmodel->skinscenes[i].framecount = 1;
620 loadmodel->skinscenes[i].loop = true;
621 loadmodel->skinscenes[i].framerate = 10;
622 loadmodel->skinframes[i].base = loadtextureimagewithmask (loadmodel->texturepool, inskin, 0, 0, true, r_mipskins.integer, true);
623 loadmodel->skinframes[i].fog = image_masktex;
624 loadmodel->skinframes[i].pants = NULL;
625 loadmodel->skinframes[i].shirt = NULL;
626 loadmodel->skinframes[i].glow = NULL;
627 loadmodel->skinframes[i].merged = NULL;
628 inskin += MD2MAX_SKINNAME;
632 // load the triangles and stvert data
633 inst = (void*)(base + LittleLong(pinmodel->ofs_st));
634 intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
635 skinwidth = LittleLong(pinmodel->skinwidth);
636 skinheight = LittleLong(pinmodel->skinheight);
638 stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
639 s = 1.0f / skinwidth;
640 t = 1.0f / skinheight;
641 for (i = 0;i < numst;i++)
643 j = (unsigned short) LittleShort(inst[i*2+0]);
644 k = (unsigned short) LittleShort(inst[i*2+1]);
645 if (j >= skinwidth || k >= skinheight)
648 Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
650 stverts[i*2+0] = j * s;
651 stverts[i*2+1] = k * t;
654 md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
655 md2verthashdata = Mem_Alloc(tempmempool, loadmodel->numtris * 3 * sizeof(*hash));
656 // swap the triangle list
658 loadmodel->mdlmd2data_indices = Mem_Alloc(loadmodel->mempool, loadmodel->numtris * sizeof(int[3]));
659 for (i = 0;i < loadmodel->numtris;i++)
661 for (j = 0;j < 3;j++)
663 xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
664 st = (unsigned short) LittleShort (intri[i].index_st[j]);
665 if (xyz >= numxyz || st >= numst)
667 Mem_Free(md2verthash);
668 Mem_Free(md2verthashdata);
671 Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
673 Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
677 hashindex = (xyz * 17 + st) & 255;
678 for (hash = md2verthash[hashindex];hash;hash = hash->next)
679 if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
683 hash = md2verthashdata + num++;
687 hash->next = md2verthash[hashindex];
688 md2verthash[hashindex] = hash;
690 loadmodel->mdlmd2data_indices[i*3+j] = (hash - md2verthashdata);
696 loadmodel->numverts = num;
697 vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
698 loadmodel->mdlmd2data_texcoords = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
699 for (i = 0;i < num;i++)
701 hash = md2verthashdata + i;
702 vertremap[i] = hash->xyz;
703 loadmodel->mdlmd2data_texcoords[i*2+0] = hash->st[0];
704 loadmodel->mdlmd2data_texcoords[i*2+1] = hash->st[1];
707 Mem_Free(md2verthash);
708 Mem_Free(md2verthashdata);
711 // LordHavoc: doing proper bbox for model
712 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
713 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
717 datapointer = (base + LittleLong(pinmodel->ofs_frames));
719 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
720 loadmodel->mdlmd2data_frames = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(md2frame_t));
721 loadmodel->mdlmd2data_pose = Mem_Alloc(loadmodel->mempool, loadmodel->numverts * loadmodel->numframes * sizeof(trivertx_t));
723 for (i = 0;i < loadmodel->numframes;i++)
725 pinframe = (md2frame_t *)datapointer;
726 datapointer += sizeof(md2frame_t);
727 strcpy(loadmodel->mdlmd2data_frames[i].name, pinframe->name);
728 for (j = 0;j < 3;j++)
730 loadmodel->mdlmd2data_frames[i].scale[j] = LittleFloat(pinframe->scale[j]);
731 loadmodel->mdlmd2data_frames[i].translate[j] = LittleFloat(pinframe->translate[j]);
733 Mod_MD2_ConvertVerts (loadmodel->mdlmd2data_frames[i].scale, loadmodel->mdlmd2data_frames[i].translate, (void *)datapointer, &loadmodel->mdlmd2data_pose[i * loadmodel->numverts], vertremap);
734 datapointer += numxyz * sizeof(trivertx_t);
736 strcpy(loadmodel->animscenes[i].name, loadmodel->mdlmd2data_frames[i].name);
737 loadmodel->animscenes[i].firstframe = i;
738 loadmodel->animscenes[i].framecount = 1;
739 loadmodel->animscenes[i].framerate = 10;
740 loadmodel->animscenes[i].loop = true;
745 // LordHavoc: model bbox
746 modelyawradius = sqrt(modelyawradius);
747 modelradius = sqrt(modelradius);
748 for (j = 0;j < 3;j++)
750 loadmodel->normalmins[j] = aliasbboxmin[j];
751 loadmodel->normalmaxs[j] = aliasbboxmax[j];
752 loadmodel->rotatedmins[j] = -modelradius;
753 loadmodel->rotatedmaxs[j] = modelradius;
755 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -(loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelyawradius);
756 loadmodel->yawmins[2] = loadmodel->normalmins[2];
757 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
760 static void zymswapintblock(int *m, int size)
770 void Mod_LoadZymoticModel(model_t *mod, void *buffer)
772 int i, pbase, *bonecount, numposes;
773 unsigned int count, a, b, c, *renderlist, *renderlistend;
774 rtexture_t **texture;
776 zymtype1header_t *pinmodel, *pheader;
779 float corner[2], modelradius;
781 pinmodel = (void *)buffer;
783 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
784 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
786 if (BigLong(pinmodel->type) != 1)
787 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
789 loadmodel->type = mod_alias;
790 loadmodel->aliastype = ALIASTYPE_ZYM;
792 loadmodel->zymdata_header = pheader = Mem_Alloc(loadmodel->mempool, BigLong(pinmodel->filesize));
794 pbase = (int) pheader;
796 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
799 #define SWAPLONG(var) var = BigLong(var)
800 #define SWAPFLOAT(var) var = BigFloat(var)
801 #define SWAPVEC(var) SWAPFLOAT(var[0]);SWAPFLOAT(var[1]);SWAPFLOAT(var[2])
802 SWAPLONG(pheader->type);
803 SWAPLONG(pheader->filesize);
804 SWAPVEC(pheader->mins);
805 SWAPVEC(pheader->maxs);
806 SWAPFLOAT(pheader->radius);
807 SWAPLONG(pheader->numverts);
808 SWAPLONG(pheader->numtris);
809 SWAPLONG(pheader->numshaders);
810 SWAPLONG(pheader->numbones);
811 SWAPLONG(pheader->numscenes);
813 #define SWAPLUMPINFO(var) SWAPLONG(pheader->lump_##var.start);SWAPLONG(pheader->lump_##var.length)
814 SWAPLUMPINFO(scenes);
817 SWAPLUMPINFO(vertbonecounts);
819 SWAPLUMPINFO(texcoords);
820 SWAPLUMPINFO(render);
821 SWAPLUMPINFO(shaders);
822 SWAPLUMPINFO(trizone);
824 loadmodel->flags = 0; // there are no flags
825 loadmodel->numframes = pheader->numscenes;
826 loadmodel->synctype = ST_SYNC;
827 loadmodel->numtris = pheader->numtris;
829 // FIXME: add skin support and texturing and shaders and...
830 loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
831 loadmodel->skinscenes[0].firstframe = 0;
832 loadmodel->skinscenes[0].framecount = 1;
833 loadmodel->skinscenes[0].loop = true;
834 loadmodel->skinscenes[0].framerate = 10;
835 loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
836 loadmodel->skinframes->base = NULL;
837 loadmodel->skinframes->fog = NULL;
838 loadmodel->skinframes->pants = NULL;
839 loadmodel->skinframes->shirt = NULL;
840 loadmodel->skinframes->glow = NULL;
841 loadmodel->skinframes->merged = NULL;
842 loadmodel->numskins = 1;
843 numposes = pheader->lump_poses.length / sizeof(float[3][4]) / pheader->numbones;
845 // go through the lumps, swapping things
847 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
848 scene = (void *) (pheader->lump_scenes.start + pbase);
849 loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
850 for (i = 0;i < pheader->numscenes;i++)
852 SWAPVEC(scene->mins);
853 SWAPVEC(scene->maxs);
854 SWAPFLOAT(scene->radius);
855 SWAPFLOAT(scene->framerate);
856 SWAPLONG(scene->flags);
857 SWAPLONG(scene->start);
858 SWAPLONG(scene->length);
860 memcpy(loadmodel->animscenes[i].name, scene->name, 32);
861 loadmodel->animscenes[i].firstframe = scene->start;
862 loadmodel->animscenes[i].framecount = scene->length;
863 loadmodel->animscenes[i].framerate = scene->framerate;
864 loadmodel->animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
866 if ((unsigned int) loadmodel->animscenes[i].firstframe >= numposes)
867 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
868 if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > numposes)
869 Host_Error("Mod_LoadZymoticModel: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
870 if (loadmodel->animscenes[i].framerate < 0)
871 Host_Error("Mod_LoadZymoticModel: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
875 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
876 zymswapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
878 // zymlump_t lump_bones; // zymbone_t bone[numbones];
879 bone = (void *) (pheader->lump_bones.start + pbase);
880 for (i = 0;i < pheader->numbones;i++)
882 SWAPLONG(bone[i].flags);
883 SWAPLONG(bone[i].parent);
884 if (bone[i].parent >= i)
885 Host_Error("Mod_LoadZymoticModel: bone[i].parent >= i in %s\n", loadmodel->name);
888 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
889 zymswapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
890 bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
891 for (i = 0;i < pheader->numbones;i++)
892 if (bonecount[i] < 1)
893 Host_Error("Mod_LoadZymoticModel: bone vertex count < 1 in %s\n", loadmodel->name);
895 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
896 zymswapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
898 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
899 zymswapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
901 // 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)
902 zymswapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
903 // validate renderlist and swap winding order of tris
904 renderlist = (void *) (pheader->lump_render.start + pbase);
905 renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
906 i = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
907 if (pheader->lump_render.length != i)
908 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);
909 for (i = 0;i < pheader->numshaders;i++)
911 if (renderlist >= renderlistend)
912 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
913 count = *renderlist++;
914 if (renderlist + count * 3 > renderlistend)
915 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
921 if (a >= pheader->numverts || b >= pheader->numverts || c >= pheader->numverts)
922 Host_Error("Mod_LoadZymoticModel: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
930 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
931 shadername = (void *) (pheader->lump_shaders.start + pbase);
932 texture = (void *) shadername;
933 for (i = 0;i < pheader->numshaders;i++)
936 rt = loadtextureimage(loadmodel->texturepool, shadername, 0, 0, true, r_mipskins.integer, true);
938 *texture++ = rt; // reuse shader name list for texture pointers
941 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
942 zymswapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
945 modelradius = pheader->radius;
946 for (i = 0;i < 3;i++)
948 loadmodel->normalmins[i] = pheader->mins[i];
949 loadmodel->normalmaxs[i] = pheader->maxs[i];
950 loadmodel->rotatedmins[i] = -modelradius;
951 loadmodel->rotatedmaxs[i] = modelradius;
953 corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
954 corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
955 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
956 if (loadmodel->yawmaxs[0] > modelradius)
957 loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
958 loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
959 loadmodel->yawmins[2] = loadmodel->normalmins[2];
960 loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
962 loadmodel->SERAddEntity = Mod_Alias_SERAddEntity;
963 loadmodel->Draw = R_DrawAliasModel;
964 loadmodel->DrawSky = NULL;
965 loadmodel->DrawShadow = NULL;