From 671bacf30cacd175a0ffc7b7fba8d7204cba4b4d Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Mon, 23 Aug 2021 02:02:57 -0600 Subject: [PATCH] qbsp: refactor clipnode writing (changes .bsp output) - drop reshuffling feature from ExportClipNodes As far as I can tell, the only purpose of this was to keep clipnodes for a given model contiguous within the lump (i.e. keep the different hulls contiguous). Vanilla qbsp didn't appear to have done this, and the code was unmaintainable/complex, so I'm dropping the feature. --- include/qbsp/map.hh | 1 + qbsp/bspfile.cc | 4 +- qbsp/writebsp.cc | 91 ++++++--------------------------------------- 3 files changed, 15 insertions(+), 81 deletions(-) diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index be3a91f3..5ccdf4a6 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -159,6 +159,7 @@ typedef struct mapdata_s { std::vector exported_leafs_bsp29; // FIXME: change to generic leaf std::vector exported_nodes_bsp29; // FIXME: change to generic node std::vector exported_marksurfaces; // FIXME: change type to generic + std::vector exported_clipnodes; // helpers std::string texinfoTextureName(int texinfo) const { diff --git a/qbsp/bspfile.cc b/qbsp/bspfile.cc index 66727994..4eeaffcd 100644 --- a/qbsp/bspfile.cc +++ b/qbsp/bspfile.cc @@ -299,7 +299,7 @@ WriteBSPFile(void) AddLumpFromBuffer(f, LUMP_NODES, map.exported_nodes_bsp29.data(), map.exported_nodes_bsp29.size() * sizeof(map.exported_nodes_bsp29[0])); AddLumpFromBuffer(f, LUMP_TEXINFO, map.exported_texinfos.data(), map.exported_texinfos.size() * sizeof(map.exported_texinfos[0])); AddLump(f, LUMP_FACES); - AddLump(f, LUMP_CLIPNODES); + AddLumpFromBuffer(f, LUMP_CLIPNODES, map.exported_clipnodes.data(), map.exported_clipnodes.size() * sizeof(map.exported_clipnodes[0])); AddLumpFromBuffer(f, LUMP_MARKSURFACES, map.exported_marksurfaces.data(), map.exported_marksurfaces.size() * sizeof(map.exported_marksurfaces[0])); AddLump(f, LUMP_SURFEDGES); AddLump(f, LUMP_EDGES); @@ -386,7 +386,7 @@ PrintBSPFileSizes(void) Message(msgStat, "%8d nodes %10d", static_cast(map.exported_nodes_bsp29.size()), static_cast(map.exported_nodes_bsp29.size()) * MemSize[BSP_NODE]); Message(msgStat, "%8d texinfo %10d", static_cast(map.exported_texinfos.size()), static_cast(map.exported_texinfos.size()) * MemSize[BSP_TEXINFO]); Message(msgStat, "%8d faces %10d", map.cTotal[LUMP_FACES], map.cTotal[LUMP_FACES] * MemSize[BSP_FACE]); - Message(msgStat, "%8d clipnodes %10d", map.cTotal[LUMP_CLIPNODES], map.cTotal[LUMP_CLIPNODES] * MemSize[BSP_CLIPNODE]); + Message(msgStat, "%8d clipnodes %10d", static_cast(map.exported_clipnodes.size()), static_cast(map.exported_clipnodes.size()) * MemSize[BSP_CLIPNODE]); Message(msgStat, "%8d leafs %10d", static_cast(map.exported_leafs_bsp29.size()), static_cast(map.exported_leafs_bsp29.size()) * MemSize[BSP_LEAF]); Message(msgStat, "%8d marksurfaces %10d", static_cast(map.exported_marksurfaces.size()), static_cast(map.exported_marksurfaces.size()) * MemSize[BSP_MARKSURF]); Message(msgStat, "%8d surfedges %10d", map.cTotal[LUMP_SURFEDGES], map.cTotal[LUMP_SURFEDGES] * MemSize[BSP_SURFEDGE]); diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index c1ce0d72..598dc1c1 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -114,24 +114,6 @@ ExportMapTexinfo(int texinfonum) //=========================================================================== - -/* -================== -CountClipNodes_r -================== -*/ -static void -CountClipNodes_r(mapentity_t *entity, node_t *node) -{ - if (node->planenum == -1) - return; - - entity->lumps[LUMP_CLIPNODES].count++; - - CountClipNodes_r(entity, node->children[0]); - CountClipNodes_r(entity, node->children[1]); -} - /* ================== ExportClipNodes @@ -140,10 +122,8 @@ ExportClipNodes static int ExportClipNodes_BSP29(mapentity_t *entity, node_t *node) { - int nodenum; bsp29_dclipnode_t *clipnode; face_t *face, *next; - struct lumpdata *clipnodes = &entity->lumps[LUMP_CLIPNODES]; // FIXME: free more stuff? if (node->planenum == -1) { @@ -153,14 +133,17 @@ ExportClipNodes_BSP29(mapentity_t *entity, node_t *node) } /* emit a clipnode */ - clipnode = (bsp29_dclipnode_t *)clipnodes->data + clipnodes->index; - clipnodes->index++; - nodenum = map.cTotal[LUMP_CLIPNODES]; - map.cTotal[LUMP_CLIPNODES]++; + const int nodenum = static_cast(map.exported_clipnodes.size()); + map.exported_clipnodes.push_back({}); + const int child0 = ExportClipNodes_BSP29(entity, node->children[0]); + const int child1 = ExportClipNodes_BSP29(entity, node->children[1]); + + // Careful not to modify the vector while using this clipnode pointer + clipnode = &map.exported_clipnodes.at(nodenum); clipnode->planenum = ExportMapPlane(node->planenum); - clipnode->children[0] = ExportClipNodes_BSP29(entity, node->children[0]); - clipnode->children[1] = ExportClipNodes_BSP29(entity, node->children[1]); + clipnode->children[0] = child0; + clipnode->children[1] = child1; for (face = node->faces; face; face = next) { next = face->next; @@ -172,6 +155,7 @@ ExportClipNodes_BSP29(mapentity_t *entity, node_t *node) return nodenum; } +#if 0 static int ExportClipNodes_BSP2(mapentity_t *entity, node_t *node) { @@ -206,6 +190,7 @@ ExportClipNodes_BSP2(mapentity_t *entity, node_t *node) return nodenum; } +#endif /* ================== @@ -222,61 +207,9 @@ accomodate new data interleaved with old. void ExportClipNodes(mapentity_t *entity, node_t *nodes, const int hullnum) { - int oldcount, i, diff; - int clipcount = 0; - void *olddata; - struct lumpdata *clipnodes = &entity->lumps[LUMP_CLIPNODES]; dmodel_t *model = (dmodel_t *)entity->lumps[LUMP_MODELS].data; - oldcount = clipnodes->count; - - /* Count nodes before this one */ - const int entnum = entity - &map.entities.at(0); - for (i = 0; i < entnum; i++) - clipcount += map.entities.at(i).lumps[LUMP_CLIPNODES].count; - model->headnode[hullnum] = clipcount + oldcount; - - CountClipNodes_r(entity, nodes); - if (clipnodes->count > MAX_BSP_CLIPNODES && (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION)) - Error("Clipnode count exceeds bsp 29 max (%d > %d)", - clipnodes->count, MAX_BSP_CLIPNODES); - - olddata = clipnodes->data; - clipnodes->data = AllocMem(BSP_CLIPNODE, clipnodes->count, true); - if (olddata) { - memcpy(clipnodes->data, olddata, oldcount * MemSize[BSP_CLIPNODE]); - FreeMem(olddata, BSP_CLIPNODE, oldcount); - - /* Worth special-casing for entity 0 (no modification needed) */ - diff = clipcount - model->headnode[1]; - if (diff != 0) { - for (i = 1; i < hullnum; i++) - model->headnode[i] += diff; - if (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION) { - bsp29_dclipnode_t *clipnode = (bsp29_dclipnode_t *)clipnodes->data; - for (i = 0; i < oldcount; i++, clipnode++) { - if (clipnode->children[0] < MAX_BSP_CLIPNODES) - clipnode->children[0] += diff; - if (clipnode->children[1] < MAX_BSP_CLIPNODES) - clipnode->children[1] += diff; - } - } else { - bsp2_dclipnode_t *clipnode = (bsp2_dclipnode_t *)clipnodes->data; - for (i = 0; i < oldcount; i++, clipnode++) { - if (clipnode->children[0] >= 0) - clipnode->children[0] += diff; - if (clipnode->children[1] >= 0) - clipnode->children[1] += diff; - } - } - } - } - - map.cTotal[LUMP_CLIPNODES] = clipcount + oldcount; - if (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION) - ExportClipNodes_BSP29(entity, nodes); - else - ExportClipNodes_BSP2(entity, nodes); + model->headnode[hullnum] = ExportClipNodes_BSP29(entity, nodes); } //===========================================================================