diff --git a/include/qbsp/map.hh b/include/qbsp/map.hh index 244cbd0c..31615063 100644 --- a/include/qbsp/map.hh +++ b/include/qbsp/map.hh @@ -104,6 +104,7 @@ public: struct lumpdata lumps[BSPX_LUMPS]; int firstoutputfacenumber; + int outputmodelnumber; const mapbrush_t &mapbrush(int i) const; @@ -119,7 +120,8 @@ public: epairs(nullptr), brushes(nullptr), numbrushes(0), - firstoutputfacenumber(-1) { + firstoutputfacenumber(-1), + outputmodelnumber(-1) { VectorSet(origin,0,0,0); VectorSet(mins,0,0,0); VectorSet(maxs,0,0,0); @@ -167,6 +169,7 @@ typedef struct mapdata_s { std::vector exported_vertexes; std::vector exported_surfedges; std::vector exported_faces; + std::vector exported_models; // helpers std::string texinfoTextureName(int texinfo) const { diff --git a/qbsp/bspfile.cc b/qbsp/bspfile.cc index 315f3cdb..65c322c7 100644 --- a/qbsp/bspfile.cc +++ b/qbsp/bspfile.cc @@ -303,7 +303,7 @@ WriteBSPFile(void) 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])); AddLumpFromBuffer(f, LUMP_EDGES, map.exported_edges.data(), map.exported_edges.size() * sizeof(map.exported_edges[0])); - AddLump(f, LUMP_MODELS); + AddLumpFromBuffer(f, LUMP_MODELS, map.exported_models.data(), map.exported_models.size() * sizeof(map.exported_models[0])); AddLump(f, LUMP_LIGHTING); AddLump(f, LUMP_VISIBILITY); diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index e28b9efa..5dfae11b 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -58,12 +58,18 @@ ProcessEntity(mapentity_t *entity, const int hullnum) if (IsWorldBrushEntity(entity)) return; + // Export a blank model struct, and reserve the index (only do this once, for all hulls) + if (entity->outputmodelnumber == -1) { + entity->outputmodelnumber = static_cast(map.exported_models.size()); + map.exported_models.push_back({}); + } + if (entity != pWorldEnt()) { char mod[20]; if (entity == pWorldEnt() + 1) Message(msgProgress, "Internal Entities"); - q_snprintf(mod, sizeof(mod), "*%d", map.cTotal[LUMP_MODELS]); + q_snprintf(mod, sizeof(mod), "*%d", entity->outputmodelnumber); if (options.fVerbose) PrintEntity(entity); @@ -242,8 +248,6 @@ ProcessEntity(mapentity_t *entity, const int hullnum) } FreeBrushes(entity); - - map.cTotal[LUMP_MODELS]++; } /* @@ -541,7 +545,6 @@ CreateSingleHull(const int hullnum) mapentity_t *entity; Message(msgLiteral, "Processing hull %d...\n", hullnum); - map.cTotal[LUMP_MODELS] = 0; // for each entity in the map file that has geometry for (i = 0; i < map.numentities(); i++) { diff --git a/qbsp/writebsp.cc b/qbsp/writebsp.cc index 6d23635b..a3dc8edf 100644 --- a/qbsp/writebsp.cc +++ b/qbsp/writebsp.cc @@ -170,7 +170,7 @@ accomodate new data interleaved with old. void ExportClipNodes(mapentity_t *entity, node_t *nodes, const int hullnum) { - dmodel_t *model = (dmodel_t *)entity->lumps[LUMP_MODELS].data; + auto *model = &map.exported_models.at(static_cast(entity->outputmodelnumber)); model->headnode[hullnum] = ExportClipNodes_BSP29(entity, nodes); } @@ -295,14 +295,10 @@ void ExportDrawNodes(mapentity_t *entity, node_t *headnode, int firstface) { int i; - dmodel_t *dmodel; + dmodelq1_t *dmodel; - // Allocate a model - entity->lumps[LUMP_MODELS].data = AllocMem(BSP_MODEL, 1, true); - entity->lumps[LUMP_MODELS].count = 1; - - // emit a model - dmodel = (dmodel_t *)entity->lumps[LUMP_MODELS].data; + // populate model struct (which was emitted previously) + dmodel = &map.exported_models.at(static_cast(entity->outputmodelnumber)); dmodel->headnode[0] = static_cast(map.exported_nodes_bsp29.size()); dmodel->firstface = firstface; dmodel->numfaces = static_cast(map.exported_faces.size()) - firstface;