Skip to content

Commit

Permalink
fix faces at load
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Aug 24, 2024
1 parent 1f5d13b commit 23e7cee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 332 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ vs-project/fmt/fmt/Debug/fmtd.idb
vs-project/glfw/src/Debug/glfw3.idb
*.ent
!demo/*.bsp
vs-project/.vs/bspguy/v17/DocumentLayout.json
/vs-project/.vs
18 changes: 18 additions & 0 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ Bsp::Bsp(std::string fpath)
world->setOrAddKeyvalue("_editor", g_version_string);
}

for (int i = 0; i < faceCount; i++)
{
BSPFACE32& face = faces[i];
BSPTEXTUREINFO& info = texinfos[face.iTextureInfo];
if (info.nFlags & TEX_SPECIAL)
{
continue;
}
int bmins[2];
int bmaxs[2];
if (!GetFaceExtents(i, bmins, bmaxs))
{
info.nFlags |= TEX_SPECIAL;
}
}

save_undo_lightmaps();
}

Expand Down Expand Up @@ -1849,6 +1865,8 @@ unsigned int Bsp::remove_unused_visdata(BSPLEAF32* oldLeaves, int oldLeafCount,
int decompressedVisSize = oldLeafCount * oldVisRowSize;
unsigned char* decompressedVis = new unsigned char[decompressedVisSize];
memset(decompressedVis, 0xFF, decompressedVisSize);


decompress_vis_lump(this, oldLeaves, lumps[LUMP_VISIBILITY], decompressedVis,
oldWorldLeaves, oldVisLeafCount - 1, oldVisLeafCount - 1, oldLeavesMemSize, bsp_header.lump[LUMP_VISIBILITY].nLength);

Expand Down
25 changes: 18 additions & 7 deletions src/qtools/vis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ void decompress_vis_lump(Bsp* map, BSPLEAF32* leafLump, unsigned char* visLump,
}
// Tracing ...
// print_log(get_localized_string(LANG_0996),leafLump[i].nVisOffset,visLumpMemSize);
DecompressVis((unsigned char*)(visLump + leafLump[i + 1].nVisOffset), dest, oldVisRowSize, visDataLeafCount, visLumpMemSize - leafLump[i + 1].nVisOffset);
if (!DecompressVis((unsigned char*)(visLump + leafLump[i + 1].nVisOffset), dest, oldVisRowSize, visDataLeafCount,
visLumpMemSize - leafLump[i + 1].nVisOffset))
{
//print_log("Error {} - {}\n", i, iterationLeaves);
}

// Leaf visibility row lengths are multiples of 64 leaves, so there are usually some unused bits at the end.
// Maps sometimes set those unused bits randomly (e.g. leaf index 100 is marked visible, but there are only 90 leaves...)
Expand Down Expand Up @@ -253,7 +257,7 @@ void decompress_vis_lump(Bsp* map, BSPLEAF32* leafLump, unsigned char* visLump,
// BEGIN COPIED QVIS CODE
//

void DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_length, unsigned int numLeaves, unsigned int src_length)
bool DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_length, unsigned int numLeaves, unsigned int src_length)
{
unsigned char* startsrc = src;
unsigned char* startdst = dest;
Expand All @@ -262,7 +266,9 @@ void DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_le
unsigned char* out;
int row;

row = (numLeaves + 7) >> 3; // same as the length used by VIS program in CompressVis
row = (numLeaves + 7) >> 3;

// same as the length used by VIS program in CompressVis
// The wrong size will cause DecompressVis to spend extremely long time once the source pointer runs into the invalid area in g_dvisdata (for example, in BuildFaceLights, some faces could hang for a few seconds), and sometimes to crash.

out = dest;
Expand All @@ -274,13 +280,15 @@ void DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_le
if (out > startdst + dest_length)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0998), (int)(out - startdst), dest_length);
return;
return false;
}

if (src > startsrc + src_length)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_0999), (int)(src - startsrc), src_length);
return;
return false;
}

*out = *src;
out++;
src++;
Expand All @@ -294,18 +302,21 @@ void DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_le
if (out > startdst + dest_length)
{
print_log(PRINT_RED | PRINT_INTENSITY, get_localized_string(LANG_1142), (int)(out - startdst), dest_length);
return;
return false;
}

*out = 0;
out++;
c--;

if (out - dest >= row)
{
return;
return true;
}
}
} while (out - dest < row);

return true;
}

int CompressVis(unsigned char* src, unsigned int src_length, unsigned char* dest, unsigned int dest_length)
Expand Down
2 changes: 1 addition & 1 deletion src/qtools/vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int shiftVis(unsigned char* vis, int len, int offsetLeaf, int shift);
void decompress_vis_lump(Bsp * map,BSPLEAF32* leafLump, unsigned char* visLump, unsigned char* output,
int iterationLeaves, int visDataLeafCount, int newNumLeaves, int leafMemSize, int visLumpMemSize);

void DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_length, unsigned int numLeaves, unsigned int src_length);
bool DecompressVis(unsigned char* src, unsigned char* dest, unsigned int dest_length, unsigned int numLeaves, unsigned int src_length);

int CompressVis(unsigned char* src, unsigned int src_length, unsigned char* dest, unsigned int dest_length);

Expand Down
Loading

0 comments on commit 23e7cee

Please sign in to comment.