diff --git a/include/qbsp/csg4.hh b/include/qbsp/csg4.hh index 7bab245e..d1d3d3cd 100644 --- a/include/qbsp/csg4.hh +++ b/include/qbsp/csg4.hh @@ -33,6 +33,7 @@ struct face_t; int MakeSkipTexinfo(); face_t *NewFaceFromFace(const face_t *in); +face_t *CopyFace(const face_t *in); face_t *MirrorFace(const face_t *face); std::tuple SplitFace(face_t *in, const qplane3d &split); void UpdateFaceSphere(face_t *in); diff --git a/qbsp/csg4.cc b/qbsp/csg4.cc index b3a0933f..b22e6606 100644 --- a/qbsp/csg4.cc +++ b/qbsp/csg4.cc @@ -86,6 +86,13 @@ face_t *NewFaceFromFace(const face_t *in) return newf; } +face_t* CopyFace(const face_t* in) +{ + face_t *temp = NewFaceFromFace(in); + temp->w = in->w; + return temp; +} + void UpdateFaceSphere(face_t *in) { in->origin = in->w.center(); diff --git a/qbsp/surfaces.cc b/qbsp/surfaces.cc index 84cbb1a2..9ca28ade 100644 --- a/qbsp/surfaces.cc +++ b/qbsp/surfaces.cc @@ -499,19 +499,25 @@ AddMarksurfaces_r Adds the given face to the markfaces lists of all descendant leafs of `node`. -fixme-brushbsp: do this accurately, not exhaustively fixme-brushbsp: all leafs in a cluster can share the same marksurfaces, right? ================ */ -static void AddMarksurfaces_r(face_t *face, node_t *node) +static void AddMarksurfaces_r(face_t *face, face_t *face_copy, node_t *node) { if (node->planenum == PLANENUM_LEAF) { node->markfaces.push_back(face); return; } - AddMarksurfaces_r(face, node->children[0]); - AddMarksurfaces_r(face, node->children[1]); + const qbsp_plane_t &splitplane = map.planes[node->planenum]; + + auto [frontFragment, backFragment] = SplitFace(face_copy, splitplane); + if (frontFragment) { + AddMarksurfaces_r(face, frontFragment, node->children[0]); + } + if (backFragment) { + AddMarksurfaces_r(face, backFragment, node->children[1]); + } } /* @@ -529,8 +535,16 @@ void MakeMarkFaces(mapentity_t* entity, node_t* node) // for the faces on this splitting node.. for (face_t *face : node->facelist) { - // add this face to all descendant leafs (temporary hack) - AddMarksurfaces_r(face, node); + // add this face to all descendant leafs it touches + + // make a copy we can clip + face_t *face_copy = CopyFace(face); + + if (face->planeside == 0) { + AddMarksurfaces_r(face, face_copy, node->children[0]); + } else { + AddMarksurfaces_r(face, face_copy, node->children[1]); + } } // process child nodes recursively @@ -692,8 +706,7 @@ void MakeVisibleFaces(mapentity_t* entity, node_t* headnode) if (!face.visible) { continue; } - face_t *temp = NewFaceFromFace(&face); - temp->w = face.w; + face_t *temp = CopyFace(&face); AddFaceToTree_r(entity, temp, &brush, headnode); } diff --git a/qbsp/test_qbsp.cc b/qbsp/test_qbsp.cc index 47c7ad70..52a3eca2 100644 --- a/qbsp/test_qbsp.cc +++ b/qbsp/test_qbsp.cc @@ -347,7 +347,7 @@ TEST(testmaps_q1, simple_sealed2) const qvec3d player_pos{-56, -96, 120}; const qvec3d other_empty_leaf_pos{-71, -288, 102}; auto *player_leaf = BSP_FindLeafAtPoint(&bsp, &bsp.dmodels[0], player_pos); - auto *other_leaf = BSP_FindLeafAtPoint(&bsp, &bsp.dmodels[0], player_pos); + auto *other_leaf = BSP_FindLeafAtPoint(&bsp, &bsp.dmodels[0], other_empty_leaf_pos); auto player_markfaces = Leaf_Markfaces(&bsp, player_leaf); auto other_markfaces = Leaf_Markfaces(&bsp, other_leaf);