Skip to content

Commit

Permalink
Delete unused frames in textures.
Browse files Browse the repository at this point in the history
Delete unused frames in textures.
  • Loading branch information
Karaulov committed Jan 6, 2023
1 parent 77c15e8 commit 6e1fc66
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 109 deletions.
49 changes: 37 additions & 12 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1592,11 +1592,32 @@ unsigned int Bsp::remove_unused_textures(bool* usedTextures, int* remappedIndexe
{
BSPMIPTEX* tex = (BSPMIPTEX*)(textures + offset);
// don't delete single frames from animated textures or else game crashes
if (tex->szName[0] == '-' || tex->szName[0] == '+')
if ((tex->szName[0] == '-' || tex->szName[0] == '+') && strlen(tex->szName) > 2)
{
usedTextures[i] = true;
// TODO: delete all frames if none are used
continue;
// TODO: delete all frames if none are used. Success ?!

char* newname = &tex->szName[2]; // +0BTN1 +1BTN1 +ABTN1 +BBTN1
for (int n = i + 1; n < oldTexCount; n++)
{
if (usedTextures[n])
{
int offset2 = ((int*)textures)[n + 1];
if (offset2 >= 0)
{
BSPMIPTEX* tex2 = (BSPMIPTEX*)(textures + offset2);
if (strcasecmp(newname, &tex2->szName[2]) == 0)
{
usedTextures[i] = true;
break;
}
}
}
}

if (usedTextures[i])
{
continue;
}
}
removeSize += getBspTextureSize(i) + sizeof(int);
}
Expand All @@ -1609,10 +1630,11 @@ unsigned int Bsp::remove_unused_textures(bool* usedTextures, int* remappedIndexe
memset(newTexData, 0, bsp_header.lump[LUMP_TEXTURES].nLength - removeSize);

int* texHeader = (int*)newTexData;
texHeader[0] = newTexCount;


int newOffset = (newTexCount + 1) * sizeof(int);
for (int i = 0, k = 0; i < oldTexCount; i++)
int k = 0;
for (int i = 0; i < oldTexCount; i++)
{
if (!usedTextures[i])
{
Expand All @@ -1621,7 +1643,7 @@ unsigned int Bsp::remove_unused_textures(bool* usedTextures, int* remappedIndexe
int oldOffset = ((int*)textures)[i + 1];
if (oldOffset < 0)
{
texHeader[k + 1] = oldOffset;

}
else
{
Expand All @@ -1631,12 +1653,14 @@ unsigned int Bsp::remove_unused_textures(bool* usedTextures, int* remappedIndexe
memcpy(newTexData + newOffset, textures + oldOffset, sz);
texHeader[k + 1] = newOffset;
newOffset += sz;
}

remappedIndexes[i] = k;
k++;
remappedIndexes[i] = k;
k++;
}
}

texHeader[0] = k;

replace_lump(LUMP_TEXTURES, newTexData, bsp_header.lump[LUMP_TEXTURES].nLength - removeSize);

return removeCount;
Expand Down Expand Up @@ -1814,7 +1838,7 @@ bool operator > (const BSPTEXTUREINFO& struct1, const BSPTEXTUREINFO& struct2)

bool operator == (const BSPTEXTUREINFO& struct1, const BSPTEXTUREINFO& struct2)
{
return memcmp(&struct1,&struct2,sizeof(BSPTEXTUREINFO)) == 0;
return memcmp(&struct1, &struct2, sizeof(BSPTEXTUREINFO)) == 0;
}

void Bsp::clean_unused_texinfos()
Expand Down Expand Up @@ -1845,14 +1869,15 @@ void Bsp::clean_unused_texinfos()
}
}


STRUCTCOUNT Bsp::remove_unused_model_structures(unsigned int target)
{
if (!modelCount)
return STRUCTCOUNT();

update_lump_pointers();

if (g_settings.mark_unused_texinfos)
if (g_settings.mark_unused_texinfos && target & CLEAN_TEXINFOS)
clean_unused_texinfos();

// marks which structures should not be moved
Expand Down
101 changes: 4 additions & 97 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,102 +313,6 @@ void Gui::pasteLightmap()
map->getBspRender()->reloadLightmaps();
}

void ExportModelOrigin(Bsp* map, int id, int ExportType)
{
map->update_ent_lump();

Bsp tmpMap = Bsp(map->bsp_path);
tmpMap.is_bsp_model = true;

BSPMODEL tmpModel = map->models[id];

Entity* tmpEnt = new Entity(*map->ents[0]);

vec3 EntOffset = vec3();

for (int i = 0; i < map->ents.size(); i++)
{
if (map->ents[i]->getBspModelIdx() == id)
{
EntOffset = map->ents[i]->getOrigin();
break;
}
}

vec3 modelOrigin = map->get_model_center(id);

tmpMap.modelCount = 1;
tmpMap.models[0] = tmpModel;

// Move model to 0 0 0
tmpMap.move(-modelOrigin, 0, true);

for (int i = 1; i < tmpMap.ents.size(); i++)
{
delete tmpMap.ents[i];
}

tmpMap.ents.clear();

tmpEnt->setOrAddKeyvalue("origin", vec3(0, 0, 0).toKeyvalueString());
tmpEnt->setOrAddKeyvalue("compiler", g_version_string);
tmpEnt->setOrAddKeyvalue("message", "bsp model");
tmpMap.ents.push_back(tmpEnt);

tmpMap.update_ent_lump();

tmpMap.lumps[LUMP_MODELS] = (unsigned char*)tmpMap.models;
tmpMap.bsp_header.lump[LUMP_MODELS].nLength = sizeof(tmpModel);
tmpMap.update_lump_pointers();



for (int i = 0; i < 4; i++)
{
logf("tmpMap.models[0].iHeadnodes[{}] = {}\n", i, tmpMap.models[0].iHeadnodes[i]);
}

STRUCTCOUNT removed = tmpMap.remove_unused_model_structures(CLEAN_LIGHTMAP | CLEAN_PLANES | CLEAN_NODES | CLEAN_LEAVES | CLEAN_MARKSURFACES | CLEAN_FACES | CLEAN_SURFEDGES | CLEAN_TEXINFOS |
CLEAN_EDGES | CLEAN_VERTICES | CLEAN_TEXTURES | CLEAN_VISDATA);
if (!removed.allZero())
removed.print_delete_stats(1);

if (!tmpMap.validate())
{
logf("Tried to fix model by adding emply missing data {}\n", id);
int markid = 0;
for (int i = 0; i < tmpMap.leafCount; i++)
{
BSPLEAF32& tmpLeaf = tmpMap.leaves[i];
tmpLeaf.iFirstMarkSurface = markid;
markid += tmpLeaf.nMarkSurfaces;
}

while (tmpMap.models[0].nVisLeafs >= tmpMap.leafCount)
tmpMap.create_leaf(-2);

tmpMap.lumps[LUMP_LEAVES] = (unsigned char*)tmpMap.leaves;
tmpMap.update_lump_pointers();
}

if (!tmpMap.validate())
{
logf("Failed to export model {}\n", id);
return;
}


for (int i = 0; i < 4; i++)
{
logf("tmpMap.models[0].iHeadnodes[{}] = {}\n", i, tmpMap.models[0].iHeadnodes[i]);
}

if (!dirExists(g_settings.gamedir + g_settings.workingdir))
createDir(g_settings.gamedir + g_settings.workingdir);
logf("Export model {} to {}\n", id, g_settings.gamedir + g_settings.workingdir + "model" + std::to_string(id) + ".bsp");
tmpMap.write(g_settings.gamedir + g_settings.workingdir + "model" + std::to_string(id) + ".bsp");
}

void ExportModel(Bsp* map, int id, int ExportType)
{
logf("Save current map to temporary file.\n");
Expand Down Expand Up @@ -510,7 +414,9 @@ void ExportModel(Bsp* map, int id, int ExportType)

tmpMap->update_lump_pointers();
logf("Remove all unused map data #2.\n");
removed = tmpMap->remove_unused_model_structures(CLEAN_MARKSURFACES);
removed = tmpMap->remove_unused_model_structures(CLEAN_LIGHTMAP | CLEAN_PLANES | CLEAN_NODES | CLEAN_CLIPNODES | CLEAN_CLIPNODES_SOMETHING | CLEAN_LEAVES | CLEAN_FACES | CLEAN_SURFEDGES | CLEAN_TEXINFOS |
CLEAN_EDGES | CLEAN_VERTICES | CLEAN_TEXTURES | CLEAN_VISDATA | CLEAN_MARKSURFACES);

if (!removed.allZero())
removed.print_delete_stats(1);

Expand All @@ -522,6 +428,7 @@ void ExportModel(Bsp* map, int id, int ExportType)
}
}


void Gui::draw3dContextMenus()
{
ImGuiContext& g = *GImGui;
Expand Down

0 comments on commit 6e1fc66

Please sign in to comment.