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 = {"r_mipskins", "1", true};
30 void Mod_AliasInit (void)
32 Cvar_RegisterVariable(&r_mipskins);
37 float aliasbboxmin[3], aliasbboxmax[3]; // LordHavoc: proper bounding box considerations
40 float vertst[MAXVERTS][2];
41 int vertusage[MAXVERTS];
42 int vertonseam[MAXVERTS];
43 int vertremap[MAXVERTS];
44 unsigned short temptris[MAXVERTS][3];
46 void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
48 int i, j, invalidnormals = 0;
50 for (i = 0;i < inverts;i++)
52 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 j = vertremap[i]; // not onseam
67 VectorCopy(v[i].v, out[j].v);
68 out[j].lightnormalindex = v[i].lightnormalindex;
69 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
72 out[j].lightnormalindex = 0;
75 j = vertremap[i+inverts]; // onseam
78 VectorCopy(v[i].v, out[j].v);
79 out[j].lightnormalindex = v[i].lightnormalindex;
80 if (out[j].lightnormalindex >= NUMVERTEXNORMALS)
83 out[j].lightnormalindex = 0;
88 Con_Printf("Mod_ConvertAliasVerts: %i invalid normal indices found\n", invalidnormals);
96 void * Mod_LoadAliasFrame (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
99 daliasframe_t *pdaliasframe;
101 pdaliasframe = (daliasframe_t *)pin;
103 strcpy(scene->name, pdaliasframe->name);
104 scene->firstframe = posenum;
105 scene->framecount = 1;
106 scene->framerate = 10.0f; // unnecessary but...
109 pinframe = (trivertx_t *)(pdaliasframe + 1);
111 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
112 *posevert += outverts;
117 return (void *)pinframe;
126 void *Mod_LoadAliasGroup (void *pin, maliashdr_t *mheader, int inverts, int outverts, trivertx_t **posevert, animscene_t *scene)
132 numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
134 strcpy(scene->name, ((daliasframe_t *) (sizeof(daliasinterval_t) * numframes + sizeof(daliasgroup_t) + (int) pin))->name);
135 scene->firstframe = posenum;
136 scene->framecount = numframes;
137 interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
138 if (interval < 0.01f)
139 Host_Error("Mod_LoadAliasGroup: invalid interval");
140 scene->framerate = 1.0f / interval;
143 ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
145 for (i=0 ; i<numframes ; i++)
147 ((daliasframe_t *)ptemp)++;
148 Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
149 *posevert += outverts;
151 ptemp = (trivertx_t *)ptemp + inverts;
157 //=========================================================
163 Fill background pixels so mipmapping doesn't have haloes - Ed
172 // must be a power of 2
173 #define FLOODFILL_FIFO_SIZE 0x1000
174 #define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
176 #define FLOODFILL_STEP( off, dx, dy ) \
178 if (pos[off] == fillcolor) \
181 fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
182 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
184 else if (pos[off] != 255) fdc = pos[off]; \
187 void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
189 byte fillcolor = *skin; // assume this is the pixel to fill
190 floodfill_t fifo[FLOODFILL_FIFO_SIZE];
191 int inpt = 0, outpt = 0;
192 int filledcolor = -1;
195 if (filledcolor == -1)
198 // attempt to find opaque black
199 for (i = 0; i < 256; ++i)
200 if (d_8to24table[i] == (255 << 0)) // alpha 1.0
207 // can't fill to filled color or to transparent color (used as visited marker)
208 if ((fillcolor == filledcolor) || (fillcolor == 255))
210 //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
214 fifo[inpt].x = 0, fifo[inpt].y = 0;
215 inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
217 while (outpt != inpt)
219 int x = fifo[outpt].x, y = fifo[outpt].y;
220 int fdc = filledcolor;
221 byte *pos = &skin[x + skinwidth * y];
223 outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
225 if (x > 0) FLOODFILL_STEP( -1, -1, 0 );
226 if (x < skinwidth - 1) FLOODFILL_STEP( 1, 1, 0 );
227 if (y > 0) FLOODFILL_STEP( -skinwidth, 0, -1 );
228 if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
229 skin[x + skinwidth * y] = fdc;
233 rtexture_t *GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
235 int i, pixels, passed;
237 for (i = 0;i < 16;i++)
238 pixeltest[i] = (bits & (1 << i)) != 0;
239 pixels = width*height;
243 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
246 // turn to white while copying
247 if (*in >= 128 && *in < 224) // backwards ranges
248 *out = (*in & 15) ^ 15;
258 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
263 rtexture_t *GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name, int precache)
265 int i, pixels, passed;
267 for (i = 0;i < 16;i++)
268 pixeltest[i] = (bits & (1 << i)) != 0;
269 pixels = width*height;
273 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
284 return R_LoadTexture (name, width, height, out - width*height, (r_mipskins.value ? TEXF_MIPMAP : 0) | (precache ? TEXF_PRECACHE : 0));
289 int GL_SkinCheck(byte *in, int width, int height, unsigned short bits)
291 int i, pixels, passed;
293 for (i = 0;i < 16;i++)
294 pixeltest[i] = (bits & (1 << i)) != 0;
295 pixels = width*height;
299 if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
306 void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, rtexture_t **skintex)
309 int skin_normal, skin_pants, skin_shirt, skin_glow, skin_body, temp;
310 skin_normal = loadtextureimage(va("%s_normal", basename));
311 skin_pants = loadtextureimage(va("%s_pants" , basename));
312 skin_shirt = loadtextureimage(va("%s_shirt" , basename));
313 skin_glow = loadtextureimage(va("%s_glow" , basename));
314 skin_body = loadtextureimage(va("%s_body" , basename));
315 if (!(skin_normal || skin_pants || skin_shirt || skin_glow || skin_body))
316 skin_body = loadtextureimage(name);
317 if (skin_normal || skin_pants || skin_shirt || skin_glow || skin_body)
319 skintexnum[0] = skin_normal;
320 skintexnum[1] = skin_pants;
321 skintexnum[2] = skin_shirt;
322 skintexnum[3] = skin_glow;
323 skintexnum[4] = skin_body;
327 Mod_FloodFillSkin(skin, width, height);
328 skin_normal = GL_SkinCheck((byte *)pskintype, width, height, 0x3FBD);
329 skin_pants = GL_SkinCheck((byte *)pskintype, width, height, 0x0040);
330 skin_shirt = GL_SkinCheck((byte *)pskintype, width, height, 0x0002);
331 skin_glow = GL_SkinCheck((byte *)pskintype, width, height, 0xC000);
332 skin_body = GL_SkinCheck((byte *)pskintype, width, height, 0x3FFF);
333 if (skin_pants || skin_shirt)
336 saveskin = Hunk_AllocName(width*height, va("%s skin", loadname));
337 memcpy((saveskin, byte *)pskintype, width*height);
338 temp = (int) saveskin - (int) mheader;
339 skintexnum[0] = skin_normal ? -temp : 0;
340 skintexnum[1] = skin_pants ? -temp : 0;
341 skintexnum[2] = skin_shirt ? -temp : 0;
342 skintexnum[3] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", basename)); // glow
343 skintexnum[4] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", basename)); // body (normal + pants + shirt, but not glow)
350 skintexnum[3] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", basename)); // glow
351 skintexnum[4] = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", basename)); // body (normal + pants + shirt, but not glow)
355 skintex[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, r_mipskins.value, true);
358 skintex[3] = loadtextureimage(va("%s_glow" , basename), 0, 0, false, r_mipskins.value, true);
362 skintex[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, r_mipskins.value, true);
363 skintex[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, r_mipskins.value, true);
367 skintex[0] = loadtextureimage(basename, 0, 0, false, true, true);
370 skintex[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
371 skintex[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
372 skintex[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), true); // glow
373 if (skintex[1] || skintex[2])
375 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
376 skintex[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), true); // body (normal + pants + shirt, but not glow)
379 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // no special colors
390 void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
396 daliasskingroup_t *pinskingroup;
398 daliasskininterval_t *pinskinintervals;
399 int skinranges, skincount, *skinrange, skinnum;
400 rtexture_t **skintex;
402 byte *skintemp = NULL;
404 skin = (byte *)(pskintype + 1);
406 if (numskins < 1 || numskins > MAX_SKINS)
407 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
410 skintemp = qmalloc(s);
412 // LordHavoc: skim the data, measure the number of skins and number of groups
413 skinranges = numskins;
416 for (i = 0;i < numskins;i++)
419 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
422 (byte *)pskintype += s;
426 groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
427 skincount += groupskins;
428 (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
433 skinrange = loadmodel->skinanimrange;
434 skintex = loadmodel->skinanim;
435 // skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
436 // skintexnum = skinrange + skinranges * 2;
437 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
438 // loadmodel->skinanim = (int) skintexnum - (int) pheader;
440 for (i = 0;i < numskins;i++)
442 *skinrange++ = skinnum; // start of the range
444 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
446 *skinrange++ = 1; // single skin
448 sprintf (name, "%s_%i", loadmodel->name, i);
449 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
451 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
455 // animating skin group. yuck.
456 pinskingroup = (daliasskingroup_t *)pskintype;
457 groupskins = LittleLong (pinskingroup->numskins);
458 pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
460 pskintype = (void *)(pinskinintervals + groupskins);
462 *skinrange++ = groupskins; // number of skins
463 skinnum += groupskins;
464 for (j = 0;j < groupskins;j++)
466 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
467 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
469 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
473 loadmodel->numskins = numskins;
476 return (void *)pskintype;
479 void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
482 for (i = 0;i < numskins;i++)
485 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
486 (byte *)pskintype += skinsize;
488 (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
493 //=========================================================================
495 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
502 #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);
503 #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);
504 void Mod_LoadAliasModel (model_t *mod, void *buffer)
506 int i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
508 stvert_t *pinstverts;
509 dtriangle_t *pintriangles;
510 daliasframetype_t *pframetype;
511 daliasskintype_t *pskintype;
512 float *pouttexcoords, scales, scalet;
513 maliashdr_t *mheader;
514 unsigned short *pouttris;
515 trivertx_t *posevert;
516 animscene_t *animscenes;
518 start = Hunk_LowMark ();
520 pinmodel = (mdl_t *)buffer;
522 version = LittleLong (pinmodel->version);
523 if (version != ALIAS_VERSION)
524 Host_Error ("%s has wrong version number (%i should be %i)",
525 mod->name, version, ALIAS_VERSION);
527 mod->type = mod_alias;
528 mod->aliastype = ALIASTYPE_MDL;
530 numframes = LittleLong(pinmodel->numframes);
531 BOUNDI(numframes,0,65536);
532 numverts = LittleLong(pinmodel->numverts);
533 BOUNDI(numverts,0,MAXALIASVERTS);
534 numtris = LittleLong(pinmodel->numtris);
535 BOUNDI(numtris,0,65536);
536 numskins = LittleLong(pinmodel->numskins);
537 BOUNDI(numskins,0,256);
538 skinwidth = LittleLong (pinmodel->skinwidth);
539 BOUNDI(skinwidth,0,4096);
540 skinheight = LittleLong (pinmodel->skinheight);
541 BOUNDI(skinheight,0,1024);
543 pskintype = (daliasskintype_t *)&pinmodel[1];
544 pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
545 pintriangles = (dtriangle_t *)&pinstverts[numverts];
546 pframetype = (daliasframetype_t *)&pintriangles[numtris];
549 for (i=0 ; i<numframes ; i++)
551 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
554 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + (sizeof(daliasframe_t) + sizeof(trivertx_t) * numverts) );
558 f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
560 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
565 mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
566 mod->flags = LittleLong (pinmodel->flags);
567 // endian-adjust and copy the data, starting with the alias model header
568 mheader->numverts = numverts;
569 mod->numtris = mheader->numtris = numtris;
570 mod->numframes = mheader->numframes = numframes;
571 mod->synctype = LittleLong (pinmodel->synctype);
572 BOUNDI(mod->synctype,0,2);
574 for (i=0 ; i<3 ; i++)
576 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
577 BOUNDF(mheader->scale[i],0,65536);
578 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
579 BOUNDF(mheader->scale_origin[i],-65536,65536);
583 pskintype = (daliasskintype_t *)&pinmodel[1];
584 pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
586 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
587 pinstverts = (stvert_t *)pskintype;
589 // LordHavoc: byteswap and convert stvert data
590 scales = 1.0 / skinwidth;
591 scalet = 1.0 / skinheight;
592 for (i = 0;i < numverts;i++)
594 vertonseam[i] = LittleLong(pinstverts[i].onseam);
595 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
596 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
597 vertst[i+numverts][0] = vertst[i][0] + 0.5;
598 vertst[i+numverts][1] = vertst[i][1];
600 vertusage[i+numverts] = 0;
603 // load triangle data
604 pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
605 mheader->tridata = (int) pouttris - (int) mheader;
606 pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
608 // count the vertices used
609 for (i = 0;i < numverts*2;i++)
611 for (i = 0;i < numtris;i++)
613 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
614 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
615 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
616 if (!LittleLong(pintriangles[i].facesfront)) // backface
618 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
619 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
620 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
622 vertusage[temptris[i][0]]++;
623 vertusage[temptris[i][1]]++;
624 vertusage[temptris[i][2]]++;
626 // build remapping table and compact array
628 for (i = 0;i < numverts*2;i++)
632 vertremap[i] = totalverts;
633 vertst[totalverts][0] = vertst[i][0];
634 vertst[totalverts][1] = vertst[i][1];
638 vertremap[i] = -1; // not used at all
640 mheader->numverts = totalverts;
641 // remap the triangle references
642 for (i = 0;i < numtris;i++)
644 *pouttris++ = vertremap[temptris[i][0]];
645 *pouttris++ = vertremap[temptris[i][1]];
646 *pouttris++ = vertremap[temptris[i][2]];
648 // store the texture coordinates
649 pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
650 mheader->texdata = (int) pouttexcoords - (int) mheader;
651 for (i = 0;i < totalverts;i++)
653 *pouttexcoords++ = vertst[i][0];
654 *pouttexcoords++ = vertst[i][1];
659 posevert = Hunk_AllocName(sizeof(trivertx_t) * numposes * totalverts, va("%s vertex data", loadname));
660 mheader->posedata = (int) posevert - (int) mheader;
661 pframetype = (daliasframetype_t *)&pintriangles[numtris];
663 // LordHavoc: doing proper bbox for model
664 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
665 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
667 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
669 for (i = 0;i < numframes;i++)
671 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
672 pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
674 pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
677 mod->ofs_scenes = (int) animscenes - (int) mheader;
679 // LordHavoc: fixed model bbox - was //FIXME: do this right
680 //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
681 //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
682 for (j = 0;j < 3;j++)
684 mod->mins[j] = aliasbboxmin[j];
685 mod->maxs[j] = aliasbboxmax[j];
688 // move the complete, relocatable alias model to the cache
689 end = Hunk_LowMark ();
690 mod->cachesize = total = end - start;
692 Cache_Alloc (&mod->cache, total, loadname);
693 if (!mod->cache.data)
695 memcpy (mod->cache.data, mheader, total);
697 Hunk_FreeToLowMark (start);
700 void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
702 int i, invalidnormals = 0;
704 for (i = 0;i < numverts;i++)
706 VectorCopy(v[i].v, out[i].v);
707 temp[0] = v[i].v[0] * scale[0] + translate[0];
708 temp[1] = v[i].v[1] * scale[1] + translate[1];
709 temp[2] = v[i].v[2] * scale[2] + translate[2];
710 // update bounding box
711 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
712 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
713 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
714 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
715 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
716 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
717 out[i].lightnormalindex = v[i].lightnormalindex;
718 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
721 out[i].lightnormalindex = 0;
725 Con_Printf("Mod_ConvertQ2AliasVerts: %i invalid normal indices found\n", invalidnormals);
733 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
735 int i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
738 md2triangle_t *pintriangles, *pouttriangles;
739 md2frame_t *pinframe;
740 md2frame_t *poutframe;
743 animscene_t *animscenes;
745 start = Hunk_LowMark ();
748 // temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
750 pinmodel = (md2_t *)buffer;
752 version = LittleLong (pinmodel->version);
753 if (version != MD2ALIAS_VERSION)
754 Host_Error ("%s has wrong version number (%i should be %i)",
755 mod->name, version, MD2ALIAS_VERSION);
757 mod->type = mod_alias;
758 mod->aliastype = ALIASTYPE_MD2;
760 framesize = sizeof(md2framesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivertx_t);
761 // LordHavoc: calculate size for in memory version
762 size = sizeof(md2mem_t)
763 + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
764 + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
765 + LittleLong(pinmodel->num_frames) * framesize
766 + LittleLong(pinmodel->num_glcmds) * sizeof(int);
767 if (size <= 0 || size >= MD2MAX_SIZE)
768 Host_Error ("%s is not a valid model", mod->name);
769 pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
771 mod->flags = 0; // there are no MD2 flags
772 mod->numframes = LittleLong(pinmodel->num_frames);
773 mod->synctype = ST_RAND;
774 mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
776 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
777 Host_Error ("%s is not a valid model", mod->name);
778 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
779 Host_Error ("%s is not a valid model", mod->name);
780 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
781 Host_Error ("%s is not a valid model", mod->name);
782 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
783 Host_Error ("%s is not a valid model", mod->name);
784 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
785 Host_Error ("%s is not a valid model", mod->name);
787 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
788 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
789 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
790 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
791 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
792 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
793 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
794 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
796 pheader->framesize = framesize;
797 pheader->num_skins = LittleLong(pinmodel->num_skins);
798 pheader->num_xyz = LittleLong(pinmodel->num_xyz);
799 pheader->num_st = LittleLong(pinmodel->num_st);
800 pheader->num_tris = LittleLong(pinmodel->num_tris);
801 pheader->num_frames = LittleLong(pinmodel->num_frames);
802 pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
805 if (pheader->num_skins)
809 skinrange = loadmodel->skinanimrange;
810 skin = loadmodel->skinanim;
811 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
812 // skin = skinrange + pheader->num_skins * 2;
813 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
814 // loadmodel->skinanim = (int) skin - (int) pheader;
815 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
816 for (i = 0;i < pheader->num_skins;i++)
820 *skin++ = loadtextureimage (pinskins, 0, 0, true, r_mipskins.value, true);
821 *skin++ = NULL; // the extra 4 layers are currently unused
825 pinskins += MD2MAX_SKINNAME;
828 loadmodel->numskins = pheader->num_skins;
831 pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
832 pouttriangles = (void*)&pheader[1];
833 pheader->ofs_tris = (int) pouttriangles - (int) pheader;
835 // swap the triangle list
836 for (i = 0;i < pheader->num_tris;i++)
838 for (j = 0;j < 3;j++)
840 temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
841 pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
842 if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
843 Host_Error ("%s has invalid vertex indices", mod->name);
844 if (pouttriangles->index_st[j] >= pheader->num_st)
845 Host_Error ("%s has invalid vertex indices", mod->name);
851 // LordHavoc: doing proper bbox for model
852 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
853 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
856 pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
857 poutframe = (void*) pouttriangles;
858 pheader->ofs_frames = (int) poutframe - (int) pheader;
860 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
862 for (i = 0;i < pheader->num_frames;i++)
864 strcpy(poutframe->name, pinframe->name);
865 for (j = 0;j < 3;j++)
867 poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
868 poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
870 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
872 strcpy(animscenes[i].name, poutframe->name);
873 animscenes[i].firstframe = i;
874 animscenes[i].framecount = 1;
875 animscenes[i].framerate = 10;
876 animscenes[i].loop = true;
878 pinframe = (void*) &pinframe->verts[j];
879 poutframe = (void*) &poutframe->verts[j];
882 mod->ofs_scenes = (int) animscenes - (int) pheader;
884 // LordHavoc: model bbox
885 for (j = 0;j < 3;j++)
887 mod->mins[j] = aliasbboxmin[j];
888 mod->maxs[j] = aliasbboxmax[j];
891 // load the draw list
892 pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
893 poutglcmd = (void*) poutframe;
894 pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
895 for (i = 0;i < pheader->num_glcmds;i++)
896 *poutglcmd++ = LittleLong(*pinglcmd++);
898 // move the complete, relocatable alias model to the cache
899 end = Hunk_LowMark ();
900 mod->cachesize = total = end - start;
902 Cache_Alloc (&mod->cache, total, loadname);
903 if (!mod->cache.data)
905 memcpy (mod->cache.data, pheader, total);
907 Hunk_FreeToLowMark (start);
910 void swapintblock(int *m, int size)
917 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
919 int i, pbase, start, end, total, *skinrange;
920 rtexture_t **texture, **skin;
922 zymtype1header_t *pinmodel, *pheader;
925 animscene_t *animscenes;
927 start = Hunk_LowMark ();
929 pinmodel = (void *)buffer;
931 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
932 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
934 if (BigLong(pinmodel->type) != 1)
935 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
937 mod->type = mod_alias;
938 mod->aliastype = ALIASTYPE_ZYM;
940 pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
942 pbase = (int) pheader;
944 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
947 memcpy(pheader->id, pinmodel->id, 12);
948 pheader->type = BigLong(pheader->type);
949 pheader->filesize = BigLong(pheader->filesize);
950 pheader->mins[0] = BigFloat(pheader->mins[0]);
951 pheader->mins[1] = BigFloat(pheader->mins[1]);
952 pheader->mins[2] = BigFloat(pheader->mins[2]);
953 pheader->maxs[0] = BigFloat(pheader->maxs[0]);
954 pheader->maxs[1] = BigFloat(pheader->maxs[1]);
955 pheader->maxs[2] = BigFloat(pheader->maxs[2]);
956 pheader->radius = BigFloat(pheader->radius);
957 pheader->numverts = BigLong(pheader->numverts);
958 pheader->numtris = BigLong(pheader->numtris);
959 pheader->numshaders = BigLong(pheader->numshaders);
960 pheader->numbones = BigLong(pheader->numbones);
961 pheader->numscenes = BigLong(pheader->numscenes);
964 pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
965 pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
966 pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
967 pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
968 pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
969 pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
970 pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
971 pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
972 pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
974 mod->flags = 0; // there are no flags
975 mod->numframes = pheader->numscenes;
976 mod->synctype = ST_SYNC;
977 mod->numtris = pheader->numtris;
979 // FIXME: add skin support and texturing and shaders and...
981 skinrange = loadmodel->skinanimrange;
982 skin = loadmodel->skinanim;
983 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
984 // skin = skinrange + pheader->num_skins * 2;
985 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
986 // loadmodel->skinanim = (int) skin - (int) pheader;
994 loadmodel->numskins = 1;
996 // go through the lumps, swapping things
998 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
999 scene = (void *) (pheader->lump_scenes.start + pbase);
1000 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
1001 for (i = 0;i < pheader->numscenes;i++)
1003 scene->mins[0] = BigFloat(scene->mins[0]);
1004 scene->mins[1] = BigFloat(scene->mins[1]);
1005 scene->mins[2] = BigFloat(scene->mins[2]);
1006 scene->maxs[0] = BigFloat(scene->maxs[0]);
1007 scene->maxs[1] = BigFloat(scene->maxs[1]);
1008 scene->maxs[2] = BigFloat(scene->maxs[2]);
1009 scene->radius = BigFloat(scene->radius);
1010 scene->framerate = BigFloat(scene->framerate);
1011 scene->flags = BigLong(scene->flags);
1012 scene->start = BigLong(scene->start);
1013 scene->length = BigLong(scene->length);
1015 memcpy(animscenes[i].name, scene->name, 32);
1016 animscenes[i].firstframe = scene->start;
1017 animscenes[i].framecount = scene->length;
1018 animscenes[i].framerate = scene->framerate;
1019 animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
1023 mod->ofs_scenes = (int) animscenes - pbase;
1025 // zymlump_t lump_poses; // float pose[numposes][numbones][6]; // animation data
1026 swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
1028 // zymlump_t lump_bones; // zymbone_t bone[numbones];
1029 bone = (void *) (pheader->lump_bones.start + pbase);
1030 for (i = 0;i < pheader->numbones;i++)
1032 bone->flags = BigLong(bone->flags);
1033 bone->parent = BigLong(bone->parent);
1037 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
1038 swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
1040 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
1041 swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
1043 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
1044 swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
1046 // zymlump_t lump_render; // int renderlist[rendersize]; // sorted by shader with run lengths (int shader, count), each run can be used with glDrawElements (each triangle is 3 int indices)
1047 swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
1049 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
1050 shadername = (void *) (pheader->lump_shaders.start + pbase);
1051 texture = (void *) shadername;
1052 for (i = 0;i < pheader->numshaders;i++)
1055 rt = loadtextureimage(shadername, 0, 0, true, r_mipskins.value, true);
1057 *texture++ = rt; // reuse shader name list for texture pointers
1060 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
1061 swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
1064 for (i = 0;i < 3;i++)
1066 mod->mins[i] = pheader->mins[i];
1067 mod->maxs[i] = pheader->maxs[i];
1070 // move the complete, relocatable alias model to the cache
1071 end = Hunk_LowMark ();
1072 mod->cachesize = total = end - start;
1074 Cache_Alloc (&mod->cache, total, loadname);
1075 if (!mod->cache.data)
1077 memcpy (mod->cache.data, pheader, total);
1079 Hunk_FreeToLowMark (start);