Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Dec 15, 2024
1 parent bd94964 commit ef64a23
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 76 deletions.
94 changes: 48 additions & 46 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9571,24 +9571,14 @@ void Bsp::remove_faces_by_content(int content)
print_log("Removed {} faces from map!\n", removedFaces);
}

bool Bsp::remove_face(int faceIdx)
bool Bsp::remove_face(int faceIdx, bool fromModels)
{
// Check if face index is valid
if (faceIdx < 0 || faceIdx >= faceCount)
{
return false;
}

// Create a vector to hold all the faces except the one to be removed
std::vector<BSPFACE32> all_faces;
for (int f = 0; f < faceCount; f++)
{
if (f != faceIdx)
{
all_faces.push_back(faces[f]);
}
}

// Shift face count in models
for (int m = 0; m < modelCount; m++)
{
Expand All @@ -9615,51 +9605,63 @@ bool Bsp::remove_face(int faceIdx)
}
}

// Shift face count in nodes
for (int n = 0; n < nodeCount; n++)
if (!fromModels)
{
// If the node has no faces or its first face index is out of bounds, reset the face count and continue to the next node
if (nodes[n].nFaces <= 0 || nodes[n].iFirstFace < 0)
{
nodes[n].iFirstFace = 0;
nodes[n].nFaces = 0;
continue;
}
if (faceIdx >= nodes[n].iFirstFace && faceIdx < nodes[n].iFirstFace + nodes[n].nFaces)
// Create a vector to hold all the faces except the one to be removed
std::vector<BSPFACE32> all_faces;
for (int f = 0; f < faceCount; f++)
{
nodes[n].nFaces--;
}
else if (faceIdx < nodes[n].iFirstFace)
{
nodes[n].iFirstFace--;
if (f != faceIdx)
{
all_faces.push_back(faces[f]);
}
}
if (nodes[n].nFaces <= 0 || nodes[n].iFirstFace < 0)

// Shift face count in nodes
for (int n = 0; n < nodeCount; n++)
{
nodes[n].iFirstFace = 0;
nodes[n].nFaces = 0;
// If the node has no faces or its first face index is out of bounds, reset the face count and continue to the next node
if (nodes[n].nFaces <= 0 || nodes[n].iFirstFace < 0)
{
nodes[n].iFirstFace = 0;
nodes[n].nFaces = 0;
continue;
}
if (faceIdx >= nodes[n].iFirstFace && faceIdx < nodes[n].iFirstFace + nodes[n].nFaces)
{
nodes[n].nFaces--;
}
else if (faceIdx < nodes[n].iFirstFace)
{
nodes[n].iFirstFace--;
}
if (nodes[n].nFaces <= 0 || nodes[n].iFirstFace < 0)
{
nodes[n].iFirstFace = 0;
nodes[n].nFaces = 0;
}
}
}

// Update the faces array after removing the specified face
unsigned char* newLump = new unsigned char[sizeof(BSPFACE32) * all_faces.size()];
memcpy(newLump, &all_faces[0], sizeof(BSPFACE32) * all_faces.size());
replace_lump(LUMP_FACES, newLump, sizeof(BSPFACE32) * all_faces.size());
delete[] newLump;
// Remove face from all leaves
leaf_del_face(faceIdx, -1);
// Update the faces array after removing the specified face
unsigned char* newLump = new unsigned char[sizeof(BSPFACE32) * all_faces.size()];
memcpy(newLump, &all_faces[0], sizeof(BSPFACE32) * all_faces.size());
replace_lump(LUMP_FACES, newLump, sizeof(BSPFACE32) * all_faces.size());
delete[] newLump;
// Remove face from all leaves
leaf_del_face(faceIdx, -1);

// Shift face count in marksurfs and mark the surfaces to be deleted
for (int s = 0; s < marksurfCount; s++)
{
if (marksurfs[s] < 0)
continue;

if (faceIdx < marksurfs[s])
// Shift face count in marksurfs and mark the surfaces to be deleted
for (int s = 0; s < marksurfCount; s++)
{
marksurfs[s]--;
if (marksurfs[s] < 0)
continue;

if (faceIdx < marksurfs[s])
{
marksurfs[s]--;
}
}
}

return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class Bsp
bool cull_leaf_faces(int leafIdx);
bool leaf_add_face(int faceIdx, int leafIdx);
bool leaf_del_face(int faceIdx, int leafIdx);
bool remove_face(int faceid);
bool remove_face(int faceid, bool fromModels = false);
void remove_faces_by_content(int content);
std::vector<int> getFaceContents(int faceIdx);
int clone_world_leaf(int oldleafIdx);
Expand Down
45 changes: 45 additions & 0 deletions src/bsp/bsptypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,49 @@ std::vector<int> getDiffModels(LumpState& oldLump, LumpState& newLump)
}

return updateModels;
}


