From f59bc08db8aa7b5980d1a89fba40df446396e91c Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Mon, 23 Aug 2021 20:16:40 -0600 Subject: [PATCH] qbsp: refactor face writing --- include/qbsp/map.hh | 6 ++++- qbsp/bspfile.cc | 4 +-- qbsp/surfaces.cc | 61 ++++++++++++++++++++------------------------- qbsp/writebsp.cc | 2 +- 4 files changed, 35 insertions(+), 38 deletions(-) diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index 8662de4c..244cbd0c 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -103,6 +103,8 @@ public: int numbrushes; struct lumpdata lumps[BSPX_LUMPS]; + int firstoutputfacenumber; + const mapbrush_t &mapbrush(int i) const; mapentity_t() : @@ -116,7 +118,8 @@ public: liquid(nullptr), epairs(nullptr), brushes(nullptr), - numbrushes(0) { + numbrushes(0), + firstoutputfacenumber(-1) { VectorSet(origin,0,0,0); VectorSet(mins,0,0,0); VectorSet(maxs,0,0,0); @@ -163,6 +166,7 @@ typedef struct mapdata_s { std::vector exported_edges; std::vector exported_vertexes; std::vector exported_surfedges; + std::vector exported_faces; // helpers std::string texinfoTextureName(int texinfo) const { diff --git a/qbsp/bspfile.cc b/qbsp/bspfile.cc index 1e38e2c2..315f3cdb 100644 --- a/qbsp/bspfile.cc +++ b/qbsp/bspfile.cc @@ -298,7 +298,7 @@ WriteBSPFile(void) AddLumpFromBuffer(f, LUMP_VERTEXES, map.exported_vertexes.data(), map.exported_vertexes.size() * sizeof(map.exported_vertexes[0])); 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); + AddLumpFromBuffer(f, LUMP_FACES, map.exported_faces.data(), map.exported_faces.size() * sizeof(map.exported_faces[0])); 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])); AddLumpFromBuffer(f, LUMP_SURFEDGES, map.exported_surfedges.data(), map.exported_surfedges.size() * sizeof(map.exported_surfedges[0])); @@ -385,7 +385,7 @@ PrintBSPFileSizes(void) Message(msgStat, "%8d vertexes %10d", static_cast(map.exported_vertexes.size()), static_cast(map.exported_vertexes.size()) * MemSize[BSP_VERTEX]); 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 faces %10d", static_cast(map.exported_faces.size()), static_cast(map.exported_faces.size()) * MemSize[BSP_FACE]); 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]); diff --git a/qbsp/surfaces.cc b/qbsp/surfaces.cc index 711adeaf..3127f194 100644 --- a/qbsp/surfaces.cc +++ b/qbsp/surfaces.cc @@ -388,13 +388,11 @@ MakeFaceEdges_r(mapentity_t *entity, node_t *node, int progress) EmitFace ============== */ -template static void -EmitFace_Internal(mapentity_t *entity, face_t *face) +EmitFace(mapentity_t *entity, face_t *face) { - struct lumpdata *faces = &entity->lumps[LUMP_FACES]; struct lumpdata *lmshifts = &entity->lumps[BSPX_LMSHIFT]; - DFACE *out; + bsp29_dface_t *out; int i; if (map.mtexinfos.at(face->texinfo).flags & (TEX_SKIP | TEX_HINT)) @@ -402,10 +400,15 @@ EmitFace_Internal(mapentity_t *entity, face_t *face) // emit a region Q_assert(face->outputnumber == -1); - face->outputnumber = map.cTotal[LUMP_FACES]; - if (lmshifts->data) - ((unsigned char*)lmshifts->data)[faces->index] = face->lmshift[1]; - out = (DFACE *)faces->data + faces->index; + face->outputnumber = static_cast(map.exported_faces.size()); + map.exported_faces.push_back({}); + + if (lmshifts->data) { + const int localfacenumber = face->outputnumber - entity->firstoutputfacenumber; + ((unsigned char*)lmshifts->data)[localfacenumber] = face->lmshift[1]; + } + + out = &map.exported_faces.at(face->outputnumber); out->planenum = ExportMapPlane(face->planenum); out->side = face->planeside; out->texinfo = ExportMapTexinfo(face->texinfo); @@ -421,18 +424,6 @@ EmitFace_Internal(mapentity_t *entity, face_t *face) FreeMem(face->edges, OTHER, face->w.numpoints * sizeof(int)); out->numedges = static_cast(map.exported_surfedges.size()) - out->firstedge; - - map.cTotal[LUMP_FACES]++; - faces->index++; -} - -static void -EmitFace(mapentity_t *entity, face_t *face) -{ - if (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION) - EmitFace_Internal(entity, face); - else - EmitFace_Internal(entity, face); } /* @@ -446,7 +437,7 @@ GrowNodeRegion(mapentity_t *entity, node_t *node) if (node->planenum == PLANENUM_LEAF) return; - node->firstface = map.cTotal[LUMP_FACES]; + node->firstface = static_cast(map.exported_faces.size()); for (face_t *face = node->faces; face; face = face->next) { Q_assert(face->planenum == node->planenum); @@ -455,21 +446,22 @@ GrowNodeRegion(mapentity_t *entity, node_t *node) EmitFace(entity, face); } - node->numfaces = map.cTotal[LUMP_FACES] - node->firstface; + node->numfaces = static_cast(map.exported_faces.size()) - node->firstface; GrowNodeRegion(entity, node->children[0]); GrowNodeRegion(entity, node->children[1]); } static void -CountFace(mapentity_t *entity, face_t *f, int* vertexesCount) +CountFace(mapentity_t *entity, face_t *f, int *facesCount, int *vertexesCount) { if (map.mtexinfos.at(f->texinfo).flags & (TEX_SKIP | TEX_HINT)) return; if (f->lmshift[1] != 4) needlmshifts = true; - entity->lumps[LUMP_FACES].count++; + + (*facesCount)++; (*vertexesCount) += f->w.numpoints; } @@ -479,7 +471,7 @@ CountData_r ============== */ static void -CountData_r(mapentity_t *entity, node_t *node, int* vertexesCount) +CountData_r(mapentity_t *entity, node_t *node, int *facesCount, int *vertexesCount) { face_t *f; @@ -487,11 +479,11 @@ CountData_r(mapentity_t *entity, node_t *node, int* vertexesCount) return; for (f = node->faces; f; f = f->next) { - CountFace(entity, f, vertexesCount); + CountFace(entity, f, facesCount, vertexesCount); } - CountData_r(entity, node->children[0], vertexesCount); - CountData_r(entity, node->children[1], vertexesCount); + CountData_r(entity, node->children[0], facesCount, vertexesCount); + CountData_r(entity, node->children[1], facesCount, vertexesCount); } /* @@ -503,28 +495,29 @@ int MakeFaceEdges(mapentity_t *entity, node_t *headnode) { int i, firstface; - struct lumpdata *faces = &entity->lumps[LUMP_FACES]; struct lumpdata *lmshifts = &entity->lumps[BSPX_LMSHIFT]; Message(msgProgress, "MakeFaceEdges"); needlmshifts = false; + Q_assert(entity->firstoutputfacenumber == -1); + entity->firstoutputfacenumber = static_cast(map.exported_faces.size()); + + int facesCount = 0; int vertexesCount = 0; - CountData_r(entity, headnode, &vertexesCount); + CountData_r(entity, headnode, &facesCount, &vertexesCount); // Accessory data InitHash(); - firstface = map.cTotal[LUMP_FACES]; + firstface = static_cast(map.exported_faces.size()); MakeFaceEdges_r(entity, headnode, 0); pEdgeFaces0.clear(); pEdgeFaces1.clear(); - faces->data = AllocMem(BSP_FACE, faces->count, true); - - lmshifts->count = needlmshifts?faces->count:0; + lmshifts->count = needlmshifts?facesCount:0; lmshifts->data = needlmshifts?AllocMem(OTHER, sizeof(uint8_t) * lmshifts->count, true):NULL; Message(msgProgress, "GrowRegions"); diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index 8dfe43bc..e33e4a2b 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -342,7 +342,7 @@ ExportDrawNodes(mapentity_t *entity, node_t *headnode, int firstface) dmodel = (dmodel_t *)entity->lumps[LUMP_MODELS].data; dmodel->headnode[0] = static_cast(map.exported_nodes_bsp29.size()); dmodel->firstface = firstface; - dmodel->numfaces = map.cTotal[LUMP_FACES] - firstface; + dmodel->numfaces = static_cast(map.exported_faces.size()) - firstface; const size_t mapleafsAtStart = map.exported_leafs_bsp29.size();