2001-02-19 21:15:25 +00:00
|
|
|
/*
|
2002-08-25 14:25:38 +00:00
|
|
|
model_alias.c
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-25 14:25:38 +00:00
|
|
|
alias model loading and caching
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
// models are the only shared resource between a client and server running
|
|
|
|
// on the same machine.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/crc.h"
|
2001-04-10 06:55:28 +00:00
|
|
|
#include "QF/model.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/msg.h"
|
|
|
|
#include "QF/qendian.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/quakefs.h"
|
2001-05-10 06:01:11 +00:00
|
|
|
#include "QF/sys.h"
|
|
|
|
|
2002-08-20 00:48:59 +00:00
|
|
|
#include "compat.h"
|
2001-05-10 06:01:11 +00:00
|
|
|
#include "d_iface.h"
|
2001-02-19 21:15:25 +00:00
|
|
|
#include "r_local.h"
|
|
|
|
|
|
|
|
aliashdr_t *pheader;
|
|
|
|
|
2002-01-04 07:40:00 +00:00
|
|
|
stvert_t *stverts;
|
|
|
|
mtriangle_t *triangles;
|
|
|
|
int stverts_size = 0;
|
|
|
|
int triangles_size = 0;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// a pose is a single set of vertexes. a frame may be an animating
|
|
|
|
// sequence of poses
|
2001-02-19 21:15:25 +00:00
|
|
|
trivertx_t *poseverts[MAXALIASFRAMES];
|
|
|
|
int posenum = 0;
|
2002-06-14 09:33:37 +00:00
|
|
|
int aliasbboxmins[3], aliasbboxmaxs[3];
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void *
|
2001-11-21 19:13:53 +00:00
|
|
|
Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int *pskinindex)
|
|
|
|
{
|
|
|
|
byte *skin;
|
2002-08-20 00:48:59 +00:00
|
|
|
float *poutskinintervals;
|
|
|
|
int groupskins, skinsize, gnum, snum, t;
|
2001-11-21 19:13:53 +00:00
|
|
|
daliasskingroup_t *pinskingroup;
|
|
|
|
daliasskininterval_t *pinskinintervals;
|
|
|
|
maliasskindesc_t *pskindesc;
|
|
|
|
maliasskingroup_t *paliasskingroup;
|
|
|
|
|
|
|
|
if (numskins < 1 || numskins > MAX_SKINS)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Mod_LoadAliasModel: Invalid # of skins: %d", numskins);
|
2001-11-21 19:13:53 +00:00
|
|
|
|
|
|
|
skinsize = pheader->mdl.skinwidth * pheader->mdl.skinheight;
|
|
|
|
pskindesc = Hunk_AllocName (numskins * sizeof (maliasskindesc_t),
|
|
|
|
loadname);
|
|
|
|
|
|
|
|
*pskinindex = (byte *) pskindesc - (byte *) pheader;
|
|
|
|
|
|
|
|
for (snum = 0; snum < numskins; snum++) {
|
|
|
|
pskindesc[snum].type = pskintype->type;
|
|
|
|
if (pskintype->type == ALIAS_SKIN_SINGLE) {
|
|
|
|
skin = (byte *) (pskintype + 1);
|
|
|
|
skin = Mod_LoadSkin (skin, skinsize, snum, 0, false,
|
|
|
|
&pskindesc[snum]);
|
|
|
|
} else {
|
|
|
|
pskintype++;
|
|
|
|
pinskingroup = (daliasskingroup_t *) pskintype;
|
|
|
|
groupskins = LittleLong (pinskingroup->numskins);
|
|
|
|
|
|
|
|
t = field_offset (maliasskingroup_t, skindescs[groupskins]);
|
|
|
|
paliasskingroup = Hunk_AllocName (t, loadname);
|
|
|
|
paliasskingroup->numskins = groupskins;
|
|
|
|
|
|
|
|
pskindesc[snum].skin = (byte *) paliasskingroup - (byte *) pheader;
|
|
|
|
|
|
|
|
pinskinintervals = (daliasskininterval_t *) (pinskingroup + 1);
|
|
|
|
poutskinintervals = Hunk_AllocName (groupskins * sizeof (float),
|
|
|
|
loadname);
|
|
|
|
paliasskingroup->intervals =
|
|
|
|
(byte *) poutskinintervals - (byte *) pheader;
|
|
|
|
for (gnum = 0; gnum < groupskins; gnum++) {
|
|
|
|
*poutskinintervals = LittleFloat (pinskinintervals->interval);
|
|
|
|
if (*poutskinintervals <= 0)
|
|
|
|
Sys_Error ("Mod_LoadAliasSkinGroup: interval<=0");
|
|
|
|
|
|
|
|
poutskinintervals++;
|
|
|
|
pinskinintervals++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pskintype = (void *) pinskinintervals;
|
|
|
|
skin = (byte *) pskintype;
|
|
|
|
|
|
|
|
for (gnum = 0; gnum < groupskins; gnum++) {
|
|
|
|
paliasskingroup->skindescs[gnum].type = ALIAS_SKIN_SINGLE;
|
|
|
|
skin = Mod_LoadSkin (skin, skinsize, snum, gnum, true,
|
|
|
|
&paliasskingroup->skindescs[gnum]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pskintype = (daliasskintype_t *) skin;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pskintype;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
void
|
2001-08-31 12:22:45 +00:00
|
|
|
Mod_LoadAliasModel (model_t *mod, void *buffer, cache_allocator_t allocator)
|
2001-02-19 21:15:25 +00:00
|
|
|
{
|
2004-02-08 02:49:38 +00:00
|
|
|
int size, version, numframes, start, end, total, i, j;
|
|
|
|
int extra = 0; // extra precision bytes
|
|
|
|
void *mem;
|
2001-02-19 21:15:25 +00:00
|
|
|
dtriangle_t *pintriangles;
|
|
|
|
daliasframetype_t *pframetype;
|
|
|
|
daliasskintype_t *pskintype;
|
2002-08-25 14:25:38 +00:00
|
|
|
mdl_t *pinmodel, *pmodel;
|
2001-05-29 17:37:48 +00:00
|
|
|
unsigned short crc;
|
2002-08-25 14:25:38 +00:00
|
|
|
stvert_t *pinstverts;
|
2001-12-30 02:30:26 +00:00
|
|
|
|
2004-02-08 02:49:38 +00:00
|
|
|
if (LittleLong (* (unsigned int *) buffer) == HEADER_MDL16)
|
|
|
|
extra = 1; // extra precision bytes
|
2001-12-30 02:30:26 +00:00
|
|
|
|
2001-05-29 17:37:48 +00:00
|
|
|
CRC_Init (&crc);
|
2003-08-11 06:05:07 +00:00
|
|
|
CRC_ProcessBlock (buffer, &crc, qfs_filesize);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
start = Hunk_LowMark ();
|
|
|
|
|
|
|
|
pinmodel = (mdl_t *) buffer;
|
|
|
|
|
|
|
|
version = LittleLong (pinmodel->version);
|
2004-02-08 02:49:38 +00:00
|
|
|
if (version != ALIAS_VERSION_MDL)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("%s has wrong version number (%i should be %i)",
|
2004-02-08 02:49:38 +00:00
|
|
|
mod->name, version, ALIAS_VERSION_MDL);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// allocate space for a working header, plus all the data except the
|
|
|
|
// frames, skin and group info
|
2001-05-31 18:11:05 +00:00
|
|
|
size = field_offset (aliashdr_t, frames[LittleLong (pinmodel->numframes)]);
|
2001-02-19 21:15:25 +00:00
|
|
|
pheader = Hunk_AllocName (size, loadname);
|
|
|
|
memset (pheader, 0, size);
|
|
|
|
pmodel = &pheader->mdl;
|
|
|
|
pheader->model = (byte *) pmodel - (byte *) pheader;
|
|
|
|
|
2001-05-29 17:37:48 +00:00
|
|
|
pheader->crc = crc;
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
mod->flags = LittleLong (pinmodel->flags);
|
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// endian-adjust and copy the data, starting with the alias model header
|
2002-01-05 03:21:02 +00:00
|
|
|
pmodel->ident = LittleLong (pinmodel->ident);
|
2001-02-19 21:15:25 +00:00
|
|
|
pmodel->boundingradius = LittleFloat (pinmodel->boundingradius);
|
|
|
|
pmodel->numskins = LittleLong (pinmodel->numskins);
|
|
|
|
pmodel->skinwidth = LittleLong (pinmodel->skinwidth);
|
|
|
|
pmodel->skinheight = LittleLong (pinmodel->skinheight);
|
|
|
|
|
|
|
|
if (pmodel->skinheight > MAX_LBM_HEIGHT)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("model %s has a skin taller than %d", mod->name,
|
2004-02-08 02:49:38 +00:00
|
|
|
MAX_LBM_HEIGHT);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
pmodel->numverts = LittleLong (pinmodel->numverts);
|
|
|
|
|
|
|
|
if (pmodel->numverts <= 0)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("model %s has no vertices", mod->name);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-20 00:48:59 +00:00
|
|
|
if (pmodel->numverts > stverts_size) {
|
2002-01-04 07:40:00 +00:00
|
|
|
stverts = realloc (stverts, pmodel->numverts * sizeof (stvert_t));
|
|
|
|
if (!stverts)
|
|
|
|
Sys_Error ("model_alias: out of memory");
|
|
|
|
stverts_size = pmodel->numverts;
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
pmodel->numtris = LittleLong (pinmodel->numtris);
|
|
|
|
|
|
|
|
if (pmodel->numtris <= 0)
|
2001-05-10 06:01:11 +00:00
|
|
|
Sys_Error ("model %s has no triangles", mod->name);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-20 00:48:59 +00:00
|
|
|
if (pmodel->numtris > triangles_size) {
|
2006-07-23 01:34:14 +00:00
|
|
|
triangles = realloc (triangles,
|
|
|
|
pmodel->numtris * sizeof (mtriangle_t));
|
2002-01-04 07:40:00 +00:00
|
|
|
if (!triangles)
|
|
|
|
Sys_Error ("model_alias: out of memory");
|
|
|
|
triangles_size = pmodel->numtris;
|
|
|
|
}
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
pmodel->numframes = LittleLong (pinmodel->numframes);
|
|
|
|
numframes = pmodel->numframes;
|
|
|
|
if (numframes < 1)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Mod_LoadAliasModel: Invalid # of frames: %d", numframes);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
pmodel->size = LittleFloat (pinmodel->size) * ALIAS_BASE_SIZE_RATIO;
|
|
|
|
mod->synctype = LittleLong (pinmodel->synctype);
|
|
|
|
mod->numframes = pmodel->numframes;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
pmodel->scale[i] = LittleFloat (pinmodel->scale[i]);
|
|
|
|
pmodel->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
|
|
|
|
pmodel->eyeposition[i] = LittleFloat (pinmodel->eyeposition[i]);
|
|
|
|
}
|
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// load the skins
|
2001-02-19 21:15:25 +00:00
|
|
|
pskintype = (daliasskintype_t *) &pinmodel[1];
|
2002-08-20 00:48:59 +00:00
|
|
|
pskintype = Mod_LoadAllSkins (pheader->mdl.numskins, pskintype,
|
|
|
|
&pheader->skindesc);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// load base s and t vertices
|
2001-02-19 21:15:25 +00:00
|
|
|
pinstverts = (stvert_t *) pskintype;
|
|
|
|
|
|
|
|
for (i = 0; i < pheader->mdl.numverts; i++) {
|
|
|
|
stverts[i].onseam = LittleLong (pinstverts[i].onseam);
|
|
|
|
stverts[i].s = LittleLong (pinstverts[i].s);
|
|
|
|
stverts[i].t = LittleLong (pinstverts[i].t);
|
|
|
|
}
|
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// load triangle lists
|
2001-02-19 21:15:25 +00:00
|
|
|
pintriangles = (dtriangle_t *) &pinstverts[pheader->mdl.numverts];
|
|
|
|
|
|
|
|
for (i = 0; i < pheader->mdl.numtris; i++) {
|
|
|
|
triangles[i].facesfront = LittleLong (pintriangles[i].facesfront);
|
|
|
|
|
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
triangles[i].vertindex[j] =
|
|
|
|
LittleLong (pintriangles[i].vertindex[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// load the frames
|
2001-02-19 21:15:25 +00:00
|
|
|
posenum = 0;
|
|
|
|
pframetype = (daliasframetype_t *) &pintriangles[pheader->mdl.numtris];
|
2002-06-14 09:33:37 +00:00
|
|
|
aliasbboxmins[0] = aliasbboxmins[1] = aliasbboxmins[2] = 99999;
|
|
|
|
aliasbboxmaxs[0] = aliasbboxmaxs[1] = aliasbboxmaxs[2] = -99999;
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
for (i = 0; i < numframes; i++) {
|
|
|
|
aliasframetype_t frametype;
|
|
|
|
|
|
|
|
frametype = LittleLong (pframetype->type);
|
|
|
|
pheader->frames[i].type = frametype;
|
|
|
|
|
|
|
|
if (frametype == ALIAS_SINGLE) {
|
|
|
|
pframetype = (daliasframetype_t *)
|
2001-10-28 05:43:53 +00:00
|
|
|
Mod_LoadAliasFrame (pframetype + 1, &posenum,
|
2001-12-30 02:30:26 +00:00
|
|
|
&pheader->frames[i], extra);
|
2001-02-19 21:15:25 +00:00
|
|
|
} else {
|
|
|
|
pframetype = (daliasframetype_t *)
|
2001-10-28 05:43:53 +00:00
|
|
|
Mod_LoadAliasGroup (pframetype + 1, &posenum,
|
2001-12-30 02:30:26 +00:00
|
|
|
&pheader->frames[i], extra);
|
2001-02-19 21:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pheader->numposes = posenum;
|
|
|
|
|
|
|
|
mod->type = mod_alias;
|
|
|
|
|
2002-06-14 09:33:37 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
mod->mins[i] = aliasbboxmins[i] * pheader->mdl.scale[i] +
|
|
|
|
pheader->mdl.scale_origin[i];
|
|
|
|
mod->maxs[i] = aliasbboxmaxs[i] * pheader->mdl.scale[i] +
|
|
|
|
pheader->mdl.scale_origin[i];
|
|
|
|
}
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2002-08-20 00:48:59 +00:00
|
|
|
mod->radius = RadiusFromBounds (mod->mins, mod->maxs);
|
|
|
|
|
2001-02-19 21:15:25 +00:00
|
|
|
// build the draw lists
|
2003-02-14 19:46:07 +00:00
|
|
|
Mod_MakeAliasModelDisplayLists (mod, pheader, buffer, qfs_filesize, extra);
|
2001-11-20 08:02:35 +00:00
|
|
|
|
|
|
|
Mod_FinalizeAliasModel (mod, pheader);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
2003-12-28 05:25:06 +00:00
|
|
|
Mod_LoadExternalSkins (mod);
|
|
|
|
|
2001-07-22 05:40:34 +00:00
|
|
|
// move the complete, relocatable alias model to the cache
|
2001-02-19 21:15:25 +00:00
|
|
|
end = Hunk_LowMark ();
|
|
|
|
total = end - start;
|
|
|
|
|
2001-08-31 12:22:45 +00:00
|
|
|
mem = allocator (&mod->cache, total, loadname);
|
2002-01-12 04:46:15 +00:00
|
|
|
if (mem)
|
|
|
|
memcpy (mem, pheader, total);
|
2001-02-19 21:15:25 +00:00
|
|
|
|
|
|
|
Hunk_FreeToLowMark (start);
|
|
|
|
}
|