std::vector<int> getDiffFaces(LumpState& oldLump, LumpState& newLump)
{
std::vector<int> updateFaces{};
if (newLump.lumps[LUMP_FACES].empty())
return updateFaces;

if (oldLump.lumps[LUMP_FACES].empty() && newLump.lumps[LUMP_FACES].size())
{
int addModelCount = (int)(newLump.lumps[LUMP_FACES].size() / sizeof(BSPFACE32));
for (int i = 0; i < addModelCount; i++)
{
updateFaces.push_back(i);
}
return updateFaces;
}

if (newLump.lumps[LUMP_FACES].size() > oldLump.lumps[LUMP_FACES].size())
{
int curModelCount = (int)(oldLump.lumps[LUMP_FACES].size() / sizeof(BSPFACE32));
int addModelCount = (int)((newLump.lumps[LUMP_FACES].size() - oldLump.lumps[LUMP_FACES].size()) / sizeof(BSPFACE32));
for (int i = curModelCount; i < curModelCount + addModelCount; i++)
{
updateFaces.push_back(i);
}
}

size_t modelLumpCount = std::min(newLump.lumps[LUMP_FACES].size(), oldLump.lumps[LUMP_FACES].size());

modelLumpCount /= sizeof(BSPFACE32);

BSPFACE32* listOld = (BSPFACE32*)oldLump.lumps[LUMP_FACES].data();
BSPFACE32* listNew = (BSPFACE32*)newLump.lumps[LUMP_FACES].data();

for (size_t i = 0; i < modelLumpCount; i++)
{
if (memcmp(&listOld[i], &listNew[i], sizeof(BSPFACE32)) != 0)
{
updateFaces.push_back((int)i);
}
}

return updateFaces;
}
3 changes: 2 additions & 1 deletion src/bsp/bsptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,5 @@ struct LumpState
LumpState(void* _map) : map(_map) {}
};

std::vector<int> getDiffModels(LumpState& oldLump, LumpState& newLump);
std::vector<int> getDiffModels(LumpState& oldLump, LumpState& newLump);
std::vector<int> getDiffFaces(LumpState& oldLump, LumpState& newLump);
1 change: 1 addition & 0 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ int BspRenderer::refreshModel(int modelIdx, bool refreshClipnodes, bool triangul
refreshFace(model.iFirstFace + i);
}

delete renderModels[modelIdx];
renderModels[modelIdx] = renderModel;

if (refreshClipnodes)
Expand Down
88 changes: 64 additions & 24 deletions src/editor/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::vector<unsigned char> decompressData(const std::vector<unsigned char>& comp
}


EditBspCommand::EditBspCommand(const std::string & desc, LumpState _oldLumps, LumpState _newLumps, unsigned int targetLumps) :
EditBspCommand::EditBspCommand(const std::string& desc, LumpState _oldLumps, LumpState _newLumps, unsigned int targetLumps) :
desc(desc), oldLumps(std::move(_oldLumps)), newLumps(std::move(_newLumps)), targetLumps(targetLumps), memoryused(0)
{
for (int i = 0; i < HEADER_LUMPS; i++)
Expand Down Expand Up @@ -93,11 +93,34 @@ void EditBspCommand::execute()
}
map->replace_lumps(newLumps);

refresh(renderer);

auto mdls = getDiffModels(oldLumps, newLumps);
auto faces = getDiffFaces(oldLumps, newLumps);
for (auto& face : faces)
{
if (std::find(mdls.begin(), mdls.end(), map->get_model_from_face(face)) == mdls.end())
{
mdls.push_back(map->get_model_from_face(face));
}
}

/*if (mdls.size() > 5)
{
renderer->genRenderFaces();
}
else
{*/
if (mdls.size() && !(targetLumps & FL_LIGHTING))
{
renderer->loadLightmaps();
}
for (auto& mdl : mdls)
{
renderer->refreshModel(mdl);
renderer->refreshModel(mdl, false);
}
//}


for (int i = 0; i < HEADER_LUMPS; i++)
{
Expand All @@ -111,7 +134,6 @@ void EditBspCommand::execute()
newLumps.lumps[i] = compressData(newLumps.lumps[i]);
}
}

}

void EditBspCommand::undo()
Expand Down Expand Up @@ -146,41 +168,35 @@ void EditBspCommand::undo()

map->replace_lumps(oldLumps);

if (targetLumps & FL_TEXTURES)
refresh(renderer);

auto mdls = getDiffModels(newLumps, oldLumps);
auto faces = getDiffFaces(newLumps, oldLumps);
for (auto& face : faces)
{
if (renderer)
if (std::find(mdls.begin(), mdls.end(), map->get_model_from_face(face)) == mdls.end())
{
renderer->reuploadTextures();
mdls.push_back(map->get_model_from_face(face));
}
}

if (targetLumps & FL_LIGHTING)
/*if (mdls.size() > 5)
{
if (renderer)
{
renderer->loadLightmaps();
}
renderer->genRenderFaces();
}

if (targetLumps & FL_VERTICES || targetLumps & FL_TEXTURES)
else
{*/
if (mdls.size() && !(targetLumps & FL_LIGHTING))
{
if (renderer)
{
renderer->preRenderFaces();
}
renderer->loadLightmaps();
}


auto mdls = getDiffModels(oldLumps, newLumps);
for (auto& mdl : mdls)
{
renderer->refreshModel(mdl);
renderer->refreshModel(mdl, false);
}
//}

pickCount++;
vertPickCount++;


for (int i = 0; i < HEADER_LUMPS; i++)
{
if (oldLumps.lumps[i].size())
Expand All @@ -195,6 +211,30 @@ void EditBspCommand::undo()
}
}

void EditBspCommand::refresh(BspRenderer* renderer)
{
if (renderer)
{
if (targetLumps & FL_TEXTURES)
{
renderer->reuploadTextures();
}

if (targetLumps & FL_LIGHTING)
{
renderer->loadLightmaps();
}

if (targetLumps & FL_VERTICES || targetLumps & FL_TEXTURES)
{
renderer->preRenderFaces();
}
}

pickCount++;
vertPickCount++;
}

size_t EditBspCommand::memoryUsageZip()
{
size_t size = sizeof(EditBspCommand);
Expand Down
1 change: 1 addition & 0 deletions src/editor/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ class EditBspCommand
LumpState oldLumps;
LumpState newLumps;
private:
void refresh(BspRenderer* renderer);
unsigned int targetLumps;
};
Loading

0 comments on commit ef64a23

Please sign in to comment.