From 76ed5d5ee3a2517bfae3df09fb0b8d912f7286bc Mon Sep 17 00:00:00 2001 From: havoc Date: Wed, 25 Apr 2007 18:17:38 +0000 Subject: [PATCH] fixed bug where animated mdl skins (such as Tomaz's health box and explobox models in dpmod) were getting freed because they were not properly counted in the model->num_textures git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7197 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rmain.c | 2 +- model_alias.c | 26 ++++++++++++++++++-------- model_brush.c | 3 +++ model_shared.c | 8 ++------ model_shared.h | 1 + model_sprite.c | 2 ++ 6 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gl_rmain.c b/gl_rmain.c index 3d35a29c..088e3bf0 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -3254,7 +3254,7 @@ void R_UpdateAllTextureInfo(entity_render_t *ent) { int i; if (ent->model) - for (i = 0;i < ent->model->num_textures;i++) + for (i = 0;i < ent->model->num_texturesperskin;i++) R_UpdateTextureInfo(ent, ent->model->data_textures + i); } diff --git a/model_alias.c b/model_alias.c index a0621802..55032835 100644 --- a/model_alias.c +++ b/model_alias.c @@ -936,7 +936,8 @@ void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend) // load the skins skinfiles = Mod_LoadSkinFiles(); loadmodel->skinscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, loadmodel->numskins * sizeof(animscene_t)); - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * totalskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * totalskins * sizeof(texture_t)); if (skinfiles) { @@ -1134,7 +1135,8 @@ void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend) skinfiles = Mod_LoadSkinFiles(); if (skinfiles) { - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); Mod_BuildAliasSkinsFromSkinFiles(loadmodel->data_textures, skinfiles, "default", ""); Mod_FreeSkinFiles(skinfiles); @@ -1142,7 +1144,8 @@ void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend) else if (loadmodel->numskins) { // skins found (most likely not a player model) - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); for (i = 0;i < loadmodel->numskins;i++, inskin += MD2_SKINNAME) { @@ -1156,7 +1159,8 @@ void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend) { // no skins (most likely a player model) loadmodel->numskins = 1; - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t)); Mod_BuildAliasSkinFromSkinFrame(loadmodel->data_textures, NULL); } @@ -1367,7 +1371,8 @@ void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend) } loadmodel->nummodelsurfaces = loadmodel->num_surfaces; - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * sizeof(float[2]) + meshvertices * loadmodel->numframes * sizeof(md3vertex_t)); loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t); loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int); @@ -1598,7 +1603,8 @@ void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend) meshtriangles = pheader->numtris; loadmodel->nummodelsurfaces = loadmodel->num_surfaces; - loadmodel->num_textures = loadmodel->num_surfaces; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * sizeof(float[14]) + meshvertices * sizeof(int[4]) + meshvertices * sizeof(float[4]) + loadmodel->num_poses * sizeof(float[12]) + loadmodel->num_bones * sizeof(float[12])); loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t); loadmodel->surfacelist = (int *)data;data += loadmodel->num_surfaces * sizeof(int); @@ -1835,7 +1841,9 @@ void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend) loadmodel->numframes = pheader->num_frames; loadmodel->num_bones = pheader->num_bones; loadmodel->num_poses = loadmodel->num_bones * loadmodel->numframes; - loadmodel->num_textures = loadmodel->nummodelsurfaces = loadmodel->num_surfaces = pheader->num_meshs; + loadmodel->nummodelsurfaces = loadmodel->num_surfaces = pheader->num_meshs; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; // do most allocations as one merged chunk data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * (sizeof(float[14]) + sizeof(int[4]) + sizeof(float[4])) + loadmodel->num_poses * sizeof(float[12]) + loadmodel->num_bones * sizeof(float[12]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t)); loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t); @@ -2410,7 +2418,9 @@ void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend) loadmodel->numskins = 1; loadmodel->num_bones = numbones; loadmodel->num_poses = loadmodel->num_bones * loadmodel->numframes; - loadmodel->num_textures = loadmodel->nummodelsurfaces = loadmodel->num_surfaces = nummatts; + loadmodel->nummodelsurfaces = loadmodel->num_surfaces = nummatts; + loadmodel->num_textures = loadmodel->num_surfaces * loadmodel->numskins; + loadmodel->num_texturesperskin = loadmodel->num_surfaces; // do most allocations as one merged chunk data = (unsigned char *)Mem_Alloc(loadmodel->mempool, loadmodel->num_surfaces * sizeof(msurface_t) + loadmodel->num_surfaces * sizeof(int) + loadmodel->num_surfaces * loadmodel->numskins * sizeof(texture_t) + meshtriangles * sizeof(int[3]) + meshtriangles * sizeof(int[3]) + meshvertices * (sizeof(float[14]) + sizeof(int[4]) + sizeof(float[4])) + loadmodel->num_poses * sizeof(float[12]) + loadmodel->num_bones * sizeof(float[12]) + loadmodel->numskins * sizeof(animscene_t) + loadmodel->num_bones * sizeof(aliasbone_t) + loadmodel->numframes * sizeof(animscene_t)); loadmodel->data_surfaces = (msurface_t *)data;data += loadmodel->num_surfaces * sizeof(msurface_t); diff --git a/model_brush.c b/model_brush.c index 532632a1..76219a6f 100644 --- a/model_brush.c +++ b/model_brush.c @@ -1334,11 +1334,13 @@ static void Mod_Q1BSP_LoadTextures(lump_t *l) m = (dmiptexlump_t *)(mod_base + l->fileofs); m->nummiptex = LittleLong (m->nummiptex); loadmodel->num_textures = m->nummiptex + 2; + loadmodel->num_texturesperskin = loadmodel->num_textures; } else { m = NULL; loadmodel->num_textures = 2; + loadmodel->num_texturesperskin = loadmodel->num_textures; } loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, loadmodel->num_textures * sizeof(texture_t)); @@ -4467,6 +4469,7 @@ static void Mod_Q3BSP_LoadTextures(lump_t *l) loadmodel->data_textures = out; loadmodel->num_textures = count; + loadmodel->num_texturesperskin = loadmodel->num_textures; for (i = 0;i < count;i++) { diff --git a/model_shared.c b/model_shared.c index 12e32847..9c63e458 100644 --- a/model_shared.c +++ b/model_shared.c @@ -67,18 +67,14 @@ static void mod_shutdown(void) static void mod_newmap(void) { msurface_t *surface; - int i, j, k, numtextures, surfacenum, ssize, tsize; + int i, j, k, surfacenum, ssize, tsize; R_SkinFrame_PrepareForPurge(); for (i = 0;i < mod_numknown;i++) { if (mod_known[i].mempool && mod_known[i].data_textures) { - numtextures = mod_known[i].num_textures; - // models can have multiple sets of textures - if (mod_known[i].numskins > 1) - numtextures *= mod_known[i].numskins; - for (j = 0;j < numtextures;j++) + for (j = 0;j < mod_known[i].num_textures;j++) { for (k = 0;k < mod_known[i].data_textures[j].numskinframes;k++) R_SkinFrame_MarkUsed(mod_known[i].data_textures[j].skinframes[k]); diff --git a/model_shared.h b/model_shared.h index dc1c969e..69332b2e 100644 --- a/model_shared.h +++ b/model_shared.h @@ -611,6 +611,7 @@ typedef struct model_s float *data_baseboneposeinverse; // textures of this model int num_textures; + int num_texturesperskin; texture_t *data_textures; // surfaces of this model int num_surfaces; diff --git a/model_sprite.c b/model_sprite.c index 1e3bd7a4..30a7ec4b 100644 --- a/model_sprite.c +++ b/model_sprite.c @@ -114,6 +114,7 @@ static void Mod_Sprite_SharedSetup(const unsigned char *datapointer, int version loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes); loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes); loadmodel->num_textures = realframes; + loadmodel->num_texturesperskin = 1; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures); datapointer = (unsigned char *)startframes; @@ -360,6 +361,7 @@ void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend) loadmodel->animscenes = (animscene_t *)Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes); loadmodel->sprite.sprdata_frames = (mspriteframe_t *)Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * loadmodel->numframes); loadmodel->num_textures = loadmodel->numframes; + loadmodel->num_texturesperskin = 1; loadmodel->data_textures = (texture_t *)Mem_Alloc(loadmodel->mempool, sizeof(texture_t) * loadmodel->num_textures); modelradius = 0; -- 2.39.2