qbsp: refactor vertexes writing

This commit is contained in:
Eric Wasylishen 2021-08-23 19:45:31 -06:00
parent f4700f978d
commit 8147b97dc6
3 changed files with 15 additions and 32 deletions

View File

@ -161,6 +161,7 @@ typedef struct mapdata_s {
std::vector<uint16_t> exported_marksurfaces; // FIXME: change type to generic std::vector<uint16_t> exported_marksurfaces; // FIXME: change type to generic
std::vector<bsp29_dclipnode_t> exported_clipnodes; std::vector<bsp29_dclipnode_t> exported_clipnodes;
std::vector<bsp29_dedge_t> exported_edges; std::vector<bsp29_dedge_t> exported_edges;
std::vector<dvertex_t> exported_vertexes;
// helpers // helpers
std::string texinfoTextureName(int texinfo) const { std::string texinfoTextureName(int texinfo) const {

View File

@ -295,7 +295,7 @@ WriteBSPFile(void)
AddLumpFromBuffer(f, LUMP_PLANES, map.exported_planes.data(), map.exported_planes.size() * sizeof(map.exported_planes[0])); AddLumpFromBuffer(f, LUMP_PLANES, map.exported_planes.data(), map.exported_planes.size() * sizeof(map.exported_planes[0]));
AddLumpFromBuffer(f, LUMP_LEAFS, map.exported_leafs_bsp29.data(), map.exported_leafs_bsp29.size() * sizeof(map.exported_leafs_bsp29[0])); AddLumpFromBuffer(f, LUMP_LEAFS, map.exported_leafs_bsp29.data(), map.exported_leafs_bsp29.size() * sizeof(map.exported_leafs_bsp29[0]));
AddLump(f, LUMP_VERTEXES); 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_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])); AddLumpFromBuffer(f, LUMP_TEXINFO, map.exported_texinfos.data(), map.exported_texinfos.size() * sizeof(map.exported_texinfos[0]));
AddLump(f, LUMP_FACES); AddLump(f, LUMP_FACES);
@ -382,7 +382,7 @@ PrintBSPFileSizes(void)
struct lumpdata *lump; struct lumpdata *lump;
Message(msgStat, "%8d planes %10d", static_cast<int>(map.exported_planes.size()), static_cast<int>(map.exported_planes.size()) * MemSize[BSP_PLANE]); Message(msgStat, "%8d planes %10d", static_cast<int>(map.exported_planes.size()), static_cast<int>(map.exported_planes.size()) * MemSize[BSP_PLANE]);
Message(msgStat, "%8d vertexes %10d", map.cTotal[LUMP_VERTEXES], map.cTotal[LUMP_VERTEXES] * MemSize[BSP_VERTEX]); Message(msgStat, "%8d vertexes %10d", static_cast<int>(map.exported_vertexes.size()), static_cast<int>(map.exported_vertexes.size()) * MemSize[BSP_VERTEX]);
Message(msgStat, "%8d nodes %10d", static_cast<int>(map.exported_nodes_bsp29.size()), static_cast<int>(map.exported_nodes_bsp29.size()) * MemSize[BSP_NODE]); Message(msgStat, "%8d nodes %10d", static_cast<int>(map.exported_nodes_bsp29.size()), static_cast<int>(map.exported_nodes_bsp29.size()) * MemSize[BSP_NODE]);
Message(msgStat, "%8d texinfo %10d", static_cast<int>(map.exported_texinfos.size()), static_cast<int>(map.exported_texinfos.size()) * MemSize[BSP_TEXINFO]); Message(msgStat, "%8d texinfo %10d", static_cast<int>(map.exported_texinfos.size()), static_cast<int>(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", map.cTotal[LUMP_FACES], map.cTotal[LUMP_FACES] * MemSize[BSP_FACE]);

View File

@ -239,7 +239,6 @@ GetVertex(mapentity_t *entity, const vec3_t in)
{ {
int i; int i;
vec3_t vert; vec3_t vert;
struct lumpdata *vertices = &entity->lumps[LUMP_VERTEXES];
dvertex_t *dvertex; dvertex_t *dvertex;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
@ -262,19 +261,16 @@ GetVertex(mapentity_t *entity, const vec3_t in)
} }
} }
const int global_vert_num = map.cTotal[LUMP_VERTEXES]++; const int global_vert_num = static_cast<int>(map.exported_vertexes.size());
AddHashVert(vert, global_vert_num); AddHashVert(vert, global_vert_num);
if (vertices->index == vertices->count)
Error("Internal error: didn't allocate enough vertices?");
/* emit a vertex */ /* emit a vertex */
dvertex = (dvertex_t *)vertices->data + vertices->index; map.exported_vertexes.push_back({});
dvertex = &map.exported_vertexes.at(global_vert_num);
dvertex->point[0] = vert[0]; dvertex->point[0] = vert[0];
dvertex->point[1] = vert[1]; dvertex->point[1] = vert[1];
dvertex->point[2] = vert[2]; dvertex->point[2] = vert[2];
vertices->index++;
return global_vert_num; return global_vert_num;
} }
@ -494,7 +490,7 @@ GrowNodeRegion(mapentity_t *entity, node_t *node)
} }
static void static void
CountFace(mapentity_t *entity, face_t *f) CountFace(mapentity_t *entity, face_t *f, int* vertexesCount)
{ {
if (map.mtexinfos.at(f->texinfo).flags & (TEX_SKIP | TEX_HINT)) if (map.mtexinfos.at(f->texinfo).flags & (TEX_SKIP | TEX_HINT))
return; return;
@ -502,7 +498,7 @@ CountFace(mapentity_t *entity, face_t *f)
if (f->lmshift[1] != 4) if (f->lmshift[1] != 4)
needlmshifts = true; needlmshifts = true;
entity->lumps[LUMP_FACES].count++; entity->lumps[LUMP_FACES].count++;
entity->lumps[LUMP_VERTEXES].count += f->w.numpoints; (*vertexesCount) += f->w.numpoints;
} }
/* /*
@ -511,7 +507,7 @@ CountData_r
============== ==============
*/ */
static void static void
CountData_r(mapentity_t *entity, node_t *node) CountData_r(mapentity_t *entity, node_t *node, int* vertexesCount)
{ {
face_t *f; face_t *f;
@ -519,11 +515,11 @@ CountData_r(mapentity_t *entity, node_t *node)
return; return;
for (f = node->faces; f; f = f->next) { for (f = node->faces; f; f = f->next) {
CountFace(entity, f); CountFace(entity, f, vertexesCount);
} }
CountData_r(entity, node->children[0]); CountData_r(entity, node->children[0], vertexesCount);
CountData_r(entity, node->children[1]); CountData_r(entity, node->children[1], vertexesCount);
} }
/* /*
@ -536,7 +532,6 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
{ {
int i, firstface; int i, firstface;
struct lumpdata *surfedges = &entity->lumps[LUMP_SURFEDGES]; struct lumpdata *surfedges = &entity->lumps[LUMP_SURFEDGES];
struct lumpdata *vertices = &entity->lumps[LUMP_VERTEXES];
struct lumpdata *faces = &entity->lumps[LUMP_FACES]; struct lumpdata *faces = &entity->lumps[LUMP_FACES];
struct lumpdata *lmshifts = &entity->lumps[BSPX_LMSHIFT]; struct lumpdata *lmshifts = &entity->lumps[BSPX_LMSHIFT];
@ -544,7 +539,8 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
needlmshifts = false; needlmshifts = false;
CountData_r(entity, headnode); int vertexesCount = 0;
CountData_r(entity, headnode, &vertexesCount);
/* /*
* Remember edges are +1 in BeginBSPFile. Often less than half * Remember edges are +1 in BeginBSPFile. Often less than half
@ -552,9 +548,7 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
* faces will break that assumption. 2/3 should be safe most of * faces will break that assumption. 2/3 should be safe most of
* the time without wasting too much memory... * the time without wasting too much memory...
*/ */
surfedges->count = vertices->count; surfedges->count = vertexesCount;
vertices->data = AllocMem(BSP_VERTEX, vertices->count, true);
// Accessory data // Accessory data
InitHash(); InitHash();
@ -565,18 +559,6 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
pEdgeFaces0.clear(); pEdgeFaces0.clear();
pEdgeFaces1.clear(); pEdgeFaces1.clear();
/* Free any excess allocated memory */
if (vertices->index < vertices->count) {
dvertex_t *temp = (dvertex_t *)AllocMem(BSP_VERTEX, vertices->index, true);
memcpy(temp, vertices->data, sizeof(*temp) * vertices->index);
FreeMem(vertices->data, BSP_VERTEX, vertices->count);
vertices->data = temp;
vertices->count = vertices->index;
}
if (map.cTotal[LUMP_VERTEXES] > 65535 && (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION))
Error("Too many vertices (%d > 65535). Recompile with the \"-bsp2\" flag to lift this restriction.", map.cTotal[LUMP_VERTEXES]);
surfedges->data = AllocMem(BSP_SURFEDGE, surfedges->count, true); surfedges->data = AllocMem(BSP_SURFEDGE, surfedges->count, true);
faces->data = AllocMem(BSP_FACE, faces->count, true); faces->data = AllocMem(BSP_FACE, faces->count, true);