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"};
30 void Mod_AliasInit (void)
32 Cvar_RegisterVariable(&r_mipskins);
35 static void Mod_Alias_SERAddEntity(void)
37 R_Clip_AddBox(currentrenderentity->mins, currentrenderentity->maxs, R_Entity_Callback, currentrenderentity, NULL);
42 // LordHavoc: proper bounding box considerations
43 static float aliasbboxmin[3], aliasbboxmax[3], modelyawradius, modelradius;
45 static float vertst[MAXALIASVERTS][2];
46 static int vertusage[MAXALIASVERTS];
47 static int vertonseam[MAXALIASVERTS];
48 static int vertremap[MAXALIASVERTS];
49 static unsigned short temptris[MAXALIASTRIS][3];
51 static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
53 int i, j, invalidnormals = 0;
56 for (i = 0;i < inverts;i++)
58 if (vertremap[i] < 0 && vertremap[i+inverts] < 0) // only used vertices need apply...
60 temp[0] = v[i].v[0] * scale[0] + translate[0];
61 temp[1] = v[i].v[1] * scale[1] + translate[1];
62 temp[2] = v[i].v[2] * scale[2] + translate[2];
63 // update bounding box
64 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
65 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
66 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
67 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
68 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
69 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
70 dist = temp[0]*temp[0]+temp[1]*temp[1];
71 if (modelyawradius < dist)
72 modelyawradius = dist;
73 dist += temp[2]*temp[2];
74 if (modelradius < dist)
76 j = vertremap[i]; // not onseam
79 VectorCopy(v[i].v, out[j].v);
80 out[j].lightnormalindex = v[i].lightnormalindex;
81 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
84 out[j].lightnormalindex = 0;
87 j = vertremap[i+inverts]; // onseam
90 VectorCopy(v[i].v, out[j].v);
91 out[j].lightnormalindex = v[i].lightnormalindex;
92 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
95 out[j].lightnormalindex = 0;
100 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
108 static void * Mod_LoadAliasFrame (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
110 trivertx_t *pinframe;
111 daliasframe_t *pdaliasframe;
113 pdaliasframe = (daliasframe_t *)pin;
115 strcpy(scene->name, pdaliasframe->name);
116 scene->firstframe = posenum;
117 scene->framecount = 1;
118 scene->framerate = 10.0f; // unnecessary but...
121 pinframe = (trivertx_t *)(pdaliasframe + 1);
123 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
124 *posevert += outverts;
129 return (void *)pinframe;
138 static void *Mod_LoadAliasGroup (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
144 numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
146 strcpy(scene->name, ((daliasframe_t *) (sizeof(daliasinterval_t) * numframes + sizeof(daliasgroup_t) + (int) pin))->name);
147 scene->firstframe = posenum;
148 scene->framecount = numframes;
149 interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
150 if (interval < 0.01f)
151 Host_Error("Mod_LoadAliasGroup: invalid interval");
152 scene->framerate = 1.0f / interval;
155 ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
157 for (i=0 ; i<numframes ; i++)
159 ((daliasframe_t *)ptemp)++;
160 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
161 *posevert += outverts;
163 ptemp = (trivertx_t *)ptemp + inverts;
169 //=========================================================
171 static rtexture_t *GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
173 int i, pixels, passed;
175 for (i = 0;i < 16;i++)
176 pixeltest[i] = (bits & (1 << i)) != 0;
177 pixels = width*height;
181 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
184 // turn to white while copying
185 if (*in >= 128 && *in < 224) // backwards ranges
186 *out = (*in & 15) ^ 15;
196 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
201 static rtexture_t *GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
203 int i, pixels, passed;
205 for (i = 0;i < 16;i++)
206 pixeltest[i] = (bits & (1 << i)) != 0;
207 pixels = width*height;
211 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
222 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
227 static int GL_SkinCheck(byte *in, int width, int height, unsigned short bits)
229 int i, pixels, passed;
231 for (i = 0;i < 16;i++)
232 pixeltest[i] = (bits & (1 << i)) != 0;
233 pixels = width*height;
237 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
244 static void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, rtexture_t **skintex)
246 skintex[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, r_mipskins.value, true);
249 skintex[3] = loadtextureimage(va("%s_glow" , basename), 0, 0, false, r_mipskins.value, true);
253 skintex[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, r_mipskins.value, true);
254 skintex[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, r_mipskins.value, true);
258 skintex[0] = loadtextureimage(basename, 0, 0, false, true, true);
261 skintex[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
262 skintex[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
263 skintex[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), true); // glow
264 if (skintex[1] || skintex[2])
266 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
267 skintex[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), true); // body (normal + pants + shirt, but not glow)
270 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // no special colors
273 if (R_TextureHasAlpha(skintex[0]))
274 loadmodel->flags2 |= MODF_TRANSPARENT;
282 static void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
288 daliasskingroup_t *pinskingroup;
290 daliasskininterval_t *pinskinintervals;
291 int skinranges, skincount, *skinrange, skinnum;
292 rtexture_t **skintex;
294 byte *skintemp = NULL;
296 skin = (byte *)(pskintype + 1);
298 if (numskins < 1 || numskins > MAX_SKINS)
299 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
302 skintemp = qmalloc(s);
304 // LordHavoc: skim the data, measure the number of skins and number of groups
305 skinranges = numskins;
308 for (i = 0;i < numskins;i++)
311 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
314 (byte *)pskintype += s;
318 groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
319 skincount += groupskins;
320 (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
325 skinrange = loadmodel->skinanimrange;
326 skintex = loadmodel->skinanim;
327 // skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
328 // skintexnum = skinrange + skinranges * 2;
329 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
330 // loadmodel->skinanim = (int) skintexnum - (int) pheader;
332 for (i = 0;i < numskins;i++)
334 *skinrange++ = skinnum; // start of the range
336 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
338 *skinrange++ = 1; // single skin
340 sprintf (name, "%s_%i", loadmodel->name, i);
341 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
343 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
347 // animating skin group. yuck.
348 pinskingroup = (daliasskingroup_t *)pskintype;
349 groupskins = LittleLong (pinskingroup->numskins);
350 pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
352 pskintype = (void *)(pinskinintervals + groupskins);
354 *skinrange++ = groupskins; // number of skins
355 skinnum += groupskins;
356 for (j = 0;j < groupskins;j++)
358 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
359 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
361 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
365 loadmodel->numskins = numskins;
368 return (void *)pskintype;
371 static void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
374 for (i = 0;i < numskins;i++)
377 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
378 (byte *)pskintype += skinsize;
380 (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
385 //=========================================================================
387 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
394 #define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)\n", mod->name, VALUE, MIN, MAX);
395 #define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%f exceeds %f - %f)\n", mod->name, VALUE, MIN, MAX);
396 void Mod_LoadAliasModel (model_t *mod, void *buffer)
398 int i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
400 stvert_t *pinstverts;
401 dtriangle_t *pintriangles;
402 daliasframetype_t *pframetype;
403 daliasskintype_t *pskintype;
404 float *pouttexcoords, scales, scalet;
405 maliashdr_t *mheader;
406 unsigned short *pouttris;
407 trivertx_t *posevert;
408 animscene_t *animscenes;
413 start = Hunk_LowMark ();
415 pinmodel = (mdl_t *)buffer;
417 version = LittleLong (pinmodel->version);
418 if (version != ALIAS_VERSION)
419 Host_Error ("%s has wrong version number (%i should be %i)",
420 mod->name, version, ALIAS_VERSION);
422 mod->type = mod_alias;
423 mod->aliastype = ALIASTYPE_MDL;
425 numframes = LittleLong(pinmodel->numframes);
426 BOUNDI(numframes,0,65536);
427 numverts = LittleLong(pinmodel->numverts);
428 BOUNDI(numverts,0,MAXALIASVERTS);
429 numtris = LittleLong(pinmodel->numtris);
430 BOUNDI(numtris,0,MAXALIASTRIS);
431 numskins = LittleLong(pinmodel->numskins);
432 BOUNDI(numskins,0,256);
433 skinwidth = LittleLong (pinmodel->skinwidth);
434 BOUNDI(skinwidth,0,4096);
435 skinheight = LittleLong (pinmodel->skinheight);
436 BOUNDI(skinheight,0,1024);
438 pskintype = (daliasskintype_t *)&pinmodel[1];
439 pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
440 pintriangles = (dtriangle_t *)&pinstverts[numverts];
441 pframetype = (daliasframetype_t *)&pintriangles[numtris];
444 for (i=0 ; i<numframes ; i++)
446 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
449 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + (sizeof(daliasframe_t) + sizeof(trivertx_t) * numverts) );
453 f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
455 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
460 mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
461 mod->flags = LittleLong (pinmodel->flags);
462 // endian-adjust and copy the data, starting with the alias model header
463 mheader->numverts = numverts;
464 mod->numtris = mheader->numtris = numtris;
465 mod->numframes = mheader->numframes = numframes;
466 mod->synctype = LittleLong (pinmodel->synctype);
467 BOUNDI(mod->synctype,0,2);
469 for (i=0 ; i<3 ; i++)
471 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
472 BOUNDF(mheader->scale[i],0,65536);
473 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
474 BOUNDF(mheader->scale_origin[i],-65536,65536);
478 pskintype = (daliasskintype_t *)&pinmodel[1];
479 pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
481 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
482 pinstverts = (stvert_t *)pskintype;
484 // LordHavoc: byteswap and convert stvert data
485 scales = 1.0 / skinwidth;
486 scalet = 1.0 / skinheight;
487 for (i = 0;i < numverts;i++)
489 vertonseam[i] = LittleLong(pinstverts[i].onseam);
490 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
491 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
492 vertst[i+numverts][0] = vertst[i][0] + 0.5;
493 vertst[i+numverts][1] = vertst[i][1];
495 vertusage[i+numverts] = 0;
498 // load triangle data
499 pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
500 mheader->tridata = (int) pouttris - (int) mheader;
501 pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
503 // count the vertices used
504 for (i = 0;i < numverts*2;i++)
506 for (i = 0;i < numtris;i++)
508 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
509 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
510 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
511 if (!LittleLong(pintriangles[i].facesfront)) // backface
513 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
514 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
515 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
517 vertusage[temptris[i][0]]++;
518 vertusage[temptris[i][1]]++;
519 vertusage[temptris[i][2]]++;
521 // build remapping table and compact array
523 for (i = 0;i < numverts*2;i++)
527 vertremap[i] = totalverts;
528 vertst[totalverts][0] = vertst[i][0];
529 vertst[totalverts][1] = vertst[i][1];
533 vertremap[i] = -1; // not used at all
535 mheader->numverts = totalverts;
536 // remap the triangle references
537 for (i = 0;i < numtris;i++)
539 *pouttris++ = vertremap[temptris[i][0]];
540 *pouttris++ = vertremap[temptris[i][1]];
541 *pouttris++ = vertremap[temptris[i][2]];
543 // store the texture coordinates
544 pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
545 mheader->texdata = (int) pouttexcoords - (int) mheader;
546 for (i = 0;i < totalverts;i++)
548 *pouttexcoords++ = vertst[i][0];
549 *pouttexcoords++ = vertst[i][1];
554 posevert = Hunk_AllocName(sizeof(trivertx_t) * numposes * totalverts, va("%s vertex data", loadname));
555 mheader->posedata = (int) posevert - (int) mheader;
556 pframetype = (daliasframetype_t *)&pintriangles[numtris];
558 // LordHavoc: doing proper bbox for model
559 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
560 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
562 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
564 for (i = 0;i < numframes;i++)
566 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
567 pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
569 pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
572 mod->ofs_scenes = (int) animscenes - (int) mheader;
574 // LordHavoc: fixed model bbox - was //FIXME: do this right
575 //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
576 //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
577 modelyawradius = sqrt(modelyawradius);
578 modelradius = sqrt(modelradius);
579 // mod->modelradius = modelradius;
580 for (j = 0;j < 3;j++)
582 mod->normalmins[j] = aliasbboxmin[j];
583 mod->normalmaxs[j] = aliasbboxmax[j];
584 mod->rotatedmins[j] = -modelradius;
585 mod->rotatedmaxs[j] = modelradius;
587 mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
588 mod->yawmins[2] = mod->normalmins[2];
589 mod->yawmaxs[2] = mod->normalmaxs[2];
591 mod->SERAddEntity = Mod_Alias_SERAddEntity;
592 mod->DrawEarly = NULL;
593 mod->DrawLate = R_DrawAliasModel;
594 mod->DrawShadow = NULL;
596 // move the complete, relocatable alias model to the cache
597 end = Hunk_LowMark ();
598 mod->cachesize = total = end - start;
600 Cache_Alloc (&mod->cache, total, loadname);
601 if (!mod->cache.data)
603 memcpy (mod->cache.data, mheader, total);
605 Hunk_FreeToLowMark (start);
608 static void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
610 int i, invalidnormals = 0;
613 for (i = 0;i < numverts;i++)
615 VectorCopy(v[i].v, out[i].v);
616 temp[0] = v[i].v[0] * scale[0] + translate[0];
617 temp[1] = v[i].v[1] * scale[1] + translate[1];
618 temp[2] = v[i].v[2] * scale[2] + translate[2];
619 // update bounding box
620 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
621 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
622 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
623 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
624 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
625 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
626 dist = temp[0]*temp[0]+temp[1]*temp[1];
627 if (modelyawradius < dist)
628 modelyawradius = dist;
629 dist += temp[2]*temp[2];
630 if (modelradius < dist)
632 out[i].lightnormalindex = v[i].lightnormalindex;
633 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
636 out[i].lightnormalindex = 0;
640 Con_Printf("Mod_ConvertQ2AliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
648 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
650 int i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
653 md2triangle_t *pintriangles, *pouttriangles;
654 md2frame_t *pinframe;
655 md2frame_t *poutframe;
658 animscene_t *animscenes;
663 start = Hunk_LowMark ();
666 // temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
668 pinmodel = (md2_t *)buffer;
670 version = LittleLong (pinmodel->version);
671 if (version != MD2ALIAS_VERSION)
672 Host_Error ("%s has wrong version number (%i should be %i)",
673 mod->name, version, MD2ALIAS_VERSION);
675 mod->type = mod_alias;
676 mod->aliastype = ALIASTYPE_MD2;
678 framesize = sizeof(md2framesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivertx_t);
679 // LordHavoc: calculate size for in memory version
680 size = sizeof(md2mem_t)
681 + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
682 + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
683 + LittleLong(pinmodel->num_frames) * framesize
684 + LittleLong(pinmodel->num_glcmds) * sizeof(int);
685 if (size <= 0 || size >= MD2MAX_SIZE)
686 Host_Error ("%s is not a valid model", mod->name);
687 pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
689 mod->flags = 0; // there are no MD2 flags
690 mod->numframes = LittleLong(pinmodel->num_frames);
691 mod->synctype = ST_RAND;
692 mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
694 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
695 Host_Error ("%s is not a valid model", mod->name);
696 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
697 Host_Error ("%s is not a valid model", mod->name);
698 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
699 Host_Error ("%s is not a valid model", mod->name);
700 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
701 Host_Error ("%s is not a valid model", mod->name);
702 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
703 Host_Error ("%s is not a valid model", mod->name);
705 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
706 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
707 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
708 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
709 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
710 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
711 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
712 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
714 pheader->framesize = framesize;
715 pheader->num_skins = LittleLong(pinmodel->num_skins);
716 pheader->num_xyz = LittleLong(pinmodel->num_xyz);
717 pheader->num_st = LittleLong(pinmodel->num_st);
718 pheader->num_tris = LittleLong(pinmodel->num_tris);
719 pheader->num_frames = LittleLong(pinmodel->num_frames);
720 pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
723 if (pheader->num_skins)
727 skinrange = loadmodel->skinanimrange;
728 skin = loadmodel->skinanim;
729 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
730 // skin = skinrange + pheader->num_skins * 2;
731 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
732 // loadmodel->skinanim = (int) skin - (int) pheader;
733 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
734 for (i = 0;i < pheader->num_skins;i++)
738 *skin++ = loadtextureimage (pinskins, 0, 0, true, r_mipskins.value, true);
739 *skin++ = NULL; // the extra 4 layers are currently unused
743 pinskins += MD2MAX_SKINNAME;
746 loadmodel->numskins = pheader->num_skins;
749 pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
750 pouttriangles = (void*)&pheader[1];
751 pheader->ofs_tris = (int) pouttriangles - (int) pheader;
753 // swap the triangle list
754 for (i = 0;i < pheader->num_tris;i++)
756 for (j = 0;j < 3;j++)
758 temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
759 pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
760 if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
761 Host_Error ("%s has invalid vertex indices", mod->name);
762 if (pouttriangles->index_st[j] >= pheader->num_st)
763 Host_Error ("%s has invalid vertex indices", mod->name);
769 // LordHavoc: doing proper bbox for model
770 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
771 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
774 pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
775 poutframe = (void*) pouttriangles;
776 pheader->ofs_frames = (int) poutframe - (int) pheader;
778 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
780 for (i = 0;i < pheader->num_frames;i++)
782 strcpy(poutframe->name, pinframe->name);
783 for (j = 0;j < 3;j++)
785 poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
786 poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
788 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
790 strcpy(animscenes[i].name, poutframe->name);
791 animscenes[i].firstframe = i;
792 animscenes[i].framecount = 1;
793 animscenes[i].framerate = 10;
794 animscenes[i].loop = true;
796 pinframe = (void*) &pinframe->verts[j];
797 poutframe = (void*) &poutframe->verts[j];
800 mod->ofs_scenes = (int) animscenes - (int) pheader;
802 // LordHavoc: model bbox
803 modelyawradius = sqrt(modelyawradius);
804 modelradius = sqrt(modelradius);
805 // mod->modelradius = modelradius;
806 for (j = 0;j < 3;j++)
808 mod->normalmins[j] = aliasbboxmin[j];
809 mod->normalmaxs[j] = aliasbboxmax[j];
810 mod->rotatedmins[j] = -modelradius;
811 mod->rotatedmaxs[j] = modelradius;
813 mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
814 mod->yawmins[2] = mod->normalmins[2];
815 mod->yawmaxs[2] = mod->normalmaxs[2];
817 // load the draw list
818 pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
819 poutglcmd = (void*) poutframe;
820 pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
821 for (i = 0;i < pheader->num_glcmds;i++)
822 *poutglcmd++ = LittleLong(*pinglcmd++);
824 mod->SERAddEntity = Mod_Alias_SERAddEntity;
825 mod->DrawEarly = NULL;
826 mod->DrawLate = R_DrawAliasModel;
827 mod->DrawShadow = NULL;
829 // move the complete, relocatable alias model to the cache
830 end = Hunk_LowMark ();
831 mod->cachesize = total = end - start;
833 Cache_Alloc (&mod->cache, total, loadname);
834 if (!mod->cache.data)
836 memcpy (mod->cache.data, pheader, total);
838 Hunk_FreeToLowMark (start);
841 static void swapintblock(int *m, int size)
851 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
853 int i, pbase, start, end, total, *skinrange;
854 rtexture_t **texture, **skin;
856 zymtype1header_t *pinmodel, *pheader;
859 animscene_t *animscenes;
860 float corner[2], modelradius;
862 start = Hunk_LowMark ();
864 pinmodel = (void *)buffer;
866 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
867 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
869 if (BigLong(pinmodel->type) != 1)
870 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
872 mod->type = mod_alias;
873 mod->aliastype = ALIASTYPE_ZYM;
875 pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
877 pbase = (int) pheader;
879 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
882 memcpy(pheader->id, pinmodel->id, 12);
883 pheader->type = BigLong(pheader->type);
884 pheader->filesize = BigLong(pheader->filesize);
885 pheader->mins[0] = BigFloat(pheader->mins[0]);
886 pheader->mins[1] = BigFloat(pheader->mins[1]);
887 pheader->mins[2] = BigFloat(pheader->mins[2]);
888 pheader->maxs[0] = BigFloat(pheader->maxs[0]);
889 pheader->maxs[1] = BigFloat(pheader->maxs[1]);
890 pheader->maxs[2] = BigFloat(pheader->maxs[2]);
891 pheader->radius = BigFloat(pheader->radius);
892 pheader->numverts = BigLong(pheader->numverts);
893 pheader->numtris = BigLong(pheader->numtris);
894 pheader->numshaders = BigLong(pheader->numshaders);
895 pheader->numbones = BigLong(pheader->numbones);
896 pheader->numscenes = BigLong(pheader->numscenes);
899 pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
900 pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
901 pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
902 pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
903 pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
904 pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
905 pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
906 pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
907 pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
909 mod->flags = 0; // there are no flags
910 mod->numframes = pheader->numscenes;
911 mod->synctype = ST_SYNC;
912 mod->numtris = pheader->numtris;
914 // FIXME: add skin support and texturing and shaders and...
916 skinrange = loadmodel->skinanimrange;
917 skin = loadmodel->skinanim;
918 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
919 // skin = skinrange + pheader->num_skins * 2;
920 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
921 // loadmodel->skinanim = (int) skin - (int) pheader;
929 loadmodel->numskins = 1;
931 // go through the lumps, swapping things
933 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
934 scene = (void *) (pheader->lump_scenes.start + pbase);
935 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
936 for (i = 0;i < pheader->numscenes;i++)
938 scene->mins[0] = BigFloat(scene->mins[0]);
939 scene->mins[1] = BigFloat(scene->mins[1]);
940 scene->mins[2] = BigFloat(scene->mins[2]);
941 scene->maxs[0] = BigFloat(scene->maxs[0]);
942 scene->maxs[1] = BigFloat(scene->maxs[1]);
943 scene->maxs[2] = BigFloat(scene->maxs[2]);
944 scene->radius = BigFloat(scene->radius);
945 scene->framerate = BigFloat(scene->framerate);
946 scene->flags = BigLong(scene->flags);
947 scene->start = BigLong(scene->start);
948 scene->length = BigLong(scene->length);
950 memcpy(animscenes[i].name, scene->name, 32);
951 animscenes[i].firstframe = scene->start;
952 animscenes[i].framecount = scene->length;
953 animscenes[i].framerate = scene->framerate;
954 animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
958 mod->ofs_scenes = (int) animscenes - pbase;
960 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
961 swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
963 // zymlump_t lump_bones; // zymbone_t bone[numbones];
964 bone = (void *) (pheader->lump_bones.start + pbase);
965 for (i = 0;i < pheader->numbones;i++)
967 bone->flags = BigLong(bone->flags);
968 bone->parent = BigLong(bone->parent);
972 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
973 swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
975 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
976 swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
978 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
979 swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
981 // 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)
982 swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
984 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
985 shadername = (void *) (pheader->lump_shaders.start + pbase);
986 texture = (void *) shadername;
987 for (i = 0;i < pheader->numshaders;i++)
990 rt = loadtextureimage(shadername, 0, 0, true, r_mipskins.value, true);
992 *texture++ = rt; // reuse shader name list for texture pointers
995 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
996 swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
999 modelradius = pheader->radius;
1000 // mod->modelradius = pheader->radius;
1001 for (i = 0;i < 3;i++)
1003 mod->normalmins[i] = pheader->mins[i];
1004 mod->normalmaxs[i] = pheader->maxs[i];
1005 mod->rotatedmins[i] = -modelradius;
1006 mod->rotatedmaxs[i] = modelradius;
1008 corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
1009 corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
1010 mod->yawmaxs[0] = mod->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
1011 if (mod->yawmaxs[0] > modelradius)
1012 mod->yawmaxs[0] = mod->yawmaxs[1] = modelradius;
1013 mod->yawmins[0] = mod->yawmins[1] = -mod->yawmaxs[0];
1014 mod->yawmins[2] = mod->normalmins[2];
1015 mod->yawmaxs[2] = mod->normalmaxs[2];
1017 mod->SERAddEntity = Mod_Alias_SERAddEntity;
1018 mod->DrawEarly = NULL;
1019 mod->DrawLate = R_DrawAliasModel;
1020 mod->DrawShadow = NULL;
1022 // move the complete, relocatable alias model to the cache
1023 end = Hunk_LowMark ();
1024 mod->cachesize = total = end - start;
1026 Cache_Alloc (&mod->cache, total, loadname);
1027 if (!mod->cache.data)
1029 memcpy (mod->cache.data, pheader, total);
1031 Hunk_FreeToLowMark (start);