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));
228 static void Mod_LoadSkin (maliashdr_t *mheader, char *basename, byte *skindata, byte *skintemp, int width, int height, rtexture_t **skintex)
230 skintex[0] = loadtextureimage(va("%s_normal", basename), 0, 0, false, r_mipskins.value, true);
233 skintex[3] = loadtextureimage(va("%s_glow" , basename), 0, 0, false, r_mipskins.value, true);
237 skintex[1] = loadtextureimage(va("%s_pants" , basename), 0, 0, false, r_mipskins.value, true);
238 skintex[2] = loadtextureimage(va("%s_shirt" , basename), 0, 0, false, r_mipskins.value, true);
242 skintex[0] = loadtextureimage(basename, 0, 0, false, true, true);
245 skintex[1] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0040, va("%s_pants", basename), false); // pants
246 skintex[2] = GL_SkinSplitShirt(skindata, skintemp, width, height, 0x0002, va("%s_shirt", basename), false); // shirt
247 skintex[3] = GL_SkinSplit(skindata, skintemp, width, height, 0xC000, va("%s_glow", basename), true); // glow
248 if (skintex[1] || skintex[2])
250 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FBD, va("%s_normal", basename), false); // normal (no special colors)
251 skintex[4] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_body", basename), true); // body (normal + pants + shirt, but not glow)
254 skintex[0] = GL_SkinSplit(skindata, skintemp, width, height, 0x3FFF, va("%s_base", basename), true); // no special colors
257 if (R_TextureHasAlpha(skintex[0]))
258 loadmodel->flags2 |= MODF_TRANSPARENT;
266 static void *Mod_LoadAllSkins (maliashdr_t *mheader, int numskins, daliasskintype_t *pskintype, int width, int height)
272 daliasskingroup_t *pinskingroup;
274 daliasskininterval_t *pinskinintervals;
275 int skinranges, skincount, *skinrange, skinnum;
276 rtexture_t **skintex;
278 byte *skintemp = NULL;
280 skin = (byte *)(pskintype + 1);
282 if (numskins < 1 || numskins > MAX_SKINS)
283 Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
286 skintemp = qmalloc(s);
288 // LordHavoc: skim the data, measure the number of skins and number of groups
289 skinranges = numskins;
292 for (i = 0;i < numskins;i++)
295 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
298 (byte *)pskintype += s;
302 groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
303 skincount += groupskins;
304 (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
309 skinrange = loadmodel->skinanimrange;
310 skintex = loadmodel->skinanim;
311 // skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
312 // skintexnum = skinrange + skinranges * 2;
313 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
314 // loadmodel->skinanim = (int) skintexnum - (int) pheader;
316 for (i = 0;i < numskins;i++)
318 *skinrange++ = skinnum; // start of the range
320 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
322 *skinrange++ = 1; // single skin
324 sprintf (name, "%s_%i", loadmodel->name, i);
325 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
327 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
331 // animating skin group. yuck.
332 pinskingroup = (daliasskingroup_t *)pskintype;
333 groupskins = LittleLong (pinskingroup->numskins);
334 pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
336 pskintype = (void *)(pinskinintervals + groupskins);
338 *skinrange++ = groupskins; // number of skins
339 skinnum += groupskins;
340 for (j = 0;j < groupskins;j++)
342 sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
343 Mod_LoadSkin(mheader, name, (byte *)pskintype, skintemp, width, height, skintex);
345 pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
349 loadmodel->numskins = numskins;
352 return (void *)pskintype;
355 static void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
358 for (i = 0;i < numskins;i++)
361 if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
362 (byte *)pskintype += skinsize;
364 (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
369 //=========================================================================
371 //void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
378 #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);
379 #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);
380 void Mod_LoadAliasModel (model_t *mod, void *buffer)
382 int i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
384 stvert_t *pinstverts;
385 dtriangle_t *pintriangles;
386 daliasframetype_t *pframetype;
387 daliasskintype_t *pskintype;
388 float *pouttexcoords, scales, scalet;
389 maliashdr_t *mheader;
390 unsigned short *pouttris;
391 trivertx_t *posevert;
392 animscene_t *animscenes;
397 start = Hunk_LowMark ();
399 pinmodel = (mdl_t *)buffer;
401 version = LittleLong (pinmodel->version);
402 if (version != ALIAS_VERSION)
403 Host_Error ("%s has wrong version number (%i should be %i)",
404 mod->name, version, ALIAS_VERSION);
406 mod->type = mod_alias;
407 mod->aliastype = ALIASTYPE_MDL;
409 numframes = LittleLong(pinmodel->numframes);
410 BOUNDI(numframes,0,65536);
411 numverts = LittleLong(pinmodel->numverts);
412 BOUNDI(numverts,0,MAXALIASVERTS);
413 numtris = LittleLong(pinmodel->numtris);
414 BOUNDI(numtris,0,MAXALIASTRIS);
415 numskins = LittleLong(pinmodel->numskins);
416 BOUNDI(numskins,0,256);
417 skinwidth = LittleLong (pinmodel->skinwidth);
418 BOUNDI(skinwidth,0,4096);
419 skinheight = LittleLong (pinmodel->skinheight);
420 BOUNDI(skinheight,0,1024);
422 pskintype = (daliasskintype_t *)&pinmodel[1];
423 pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
424 pintriangles = (dtriangle_t *)&pinstverts[numverts];
425 pframetype = (daliasframetype_t *)&pintriangles[numtris];
428 for (i=0 ; i<numframes ; i++)
430 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
433 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + (sizeof(daliasframe_t) + sizeof(trivertx_t) * numverts) );
437 f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
439 pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
444 mheader = Hunk_AllocName (sizeof(maliashdr_t), va("%s model header", loadname));
445 mod->flags = LittleLong (pinmodel->flags);
446 // endian-adjust and copy the data, starting with the alias model header
447 mheader->numverts = numverts;
448 mod->numtris = mheader->numtris = numtris;
449 mod->numframes = mheader->numframes = numframes;
450 mod->synctype = LittleLong (pinmodel->synctype);
451 BOUNDI(mod->synctype,0,2);
453 for (i=0 ; i<3 ; i++)
455 mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
456 BOUNDF(mheader->scale[i],0,65536);
457 mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
458 BOUNDF(mheader->scale_origin[i],-65536,65536);
462 pskintype = (daliasskintype_t *)&pinmodel[1];
463 pskintype = Mod_LoadAllSkins(mheader, numskins, pskintype, skinwidth, skinheight);
465 // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
466 pinstverts = (stvert_t *)pskintype;
468 // LordHavoc: byteswap and convert stvert data
469 scales = 1.0 / skinwidth;
470 scalet = 1.0 / skinheight;
471 for (i = 0;i < numverts;i++)
473 vertonseam[i] = LittleLong(pinstverts[i].onseam);
474 vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
475 vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
476 vertst[i+numverts][0] = vertst[i][0] + 0.5;
477 vertst[i+numverts][1] = vertst[i][1];
479 vertusage[i+numverts] = 0;
482 // load triangle data
483 pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, va("%s triangles", loadname));
484 mheader->tridata = (int) pouttris - (int) mheader;
485 pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
487 // count the vertices used
488 for (i = 0;i < numverts*2;i++)
490 for (i = 0;i < numtris;i++)
492 temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
493 temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
494 temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
495 if (!LittleLong(pintriangles[i].facesfront)) // backface
497 if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
498 if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
499 if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
501 vertusage[temptris[i][0]]++;
502 vertusage[temptris[i][1]]++;
503 vertusage[temptris[i][2]]++;
505 // build remapping table and compact array
507 for (i = 0;i < numverts*2;i++)
511 vertremap[i] = totalverts;
512 vertst[totalverts][0] = vertst[i][0];
513 vertst[totalverts][1] = vertst[i][1];
517 vertremap[i] = -1; // not used at all
519 mheader->numverts = totalverts;
520 // remap the triangle references
521 for (i = 0;i < numtris;i++)
523 *pouttris++ = vertremap[temptris[i][0]];
524 *pouttris++ = vertremap[temptris[i][1]];
525 *pouttris++ = vertremap[temptris[i][2]];
527 // store the texture coordinates
528 pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, va("%s texcoords", loadname));
529 mheader->texdata = (int) pouttexcoords - (int) mheader;
530 for (i = 0;i < totalverts;i++)
532 *pouttexcoords++ = vertst[i][0];
533 *pouttexcoords++ = vertst[i][1];
538 posevert = Hunk_AllocName(sizeof(trivertx_t) * numposes * totalverts, va("%s vertex data", loadname));
539 mheader->posedata = (int) posevert - (int) mheader;
540 pframetype = (daliasframetype_t *)&pintriangles[numtris];
542 // LordHavoc: doing proper bbox for model
543 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
544 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
546 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
548 for (i = 0;i < numframes;i++)
550 if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
551 pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
553 pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, mheader, numverts, totalverts, &posevert, animscenes + i);
556 mod->ofs_scenes = (int) animscenes - (int) mheader;
558 // LordHavoc: fixed model bbox - was //FIXME: do this right
559 //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
560 //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
561 modelyawradius = sqrt(modelyawradius);
562 modelradius = sqrt(modelradius);
563 // mod->modelradius = modelradius;
564 for (j = 0;j < 3;j++)
566 mod->normalmins[j] = aliasbboxmin[j];
567 mod->normalmaxs[j] = aliasbboxmax[j];
568 mod->rotatedmins[j] = -modelradius;
569 mod->rotatedmaxs[j] = modelradius;
571 mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
572 mod->yawmins[2] = mod->normalmins[2];
573 mod->yawmaxs[2] = mod->normalmaxs[2];
575 mod->SERAddEntity = Mod_Alias_SERAddEntity;
576 mod->DrawEarly = NULL;
577 mod->DrawLate = R_DrawAliasModel;
578 mod->DrawShadow = NULL;
580 // move the complete, relocatable alias model to the cache
581 end = Hunk_LowMark ();
582 mod->cachesize = total = end - start;
584 Cache_Alloc (&mod->cache, total, loadname);
585 if (!mod->cache.data)
587 memcpy (mod->cache.data, mheader, total);
589 Hunk_FreeToLowMark (start);
592 static void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivertx_t *out)
594 int i, invalidnormals = 0;
597 for (i = 0;i < numverts;i++)
599 VectorCopy(v[i].v, out[i].v);
600 temp[0] = v[i].v[0] * scale[0] + translate[0];
601 temp[1] = v[i].v[1] * scale[1] + translate[1];
602 temp[2] = v[i].v[2] * scale[2] + translate[2];
603 // update bounding box
604 if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
605 if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
606 if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
607 if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
608 if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
609 if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
610 dist = temp[0]*temp[0]+temp[1]*temp[1];
611 if (modelyawradius < dist)
612 modelyawradius = dist;
613 dist += temp[2]*temp[2];
614 if (modelradius < dist)
616 out[i].lightnormalindex = v[i].lightnormalindex;
617 if (out[i].lightnormalindex >= NUMVERTEXNORMALS)
620 out[i].lightnormalindex = 0;
624 Con_Printf("Mod_ConvertQ2AliasVerts: \"%s\", %i invalid normal indices found\n", loadname, invalidnormals);
632 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
634 int i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
637 md2triangle_t *pintriangles, *pouttriangles;
638 md2frame_t *pinframe;
639 md2frame_t *poutframe;
642 animscene_t *animscenes;
647 start = Hunk_LowMark ();
650 // temptris = qmalloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
652 pinmodel = (md2_t *)buffer;
654 version = LittleLong (pinmodel->version);
655 if (version != MD2ALIAS_VERSION)
656 Host_Error ("%s has wrong version number (%i should be %i)",
657 mod->name, version, MD2ALIAS_VERSION);
659 mod->type = mod_alias;
660 mod->aliastype = ALIASTYPE_MD2;
662 framesize = sizeof(md2framesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivertx_t);
663 // LordHavoc: calculate size for in memory version
664 size = sizeof(md2mem_t)
665 + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
666 + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
667 + LittleLong(pinmodel->num_frames) * framesize
668 + LittleLong(pinmodel->num_glcmds) * sizeof(int);
669 if (size <= 0 || size >= MD2MAX_SIZE)
670 Host_Error ("%s is not a valid model", mod->name);
671 pheader = Hunk_AllocName (size, va("%s Quake2 model", loadname));
673 mod->flags = 0; // there are no MD2 flags
674 mod->numframes = LittleLong(pinmodel->num_frames);
675 mod->synctype = ST_RAND;
676 mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
678 if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
679 Host_Error ("%s is not a valid model", mod->name);
680 if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
681 Host_Error ("%s is not a valid model", mod->name);
682 if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
683 Host_Error ("%s is not a valid model", mod->name);
684 if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
685 Host_Error ("%s is not a valid model", mod->name);
686 if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
687 Host_Error ("%s is not a valid model", mod->name);
689 if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
690 Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
691 if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
692 Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
693 if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
694 Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
695 if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MAX_SKINS)
696 Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
698 pheader->framesize = framesize;
699 pheader->num_skins = LittleLong(pinmodel->num_skins);
700 pheader->num_xyz = LittleLong(pinmodel->num_xyz);
701 pheader->num_st = LittleLong(pinmodel->num_st);
702 pheader->num_tris = LittleLong(pinmodel->num_tris);
703 pheader->num_frames = LittleLong(pinmodel->num_frames);
704 pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
707 if (pheader->num_skins)
711 skinrange = loadmodel->skinanimrange;
712 skin = loadmodel->skinanim;
713 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
714 // skin = skinrange + pheader->num_skins * 2;
715 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
716 // loadmodel->skinanim = (int) skin - (int) pheader;
717 pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
718 for (i = 0;i < pheader->num_skins;i++)
722 *skin++ = loadtextureimage (pinskins, 0, 0, true, r_mipskins.value, true);
723 *skin++ = NULL; // the extra 4 layers are currently unused
727 pinskins += MD2MAX_SKINNAME;
730 loadmodel->numskins = pheader->num_skins;
733 pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
734 pouttriangles = (void*)&pheader[1];
735 pheader->ofs_tris = (int) pouttriangles - (int) pheader;
737 // swap the triangle list
738 for (i = 0;i < pheader->num_tris;i++)
740 for (j = 0;j < 3;j++)
742 temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
743 pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
744 if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
745 Host_Error ("%s has invalid vertex indices", mod->name);
746 if (pouttriangles->index_st[j] >= pheader->num_st)
747 Host_Error ("%s has invalid vertex indices", mod->name);
753 // LordHavoc: doing proper bbox for model
754 aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
755 aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
758 pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
759 poutframe = (void*) pouttriangles;
760 pheader->ofs_frames = (int) poutframe - (int) pheader;
762 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
764 for (i = 0;i < pheader->num_frames;i++)
766 strcpy(poutframe->name, pinframe->name);
767 for (j = 0;j < 3;j++)
769 poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
770 poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
772 Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
774 strcpy(animscenes[i].name, poutframe->name);
775 animscenes[i].firstframe = i;
776 animscenes[i].framecount = 1;
777 animscenes[i].framerate = 10;
778 animscenes[i].loop = true;
780 pinframe = (void*) &pinframe->verts[j];
781 poutframe = (void*) &poutframe->verts[j];
784 mod->ofs_scenes = (int) animscenes - (int) pheader;
786 // LordHavoc: model bbox
787 modelyawradius = sqrt(modelyawradius);
788 modelradius = sqrt(modelradius);
789 // mod->modelradius = modelradius;
790 for (j = 0;j < 3;j++)
792 mod->normalmins[j] = aliasbboxmin[j];
793 mod->normalmaxs[j] = aliasbboxmax[j];
794 mod->rotatedmins[j] = -modelradius;
795 mod->rotatedmaxs[j] = modelradius;
797 mod->yawmins[0] = mod->yawmins[1] = -(mod->yawmaxs[0] = mod->yawmaxs[1] = modelyawradius);
798 mod->yawmins[2] = mod->normalmins[2];
799 mod->yawmaxs[2] = mod->normalmaxs[2];
801 // load the draw list
802 pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
803 poutglcmd = (void*) poutframe;
804 pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
805 for (i = 0;i < pheader->num_glcmds;i++)
806 *poutglcmd++ = LittleLong(*pinglcmd++);
808 mod->SERAddEntity = Mod_Alias_SERAddEntity;
809 mod->DrawEarly = NULL;
810 mod->DrawLate = R_DrawAliasModel;
811 mod->DrawShadow = NULL;
813 // move the complete, relocatable alias model to the cache
814 end = Hunk_LowMark ();
815 mod->cachesize = total = end - start;
817 Cache_Alloc (&mod->cache, total, loadname);
818 if (!mod->cache.data)
820 memcpy (mod->cache.data, pheader, total);
822 Hunk_FreeToLowMark (start);
825 static void swapintblock(int *m, int size)
835 void Mod_LoadZymoticModel (model_t *mod, void *buffer)
837 int i, pbase, start, end, total, *skinrange;
838 rtexture_t **texture, **skin;
840 zymtype1header_t *pinmodel, *pheader;
843 animscene_t *animscenes;
844 float corner[2], modelradius;
846 start = Hunk_LowMark ();
848 pinmodel = (void *)buffer;
850 if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
851 Host_Error ("Mod_LoadZymoticModel: %s is not a zymotic model\n");
853 if (BigLong(pinmodel->type) != 1)
854 Host_Error ("Mod_LoadZymoticModel: only type 1 (skeletal pose) models are currently supported\n");
856 mod->type = mod_alias;
857 mod->aliastype = ALIASTYPE_ZYM;
859 pheader = Hunk_AllocName (BigLong(pinmodel->filesize), va("%s Zymotic model", loadname));
861 pbase = (int) pheader;
863 memcpy(pheader, pinmodel, BigLong(pinmodel->filesize));
866 memcpy(pheader->id, pinmodel->id, 12);
867 pheader->type = BigLong(pheader->type);
868 pheader->filesize = BigLong(pheader->filesize);
869 pheader->mins[0] = BigFloat(pheader->mins[0]);
870 pheader->mins[1] = BigFloat(pheader->mins[1]);
871 pheader->mins[2] = BigFloat(pheader->mins[2]);
872 pheader->maxs[0] = BigFloat(pheader->maxs[0]);
873 pheader->maxs[1] = BigFloat(pheader->maxs[1]);
874 pheader->maxs[2] = BigFloat(pheader->maxs[2]);
875 pheader->radius = BigFloat(pheader->radius);
876 pheader->numverts = BigLong(pheader->numverts);
877 pheader->numtris = BigLong(pheader->numtris);
878 pheader->numshaders = BigLong(pheader->numshaders);
879 pheader->numbones = BigLong(pheader->numbones);
880 pheader->numscenes = BigLong(pheader->numscenes);
883 pheader->lump_scenes.start = BigLong(pheader->lump_scenes.start);pheader->lump_scenes.length = BigLong(pheader->lump_scenes.length);
884 pheader->lump_poses.start = BigLong(pheader->lump_poses.start);pheader->lump_poses.length = BigLong(pheader->lump_poses.length);
885 pheader->lump_bones.start = BigLong(pheader->lump_bones.start);pheader->lump_bones.length = BigLong(pheader->lump_bones.length);
886 pheader->lump_vertbonecounts.start = BigLong(pheader->lump_vertbonecounts.start);pheader->lump_vertbonecounts.length = BigLong(pheader->lump_vertbonecounts.length);
887 pheader->lump_verts.start = BigLong(pheader->lump_verts.start);pheader->lump_verts.length = BigLong(pheader->lump_verts.length);
888 pheader->lump_texcoords.start = BigLong(pheader->lump_texcoords.start);pheader->lump_texcoords.length = BigLong(pheader->lump_texcoords.length);
889 pheader->lump_render.start = BigLong(pheader->lump_render.start);pheader->lump_render.length = BigLong(pheader->lump_render.length);
890 pheader->lump_shaders.start = BigLong(pheader->lump_shaders.start);pheader->lump_shaders.length = BigLong(pheader->lump_shaders.length);
891 pheader->lump_trizone.start = BigLong(pheader->lump_trizone.start);pheader->lump_trizone.length = BigLong(pheader->lump_trizone.length);
893 mod->flags = 0; // there are no flags
894 mod->numframes = pheader->numscenes;
895 mod->synctype = ST_SYNC;
896 mod->numtris = pheader->numtris;
898 // FIXME: add skin support and texturing and shaders and...
900 skinrange = loadmodel->skinanimrange;
901 skin = loadmodel->skinanim;
902 // skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
903 // skin = skinrange + pheader->num_skins * 2;
904 // loadmodel->skinanimrange = (int) skinrange - (int) pheader;
905 // loadmodel->skinanim = (int) skin - (int) pheader;
913 loadmodel->numskins = 1;
915 // go through the lumps, swapping things
917 // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
918 scene = (void *) (pheader->lump_scenes.start + pbase);
919 animscenes = Hunk_AllocName(sizeof(animscene_t) * mod->numframes, va("%s sceneinfo", loadname));
920 for (i = 0;i < pheader->numscenes;i++)
922 scene->mins[0] = BigFloat(scene->mins[0]);
923 scene->mins[1] = BigFloat(scene->mins[1]);
924 scene->mins[2] = BigFloat(scene->mins[2]);
925 scene->maxs[0] = BigFloat(scene->maxs[0]);
926 scene->maxs[1] = BigFloat(scene->maxs[1]);
927 scene->maxs[2] = BigFloat(scene->maxs[2]);
928 scene->radius = BigFloat(scene->radius);
929 scene->framerate = BigFloat(scene->framerate);
930 scene->flags = BigLong(scene->flags);
931 scene->start = BigLong(scene->start);
932 scene->length = BigLong(scene->length);
934 memcpy(animscenes[i].name, scene->name, 32);
935 animscenes[i].firstframe = scene->start;
936 animscenes[i].framecount = scene->length;
937 animscenes[i].framerate = scene->framerate;
938 animscenes[i].loop = (scene->flags & ZYMSCENEFLAG_NOLOOP) == 0;
942 mod->ofs_scenes = (int) animscenes - pbase;
944 // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
945 swapintblock((void *) (pheader->lump_poses.start + pbase), pheader->lump_poses.length);
947 // zymlump_t lump_bones; // zymbone_t bone[numbones];
948 bone = (void *) (pheader->lump_bones.start + pbase);
949 for (i = 0;i < pheader->numbones;i++)
951 bone->flags = BigLong(bone->flags);
952 bone->parent = BigLong(bone->parent);
956 // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
957 swapintblock((void *) (pheader->lump_vertbonecounts.start + pbase), pheader->lump_vertbonecounts.length);
959 // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
960 swapintblock((void *) (pheader->lump_verts.start + pbase), pheader->lump_verts.length);
962 // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
963 swapintblock((void *) (pheader->lump_texcoords.start + pbase), pheader->lump_texcoords.length);
965 // 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)
966 swapintblock((void *) (pheader->lump_render.start + pbase), pheader->lump_render.length);
968 // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
969 shadername = (void *) (pheader->lump_shaders.start + pbase);
970 texture = (void *) shadername;
971 for (i = 0;i < pheader->numshaders;i++)
974 rt = loadtextureimage(shadername, 0, 0, true, r_mipskins.value, true);
976 *texture++ = rt; // reuse shader name list for texture pointers
979 // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
980 swapintblock((void *) (pheader->lump_trizone.start + pbase), pheader->lump_trizone.length);
983 modelradius = pheader->radius;
984 // mod->modelradius = pheader->radius;
985 for (i = 0;i < 3;i++)
987 mod->normalmins[i] = pheader->mins[i];
988 mod->normalmaxs[i] = pheader->maxs[i];
989 mod->rotatedmins[i] = -modelradius;
990 mod->rotatedmaxs[i] = modelradius;
992 corner[0] = max(fabs(mod->normalmins[0]), fabs(mod->normalmaxs[0]));
993 corner[1] = max(fabs(mod->normalmins[1]), fabs(mod->normalmaxs[1]));
994 mod->yawmaxs[0] = mod->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
995 if (mod->yawmaxs[0] > modelradius)
996 mod->yawmaxs[0] = mod->yawmaxs[1] = modelradius;
997 mod->yawmins[0] = mod->yawmins[1] = -mod->yawmaxs[0];
998 mod->yawmins[2] = mod->normalmins[2];
999 mod->yawmaxs[2] = mod->normalmaxs[2];
1001 mod->SERAddEntity = Mod_Alias_SERAddEntity;
1002 mod->DrawEarly = NULL;
1003 mod->DrawLate = R_DrawAliasModel;
1004 mod->DrawShadow = NULL;
1006 // move the complete, relocatable alias model to the cache
1007 end = Hunk_LowMark ();
1008 mod->cachesize = total = end - start;
1010 Cache_Alloc (&mod->cache, total, loadname);
1011 if (!mod->cache.data)
1013 memcpy (mod->cache.data, pheader, total);
1015 Hunk_FreeToLowMark (start);