diff --git a/common/bsputils.cc b/common/bsputils.cc index cd837285..40a6d6bc 100644 --- a/common/bsputils.cc +++ b/common/bsputils.cc @@ -719,6 +719,25 @@ std::unordered_map> DecompressAllVis(const mbsp_t *bsp return result; } +static void BSP_VisitAllLeafs_R( + const mbsp_t &bsp, const int nodenum, const std::function &visitor) +{ + if (nodenum < 0) { + auto *leaf = BSP_GetLeafFromNodeNum(&bsp, nodenum); + visitor(*leaf); + return; + } + + const bsp2_dnode_t &node = bsp.dnodes.at(nodenum); + BSP_VisitAllLeafs_R(bsp, node.children[0], visitor); + BSP_VisitAllLeafs_R(bsp, node.children[1], visitor); +} + +void BSP_VisitAllLeafs(const mbsp_t &bsp, const dmodelh2_t &model, const std::function &visitor) +{ + BSP_VisitAllLeafs_R(bsp, model.headnode[0], visitor); +} + bspx_decoupled_lm_perface BSPX_DecoupledLM(const bspxentries_t &entries, int face_num) { auto &lump_bytes = entries.at("DECOUPLED_LM"); diff --git a/include/common/bsputils.hh b/include/common/bsputils.hh index d7a4ef23..47c78e99 100644 --- a/include/common/bsputils.hh +++ b/include/common/bsputils.hh @@ -116,6 +116,8 @@ int LeafnumToVisleaf(int leafnum); void DecompressVis(const uint8_t *in, const uint8_t *inend, uint8_t *out, uint8_t *outend); std::unordered_map> DecompressAllVis(const mbsp_t *bsp, bool trans_water = false); +void BSP_VisitAllLeafs(const mbsp_t &bsp, const dmodelh2_t &model, const std::function &visitor); + bspx_decoupled_lm_perface BSPX_DecoupledLM(const bspxentries_t &entries, int face_num); std::optional BSPX_FaceNormals(const mbsp_t &bsp, const bspxentries_t &entries); diff --git a/lightpreview/glview.cpp b/lightpreview/glview.cpp index e42c37cb..4dba8399 100644 --- a/lightpreview/glview.cpp +++ b/lightpreview/glview.cpp @@ -396,16 +396,22 @@ void GLView::updateFaceVisibility() std::vector face_flags; face_flags.resize(face_visibility_width, 0); - // check all leafs - // FIXME: only world? - - for (auto &leaf : bsp.dleafs) { + // visit all world leafs: if they're visible, mark the appropriate faces + BSP_VisitAllLeafs(bsp, bsp.dmodels[0], [&](const mleaf_t &leaf) { if (leaf_sees(&leaf)) { for (int ms = 0; ms < leaf.nummarksurfaces; ++ms) { int fnum = bsp.dleaffaces[leaf.firstmarksurface + ms]; face_flags[fnum] = 16; } } + }); + + // set all bmodel faces to visible + for (int mi = 1; mi < bsp.dmodels.size(); ++mi) { + auto &model = bsp.dmodels[mi]; + for (int fi = model.firstface; fi < (model.firstface + model.numfaces); ++fi) { + face_flags[fi] = 16; + } } setFaceVisibilityArray(face_flags.data());