qbsp: refactor edges writing

This commit is contained in:
Eric Wasylishen 2021-08-23 19:36:41 -06:00
parent 671bacf30c
commit f4700f978d
4 changed files with 24 additions and 44 deletions

View File

@ -160,6 +160,7 @@ typedef struct mapdata_s {
std::vector<bsp29_dnode_t> exported_nodes_bsp29; // FIXME: change to generic node std::vector<bsp29_dnode_t> exported_nodes_bsp29; // FIXME: change to generic node
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;
// helpers // helpers
std::string texinfoTextureName(int texinfo) const { std::string texinfoTextureName(int texinfo) const {

View File

@ -302,7 +302,7 @@ WriteBSPFile(void)
AddLumpFromBuffer(f, LUMP_CLIPNODES, map.exported_clipnodes.data(), map.exported_clipnodes.size() * sizeof(map.exported_clipnodes[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_MARKSURFACES, map.exported_marksurfaces.data(), map.exported_marksurfaces.size() * sizeof(map.exported_marksurfaces[0]));
AddLump(f, LUMP_SURFEDGES); AddLump(f, LUMP_SURFEDGES);
AddLump(f, LUMP_EDGES); AddLumpFromBuffer(f, LUMP_EDGES, map.exported_edges.data(), map.exported_edges.size() * sizeof(map.exported_edges[0]));
AddLump(f, LUMP_MODELS); AddLump(f, LUMP_MODELS);
AddLump(f, LUMP_LIGHTING); AddLump(f, LUMP_LIGHTING);
@ -390,7 +390,7 @@ PrintBSPFileSizes(void)
Message(msgStat, "%8d leafs %10d", static_cast<int>(map.exported_leafs_bsp29.size()), static_cast<int>(map.exported_leafs_bsp29.size()) * MemSize[BSP_LEAF]); Message(msgStat, "%8d leafs %10d", static_cast<int>(map.exported_leafs_bsp29.size()), static_cast<int>(map.exported_leafs_bsp29.size()) * MemSize[BSP_LEAF]);
Message(msgStat, "%8d marksurfaces %10d", static_cast<int>(map.exported_marksurfaces.size()), static_cast<int>(map.exported_marksurfaces.size()) * MemSize[BSP_MARKSURF]); Message(msgStat, "%8d marksurfaces %10d", static_cast<int>(map.exported_marksurfaces.size()), static_cast<int>(map.exported_marksurfaces.size()) * MemSize[BSP_MARKSURF]);
Message(msgStat, "%8d surfedges %10d", map.cTotal[LUMP_SURFEDGES], map.cTotal[LUMP_SURFEDGES] * MemSize[BSP_SURFEDGE]); Message(msgStat, "%8d surfedges %10d", map.cTotal[LUMP_SURFEDGES], map.cTotal[LUMP_SURFEDGES] * MemSize[BSP_SURFEDGE]);
Message(msgStat, "%8d edges %10d", map.cTotal[LUMP_EDGES], map.cTotal[LUMP_EDGES] * MemSize[BSP_EDGE]); Message(msgStat, "%8d edges %10d", static_cast<int>(map.exported_edges.size()), static_cast<int>(map.exported_edges.size()) * MemSize[BSP_EDGE]);
lump = &pWorldEnt()->lumps[LUMP_TEXTURES]; lump = &pWorldEnt()->lumps[LUMP_TEXTURES];
if (lump->data) if (lump->data)

View File

@ -173,9 +173,8 @@ GatherNodeFaces(node_t *headnode)
//=========================================================================== //===========================================================================
// This is a kludge. Should be pEdgeFaces[2]. // This is a kludge. Should be pEdgeFaces[2].
static const face_t **pEdgeFaces0; static std::map<int, const face_t *> pEdgeFaces0;
static const face_t **pEdgeFaces1; static std::map<int, const face_t *> pEdgeFaces1;
static int cStartEdge;
//============================================================================ //============================================================================
@ -187,6 +186,8 @@ static std::map<std::tuple<int,int,int>, std::list<hashvert_t>> hashverts;
static void static void
InitHash(void) InitHash(void)
{ {
pEdgeFaces0.clear();
pEdgeFaces1.clear();
hashverts.clear(); hashverts.clear();
hashedges.clear(); hashedges.clear();
} }
@ -293,7 +294,6 @@ static int
GetEdge(mapentity_t *entity, const vec3_t p1, const vec3_t p2, GetEdge(mapentity_t *entity, const vec3_t p1, const vec3_t p2,
const face_t *face) const face_t *face)
{ {
struct lumpdata *edges = &entity->lumps[LUMP_EDGES];
int v1, v2; int v1, v2;
int i; int i;
@ -306,32 +306,30 @@ GetEdge(mapentity_t *entity, const vec3_t p1, const vec3_t p2,
// search for an existing edge from v2->v1 // search for an existing edge from v2->v1
const std::pair<int,int> edge_hash_key = std::make_pair(v2, v1); const std::pair<int,int> edge_hash_key = std::make_pair(v2, v1);
if (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION) { {
bsp29_dedge_t *edge; bsp29_dedge_t *edge;
auto it = hashedges.find(edge_hash_key); auto it = hashedges.find(edge_hash_key);
if (it != hashedges.end()) { if (it != hashedges.end()) {
for (const int i : it->second) { for (const int i : it->second) {
edge = (bsp29_dedge_t *)edges->data + i; edge = &map.exported_edges.at(i);
if (!(v1 == edge->v[1] && v2 == edge->v[0])) {
Error("Too many edges for standard BSP format. Try compiling with -bsp2");
}
if (pEdgeFaces1[i] == NULL if (pEdgeFaces1[i] == NULL
&& pEdgeFaces0[i]->contents[0] == face->contents[0]) { && pEdgeFaces0[i]->contents[0] == face->contents[0]) {
pEdgeFaces1[i] = face; pEdgeFaces1[i] = face;
return -(i + cStartEdge); return -i;
} }
} }
} }
/* emit an edge */ /* emit an edge */
i = edges->index; i = static_cast<int>(map.exported_edges.size());
edge = (bsp29_dedge_t *)edges->data + i; map.exported_edges.push_back({});
if (edges->index >= edges->count) edge = &map.exported_edges.at(i);
Error("Internal error: didn't allocate enough edges?");
edge->v[0] = v1; edge->v[0] = v1;
edge->v[1] = v2; edge->v[1] = v2;
} else { }
#if 0
else {
bsp2_dedge_t *edge; bsp2_dedge_t *edge;
auto it = hashedges.find(edge_hash_key); auto it = hashedges.find(edge_hash_key);
@ -355,13 +353,12 @@ GetEdge(mapentity_t *entity, const vec3_t p1, const vec3_t p2,
edge->v[0] = v1; edge->v[0] = v1;
edge->v[1] = v2; edge->v[1] = v2;
} }
#endif
AddHashEdge(v1, v2, edges->index); AddHashEdge(v1, v2, i);
edges->index++;
map.cTotal[LUMP_EDGES]++;
pEdgeFaces0[i] = face; pEdgeFaces0[i] = face;
return i + cStartEdge; return i;
} }
@ -539,7 +536,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 *edges = &entity->lumps[LUMP_EDGES];
struct lumpdata *vertices = &entity->lumps[LUMP_VERTEXES]; 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];
@ -547,10 +543,6 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
Message(msgProgress, "MakeFaceEdges"); Message(msgProgress, "MakeFaceEdges");
needlmshifts = false; needlmshifts = false;
cStartEdge = 0;
const int entnum = entity - &map.entities.at(0);
for (i = 0; i < entnum; i++)
cStartEdge += map.entities.at(i).lumps[LUMP_EDGES].count;
CountData_r(entity, headnode); CountData_r(entity, headnode);
@ -561,22 +553,17 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
* the time without wasting too much memory... * the time without wasting too much memory...
*/ */
surfedges->count = vertices->count; surfedges->count = vertices->count;
edges->count += surfedges->count;
vertices->data = AllocMem(BSP_VERTEX, vertices->count, true); vertices->data = AllocMem(BSP_VERTEX, vertices->count, true);
edges->data = AllocMem(BSP_EDGE, edges->count, true);
// Accessory data // Accessory data
pEdgeFaces0 = (const face_t **)AllocMem(OTHER, sizeof(face_t *) * edges->count, true);
pEdgeFaces1 = (const face_t **)AllocMem(OTHER, sizeof(face_t *) * edges->count, true);
InitHash(); InitHash();
firstface = map.cTotal[LUMP_FACES]; firstface = map.cTotal[LUMP_FACES];
MakeFaceEdges_r(entity, headnode, 0); MakeFaceEdges_r(entity, headnode, 0);
FreeMem(pEdgeFaces0, OTHER, sizeof(face_t *) * edges->count); pEdgeFaces0.clear();
FreeMem(pEdgeFaces1, OTHER, sizeof(face_t *) * edges->count); pEdgeFaces1.clear();
/* Free any excess allocated memory */ /* Free any excess allocated memory */
if (vertices->index < vertices->count) { if (vertices->index < vertices->count) {
@ -586,13 +573,6 @@ MakeFaceEdges(mapentity_t *entity, node_t *headnode)
vertices->data = temp; vertices->data = temp;
vertices->count = vertices->index; vertices->count = vertices->index;
} }
if (edges->index < edges->count) {
void *temp = AllocMem(BSP_EDGE, edges->index, true);
memcpy(temp, edges->data, MemSize[BSP_EDGE] * edges->index);
FreeMem(edges->data, BSP_EDGE, edges->count);
edges->data = temp;
edges->count = edges->index;
}
if (map.cTotal[LUMP_VERTEXES] > 65535 && (options.BSPVersion == BSPVERSION || options.BSPVersion == BSPHLVERSION)) 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]); Error("Too many vertices (%d > 65535). Recompile with the \"-bsp2\" flag to lift this restriction.", map.cTotal[LUMP_VERTEXES]);

View File

@ -374,9 +374,8 @@ void
BeginBSPFile(void) BeginBSPFile(void)
{ {
// First edge must remain unused because 0 can't be negated // First edge must remain unused because 0 can't be negated
pWorldEnt()->lumps[LUMP_EDGES].count++; map.exported_edges.push_back({});
pWorldEnt()->lumps[LUMP_EDGES].index++; Q_assert(map.exported_edges.size() == 1);
map.cTotal[LUMP_EDGES]++;
// Leave room for leaf 0 (must be solid) // Leave room for leaf 0 (must be solid)
map.exported_leafs_bsp29.push_back({}); map.exported_leafs_bsp29.push_back({});