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.
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
*/
#include "quakedef.h"
+#include "image.h"
+#include "r_shadow.h"
+
+static cvar_t r_mipskins = {CVAR_SAVE, "r_mipskins", "0"};
-/*
-===============
-Mod_AliasInit
-===============
-*/
void Mod_AliasInit (void)
{
+ Cvar_RegisterVariable(&r_mipskins);
}
-int posenum;
-
-float aliasbboxmin[3], aliasbboxmax[3]; // LordHavoc: proper bounding box considerations
-
-#define MAXVERTS 8192
-float vertst[MAXVERTS][2];
-int vertusage[MAXVERTS];
-int vertonseam[MAXVERTS];
-int vertremap[MAXVERTS];
-unsigned short temptris[MAXVERTS][3];
+static void Mod_CalcAliasModelBBoxes (void)
+{
+ int vnum, meshnum;
+ float dist, yawradius, radius;
+ aliasmesh_t *mesh;
+ float *v;
+ VectorClear(loadmodel->normalmins);
+ VectorClear(loadmodel->normalmaxs);
+ yawradius = 0;
+ radius = 0;
+ for (meshnum = 0, mesh = loadmodel->alias.aliasdata_meshes;meshnum < loadmodel->alias.aliasnum_meshes;meshnum++, mesh++)
+ {
+ for (vnum = 0, v = mesh->data_aliasvertex3f;vnum < mesh->num_vertices * mesh->num_frames;vnum++, v += 3)
+ {
+ if (loadmodel->normalmins[0] > v[0]) loadmodel->normalmins[0] = v[0];
+ if (loadmodel->normalmins[1] > v[1]) loadmodel->normalmins[1] = v[1];
+ if (loadmodel->normalmins[2] > v[2]) loadmodel->normalmins[2] = v[2];
+ if (loadmodel->normalmaxs[0] < v[0]) loadmodel->normalmaxs[0] = v[0];
+ if (loadmodel->normalmaxs[1] < v[1]) loadmodel->normalmaxs[1] = v[1];
+ if (loadmodel->normalmaxs[2] < v[2]) loadmodel->normalmaxs[2] = v[2];
+ dist = v[0] * v[0] + v[1] * v[1];
+ if (yawradius < dist)
+ yawradius = dist;
+ dist += v[2] * v[2];
+ if (radius < dist)
+ radius = dist;
+ }
+ }
+ radius = sqrt(radius);
+ yawradius = sqrt(yawradius);
+ loadmodel->yawmins[0] = loadmodel->yawmins[1] = -yawradius;
+ loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = yawradius;
+ loadmodel->yawmins[2] = loadmodel->normalmins[2];
+ loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
+ loadmodel->rotatedmins[0] = loadmodel->rotatedmins[1] = loadmodel->rotatedmins[2] = -radius;
+ loadmodel->rotatedmaxs[0] = loadmodel->rotatedmaxs[1] = loadmodel->rotatedmaxs[2] = radius;
+ loadmodel->radius = radius;
+ loadmodel->radius2 = radius * radius;
+}
-#define NUMVERTEXNORMALS 162
-extern float r_avertexnormals[NUMVERTEXNORMALS][3];
-void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
+static void Mod_ConvertAliasVerts (int inverts, vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int *vertremap)
{
int i, j;
vec3_t temp;
temp[0] = v[i].v[0] * scale[0] + translate[0];
temp[1] = v[i].v[1] * scale[1] + translate[1];
temp[2] = v[i].v[2] * scale[2] + translate[2];
- // update bounding box
- if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
- if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
- if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
- if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
- if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
- if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
j = vertremap[i]; // not onseam
if (j >= 0)
- {
- VectorCopy(v[i].v, out[j].v);
- out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
- out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
- out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
- }
+ VectorCopy(temp, out3f + j * 3);
j = vertremap[i+inverts]; // onseam
if (j >= 0)
- {
- VectorCopy(v[i].v, out[j].v);
- out[j].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
- out[j].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
- out[j].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
- }
+ VectorCopy(temp, out3f + j * 3);
}
}
-/*
-=================
-Mod_LoadAliasFrame
-=================
-*/
-void * Mod_LoadAliasFrame (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
+static void Mod_MDL_LoadFrames (qbyte* datapointer, int inverts, vec3_t scale, vec3_t translate, int *vertremap)
{
- trivertx_t *pinframe;
- daliasframe_t *pdaliasframe;
-
- pdaliasframe = (daliasframe_t *)pin;
-
- strcpy(frame->name, pdaliasframe->name);
- frame->start = posenum;
- frame->length = 1;
- frame->rate = 10.0f; // unnecessary but...
-
- pinframe = (trivertx_t *)(pdaliasframe + 1);
+ int i, f, pose, groupframes;
+ float interval;
+ daliasframetype_t *pframetype;
+ daliasframe_t *pinframe;
+ daliasgroup_t *group;
+ daliasinterval_t *intervals;
+ animscene_t *scene;
+ pose = 0;
+ scene = loadmodel->animscenes;
+ for (f = 0;f < loadmodel->numframes;f++)
+ {
+ pframetype = (daliasframetype_t *)datapointer;
+ datapointer += sizeof(daliasframetype_t);
+ if (LittleLong (pframetype->type) == ALIAS_SINGLE)
+ {
+ // a single frame is still treated as a group
+ interval = 0.1f;
+ groupframes = 1;
+ }
+ else
+ {
+ // read group header
+ group = (daliasgroup_t *)datapointer;
+ datapointer += sizeof(daliasgroup_t);
+ groupframes = LittleLong (group->numframes);
+
+ // intervals (time per frame)
+ intervals = (daliasinterval_t *)datapointer;
+ datapointer += sizeof(daliasinterval_t) * groupframes;
+
+ interval = LittleFloat (intervals->interval); // FIXME: support variable framerate groups
+ if (interval < 0.01f)
+ Host_Error("Mod_MDL_LoadFrames: invalid interval");
+ }
- Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, pinframe, *posevert);
- *posevert += outverts;
- posenum++;
+ // get scene name from first frame
+ pinframe = (daliasframe_t *)datapointer;
- pinframe += inverts;
+ strcpy(scene->name, pinframe->name);
+ scene->firstframe = pose;
+ scene->framecount = groupframes;
+ scene->framerate = 1.0f / interval;
+ scene->loop = true;
+ scene++;
- return (void *)pinframe;
+ // read frames
+ for (i = 0;i < groupframes;i++)
+ {
+ pinframe = (daliasframe_t *)datapointer;
+ datapointer += sizeof(daliasframe_t);
+ Mod_ConvertAliasVerts(inverts, scale, translate, (trivertx_t *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, vertremap);
+ Mod_BuildTextureVectorsAndNormals(loadmodel->alias.aliasdata_meshes->num_vertices, loadmodel->alias.aliasdata_meshes->num_triangles, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_texcoord2f, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->data_aliassvector3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliastvector3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliasnormal3f + pose * loadmodel->alias.aliasdata_meshes->num_vertices * 3);
+ datapointer += sizeof(trivertx_t) * inverts;
+ pose++;
+ }
+ }
}
-
-/*
-=================
-Mod_LoadAliasGroup
-=================
-*/
-void *Mod_LoadAliasGroup (void *pin, maliasframe_t *frame, maliashdr_t *mheader, int inverts, int outverts, trivert2 **posevert)
+aliaslayer_t mod_alias_layersbuffer[16]; // 7 currently used
+void Mod_BuildAliasSkinFromSkinFrame(aliasskin_t *skin, skinframe_t *skinframe)
{
- int i, numframes;
- void *ptemp;
- float interval;
-
- numframes = LittleLong (((daliasgroup_t *)pin)->numframes);
-
- strcpy(frame->name, "group");
- frame->start = posenum;
- frame->length = numframes;
- interval = LittleFloat (((daliasinterval_t *)(((daliasgroup_t *)pin) + 1))->interval); // FIXME: support variable framerate groups?
- if (interval < 0.01f)
- Host_Error("Mod_LoadAliasGroup: invalid interval");
- frame->rate = 1.0f / interval;
-
- ptemp = (void *)(((daliasinterval_t *)(((daliasgroup_t *)pin) + 1)) + numframes);
-
- for (i=0 ; i<numframes ; i++)
+ aliaslayer_t *layer;
+
+ memset(&mod_alias_layersbuffer, 0, sizeof(mod_alias_layersbuffer));
+ layer = mod_alias_layersbuffer;
+ layer->flags = ALIASLAYER_SPECULAR;
+ layer->texture = skinframe->gloss;
+ layer->nmap = skinframe->nmap;
+ layer++;
+ if (skinframe->merged != NULL)
+ {
+ layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_COLORMAPPED;
+ layer->texture = skinframe->merged;
+ layer->nmap = skinframe->nmap;
+ layer++;
+ }
+ if (skinframe->base != NULL)
{
- ((daliasframe_t *)ptemp)++;
- Mod_ConvertAliasVerts(inverts, mheader->scale, mheader->scale_origin, ptemp, *posevert);
- *posevert += outverts;
- posenum++;
- ptemp = (trivertx_t *)ptemp + inverts;
+ layer->flags = ALIASLAYER_DIFFUSE;
+ if (skinframe->merged != NULL)
+ layer->flags |= ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED;
+ layer->texture = skinframe->base;
+ layer->nmap = skinframe->nmap;
+ layer++;
+ }
+ if (skinframe->pants != NULL)
+ {
+ layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_PANTS;
+ layer->texture = skinframe->pants;
+ layer->nmap = skinframe->nmap;
+ layer++;
+ }
+ if (skinframe->shirt != NULL)
+ {
+ layer->flags = ALIASLAYER_DIFFUSE | ALIASLAYER_NODRAW_IF_NOTCOLORMAPPED | ALIASLAYER_COLORMAP_SHIRT;
+ layer->texture = skinframe->shirt;
+ layer->nmap = skinframe->nmap;
+ layer++;
}
- return ptemp;
-}
+ if (skinframe->glow != NULL)
+ {
+ layer->flags = 0;
+ layer->texture = skinframe->glow;
+ layer++;
+ }
-//=========================================================
+ layer->flags = ALIASLAYER_FOG | ALIASLAYER_FORCEDRAW_IF_FIRSTPASS;
+ layer->texture = skinframe->fog;
+ layer++;
-/*
-=================
-Mod_FloodFillSkin
+ skin->flags = 0;
+ // fog texture only exists if some pixels are transparent...
+ if (skinframe->fog != NULL)
+ skin->flags |= ALIASSKIN_TRANSPARENT;
-Fill background pixels so mipmapping doesn't have haloes - Ed
-=================
-*/
-
-typedef struct
-{
- short x, y;
-} floodfill_t;
-
-// must be a power of 2
-#define FLOODFILL_FIFO_SIZE 0x1000
-#define FLOODFILL_FIFO_MASK (FLOODFILL_FIFO_SIZE - 1)
-
-#define FLOODFILL_STEP( off, dx, dy ) \
-{ \
- if (pos[off] == fillcolor) \
- { \
- pos[off] = 255; \
- fifo[inpt].x = x + (dx), fifo[inpt].y = y + (dy); \
- inpt = (inpt + 1) & FLOODFILL_FIFO_MASK; \
- } \
- else if (pos[off] != 255) fdc = pos[off]; \
+ skin->num_layers = layer - mod_alias_layersbuffer;
+ skin->data_layers = Mem_Alloc(loadmodel->mempool, skin->num_layers * sizeof(aliaslayer_t));
+ memcpy(skin->data_layers, mod_alias_layersbuffer, skin->num_layers * sizeof(aliaslayer_t));
}
-void Mod_FloodFillSkin( byte *skin, int skinwidth, int skinheight )
+#define BOUNDI(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%d exceeds %d - %d)\n", loadmodel->name, VALUE, MIN, MAX);
+#define BOUNDF(VALUE,MIN,MAX) if (VALUE < MIN || VALUE >= MAX) Host_Error("model %s has an invalid ##VALUE (%f exceeds %f - %f)\n", loadmodel->name, VALUE, MIN, MAX);
+extern void R_Model_Alias_Draw(entity_render_t *ent);
+extern void R_Model_Alias_DrawFakeShadow(entity_render_t *ent);
+extern void R_Model_Alias_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
+extern void R_Model_Alias_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
+void Mod_IDP0_Load(model_t *mod, void *buffer)
{
- byte fillcolor = *skin; // assume this is the pixel to fill
- floodfill_t fifo[FLOODFILL_FIFO_SIZE];
- int inpt = 0, outpt = 0;
- int filledcolor = -1;
- int i;
-
- if (filledcolor == -1)
- {
- filledcolor = 0;
- // attempt to find opaque black
- for (i = 0; i < 256; ++i)
- if (d_8to24table[i] == (255 << 0)) // alpha 1.0
- {
- filledcolor = i;
- break;
- }
- }
-
- // can't fill to filled color or to transparent color (used as visited marker)
- if ((fillcolor == filledcolor) || (fillcolor == 255))
- {
- //printf( "not filling skin from %d to %d\n", fillcolor, filledcolor );
- return;
- }
+ int i, j, version, totalskins, skinwidth, skinheight, groupframes, groupskins, numverts;
+ float scales, scalet, scale[3], translate[3], interval;
+ mdl_t *pinmodel;
+ stvert_t *pinstverts;
+ dtriangle_t *pintriangles;
+ daliasskintype_t *pinskintype;
+ daliasskingroup_t *pinskingroup;
+ daliasskininterval_t *pinskinintervals;
+ daliasframetype_t *pinframetype;
+ daliasgroup_t *pinframegroup;
+ qbyte *datapointer, *startframes, *startskins;
+ char name[MAX_QPATH];
+ skinframe_t tempskinframe;
+ animscene_t *tempskinscenes;
+ skinframe_t *tempskinframes;
+ float *vertst;
+ int *vertonseam, *vertremap;
+
+ datapointer = buffer;
+ pinmodel = (mdl_t *)datapointer;
+ datapointer += sizeof(mdl_t);
- fifo[inpt].x = 0, fifo[inpt].y = 0;
- inpt = (inpt + 1) & FLOODFILL_FIFO_MASK;
+ version = LittleLong (pinmodel->version);
+ if (version != ALIAS_VERSION)
+ Host_Error ("%s has wrong version number (%i should be %i)",
+ loadmodel->name, version, ALIAS_VERSION);
- while (outpt != inpt)
- {
- int x = fifo[outpt].x, y = fifo[outpt].y;
- int fdc = filledcolor;
- byte *pos = &skin[x + skinwidth * y];
+ loadmodel->type = mod_alias;
+ loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
+ loadmodel->DrawSky = NULL;
+ loadmodel->Draw = R_Model_Alias_Draw;
+ loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
+ loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
+ loadmodel->DrawLight = R_Model_Alias_DrawLight;
- outpt = (outpt + 1) & FLOODFILL_FIFO_MASK;
+ loadmodel->alias.aliasnum_meshes = 1;
+ loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
- if (x > 0) FLOODFILL_STEP( -1, -1, 0 );
- if (x < skinwidth - 1) FLOODFILL_STEP( 1, 1, 0 );
- if (y > 0) FLOODFILL_STEP( -skinwidth, 0, -1 );
- if (y < skinheight - 1) FLOODFILL_STEP( skinwidth, 0, 1 );
- skin[x + skinwidth * y] = fdc;
+ loadmodel->numskins = LittleLong(pinmodel->numskins);
+ BOUNDI(loadmodel->numskins,0,256);
+ skinwidth = LittleLong (pinmodel->skinwidth);
+ BOUNDI(skinwidth,0,4096);
+ skinheight = LittleLong (pinmodel->skinheight);
+ BOUNDI(skinheight,0,4096);
+ numverts = LittleLong(pinmodel->numverts);
+ BOUNDI(numverts,0,65536);
+ loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->numtris);
+ BOUNDI(loadmodel->alias.aliasdata_meshes->num_triangles,0,65536 / 3); // max elements limit, rather than max triangles limit
+ loadmodel->numframes = LittleLong(pinmodel->numframes);
+ BOUNDI(loadmodel->numframes,0,65536);
+ loadmodel->synctype = LittleLong (pinmodel->synctype);
+ BOUNDI(loadmodel->synctype,0,2);
+ loadmodel->flags = LittleLong (pinmodel->flags);
+
+ for (i = 0;i < 3;i++)
+ {
+ scale[i] = LittleFloat (pinmodel->scale[i]);
+ translate[i] = LittleFloat (pinmodel->scale_origin[i]);
}
-}
-int GL_SkinSplitShirt(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
-{
- int i, pixels, passed;
- byte pixeltest[16];
- for (i = 0;i < 16;i++)
- pixeltest[i] = (bits & (1 << i)) != 0;
- pixels = width*height;
- passed = 0;
- while(pixels--)
- {
- if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
+ startskins = datapointer;
+ totalskins = 0;
+ for (i = 0;i < loadmodel->numskins;i++)
+ {
+ pinskintype = (daliasskintype_t *)datapointer;
+ datapointer += sizeof(daliasskintype_t);
+ if (LittleLong(pinskintype->type) == ALIAS_SKIN_SINGLE)
+ groupskins = 1;
+ else
{
- passed++;
- // turn to white while copying
- if (*in >= 128 && *in < 224) // backwards ranges
- *out = (*in & 15) ^ 15;
- else
- *out = *in & 15;
+ pinskingroup = (daliasskingroup_t *)datapointer;
+ datapointer += sizeof(daliasskingroup_t);
+ groupskins = LittleLong(pinskingroup->numskins);
+ datapointer += sizeof(daliasskininterval_t) * groupskins;
}
- else
- *out = 0;
- in++;
- out++;
- }
- if (passed)
- return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
- else
- return 0;
-}
-int GL_SkinSplit(byte *in, byte *out, int width, int height, unsigned short bits, char *name)
-{
- int i, pixels, passed;
- byte pixeltest[16];
- for (i = 0;i < 16;i++)
- pixeltest[i] = (bits & (1 << i)) != 0;
- pixels = width*height;
- passed = 0;
- while(pixels--)
- {
- if (pixeltest[*in >> 4] && *in != 0 && *in != 255)
+ for (j = 0;j < groupskins;j++)
{
- passed++;
- *out = *in;
+ datapointer += skinwidth * skinheight;
+ totalskins++;
}
- else
- *out = 0;
- in++;
- out++;
}
- if (passed)
- return GL_LoadTexture (name, width, height, out - width*height, true, false, 1);
- else
- return 0;
-}
-/*
-===============
-Mod_LoadAllSkins
-===============
-*/
-void *Mod_LoadAllSkins (int numskins, daliasskintype_t *pskintype, int width, int height)
-{
- int i, j;
- char name[32];
- int s;
- byte *skin;
- daliasskingroup_t *pinskingroup;
- int groupskins;
- daliasskininterval_t *pinskinintervals;
- int skinranges, skincount, *skintexnum, *skinrange, skinnum;
- void *temp;
- byte *skintemp = NULL;
-
- skin = (byte *)(pskintype + 1);
-
- if (numskins < 1 || numskins > MAX_SKINS)
- Host_Error ("Mod_LoadAliasModel: Invalid # of skins: %d\n", numskins);
-
- s = width * height;
- skintemp = malloc(s);
-
- // LordHavoc: skim the data, measure the number of skins and number of groups
- skinranges = numskins;
- skincount = 0;
- temp = pskintype;
- for (i = 0;i < numskins;i++)
- {
- pskintype++;
- if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
+ pinstverts = (stvert_t *)datapointer;
+ datapointer += sizeof(stvert_t) * numverts;
+
+ pintriangles = (dtriangle_t *)datapointer;
+ datapointer += sizeof(dtriangle_t) * loadmodel->alias.aliasdata_meshes->num_triangles;
+
+ startframes = datapointer;
+ loadmodel->alias.aliasdata_meshes->num_frames = 0;
+ for (i = 0;i < loadmodel->numframes;i++)
+ {
+ pinframetype = (daliasframetype_t *)datapointer;
+ datapointer += sizeof(daliasframetype_t);
+ if (LittleLong (pinframetype->type) == ALIAS_SINGLE)
+ groupframes = 1;
+ else
{
- skincount++;
- (byte *)pskintype += s;
+ pinframegroup = (daliasgroup_t *)datapointer;
+ datapointer += sizeof(daliasgroup_t);
+ groupframes = LittleLong(pinframegroup->numframes);
+ datapointer += sizeof(daliasinterval_t) * groupframes;
}
- else
+
+ for (j = 0;j < groupframes;j++)
{
- groupskins = LittleLong (((daliasskingroup_t *)pskintype)->numskins);
- skincount += groupskins;
- (byte *)pskintype += (s + sizeof(daliasskininterval_t)) * groupskins + sizeof(daliasskingroup_t);
+ datapointer += sizeof(daliasframe_t);
+ datapointer += sizeof(trivertx_t) * numverts;
+ loadmodel->alias.aliasdata_meshes->num_frames++;
}
}
- pskintype = temp;
- skinrange = loadmodel->skinanimrange;
- skintexnum = loadmodel->skinanim;
-// skinrange = Hunk_AllocName (sizeof(int) * (skinranges + skincount), loadname);
-// skintexnum = skinrange + skinranges * 2;
-// loadmodel->skinanimrange = (int) skinrange - (int) pheader;
-// loadmodel->skinanim = (int) skintexnum - (int) pheader;
- skinnum = 0;
- for (i = 0;i < numskins;i++)
+ // load the skins
+ loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
+ loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, totalskins * sizeof(skinframe_t));
+ totalskins = 0;
+ datapointer = startskins;
+ for (i = 0;i < loadmodel->numskins;i++)
{
- *skinrange++ = skinnum; // start of the range
- pskintype++;
- if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
+ pinskintype = (daliasskintype_t *)datapointer;
+ datapointer += sizeof(daliasskintype_t);
+
+ if (pinskintype->type == ALIAS_SKIN_SINGLE)
{
- *skinrange++ = 1; // single skin
- skinnum++;
- sprintf (name, "%s_%i", loadmodel->name, i);
-
- Mod_FloodFillSkin( skin, width, height );
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("%s_normal", name)); // normal (no special colors)
- *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("%s_pants", name)); // pants
- *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("%s_shirt", name)); // shirt
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", name)); // glow
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", name)); // body (normal + pants + shirt, but not glow)
- pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
+ groupskins = 1;
+ interval = 0.1f;
}
else
{
- // animating skin group. yuck.
- pinskingroup = (daliasskingroup_t *)pskintype;
+ pinskingroup = (daliasskingroup_t *)datapointer;
+ datapointer += sizeof(daliasskingroup_t);
+
groupskins = LittleLong (pinskingroup->numskins);
- pinskinintervals = (daliasskininterval_t *)(pinskingroup + 1);
- pskintype = (void *)(pinskinintervals + groupskins);
+ pinskinintervals = (daliasskininterval_t *)datapointer;
+ datapointer += sizeof(daliasskininterval_t) * groupskins;
- *skinrange++ = groupskins; // number of skins
- skinnum += groupskins;
- for (j = 0;j < groupskins;j++)
- {
- sprintf (name, "%s_%i_%i", loadmodel->name, i,j);
-
- Mod_FloodFillSkin( skin, width, height );
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FBD, va("%s_normal", name)); // normal (no special colors)
- *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0040, va("%s_pants", name)); // pants
- *skintexnum++ = GL_SkinSplitShirt((byte *)pskintype, skintemp, width, height, 0x0002, va("%s_shirt", name)); // shirt
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0xC000, va("%s_glow", name)); // glow
- *skintexnum++ = GL_SkinSplit((byte *)pskintype, skintemp, width, height, 0x3FFF, va("%s_body", name)); // body (normal + pants + shirt, but not glow)
- pskintype = (daliasskintype_t *)((byte *)(pskintype) + s);
- }
+ interval = LittleFloat(pinskinintervals[0].interval);
+ if (interval < 0.01f)
+ Host_Error("Mod_IDP0_Load: invalid interval\n");
}
- }
- loadmodel->numskins = numskins;
- free(skintemp);
- return (void *)pskintype;
-}
+ sprintf(loadmodel->skinscenes[i].name, "skin %i", i);
+ loadmodel->skinscenes[i].firstframe = totalskins;
+ loadmodel->skinscenes[i].framecount = groupskins;
+ loadmodel->skinscenes[i].framerate = 1.0f / interval;
+ loadmodel->skinscenes[i].loop = true;
-void *Mod_SkipAllSkins (int numskins, daliasskintype_t *pskintype, int skinsize)
-{
- int i;
- for (i = 0;i < numskins;i++)
- {
- pskintype++;
- if (pskintype[-1].type == ALIAS_SKIN_SINGLE)
- (byte *)pskintype += skinsize;
- else
- (byte *)pskintype += (skinsize + sizeof(daliasskininterval_t)) * LittleLong (((daliasskingroup_t *)pskintype)->numskins) + sizeof(daliasskingroup_t);
+ for (j = 0;j < groupskins;j++)
+ {
+ if (groupskins > 1)
+ sprintf (name, "%s_%i_%i", loadmodel->name, i, j);
+ else
+ sprintf (name, "%s_%i", loadmodel->name, i);
+ if (!Mod_LoadSkinFrame(loadmodel->skinframes + totalskins, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
+ Mod_LoadSkinFrame_Internal(loadmodel->skinframes + totalskins, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true, (qbyte *)datapointer, skinwidth, skinheight);
+ datapointer += skinwidth * skinheight;
+ totalskins++;
+ }
}
- return pskintype;
-}
-
-//=========================================================================
-
-//void GL_MakeAliasModelDisplayLists (model_t *m, aliashdr_t *hdr);
-
-/*
-=================
-Mod_LoadAliasModel
-=================
-*/
-#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);
-#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);
-void Mod_LoadAliasModel (model_t *mod, void *buffer)
-{
- int i, j, version, numframes, start, end, total, numverts, numtris, numposes, numskins, skinwidth, skinheight, f, totalverts;
- mdl_t *pinmodel;
- stvert_t *pinstverts;
- dtriangle_t *pintriangles;
- daliasframetype_t *pframetype;
- daliasskintype_t *pskintype;
- float *pouttexcoords, scales, scalet;
- maliashdr_t *mheader;
- unsigned short *pouttris;
- maliasframe_t *frame;
- trivert2 *posevert;
-
- start = Hunk_LowMark ();
-
- pinmodel = (mdl_t *)buffer;
-
- version = LittleLong (pinmodel->version);
- if (version != ALIAS_VERSION)
- Host_Error ("%s has wrong version number (%i should be %i)",
- mod->name, version, ALIAS_VERSION);
-
- mod->type = ALIASTYPE_MDL;
-
- numframes = LittleLong(pinmodel->numframes);
- BOUNDI(numframes,0,65536);
- numverts = LittleLong(pinmodel->numverts);
- BOUNDI(numverts,0,MAXALIASVERTS);
- numtris = LittleLong(pinmodel->numtris);
- BOUNDI(numtris,0,65536);
- numskins = LittleLong(pinmodel->numskins);
- BOUNDI(numskins,0,256);
- skinwidth = LittleLong (pinmodel->skinwidth);
- BOUNDI(skinwidth,0,4096);
- skinheight = LittleLong (pinmodel->skinheight);
- BOUNDI(skinheight,0,1024);
-
- pskintype = (daliasskintype_t *)&pinmodel[1];
- pinstverts = (stvert_t *)Mod_SkipAllSkins (numskins, pskintype, skinwidth * skinheight);
- pintriangles = (dtriangle_t *)&pinstverts[numverts];
- pframetype = (daliasframetype_t *)&pintriangles[numtris];
-
- numposes = 0;
- for (i=0 ; i<numframes ; i++)
+ // check for skins that don't exist in the model, but do exist as external images
+ // (this was added because yummyluv kept pestering me about support for it)
+ for (;;)
{
- if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
+ sprintf (name, "%s_%i", loadmodel->name, loadmodel->numskins);
+ if (Mod_LoadSkinFrame(&tempskinframe, name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_CLAMP | TEXF_ALPHA, true, false, true))
{
- numposes++;
- pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + (sizeof(daliasframe_t) + sizeof(trivertx_t) * numverts) );
+ // expand the arrays to make room
+ tempskinscenes = loadmodel->skinscenes;
+ tempskinframes = loadmodel->skinframes;
+ loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, (loadmodel->numskins + 1) * sizeof(animscene_t));
+ loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, (totalskins + 1) * sizeof(skinframe_t));
+ memcpy(loadmodel->skinscenes, tempskinscenes, loadmodel->numskins * sizeof(animscene_t));
+ memcpy(loadmodel->skinframes, tempskinframes, totalskins * sizeof(skinframe_t));
+ Mem_Free(tempskinscenes);
+ Mem_Free(tempskinframes);
+ // store the info about the new skin
+ strcpy(loadmodel->skinscenes[loadmodel->numskins].name, name);
+ loadmodel->skinscenes[loadmodel->numskins].firstframe = totalskins;
+ loadmodel->skinscenes[loadmodel->numskins].framecount = 1;
+ loadmodel->skinscenes[loadmodel->numskins].framerate = 10.0f;
+ loadmodel->skinscenes[loadmodel->numskins].loop = true;
+ loadmodel->skinframes[totalskins] = tempskinframe;
+ loadmodel->numskins++;
+ totalskins++;
}
else
- {
- f = LittleLong (((daliasgroup_t *)((int)pframetype + sizeof(daliasframetype_t)))->numframes);
- numposes += f;
- pframetype = (daliasframetype_t *)((int)pframetype + sizeof(daliasframetype_t) + sizeof(daliasgroup_t) + (sizeof(daliasframe_t) + sizeof(daliasinterval_t) + sizeof(trivertx_t) * numverts) * f);
- }
- }
-
- // rebuild the model
- mheader = Hunk_AllocName (sizeof(maliashdr_t), loadname);
- mod->flags = LittleLong (pinmodel->flags);
- mod->type = mod_alias;
-// endian-adjust and copy the data, starting with the alias model header
- mheader->numverts = numverts;
- mod->numtris = mheader->numtris = numtris;
- mod->numframes = mheader->numframes = numframes;
- mod->synctype = LittleLong (pinmodel->synctype);
- BOUNDI(mod->synctype,0,2);
-
- for (i=0 ; i<3 ; i++)
- {
- mheader->scale[i] = LittleFloat (pinmodel->scale[i]);
- BOUNDF(mheader->scale[i],0,65536);
- mheader->scale_origin[i] = LittleFloat (pinmodel->scale_origin[i]);
- BOUNDF(mheader->scale_origin[i],-65536,65536);
+ break;
}
- // load the skins
- pskintype = (daliasskintype_t *)&pinmodel[1];
- pskintype = Mod_LoadAllSkins(numskins, pskintype, skinwidth, skinheight);
+ loadmodel->alias.aliasdata_meshes->num_skins = 0;
+ for (i = 0;i < loadmodel->numskins;i++)
+ loadmodel->alias.aliasdata_meshes->num_skins += loadmodel->skinscenes[i].framecount;
+ loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
- // store texture coordinates into temporary array, they will be stored after usage is determined (triangle data)
- pinstverts = (stvert_t *)pskintype;
+ // store texture coordinates into temporary array, they will be stored
+ // after usage is determined (triangle data)
+ vertst = Mem_Alloc(tempmempool, numverts * 2 * sizeof(float[2]));
+ vertremap = Mem_Alloc(tempmempool, numverts * 3 * sizeof(int));
+ vertonseam = vertremap + numverts * 2;
- // LordHavoc: byteswap and convert stvert data
scales = 1.0 / skinwidth;
scalet = 1.0 / skinheight;
for (i = 0;i < numverts;i++)
{
vertonseam[i] = LittleLong(pinstverts[i].onseam);
- vertst[i][0] = LittleLong(pinstverts[i].s) * scales;
- vertst[i][1] = LittleLong(pinstverts[i].t) * scalet;
- vertst[i+numverts][0] = vertst[i][0] + 0.5;
- vertst[i+numverts][1] = vertst[i][1];
- vertusage[i] = 0;
- vertusage[i+numverts] = 0;
+ vertst[i*2+0] = (LittleLong(pinstverts[i].s) + 0.5) * scales;
+ vertst[i*2+1] = (LittleLong(pinstverts[i].t) + 0.5) * scalet;
+ vertst[(i+numverts)*2+0] = vertst[i*2+0] + 0.5;
+ vertst[(i+numverts)*2+1] = vertst[i*2+1];
}
// load triangle data
- pouttris = Hunk_AllocName(sizeof(unsigned short[3]) * numtris, loadname);
- mheader->tridata = (int) pouttris - (int) mheader;
- pintriangles = (dtriangle_t *)&pinstverts[mheader->numverts];
+ loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, sizeof(int[3]) * loadmodel->alias.aliasdata_meshes->num_triangles);
- // count the vertices used
- for (i = 0;i < numverts*2;i++)
- vertusage[i] = 0;
- for (i = 0;i < numtris;i++)
- {
- temptris[i][0] = LittleLong(pintriangles[i].vertindex[0]);
- temptris[i][1] = LittleLong(pintriangles[i].vertindex[1]);
- temptris[i][2] = LittleLong(pintriangles[i].vertindex[2]);
+ // read the triangle elements
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
+ for (j = 0;j < 3;j++)
+ loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = LittleLong(pintriangles[i].vertindex[j]);
+ // validate (note numverts is used because this is the original data)
+ Mod_ValidateElements(loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles, numverts, __FILE__, __LINE__);
+ // now butcher the elements according to vertonseam and tri->facesfront
+ // and then compact the vertex set to remove duplicates
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
if (!LittleLong(pintriangles[i].facesfront)) // backface
- {
- if (vertonseam[temptris[i][0]]) temptris[i][0] += numverts;
- if (vertonseam[temptris[i][1]]) temptris[i][1] += numverts;
- if (vertonseam[temptris[i][2]]) temptris[i][2] += numverts;
- }
- vertusage[temptris[i][0]]++;
- vertusage[temptris[i][1]]++;
- vertusage[temptris[i][2]]++;
- }
+ for (j = 0;j < 3;j++)
+ if (vertonseam[loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j]])
+ loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] += numverts;
+ // count the usage
+ // (this uses vertremap to count usage to save some memory)
+ for (i = 0;i < numverts*2;i++)
+ vertremap[i] = 0;
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles*3;i++)
+ vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]]++;
// build remapping table and compact array
- totalverts = 0;
+ loadmodel->alias.aliasdata_meshes->num_vertices = 0;
for (i = 0;i < numverts*2;i++)
{
- if (vertusage[i])
+ if (vertremap[i])
{
- vertremap[i] = totalverts;
- vertst[totalverts][0] = vertst[i][0];
- vertst[totalverts][1] = vertst[i][1];
- totalverts++;
+ vertremap[i] = loadmodel->alias.aliasdata_meshes->num_vertices;
+ vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+0] = vertst[i*2+0];
+ vertst[loadmodel->alias.aliasdata_meshes->num_vertices*2+1] = vertst[i*2+1];
+ loadmodel->alias.aliasdata_meshes->num_vertices++;
}
else
vertremap[i] = -1; // not used at all
}
- mheader->numverts = totalverts;
- // remap the triangle references
- for (i = 0;i < numtris;i++)
- {
- *pouttris++ = vertremap[temptris[i][0]];
- *pouttris++ = vertremap[temptris[i][1]];
- *pouttris++ = vertremap[temptris[i][2]];
- }
+ // remap the elements to the new vertex set
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles * 3;i++)
+ loadmodel->alias.aliasdata_meshes->data_element3i[i] = vertremap[loadmodel->alias.aliasdata_meshes->data_element3i[i]];
// store the texture coordinates
- pouttexcoords = Hunk_AllocName(sizeof(float[2]) * totalverts, loadname);
- mheader->texdata = (int) pouttexcoords - (int) mheader;
- for (i = 0;i < totalverts;i++)
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, sizeof(float[2]) * loadmodel->alias.aliasdata_meshes->num_vertices);
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_vertices;i++)
{
- *pouttexcoords++ = vertst[i][0];
- *pouttexcoords++ = vertst[i][1];
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = vertst[i*2+0];
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = vertst[i*2+1];
}
// load the frames
- posenum = 0;
- frame = Hunk_AllocName(sizeof(maliasframe_t) * numframes, loadname);
- mheader->framedata = (int) frame - (int) mheader;
- posevert = Hunk_AllocName(sizeof(trivert2) * numposes * totalverts, loadname);
- mheader->posedata = (int) posevert - (int) mheader;
- pframetype = (daliasframetype_t *)&pintriangles[numtris];
+ loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
+ loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, sizeof(float[4][3]) * loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices);
+ loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 1;
+ loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 2;
+ loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + loadmodel->alias.aliasdata_meshes->num_frames * loadmodel->alias.aliasdata_meshes->num_vertices * 3 * 3;
+ Mod_MDL_LoadFrames (startframes, numverts, scale, translate, vertremap);
+ loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
+ Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_skins;i++)
+ Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, loadmodel->skinframes + i);
+ Mod_CalcAliasModelBBoxes();
+
+ Mem_Free(vertst);
+ Mem_Free(vertremap);
+}
- // LordHavoc: doing proper bbox for model
- aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
- aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
+static void Mod_MD2_ConvertVerts (vec3_t scale, vec3_t translate, trivertx_t *v, float *out3f, int numverts, int *vertremap)
+{
+ int i;
+ trivertx_t *in;
+ for (i = 0;i < numverts;i++, out3f += 3)
+ {
+ in = v + vertremap[i];
+ out3f[0] = in->v[0] * scale[0] + translate[0];
+ out3f[1] = in->v[1] * scale[1] + translate[1];
+ out3f[2] = in->v[2] * scale[2] + translate[2];
+ }
+}
- for (i=0 ; i<numframes ; i++)
+void Mod_IDP2_Load(model_t *mod, void *buffer)
+{
+ int i, j, k, hashindex, num, numxyz, numst, xyz, st, skinwidth, skinheight, *vertremap, version, end, numverts;
+ float *stverts, s, t, scale[3], translate[3];
+ md2_t *pinmodel;
+ qbyte *base, *datapointer;
+ md2frame_t *pinframe;
+ char *inskin;
+ md2triangle_t *intri;
+ unsigned short *inst;
+ struct md2verthash_s
{
- if ((aliasframetype_t) LittleLong (pframetype->type) == ALIAS_SINGLE)
- pframetype = (daliasframetype_t *) Mod_LoadAliasFrame (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
- else
- pframetype = (daliasframetype_t *) Mod_LoadAliasGroup (pframetype + 1, frame++, mheader, numverts, totalverts, &posevert);
+ struct md2verthash_s *next;
+ int xyz;
+ float st[2];
+ }
+ *hash, **md2verthash, *md2verthashdata;
+
+ pinmodel = buffer;
+ base = buffer;
+
+ version = LittleLong (pinmodel->version);
+ if (version != MD2ALIAS_VERSION)
+ Host_Error ("%s has wrong version number (%i should be %i)",
+ loadmodel->name, version, MD2ALIAS_VERSION);
+
+ loadmodel->type = mod_alias;
+ loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
+ loadmodel->DrawSky = NULL;
+ loadmodel->Draw = R_Model_Alias_Draw;
+ loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
+ loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
+ loadmodel->DrawLight = R_Model_Alias_DrawLight;
+
+ if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > (65536 / 3))
+ Host_Error ("%s has invalid number of triangles: %i", loadmodel->name, LittleLong(pinmodel->num_tris));
+ if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > 65536)
+ Host_Error ("%s has invalid number of vertices: %i", loadmodel->name, LittleLong(pinmodel->num_xyz));
+ if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > 65536)
+ Host_Error ("%s has invalid number of frames: %i", loadmodel->name, LittleLong(pinmodel->num_frames));
+ if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > 256)
+ Host_Error ("%s has invalid number of skins: %i", loadmodel->name, LittleLong(pinmodel->num_skins));
+
+ end = LittleLong(pinmodel->ofs_end);
+ if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= end))
+ Host_Error ("%s is not a valid model", loadmodel->name);
+ if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= end)
+ Host_Error ("%s is not a valid model", loadmodel->name);
+ if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= end)
+ Host_Error ("%s is not a valid model", loadmodel->name);
+ if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= end)
+ Host_Error ("%s is not a valid model", loadmodel->name);
+ if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= end)
+ Host_Error ("%s is not a valid model", loadmodel->name);
+
+ loadmodel->alias.aliasnum_meshes = 1;
+ loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, sizeof(aliasmesh_t));
+
+ loadmodel->numskins = LittleLong(pinmodel->num_skins);
+ numxyz = LittleLong(pinmodel->num_xyz);
+ numst = LittleLong(pinmodel->num_st);
+ loadmodel->alias.aliasdata_meshes->num_triangles = LittleLong(pinmodel->num_tris);
+ loadmodel->numframes = LittleLong(pinmodel->num_frames);
+ loadmodel->alias.aliasdata_meshes->num_frames = loadmodel->numframes;
+ loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
+
+ loadmodel->flags = 0; // there are no MD2 flags
+ loadmodel->synctype = ST_RAND;
+
+ // load the skins
+ inskin = (void*)(base + LittleLong(pinmodel->ofs_skins));
+ if (loadmodel->numskins)
+ {
+ // skins found (most likely not a player model)
+ loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
+ for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME)
+ Mod_LoadSkinFrame(loadmodel->skinframes + i, inskin, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true);
+ }
+ else
+ {
+ // no skins (most likely a player model)
+ loadmodel->numskins = 1;
+ loadmodel->skinframes = Mem_Alloc(loadmodel->mempool, sizeof(skinframe_t) * loadmodel->numskins);
}
+ loadmodel->alias.aliasdata_meshes->num_skins = loadmodel->numskins;
+ loadmodel->alias.aliasdata_meshes->data_skins = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_skins * sizeof(aliasskin_t));
- // LordHavoc: fixed model bbox - was //FIXME: do this right
- //mod->mins[0] = mod->mins[1] = mod->mins[2] = -16;
- //mod->maxs[0] = mod->maxs[1] = mod->maxs[2] = 16;
- for (j = 0;j < 3;j++)
+ loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numskins);
+ for (i = 0;i < loadmodel->numskins;i++)
{
- mod->mins[j] = aliasbboxmin[j];
- mod->maxs[j] = aliasbboxmax[j];
+ loadmodel->skinscenes[i].firstframe = i;
+ loadmodel->skinscenes[i].framecount = 1;
+ loadmodel->skinscenes[i].loop = true;
+ loadmodel->skinscenes[i].framerate = 10;
}
-// move the complete, relocatable alias model to the cache
- end = Hunk_LowMark ();
- total = end - start;
-
- Cache_Alloc (&mod->cache, total, loadname);
- if (!mod->cache.data)
- return;
- memcpy (mod->cache.data, mheader, total);
- Hunk_FreeToLowMark (start);
-}
+ // load the triangles and stvert data
+ inst = (void*)(base + LittleLong(pinmodel->ofs_st));
+ intri = (void*)(base + LittleLong(pinmodel->ofs_tris));
+ skinwidth = LittleLong(pinmodel->skinwidth);
+ skinheight = LittleLong(pinmodel->skinheight);
-void Mod_ConvertQ2AliasVerts (int numverts, vec3_t scale, vec3_t translate, trivertx_t *v, trivert2 *out)
-{
- int i;
- vec3_t temp;
- for (i = 0;i < numverts;i++)
+ stverts = Mem_Alloc(tempmempool, numst * sizeof(float[2]));
+ s = 1.0f / skinwidth;
+ t = 1.0f / skinheight;
+ for (i = 0;i < numst;i++)
{
- VectorCopy(v[i].v, out[i].v);
- temp[0] = v[i].v[0] * scale[0] + translate[0];
- temp[1] = v[i].v[1] * scale[1] + translate[1];
- temp[2] = v[i].v[2] * scale[2] + translate[2];
- // update bounding box
- if (temp[0] < aliasbboxmin[0]) aliasbboxmin[0] = temp[0];
- if (temp[1] < aliasbboxmin[1]) aliasbboxmin[1] = temp[1];
- if (temp[2] < aliasbboxmin[2]) aliasbboxmin[2] = temp[2];
- if (temp[0] > aliasbboxmax[0]) aliasbboxmax[0] = temp[0];
- if (temp[1] > aliasbboxmax[1]) aliasbboxmax[1] = temp[1];
- if (temp[2] > aliasbboxmax[2]) aliasbboxmax[2] = temp[2];
- out[i].n[0] = (signed char) (r_avertexnormals[v[i].lightnormalindex][0] * 127.0);
- out[i].n[1] = (signed char) (r_avertexnormals[v[i].lightnormalindex][1] * 127.0);
- out[i].n[2] = (signed char) (r_avertexnormals[v[i].lightnormalindex][2] * 127.0);
- }
- /*
- int i, j;
- vec3_t t1, t2;
- struct
- {
- vec3_t v;
- vec3_t normal;
- int count;
- } tempvert[MD2MAX_VERTS];
- temptris_t *tris;
- // decompress vertices
- for (i = 0;i < numverts;i++)
+ j = (unsigned short) LittleShort(inst[i*2+0]);
+ k = (unsigned short) LittleShort(inst[i*2+1]);
+ if (j >= skinwidth || k >= skinheight)
+ {
+ Mem_Free(stverts);
+ Host_Error("Mod_MD2_LoadGeometry: invalid skin coordinate (%i %i) on vert %i of model %s\n", j, k, i, loadmodel->name);
+ }
+ stverts[i*2+0] = j * s;
+ stverts[i*2+1] = k * t;
+ }
+
+ md2verthash = Mem_Alloc(tempmempool, 256 * sizeof(hash));
+ md2verthashdata = Mem_Alloc(tempmempool, loadmodel->alias.aliasdata_meshes->num_triangles * 3 * sizeof(*hash));
+ // swap the triangle list
+ num = 0;
+ loadmodel->alias.aliasdata_meshes->data_element3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_triangles;i++)
{
- VectorCopy(v[i].v, out[i].v);
- tempvert[i].v[0] = v[i].v[0] * scale[0] + translate[0];
- tempvert[i].v[1] = v[i].v[1] * scale[1] + translate[1];
- tempvert[i].v[2] = v[i].v[2] * scale[2] + translate[2];
- tempvert[i].normal[0] = tempvert[i].normal[1] = tempvert[i].normal[2] = 0;
- tempvert[i].count = 0;
- // update bounding box
- if (tempvert[i].v[0] < aliasbboxmin[0]) aliasbboxmin[0] = tempvert[i].v[0];
- if (tempvert[i].v[1] < aliasbboxmin[1]) aliasbboxmin[1] = tempvert[i].v[1];
- if (tempvert[i].v[2] < aliasbboxmin[2]) aliasbboxmin[2] = tempvert[i].v[2];
- if (tempvert[i].v[0] > aliasbboxmax[0]) aliasbboxmax[0] = tempvert[i].v[0];
- if (tempvert[i].v[1] > aliasbboxmax[1]) aliasbboxmax[1] = tempvert[i].v[1];
- if (tempvert[i].v[2] > aliasbboxmax[2]) aliasbboxmax[2] = tempvert[i].v[2];
- }
- // calculate surface normals
- tris = temptris;
- for (i = 0;i < numtris;i++)
- {
- VectorSubtract(tempvert[tris->v[0]].v, tempvert[tris->v[1]].v, t1);
- VectorSubtract(tempvert[tris->v[2]].v, tempvert[tris->v[1]].v, t2);
- CrossProduct(t1, t2, tris->normal);
- VectorNormalize(tris->normal);
- // add surface normal to vertices
for (j = 0;j < 3;j++)
{
- VectorAdd(tris->normal, tempvert[tris->v[j]].normal, tempvert[tris->v[j]].normal);
- tempvert[tris->v[j]].count++;
+ xyz = (unsigned short) LittleShort (intri[i].index_xyz[j]);
+ st = (unsigned short) LittleShort (intri[i].index_st[j]);
+ if (xyz >= numxyz || st >= numst)
+ {
+ Mem_Free(md2verthash);
+ Mem_Free(md2verthashdata);
+ Mem_Free(stverts);
+ if (xyz >= numxyz)
+ Host_Error("Mod_MD2_LoadGeometry: invalid xyz index (%i) on triangle %i of model %s\n", xyz, i, loadmodel->name);
+ if (st >= numst)
+ Host_Error("Mod_MD2_LoadGeometry: invalid st index (%i) on triangle %i of model %s\n", st, i, loadmodel->name);
+ }
+ s = stverts[st*2+0];
+ t = stverts[st*2+1];
+ hashindex = (xyz * 17 + st) & 255;
+ for (hash = md2verthash[hashindex];hash;hash = hash->next)
+ if (hash->xyz == xyz && hash->st[0] == s && hash->st[1] == t)
+ break;
+ if (hash == NULL)
+ {
+ hash = md2verthashdata + num++;
+ hash->xyz = xyz;
+ hash->st[0] = s;
+ hash->st[1] = t;
+ hash->next = md2verthash[hashindex];
+ md2verthash[hashindex] = hash;
+ }
+ loadmodel->alias.aliasdata_meshes->data_element3i[i*3+j] = (hash - md2verthashdata);
}
- tris++;
}
- // average normals and write out 1.7bit format
- for (i = 0;i < pheader->numtris;i++)
+
+ Mem_Free(stverts);
+
+ numverts = num;
+ loadmodel->alias.aliasdata_meshes->num_vertices = numverts;
+ vertremap = Mem_Alloc(loadmodel->mempool, num * sizeof(int));
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f = Mem_Alloc(loadmodel->mempool, num * sizeof(float[2]));
+ for (i = 0;i < num;i++)
{
- VectorNormalize(tempvert[i].normal);
- out[i].n[0] = (signed char) (tempvert[i].normal[0] * 127.0);
- out[i].n[1] = (signed char) (tempvert[i].normal[1] * 127.0);
- out[i].n[2] = (signed char) (tempvert[i].normal[2] * 127.0);
+ hash = md2verthashdata + i;
+ vertremap[i] = hash->xyz;
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+0] = hash->st[0];
+ loadmodel->alias.aliasdata_meshes->data_texcoord2f[i*2+1] = hash->st[1];
}
- */
-}
-/*
-=================
-Mod_LoadQ2AliasModel
-=================
-*/
-void Mod_LoadQ2AliasModel (model_t *mod, void *buffer)
-{
- int i, j, version, size, *pinglcmd, *poutglcmd, start, end, total, framesize;
- md2_t *pinmodel;
- md2mem_t *pheader;
- md2triangle_t *pintriangles, *pouttriangles;
- md2frame_t *pinframe;
- md2memframe_t *poutframe;
- char *pinskins;
-// temptris_t *tris;
+ Mem_Free(md2verthash);
+ Mem_Free(md2verthashdata);
- start = Hunk_LowMark ();
+ // load the frames
+ datapointer = (base + LittleLong(pinmodel->ofs_frames));
+ loadmodel->alias.aliasdata_meshes->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, numverts * loadmodel->alias.aliasdata_meshes->num_frames * sizeof(float[4][3]));
+ loadmodel->alias.aliasdata_meshes->data_aliassvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 1;
+ loadmodel->alias.aliasdata_meshes->data_aliastvector3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 2;
+ loadmodel->alias.aliasdata_meshes->data_aliasnormal3f = loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + numverts * loadmodel->alias.aliasdata_meshes->num_frames * 3 * 3;
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_frames;i++)
+ {
+ pinframe = (md2frame_t *)datapointer;
+ datapointer += sizeof(md2frame_t);
+ for (j = 0;j < 3;j++)
+ {
+ scale[j] = LittleFloat(pinframe->scale[j]);
+ translate[j] = LittleFloat(pinframe->translate[j]);
+ }
+ Mod_MD2_ConvertVerts(scale, translate, (void *)datapointer, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * numverts * 3, numverts, vertremap);
+ Mod_BuildTextureVectorsAndNormals(loadmodel->alias.aliasdata_meshes->num_vertices, loadmodel->alias.aliasdata_meshes->num_triangles, loadmodel->alias.aliasdata_meshes->data_aliasvertex3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_texcoord2f, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->data_aliassvector3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliastvector3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3, loadmodel->alias.aliasdata_meshes->data_aliasnormal3f + i * loadmodel->alias.aliasdata_meshes->num_vertices * 3);
+ datapointer += numxyz * sizeof(trivertx_t);
+
+ strcpy(loadmodel->animscenes[i].name, pinframe->name);
+ loadmodel->animscenes[i].firstframe = i;
+ loadmodel->animscenes[i].framecount = 1;
+ loadmodel->animscenes[i].framerate = 10;
+ loadmodel->animscenes[i].loop = true;
+ }
-// if (!temptris)
-// temptris = malloc(sizeof(temptris_t) * MD2MAX_TRIANGLES);
+ Mem_Free(vertremap);
- pinmodel = (md2_t *)buffer;
+ loadmodel->alias.aliasdata_meshes->data_neighbor3i = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasdata_meshes->num_triangles * sizeof(int[3]));
+ Mod_BuildTriangleNeighbors(loadmodel->alias.aliasdata_meshes->data_neighbor3i, loadmodel->alias.aliasdata_meshes->data_element3i, loadmodel->alias.aliasdata_meshes->num_triangles);
+ for (i = 0;i < loadmodel->alias.aliasdata_meshes->num_skins;i++)
+ Mod_BuildAliasSkinFromSkinFrame(loadmodel->alias.aliasdata_meshes->data_skins + i, loadmodel->skinframes + i);
+ Mod_CalcAliasModelBBoxes();
+}
+void Mod_IDP3_Load(model_t *mod, void *buffer)
+{
+ int i, j, version;
+ md3modelheader_t *pinmodel;
+ md3frameinfo_t *pinframe;
+ md3mesh_t *pinmesh;
+ aliasmesh_t *mesh;
+ skinframe_t tempskinframe;
+
+ pinmodel = buffer;
+
+ if (memcmp(pinmodel->identifier, "IDP3", 4))
+ Host_Error ("%s is not a MD3 (IDP3) file\n", loadmodel->name);
version = LittleLong (pinmodel->version);
- if (version != MD2ALIAS_VERSION)
+ if (version != MD3VERSION)
Host_Error ("%s has wrong version number (%i should be %i)",
- mod->name, version, MD2ALIAS_VERSION);
-
- mod->type = mod_alias;
- mod->aliastype = ALIASTYPE_MD2;
-
- framesize = sizeof(md2memframesize_t) + LittleLong(pinmodel->num_xyz) * sizeof(trivert2);
- // LordHavoc: calculate size for in memory version
- size = sizeof(md2mem_t)
- + LittleLong(pinmodel->num_st) * sizeof(md2stvert_t)
- + LittleLong(pinmodel->num_tris) * sizeof(md2triangle_t)
- + LittleLong(pinmodel->num_frames) * framesize
- + LittleLong(pinmodel->num_glcmds) * sizeof(int);
- if (size <= 0 || size >= MD2MAX_SIZE)
- Host_Error ("%s is not a valid model", mod->name);
- pheader = Hunk_AllocName (size, loadname);
-
- mod->flags = 0; // there are no MD2 flags
- mod->numframes = LittleLong(pinmodel->num_frames);
- mod->synctype = ST_RAND;
- mod->numtris = LittleLong(pinmodel->num_tris); // LordHavoc: to simplify renderer decisions
-
- if (LittleLong(pinmodel->num_skins) >= 1 && (LittleLong(pinmodel->ofs_skins <= 0) || LittleLong(pinmodel->ofs_skins) >= LittleLong(pinmodel->ofs_end)))
- Host_Error ("%s is not a valid model", mod->name);
- if (LittleLong(pinmodel->ofs_st <= 0) || LittleLong(pinmodel->ofs_st) >= LittleLong(pinmodel->ofs_end))
- Host_Error ("%s is not a valid model", mod->name);
- if (LittleLong(pinmodel->ofs_tris <= 0) || LittleLong(pinmodel->ofs_tris) >= LittleLong(pinmodel->ofs_end))
- Host_Error ("%s is not a valid model", mod->name);
- if (LittleLong(pinmodel->ofs_frames <= 0) || LittleLong(pinmodel->ofs_frames) >= LittleLong(pinmodel->ofs_end))
- Host_Error ("%s is not a valid model", mod->name);
- if (LittleLong(pinmodel->ofs_glcmds <= 0) || LittleLong(pinmodel->ofs_glcmds) >= LittleLong(pinmodel->ofs_end))
- Host_Error ("%s is not a valid model", mod->name);
-
- if (LittleLong(pinmodel->num_tris < 1) || LittleLong(pinmodel->num_tris) > MD2MAX_TRIANGLES)
- Host_Error ("%s has invalid number of triangles: %i", mod->name, LittleLong(pinmodel->num_tris));
- if (LittleLong(pinmodel->num_xyz < 1) || LittleLong(pinmodel->num_xyz) > MD2MAX_VERTS)
- Host_Error ("%s has invalid number of vertices: %i", mod->name, LittleLong(pinmodel->num_xyz));
- if (LittleLong(pinmodel->num_frames < 1) || LittleLong(pinmodel->num_frames) > MD2MAX_FRAMES)
- Host_Error ("%s has invalid number of frames: %i", mod->name, LittleLong(pinmodel->num_frames));
- if (LittleLong(pinmodel->num_skins < 0) || LittleLong(pinmodel->num_skins) > MD2MAX_SKINS)
- Host_Error ("%s has invalid number of skins: %i", mod->name, LittleLong(pinmodel->num_skins));
-
- pheader->framesize = framesize;
- pheader->num_skins = LittleLong(pinmodel->num_skins);
- pheader->num_xyz = LittleLong(pinmodel->num_xyz);
- pheader->num_st = LittleLong(pinmodel->num_st);
- pheader->num_tris = LittleLong(pinmodel->num_tris);
- pheader->num_frames = LittleLong(pinmodel->num_frames);
- pheader->num_glcmds = LittleLong(pinmodel->num_glcmds);
-
-// load the skins
- if (pheader->num_skins)
- {
- int *skin, *skinrange;
- skinrange = loadmodel->skinanimrange;
- skin = loadmodel->skinanim;
-// skinrange = Hunk_AllocName (sizeof(int) * (pheader->num_skins * 2), loadname);
-// skin = skinrange + pheader->num_skins * 2;
-// loadmodel->skinanimrange = (int) skinrange - (int) pheader;
-// loadmodel->skinanim = (int) skin - (int) pheader;
- pinskins = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_skins));
- for (i = 0;i < pheader->num_skins;i++)
+ loadmodel->name, version, MD3VERSION);
+
+ loadmodel->type = mod_alias;
+ loadmodel->alias.aliastype = ALIASTYPE_ALIAS;
+ loadmodel->DrawSky = NULL;
+ loadmodel->Draw = R_Model_Alias_Draw;
+ loadmodel->DrawFakeShadow = R_Model_Alias_DrawFakeShadow;
+ loadmodel->DrawShadowVolume = R_Model_Alias_DrawShadowVolume;
+ loadmodel->DrawLight = R_Model_Alias_DrawLight;
+ loadmodel->flags = 0;
+ loadmodel->synctype = ST_RAND;
+
+ // set up some global info about the model
+ loadmodel->numframes = LittleLong(pinmodel->num_frames);
+ loadmodel->numskins = 1;
+ loadmodel->alias.aliasnum_meshes = LittleLong(pinmodel->num_meshes);
+ loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t));
+ loadmodel->skinscenes[0].firstframe = 0;
+ loadmodel->skinscenes[0].framecount = 1;
+ loadmodel->skinscenes[0].loop = true;
+ loadmodel->skinscenes[0].framerate = 10;
+
+ // load frameinfo
+ loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, loadmodel->numframes * sizeof(animscene_t));
+ for (i = 0, pinframe = (md3frameinfo_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_frameinfo));i < loadmodel->numframes;i++, pinframe++)
+ {
+ strcpy(loadmodel->animscenes[i].name, pinframe->name);
+ loadmodel->animscenes[i].firstframe = i;
+ loadmodel->animscenes[i].framecount = 1;
+ loadmodel->animscenes[i].framerate = 10;
+ loadmodel->animscenes[i].loop = true;
+ }
+
+ // tags are not loaded yet
+
+ // load meshes
+ loadmodel->alias.aliasdata_meshes = Mem_Alloc(loadmodel->mempool, loadmodel->alias.aliasnum_meshes * sizeof(aliasmesh_t));
+ for (i = 0, pinmesh = (md3mesh_t *)((qbyte *)pinmodel + LittleLong(pinmodel->lump_meshes));i < loadmodel->alias.aliasnum_meshes;i++, pinmesh = (md3mesh_t *)((qbyte *)pinmesh + LittleLong(pinmesh->lump_end)))
+ {
+ if (memcmp(pinmesh->identifier, "IDP3", 4))
+ Host_Error("Mod_IDP3_Load: invalid mesh identifier (not IDP3)\n");
+ mesh = loadmodel->alias.aliasdata_meshes + i;
+ mesh->num_skins = loadmodel->numskins;
+ mesh->num_frames = LittleLong(pinmesh->num_frames);
+ mesh->num_vertices = LittleLong(pinmesh->num_vertices);
+ mesh->num_triangles = LittleLong(pinmesh->num_triangles);
+ mesh->data_skins = Mem_Alloc(loadmodel->mempool, mesh->num_skins * sizeof(aliasskin_t));
+ mesh->data_element3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
+ mesh->data_neighbor3i = Mem_Alloc(loadmodel->mempool, mesh->num_triangles * sizeof(int[3]));
+ mesh->data_texcoord2f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * sizeof(float[2]));
+ mesh->data_aliasvertex3f = Mem_Alloc(loadmodel->mempool, mesh->num_vertices * mesh->num_frames * sizeof(float[4][3]));
+ mesh->data_aliassvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 1;
+ mesh->data_aliastvector3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 2;
+ mesh->data_aliasnormal3f = mesh->data_aliasvertex3f + mesh->num_vertices * mesh->num_frames * 3 * 3;
+ for (j = 0;j < mesh->num_triangles * 3;j++)
+ mesh->data_element3i[j] = LittleLong(((int *)((qbyte *)pinmesh + pinmesh->lump_elements))[j]);
+ for (j = 0;j < mesh->num_vertices;j++)
+ {
+ mesh->data_texcoord2f[j * 2 + 0] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 0]);
+ mesh->data_texcoord2f[j * 2 + 1] = LittleLong(((float *)((qbyte *)pinmesh + pinmesh->lump_texcoords))[j * 2 + 1]);
+ }
+ for (j = 0;j < mesh->num_vertices * mesh->num_frames;j++)
{
- *skinrange++ = i;
- *skinrange++ = 1;
- *skin++ = loadtextureimage (pinskins, 0, 0, true, true);
- *skin++ = 0; // the extra 4 layers are currently unused
- *skin++ = 0;
- *skin++ = 0;
- *skin++ = 0;
- pinskins += MD2MAX_SKINNAME;
+ mesh->data_aliasvertex3f[j * 3 + 0] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 0]) * (1.0f / 64.0f);
+ mesh->data_aliasvertex3f[j * 3 + 1] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 1]) * (1.0f / 64.0f);
+ mesh->data_aliasvertex3f[j * 3 + 2] = LittleShort(((short *)((qbyte *)pinmesh + pinmesh->lump_framevertices))[j * 4 + 2]) * (1.0f / 64.0f);
}
+ for (j = 0;j < mesh->num_frames;j++)
+ Mod_BuildTextureVectorsAndNormals(mesh->num_vertices, mesh->num_triangles, mesh->data_aliasvertex3f + j * mesh->num_vertices * 3, mesh->data_texcoord2f, mesh->data_element3i, mesh->data_aliassvector3f + j * mesh->num_vertices * 3, mesh->data_aliastvector3f + j * mesh->num_vertices * 3, mesh->data_aliasnormal3f + j * mesh->num_vertices * 3);
+
+ memset(&tempskinframe, 0, sizeof(tempskinframe));
+ if (LittleLong(pinmesh->num_shaders) >= 1 && ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name[0])
+ Mod_LoadSkinFrame(&tempskinframe, ((md3shader_t *)((qbyte *) pinmesh + pinmesh->lump_shaders))->name, (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_ALPHA | TEXF_CLAMP | TEXF_PRECACHE, true, false, true);
+ Mod_ValidateElements(mesh->data_element3i, mesh->num_triangles, mesh->num_vertices, __FILE__, __LINE__);
+ Mod_BuildTriangleNeighbors(mesh->data_neighbor3i, mesh->data_element3i, mesh->num_triangles);
+ Mod_BuildAliasSkinFromSkinFrame(mesh->data_skins, &tempskinframe);
}
- loadmodel->numskins = pheader->num_skins;
+ Mod_CalcAliasModelBBoxes();
+}
+
+extern void R_Model_Zymotic_DrawSky(entity_render_t *ent);
+extern void R_Model_Zymotic_Draw(entity_render_t *ent);
+extern void R_Model_Zymotic_DrawFakeShadow(entity_render_t *ent);
+extern void R_Model_Zymotic_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius);
+extern void R_Model_Zymotic_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz);
+void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer)
+{
+ zymtype1header_t *pinmodel, *pheader;
+ qbyte *pbase;
+
+ pinmodel = (void *)buffer;
+ pbase = buffer;
+ if (memcmp(pinmodel->id, "ZYMOTICMODEL", 12))
+ Host_Error ("Mod_ZYMOTICMODEL_Load: %s is not a zymotic model\n");
+ if (BigLong(pinmodel->type) != 1)
+ Host_Error ("Mod_ZYMOTICMODEL_Load: only type 1 (skeletal pose) models are currently supported (name = %s)\n", loadmodel->name);
+
+ loadmodel->type = mod_alias;
+ loadmodel->alias.aliastype = ALIASTYPE_ZYM;
+ loadmodel->DrawSky = NULL;
+ loadmodel->Draw = R_Model_Zymotic_Draw;
+ loadmodel->DrawFakeShadow = NULL;//R_Model_Zymotic_DrawFakeShadow;
+ loadmodel->DrawShadowVolume = NULL;//R_Model_Zymotic_DrawShadowVolume;
+ loadmodel->DrawLight = NULL;//R_Model_Zymotic_DrawLight;
+
+ // byteswap header
+ pheader = pinmodel;
+ pheader->type = BigLong(pinmodel->type);
+ pheader->filesize = BigLong(pinmodel->filesize);
+ pheader->mins[0] = BigFloat(pinmodel->mins[0]);
+ pheader->mins[1] = BigFloat(pinmodel->mins[1]);
+ pheader->mins[2] = BigFloat(pinmodel->mins[2]);
+ pheader->maxs[0] = BigFloat(pinmodel->maxs[0]);
+ pheader->maxs[1] = BigFloat(pinmodel->maxs[1]);
+ pheader->maxs[2] = BigFloat(pinmodel->maxs[2]);
+ pheader->radius = BigFloat(pinmodel->radius);
+ pheader->numverts = loadmodel->alias.zymnum_verts = BigLong(pinmodel->numverts);
+ pheader->numtris = loadmodel->alias.zymnum_tris = BigLong(pinmodel->numtris);
+ pheader->numshaders = loadmodel->alias.zymnum_shaders = BigLong(pinmodel->numshaders);
+ pheader->numbones = loadmodel->alias.zymnum_bones = BigLong(pinmodel->numbones);
+ pheader->numscenes = loadmodel->alias.zymnum_scenes = BigLong(pinmodel->numscenes);
+ pheader->lump_scenes.start = BigLong(pinmodel->lump_scenes.start);
+ pheader->lump_scenes.length = BigLong(pinmodel->lump_scenes.length);
+ pheader->lump_poses.start = BigLong(pinmodel->lump_poses.start);
+ pheader->lump_poses.length = BigLong(pinmodel->lump_poses.length);
+ pheader->lump_bones.start = BigLong(pinmodel->lump_bones.start);
+ pheader->lump_bones.length = BigLong(pinmodel->lump_bones.length);
+ pheader->lump_vertbonecounts.start = BigLong(pinmodel->lump_vertbonecounts.start);
+ pheader->lump_vertbonecounts.length = BigLong(pinmodel->lump_vertbonecounts.length);
+ pheader->lump_verts.start = BigLong(pinmodel->lump_verts.start);
+ pheader->lump_verts.length = BigLong(pinmodel->lump_verts.length);
+ pheader->lump_texcoords.start = BigLong(pinmodel->lump_texcoords.start);
+ pheader->lump_texcoords.length = BigLong(pinmodel->lump_texcoords.length);
+ pheader->lump_render.start = BigLong(pinmodel->lump_render.start);
+ pheader->lump_render.length = BigLong(pinmodel->lump_render.length);
+ pheader->lump_shaders.start = BigLong(pinmodel->lump_shaders.start);
+ pheader->lump_shaders.length = BigLong(pinmodel->lump_shaders.length);
+ pheader->lump_trizone.start = BigLong(pinmodel->lump_trizone.start);
+ pheader->lump_trizone.length = BigLong(pinmodel->lump_trizone.length);
+
+ loadmodel->flags = 0; // there are no flags
+ loadmodel->numframes = pheader->numscenes;
+ loadmodel->synctype = ST_SYNC;
+ //loadmodel->numtris = pheader->numtris;
+ //loadmodel->numverts = 0;
-// load triangles
- pintriangles = (void*)((int) pinmodel + LittleLong(pinmodel->ofs_tris));
- pouttriangles = (void*)&pheader[1];
- pheader->ofs_tris = (int) pouttriangles - (int) pheader;
-// tris = temptris;
- // swap the triangle list
- for (i=0 ; i<pheader->num_tris ; i++)
{
- for (j=0 ; j<3 ; j++)
+ unsigned int i;
+ float modelradius, corner[2];
+ // model bbox
+ modelradius = pheader->radius;
+ for (i = 0;i < 3;i++)
{
- temptris[i][j] = pouttriangles->index_xyz[j] = LittleShort (pintriangles->index_xyz[j]);
- pouttriangles->index_st[j] = LittleShort (pintriangles->index_st[j]);
- if (pouttriangles->index_xyz[j] >= pheader->num_xyz)
- Host_Error ("%s has invalid vertex indices", mod->name);
- if (pouttriangles->index_st[j] >= pheader->num_st)
- Host_Error ("%s has invalid vertex indices", mod->name);
+ loadmodel->normalmins[i] = pheader->mins[i];
+ loadmodel->normalmaxs[i] = pheader->maxs[i];
+ loadmodel->rotatedmins[i] = -modelradius;
+ loadmodel->rotatedmaxs[i] = modelradius;
}
- pintriangles++;
- pouttriangles++;
+ corner[0] = max(fabs(loadmodel->normalmins[0]), fabs(loadmodel->normalmaxs[0]));
+ corner[1] = max(fabs(loadmodel->normalmins[1]), fabs(loadmodel->normalmaxs[1]));
+ loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = sqrt(corner[0]*corner[0]+corner[1]*corner[1]);
+ if (loadmodel->yawmaxs[0] > modelradius)
+ loadmodel->yawmaxs[0] = loadmodel->yawmaxs[1] = modelradius;
+ loadmodel->yawmins[0] = loadmodel->yawmins[1] = -loadmodel->yawmaxs[0];
+ loadmodel->yawmins[2] = loadmodel->normalmins[2];
+ loadmodel->yawmaxs[2] = loadmodel->normalmaxs[2];
+ loadmodel->radius = modelradius;
+ loadmodel->radius2 = modelradius * modelradius;
}
- // LordHavoc: doing proper bbox for model
- aliasbboxmin[0] = aliasbboxmin[1] = aliasbboxmin[2] = 1000000000;
- aliasbboxmax[0] = aliasbboxmax[1] = aliasbboxmax[2] = -1000000000;
+ {
+ // FIXME: add shaders, and make them switchable shader sets and...
+ loadmodel->skinscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) + sizeof(skinframe_t));
+ loadmodel->skinscenes[0].firstframe = 0;
+ loadmodel->skinscenes[0].framecount = 1;
+ loadmodel->skinscenes[0].loop = true;
+ loadmodel->skinscenes[0].framerate = 10;
+ loadmodel->skinframes = (void *)(loadmodel->skinscenes + 1);
+ loadmodel->numskins = 1;
+ }
+
+ // go through the lumps, swapping things
-// load the frames
- pinframe = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_frames));
- poutframe = (void*) pouttriangles;
- pheader->ofs_frames = (int) poutframe - (int) pheader;
- for (i=0 ; i<pheader->num_frames ; i++)
{
- for (j = 0;j < 3;j++)
+ int i, numposes;
+ zymscene_t *scene;
+ // zymlump_t lump_scenes; // zymscene_t scene[numscenes]; // name and other information for each scene (see zymscene struct)
+ loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
+ scene = (void *) (pheader->lump_scenes.start + pbase);
+ numposes = pheader->lump_poses.length / pheader->numbones / sizeof(float[3][4]);
+ for (i = 0;i < pheader->numscenes;i++)
+ {
+ memcpy(loadmodel->animscenes[i].name, scene->name, 32);
+ loadmodel->animscenes[i].firstframe = BigLong(scene->start);
+ loadmodel->animscenes[i].framecount = BigLong(scene->length);
+ loadmodel->animscenes[i].framerate = BigFloat(scene->framerate);
+ loadmodel->animscenes[i].loop = (BigLong(scene->flags) & ZYMSCENEFLAG_NOLOOP) == 0;
+ if ((unsigned int) loadmodel->animscenes[i].firstframe >= (unsigned int) numposes)
+ Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, numposes);
+ if ((unsigned int) loadmodel->animscenes[i].firstframe + (unsigned int) loadmodel->animscenes[i].framecount > (unsigned int) numposes)
+ Host_Error("Mod_ZYMOTICMODEL_Load: scene firstframe (%i) + framecount (%i) >= numposes (%i)\n", loadmodel->animscenes[i].firstframe, loadmodel->animscenes[i].framecount, numposes);
+ if (loadmodel->animscenes[i].framerate < 0)
+ Host_Error("Mod_ZYMOTICMODEL_Load: scene framerate (%f) < 0\n", loadmodel->animscenes[i].framerate);
+ scene++;
+ }
+ }
+
+ {
+ int i;
+ float *poses;
+ // zymlump_t lump_poses; // float pose[numposes][numbones][3][4]; // animation data
+ loadmodel->alias.zymdata_poses = Mem_Alloc(loadmodel->mempool, pheader->lump_poses.length);
+ poses = (void *) (pheader->lump_poses.start + pbase);
+ for (i = 0;i < pheader->lump_poses.length / 4;i++)
+ loadmodel->alias.zymdata_poses[i] = BigFloat(poses[i]);
+ }
+
+ {
+ int i;
+ zymbone_t *bone;
+ // zymlump_t lump_bones; // zymbone_t bone[numbones];
+ loadmodel->alias.zymdata_bones = Mem_Alloc(loadmodel->mempool, pheader->numbones * sizeof(zymbone_t));
+ bone = (void *) (pheader->lump_bones.start + pbase);
+ for (i = 0;i < pheader->numbones;i++)
{
- poutframe->scale[j] = LittleFloat(pinframe->scale[j]);
- poutframe->translate[j] = LittleFloat(pinframe->translate[j]);
+ memcpy(loadmodel->alias.zymdata_bones[i].name, bone[i].name, sizeof(bone[i].name));
+ loadmodel->alias.zymdata_bones[i].flags = BigLong(bone[i].flags);
+ loadmodel->alias.zymdata_bones[i].parent = BigLong(bone[i].parent);
+ if (loadmodel->alias.zymdata_bones[i].parent >= i)
+ Host_Error("Mod_ZYMOTICMODEL_Load: bone[%i].parent >= %i in %s\n", i, i, loadmodel->name);
}
- Mod_ConvertQ2AliasVerts (pheader->num_xyz, poutframe->scale, poutframe->translate, &pinframe->verts[0], &poutframe->verts[0]);
- pinframe = (void*) &pinframe->verts[j];
- poutframe = (void*) &poutframe->verts[j];
}
- // LordHavoc: model bbox
- for (j = 0;j < 3;j++)
{
- mod->mins[j] = aliasbboxmin[j];
- mod->maxs[j] = aliasbboxmax[j];
+ int i, *bonecount;
+ // zymlump_t lump_vertbonecounts; // int vertbonecounts[numvertices]; // how many bones influence each vertex (separate mainly to make this compress better)
+ loadmodel->alias.zymdata_vertbonecounts = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(int));
+ bonecount = (void *) (pheader->lump_vertbonecounts.start + pbase);
+ for (i = 0;i < pheader->numverts;i++)
+ {
+ loadmodel->alias.zymdata_vertbonecounts[i] = BigLong(bonecount[i]);
+ if (loadmodel->alias.zymdata_vertbonecounts[i] < 1)
+ Host_Error("Mod_ZYMOTICMODEL_Load: bone vertex count < 1 in %s\n", loadmodel->name);
+ }
}
- // load the draw list
- pinglcmd = (void*) ((int) pinmodel + LittleLong(pinmodel->ofs_glcmds));
- poutglcmd = (void*) poutframe;
- pheader->ofs_glcmds = (int) poutglcmd - (int) pheader;
- for (i = 0;i < pheader->num_glcmds;i++)
- *poutglcmd++ = LittleLong(*pinglcmd++);
+ {
+ int i;
+ zymvertex_t *vertdata;
+ // zymlump_t lump_verts; // zymvertex_t vert[numvertices]; // see vertex struct
+ loadmodel->alias.zymdata_verts = Mem_Alloc(loadmodel->mempool, pheader->lump_verts.length);
+ vertdata = (void *) (pheader->lump_verts.start + pbase);
+ for (i = 0;i < pheader->lump_verts.length / (int) sizeof(zymvertex_t);i++)
+ {
+ loadmodel->alias.zymdata_verts[i].bonenum = BigLong(vertdata[i].bonenum);
+ loadmodel->alias.zymdata_verts[i].origin[0] = BigFloat(vertdata[i].origin[0]);
+ loadmodel->alias.zymdata_verts[i].origin[1] = BigFloat(vertdata[i].origin[1]);
+ loadmodel->alias.zymdata_verts[i].origin[2] = BigFloat(vertdata[i].origin[2]);
+ }
+ }
-// move the complete, relocatable alias model to the cache
- end = Hunk_LowMark ();
- total = end - start;
-
- Cache_Alloc (&mod->cache, total, loadname);
- if (!mod->cache.data)
- return;
- memcpy (mod->cache.data, pheader, total);
+ {
+ int i;
+ float *intexcoord2f, *outtexcoord2f;
+ // zymlump_t lump_texcoords; // float texcoords[numvertices][2];
+ loadmodel->alias.zymdata_texcoords = outtexcoord2f = Mem_Alloc(loadmodel->mempool, pheader->numverts * sizeof(float[2]));
+ intexcoord2f = (void *) (pheader->lump_texcoords.start + pbase);
+ for (i = 0;i < pheader->numverts;i++)
+ {
+ outtexcoord2f[i*2+0] = BigFloat(intexcoord2f[i*2+0]);
+ // flip T coordinate for OpenGL
+ outtexcoord2f[i*2+1] = 1 - BigFloat(intexcoord2f[i*2+1]);
+ }
+ }
+
+ {
+ int i, count, *renderlist, *renderlistend, *outrenderlist;
+ // 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)
+ loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, pheader->lump_render.length);
+ // byteswap, validate, and swap winding order of tris
+ count = pheader->numshaders * sizeof(int) + pheader->numtris * sizeof(int[3]);
+ if (pheader->lump_render.length != count)
+ Host_Error("Mod_ZYMOTICMODEL_Load: renderlist is wrong size in %s (is %i bytes, should be %i bytes)\n", loadmodel->name, pheader->lump_render.length, count);
+ outrenderlist = loadmodel->alias.zymdata_renderlist = Mem_Alloc(loadmodel->mempool, count);
+ renderlist = (void *) (pheader->lump_render.start + pbase);
+ renderlistend = (void *) ((qbyte *) renderlist + pheader->lump_render.length);
+ for (i = 0;i < pheader->numshaders;i++)
+ {
+ if (renderlist >= renderlistend)
+ Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
+ count = BigLong(*renderlist);renderlist++;
+ if (renderlist + count * 3 > renderlistend)
+ Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (wrong size)\n", loadmodel->name);
+ *outrenderlist++ = count;
+ while (count--)
+ {
+ outrenderlist[2] = BigLong(renderlist[0]);
+ outrenderlist[1] = BigLong(renderlist[1]);
+ outrenderlist[0] = BigLong(renderlist[2]);
+ if ((unsigned int)outrenderlist[0] >= (unsigned int)pheader->numverts
+ || (unsigned int)outrenderlist[1] >= (unsigned int)pheader->numverts
+ || (unsigned int)outrenderlist[2] >= (unsigned int)pheader->numverts)
+ Host_Error("Mod_ZYMOTICMODEL_Load: corrupt renderlist in %s (out of bounds index)\n", loadmodel->name);
+ renderlist += 3;
+ outrenderlist += 3;
+ }
+ }
+ }
- Hunk_FreeToLowMark (start);
+ {
+ int i;
+ char *shadername;
+ // zymlump_t lump_shaders; // char shadername[numshaders][32]; // shaders used on this model
+ loadmodel->alias.zymdata_textures = Mem_Alloc(loadmodel->mempool, pheader->numshaders * sizeof(rtexture_t *));
+ shadername = (void *) (pheader->lump_shaders.start + pbase);
+ for (i = 0;i < pheader->numshaders;i++)
+ loadmodel->alias.zymdata_textures[i] = loadtextureimage(loadmodel->texturepool, shadername + i * 32, 0, 0, true, TEXF_ALPHA | TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0));
+ }
+
+ {
+ // zymlump_t lump_trizone; // byte trizone[numtris]; // see trizone explanation
+ loadmodel->alias.zymdata_trizone = Mem_Alloc(loadmodel->mempool, pheader->numtris);
+ memcpy(loadmodel->alias.zymdata_trizone, (void *) (pheader->lump_trizone.start + pbase), pheader->numtris);
+ }
}