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 cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "1"};
30 void Mod_AliasInit (void)
32 Cvar_RegisterVariable(&r_mipskins);
37 float aliasbboxmin[3], aliasbboxmax[3]; // LordHavoc: proper bounding box considerations
39 float vertst[MAXALIASVERTS][2];
40 int vertusage[MAXALIASVERTS];
41 int vertonseam[MAXALIASVERTS];
42 int vertremap[MAXALIASVERTS];
43 unsigned short temptris[MAXALIASTRIS][3];
45 void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
47 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...
53 temp[0] = v[i].v[0] * scale[0] + translate[0];
54 temp[1] = v[i].v[1] * scale[1] + translate[1];
55 temp[2] = v[i].v[2] * scale[2] + translate[2];
56 // update bounding box
57 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
58 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
59 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
60 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
61 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
62 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
63 j = vertremap[i]; // not onseam
66 VectorCopy(v[i].v, out[j].v);
67 out[j].lightnormalindex = v[i].lightnormalindex;
68 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
71 out[j].lightnormalindex = 0;
74 j = vertremap[i+inverts]; // onseam
77 VectorCopy(v[i].v, out[j].v);
78 out[j].lightnormalindex = v[i].lightnormalindex;
79 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
82 out[j].lightnormalindex = 0;
87 Con_Printf("Mod_ConvertAliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
95 void * Mod_LoadAliasFrame (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
98 daliasframe_t *pdaliasframe;
100 pdaliasframe = (daliasframe_t *)pin;
102 strcpy(scene->name, pdaliasframe->name);
103 scene->firstframe = posenum;
104 scene->framecount = 1;
105 scene->framerate = 10.0f; // unnecessary but...
108 pinframe = (trivertx_t *)(pdaliasframe + 1);
110 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
111 *posevert += outverts;
116 return (void *)pinframe;
125 void *Mod_LoadAliasGroup (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
131 numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
133 strcpy(scene->name, ((daliasframe_t *) (sizeof(daliasinterval_t) * numframes + sizeof(daliasgroup_t) + (int) pin))->name);
134 scene->firstframe = posenum;
135 scene->framecount = numframes;
136 interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
137 if (interval < 0.01f)
138 Host_Error("Mod_LoadAliasGroup: invalid interval");
139 scene->framerate = 1.0f / interval;
142 ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
144 for (i=0 ; i<numframes ; i++)
146 ((daliasframe_t *)ptemp)++;
147 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
148 *posevert += outverts;
150 ptemp = (trivertx_t *)ptemp + inverts;
156 //=========================================================
162 Fill background pixels so mipmapping doesn't have haloes - Ed
171 // must be a power of 2
172 #define FLOODFILL_FIFO_SIZE 0x1000
173 #define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
175 #define FLOODFILL_STEP( off, dx, dy ) \
177 if (pos[off] == fillcolor) \
180 fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
181 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
183 else if (pos[off] != 255) fdc = pos[off]; \
186 void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
188 byte fillcolor = *skin; // assume this is the pixel to fill
189 floodfill_t fifo[FLOODFILL_FIFO_SIZE];
190 int inpt = 0, outpt = 0;
191 int filledcolor = -1;
194 if (filledcolor == -1)
197 // attempt to find opaque black
198 for (i = 0; i < 256; ++i)
199 if (d_8to24table[i] == (255 << 0)) // alpha 1.0
206 // can't fill to filled color or to transparent color (used as visited marker)
207 if ((fillcolor == filledcolor) || (fillcolor == 255))
209 //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
213 fifo[inpt].x = 0, fifo[inpt].y = 0;
214 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
216 while (outpt != inpt)
218 int x = fifo[outpt].x, y = fifo[outpt].y;
219 int fdc = filledcolor;
220 byte *pos = &skin[x + skinwidth * y];
222 outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
224 if (x > 0) FLOODFILL_STEP( -1, -1, 0 );
225 if (x < skinwidth - 1) FLOODFILL_STEP( 1, 1, 0 );
226 if (y > 0) FLOODFILL_STEP( -skinwidth, 0, -1 );
227 if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
228 skin[x + skinwidth * y] = fdc;
232 rtexture_t *GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
234 int i, pixels, passed;
236 for (i = 0;i < 16;i++)
237 pixeltest[i] = (bits & (1 << i)) != 0;
238 pixels = width*height;
242 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
245 // turn to white while copying
246 if (*in >= 128 && *in < 224) // backwards ranges
247 *out = (*in & 15) ^ 15;
257 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
262 rtexture_t *GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
264 int i, pixels, passed;
266 for (i = 0;i < 16;i++)
267 pixeltest[i] = (bits & (1 << i)) != 0;
268 pixels = width*height;
272 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
283 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
288 int GL_SkinCheck(byte *in, int width, int height, unsigned short bits)
290 int i, pixels, passed;
292 for (i = 0;i < 16;i++)
293 pixeltest[i] = (bits & (1 << i)) != 0;
294 pixels = width*height;
298 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
305 void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, rtexture_t **skintex)
307 skintex[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, r_mipskins.value, true);
310 skintex[3] = loadtextureimage(va("%s_glow" , basename), 0, 0, false, r_mipskins.value, true);
314 skintex[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, r_mipskins.value, true);
315 skintex[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, r_mipskins.value, true);
319 skintex[0] = loadtextureimage(basename, 0, 0, false, true, true);
322 skintex[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
323 skintex[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
324 skintex[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), true); // glow
325 if (skintex[1] || skintex[2])
327 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
328 skintex[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), true); // body (normal + pants + shirt, but not glow)
331 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // no special colors
334 if (R_TextureHasAlpha(skintex[0]))
335 loadmodel->flags2 |= MODF_TRANSPARENT;
343 void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
349 daliasskingroup_t *pinskingroup;
351 daliasskininterval_t *pinskinintervals;
352 int skinranges, skincount, *skinrange, skinnum;
353 rtexture_t **skintex;
355 byte *skintemp = NULL;
357 skin = (byte *)(pskintype + 1);
359 if (numskins < 1 || numskins > MAX_SKINS)
360 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
363 skintemp = qmalloc(s);
365 // LordHavoc: skim the data, measure the number of skins and number of groups
366 skinranges = numskins;
369 for (i = 0;i < numskins;i++)
372 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
375 (byte *)pskintype += s;
379 groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
380 skincount += groupskins;
381 (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
386 skinrange = loadmodel->skinanimrange;
387 skintex = loadmodel->skinanim;
388 // skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
389 // skintexnum = skinrange + skinranges * 2;
390 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
391 // loadmodel->skinanim = (int) skintexnum - (int) pheader;
393 for (i = 0;i < numskins;i++)
395 *skinrange++ = skinnum; // start of the range
397 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
399 *skinrange++ = 1; // single skin
401 sprintf (name, "%s_%i", loadmodel->name, i);
402 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
404 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
408 // animating skin group. yuck.
409 pinskingroup = (daliasskingroup_t *)pskintype;
410 groupskins = LittleLong (pinskingroup->numskins);
411 pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
413 pskintype = (void *)(pinskinintervals + groupskins);
415 *skinrange++ = groupskins; // number of skins
416 skinnum += groupskins;
417 for (j = 0;j < groupskins;j++)
419 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
420 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
422 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
426 loadmodel->numskins = numskins;
429 return (void *)pskintype;
432 void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
435 for (i = 0;i < numskins;i++)
438 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
439 (byte *)pskintype += skinsize;
441 (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
446 //=========================================================================
448 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
455 #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);
456 #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);
457 void Mod_LoadAliasModel (model_t *mod, void *buffer)
459 int i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
461 stvert_t *pinstverts;
462 dtriangle_t *pintriangles;
463 daliasframetype_t *pframetype;
464 daliasskintype_t *pskintype;
465 float *pouttexcoords, scales, scalet;
466 maliashdr_t *mheader;
467 unsigned short *pouttris;
468 trivertx_t *posevert;
469 animscene_t *animscenes;
471 start = Hunk_LowMark ();
473 pinmodel = (mdl_t *)buffer;
475 version = LittleLong (pinmodel->version);
476 if (version != ALIAS_VERSION)
477 Host_Error ("%s has wrong version number (%i should be %i)",
478 mod->name, version, ALIAS_VERSION);
480 mod->type = mod_alias;
481 mod->aliastype = ALIASTYPE_MDL;
483 numframes = LittleLong(pinmodel->numframes);
484 BOUNDI(numframes,0,65536);
485 numverts = LittleLong(pinmodel->numverts);
486 BOUNDI(numverts,0,MAXALIASVERTS);
487 numtris = LittleLong(pinmodel->numtris);
488 BOUNDI(numtris,0,MAXALIASTRIS);
489 numskins = LittleLong(pinmodel->numskins);
490 BOUNDI(numskins,0,256);
491 skinwidth = LittleLong (pinmodel->skinwidth);
492 BOUNDI(skinwidth,0,4096);
493 skinheight = LittleLong (pinmodel->skinheight);
494 BOUNDI(skinheight,0,1024);
496 pskintype = (daliasskintype_t *)&pinmodel[1];
497 pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
498 pintriangles = (dtriangle_t *)&pinstverts[numverts];
499 pframetype = (daliasframetype_t *)&pintriangles[numtris];
502 for (i=0 ; i<numframes ; i++)
504 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
507 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + (sizeof(daliasframe_t) + sizeof(trivertx_t) * numverts) );
511 f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
513 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
518 mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
519 mod->flags = LittleLong (pinmodel->flags);
520 // endian-adjust and copy the data, starting with the alias model header
521 mheader->numverts = numverts;
522 mod->numtris = mheader->numtris = numtris;
523 mod->numframes = mheader->numframes = numframes;
524 mod->synctype = LittleLong (pinmodel->synctype);
525 BOUNDI(mod->synctype,0,2);
527 for (i=0 ; i<3 ; i++)
529 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
530 BOUNDF(mheader->scale[i],0,65536);
531 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
532 BOUNDF(mheader->scale_origin[i],-65536,65536);
536 pskintype = (daliasskintype_t *)&pinmodel[1];
537 pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
539 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
540 pinstverts = (stvert_t *)pskintype;
542 // LordHavoc: byteswap and convert stvert data
543 scales = 1.0 / skinwidth;
544 scalet = 1.0 / skinheight;
545 for (i = 0;i < numverts;i++)
547 vertonseam[i] = LittleLong(pinstverts[i].onseam);
548 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
549 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
550 vertst[i+numverts][0] = vertst[i][0] + 0.5;
551 vertst[i+numverts][1] = vertst[i][1];
553 vertusage[i+numverts] = 0;
556 // load triangle data
557 pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
558 mheader->tridata = (int) pouttris - (int) mheader;
559 pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
561 // count the vertices used
562 for (i = 0;i < numverts*2;i++)
564 for (i = 0;i < numtris;i++)
566 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
567 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
568 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
569 if (!LittleLong(pintriangles[i].facesfront)) // backface
571 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
572 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
573 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
575 vertusage[temptris[i][0]]++;
576 vertusage[temptris[i][1]]++;
577 vertusage[temptris[i][2]]++;
579 // build remapping table and compact array
581 for (i = 0;i < numverts*2;i++)
585 vertremap[i] = totalverts;
586 vertst[totalverts][0] = vertst[i][0];
587 vertst[totalverts][1] = vertst[i][1];
591 vertremap[i] = -1; // not used at all
593 mheader->numverts = totalverts;
594 // remap the triangle references
595 for (i = 0;i < numtris;i++)
597 *pouttris++ = vertremap[temptris[i][0]];
598 *pouttris++ = vertremap[temptris[i][1]];
599 *pouttris++ = vertremap[temptris[i][2]];
601 // store the texture coordinates
602 pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
603 mheader->texdata = (int) pouttexcoords - (int) mheader;
604 for (i = 0;i < totalverts;i++)
606 *pouttexcoords++ = vertst[i][0];
607 *pouttexcoords++ = vertst[i][1];
612 posevert = Hunk_AllocName(sizeof(trivertx_t) * numposes * totalverts, va("%s vertex data", loadname));
613 mheader->posedata = (int) posevert - (int) mheader;
614 pframetype = (daliasframetype_t *)&pintriangles[numtris];
616 // LordHavoc: doing proper bbox for model
617 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
618 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
620 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
622 for (i = 0;i < numframes;i++)
624 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
625 pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
627 pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
630 mod->ofs_scenes = (int) animscenes - (int) mheader;
632 // LordHavoc: fixed model bbox - was //FIXME: do this right
633 //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
634 //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
635 for (j = 0;j < 3;j++)
637 mod->mins[j] = aliasbboxmin[j];
638 mod->maxs[j] = aliasbboxmax[j];
641 // move the complete, relocatable alias model to the cache
642 end = Hunk_LowMark ();
643 mod->cachesize = total = end - start;
645 Cache_Alloc (&mod->cache, total, loadname);
646 if (!mod->cache.data)
648 memcpy (mod->cache.data, mheader, total);
650 Hunk_FreeToLowMark (start);
653 void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
655 int i, invalidnormals = 0;
657 for (i = 0;i < numverts;i++)
659 VectorCopy(v[i].v, out[i].v);
660 temp[0] = v[i].v[0] * scale[0] + translate[0];
661 temp[1] = v[i].v[1] * scale[1] + translate[1];
662 temp[2] = v[i].v[2] * scale[2] + translate[2];
663 // update bounding box
664 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
665 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
666 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
667 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
668 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
669 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
670 out[i].lightnormalindex = v[i].lightnormalindex;
671 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
674 out[i].lightnormalindex = 0;
678 Con_Printf("Mod_ConvertQ2AliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
686 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
688 int i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
691 md2triangle_t *pintriangles, *pouttriangles;
692 md2frame_t *pinframe;
693 md2frame_t *poutframe;
696 animscene_t *animscenes;
698 start = Hunk_LowMark ();
701 // temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
703 pinmodel = (md2_t *)buffer;
705 version = LittleLong (pinmodel->version);
706 if (version != MD2ALIAS_VERSION)
707 Host_Error ("%s has wrong version number (%i should be %i)",
708 mod->name, version, MD2ALIAS_VERSION);
710 mod->type = mod_alias;
711 mod->aliastype = ALIASTYPE_MD2;
713 framesize = sizeof(md2framesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivertx_t);
714 // LordHavoc: calculate size for in memory version
715 size = sizeof(md2mem_t)
716 + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
717 + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
718 + LittleLong(pinmodel->num_frames) * framesize
719 + LittleLong(pinmodel->num_glcmds) * sizeof(int);
720 if (size <= 0 || size >= MD2MAX_SIZE)
721 Host_Error ("%s is not a valid model", mod->name);
722 pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
724 mod->flags = 0; // there are no MD2 flags
725 mod->numframes = LittleLong(pinmodel->num_frames);
726 mod->synctype = ST_RAND;
727 mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
729 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
730 Host_Error ("%s is not a valid model", mod->name);
731 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
732 Host_Error ("%s is not a valid model", mod->name);
733 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
734 Host_Error ("%s is not a valid model", mod->name);
735 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
736 Host_Error ("%s is not a valid model", mod->name);
737 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
738 Host_Error ("%s is not a valid model", mod->name);
740 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
741 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
742 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
743 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
744 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
745 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
746 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
747 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
749 pheader->framesize = framesize;
750 pheader->num_skins = LittleLong(pinmodel->num_skins);
751 pheader->num_xyz = LittleLong(pinmodel->num_xyz);
752 pheader->num_st = LittleLong(pinmodel->num_st);
753 pheader->num_tris = LittleLong(pinmodel->num_tris);
754 pheader->num_frames = LittleLong(pinmodel->num_frames);
755 pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
758 if (pheader->num_skins)
762 skinrange = loadmodel->skinanimrange;
763 skin = loadmodel->skinanim;
764 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
765 // skin = skinrange + pheader->num_skins * 2;
766 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
767 // loadmodel->skinanim = (int) skin - (int) pheader;
768 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
769 for (i = 0;i < pheader->num_skins;i++)
773 *skin++ = loadtextureimage (pinskins, 0, 0, true, r_mipskins.value, true);
774 *skin++ = NULL; // the extra 4 layers are currently unused
778 pinskins += MD2MAX_SKINNAME;
781 loadmodel->numskins = pheader->num_skins;
784 pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
785 pouttriangles = (void*)&pheader[1];
786 pheader->ofs_tris = (int) pouttriangles - (int) pheader;
788 // swap the triangle list
789 for (i = 0;i < pheader->num_tris;i++)
791 for (j = 0;j < 3;j++)
793 temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
794 pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
795 if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
796 Host_Error ("%s has invalid vertex indices", mod->name);
797 if (pouttriangles->index_st[j] >= pheader->num_st)
798 Host_Error ("%s has invalid vertex indices", mod->name);
804 // LordHavoc: doing proper bbox for model
805 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
806 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
809 pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
810 poutframe = (void*) pouttriangles;
811 pheader->ofs_frames = (int) poutframe - (int) pheader;
813 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
815 for (i = 0;i < pheader->num_frames;i++)
817 strcpy(poutframe->name, pinframe->name);
818 for (j = 0;j < 3;j++)
820 poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
821 poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
823 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
825 strcpy(animscenes[i].name, poutframe->name);
826 animscenes[i].firstframe = i;
827 animscenes[i].framecount = 1;
828 animscenes[i].framerate = 10;
829 animscenes[i].loop = true;
831 pinframe = (void*) &pinframe->verts[j];
832 poutframe = (void*) &poutframe->verts[j];
835 mod->ofs_scenes = (int) animscenes - (int) pheader;
837 // LordHavoc: model bbox
838 for (j = 0;j < 3;j++)
840 mod->mins[j] = aliasbboxmin[j];
841 mod->maxs[j] = aliasbboxmax[j];
844 // load the draw list
845 pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
846 poutglcmd = (void*) poutframe;
847 pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
848 for (i = 0;i < pheader->num_glcmds;i++)
849 *poutglcmd++ = LittleLong(*pinglcmd++);
851 // move the complete, relocatable alias model to the cache
852 end = Hunk_LowMark ();
853 mod->cachesize = total = end - start;
855 Cache_Alloc (&mod->cache, total, loadname);
856 if (!mod->cache.data)
858 memcpy (mod->cache.data, pheader, total);
860 Hunk_FreeToLowMark (start);
863 void swapintblock(int *m, int size)
873 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
875 int i, pbase, start, end, total, *skinrange;
876 rtexture_t **texture, **skin;
878 zymtype1header_t *pinmodel, *pheader;
881 animscene_t *animscenes;
883 start = Hunk_LowMark ();
885 pinmodel = (void *)buffer;
887 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
888 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
890 if (BigLong(pinmodel->type) != 1)
891 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
893 mod->type = mod_alias;
894 mod->aliastype = ALIASTYPE_ZYM;
896 pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
898 pbase = (int) pheader;
900 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
903 memcpy(pheader->id, pinmodel->id, 12);
904 pheader->type = BigLong(pheader->type);
905 pheader->filesize = BigLong(pheader->filesize);
906 pheader->mins[0] = BigFloat(pheader->mins[0]);
907 pheader->mins[1] = BigFloat(pheader->mins[1]);
908 pheader->mins[2] = BigFloat(pheader->mins[2]);
909 pheader->maxs[0] = BigFloat(pheader->maxs[0]);
910 pheader->maxs[1] = BigFloat(pheader->maxs[1]);
911 pheader->maxs[2] = BigFloat(pheader->maxs[2]);
912 pheader->radius = BigFloat(pheader->radius);
913 pheader->numverts = BigLong(pheader->numverts);
914 pheader->numtris = BigLong(pheader->numtris);
915 pheader->numshaders = BigLong(pheader->numshaders);
916 pheader->numbones = BigLong(pheader->numbones);
917 pheader->numscenes = BigLong(pheader->numscenes);
920 pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
921 pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
922 pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
923 pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
924 pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
925 pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
926 pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
927 pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
928 pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
930 mod->flags = 0; // there are no flags
931 mod->numframes = pheader->numscenes;
932 mod->synctype = ST_SYNC;
933 mod->numtris = pheader->numtris;
935 // FIXME: add skin support and texturing and shaders and...
937 skinrange = loadmodel->skinanimrange;
938 skin = loadmodel->skinanim;
939 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
940 // skin = skinrange + pheader->num_skins * 2;
941 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
942 // loadmodel->skinanim = (int) skin - (int) pheader;
950 loadmodel->numskins = 1;
952 // go through the lumps, swapping things
954 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
955 scene = (void *) (pheader->lump_scenes.start + pbase);
956 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
957 for (i = 0;i < pheader->numscenes;i++)
959 scene->mins[0] = BigFloat(scene->mins[0]);
960 scene->mins[1] = BigFloat(scene->mins[1]);
961 scene->mins[2] = BigFloat(scene->mins[2]);
962 scene->maxs[0] = BigFloat(scene->maxs[0]);
963 scene->maxs[1] = BigFloat(scene->maxs[1]);
964 scene->maxs[2] = BigFloat(scene->maxs[2]);
965 scene->radius = BigFloat(scene->radius);
966 scene->framerate = BigFloat(scene->framerate);
967 scene->flags = BigLong(scene->flags);
968 scene->start = BigLong(scene->start);
969 scene->length = BigLong(scene->length);
971 memcpy(animscenes[i].name, scene->name, 32);
972 animscenes[i].firstframe = scene->start;
973 animscenes[i].framecount = scene->length;
974 animscenes[i].framerate = scene->framerate;
975 animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
979 mod->ofs_scenes = (int) animscenes - pbase;
981 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
982 swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
984 // zymlump_t lump_bones; // zymbone_t bone[numbones];
985 bone = (void *) (pheader->lump_bones.start + pbase);
986 for (i = 0;i < pheader->numbones;i++)
988 bone->flags = BigLong(bone->flags);
989 bone->parent = BigLong(bone->parent);
993 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
994 swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
996 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
997 swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
999 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1000 swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
1002 // 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)
1003 swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
1005 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1006 shadername = (void *) (pheader->lump_shaders.start + pbase);
1007 texture = (void *) shadername;
1008 for (i = 0;i < pheader->numshaders;i++)
1011 rt = loadtextureimage(shadername, 0, 0, true, r_mipskins.value, true);
1013 *texture++ = rt; // reuse shader name list for texture pointers
1016 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1017 swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
1020 for (i = 0;i < 3;i++)
1022 mod->mins[i] = pheader->mins[i];
1023 mod->maxs[i] = pheader->maxs[i];
1026 // move the complete, relocatable alias model to the cache
1027 end = Hunk_LowMark ();
1028 mod->cachesize = total = end - start;
1030 Cache_Alloc (&mod->cache, total, loadname);
1031 if (!mod->cache.data)
1033 memcpy (mod->cache.data, pheader, total);
1035 Hunk_FreeToLowMark (start);