From 78e902b75e9eddf65584f2f6c0679ced1ba149bf Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Thu, 14 May 2020 17:32:54 -0600 Subject: [PATCH] bsputil: add option for swapping a face's texinfo, --findfaces --- bsputil/bsputil.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/bsputil/bsputil.cc b/bsputil/bsputil.cc index 5fe199ae..d5e2765c 100644 --- a/bsputil/bsputil.cc +++ b/bsputil/bsputil.cc @@ -32,6 +32,7 @@ #include #include #include // std::sort +#include #include /* FIXME - share header with qbsp, etc. */ @@ -497,6 +498,22 @@ CompareBSPFiles(const mbsp_t *refBsp, const mbsp_t *bsp) } +static void +FindFaces(const mbsp_t *bsp, const vec3_t pos, const vec3_t normal) +{ + for (int i = 0; i < bsp->nummodels; ++i) { + const dmodelh2_t* model = &bsp->dmodels[i]; + const bsp2_dface_t* face = BSP_FindFaceAtPoint(bsp, model, pos, normal); + + if (face != nullptr) { + printf("model %d face %d: texture '%s' texinfo %d\n", + i, static_cast(face - bsp->dfaces), + Face_TextureName(bsp, face), + face->texinfo); + } + } +} + int main(int argc, char **argv) { @@ -508,8 +525,8 @@ main(int argc, char **argv) printf("---- bsputil / ericw-tools " stringify(ERICWTOOLS_VERSION) " ----\n"); if (argc == 1) { - printf("usage: bsputil [--extract-entities] [--extract-textures] [--convert bsp29|bsp2|bsp2rmq|q2bsp] [--check] [--modelinfo]" - "[--check] [--compare otherbsp] bspfile\n"); + printf("usage: bsputil [--extract-entities] [--extract-textures] [--convert bsp29|bsp2|bsp2rmq|q2bsp] [--check] [--modelinfo]\n" + "[--check] [--compare otherbsp] [--findfaces x y z nx ny nz] [--settexinfo facenum texinfonum] bspfile\n"); exit(1); } @@ -612,6 +629,50 @@ main(int argc, char **argv) printf("Done.\n"); } else if (!strcmp(argv[i], "--modelinfo")) { PrintModelInfo(bsp); + } else if (!strcmp(argv[i], "--findfaces")) { + // (i + 1) ... (i + 6) = x y z nx ny nz + // i + 7 = bsp file + + if (i + 7 >= argc) { + Error("--findfaces requires 6 arguments"); + } + + try { + const vec3_t pos = { + std::stof(argv[i + 1]), + std::stof(argv[i + 2]), + std::stof(argv[i + 3]) + }; + const vec3_t normal = { + std::stof(argv[i + 4]), + std::stof(argv[i + 5]), + std::stof(argv[i + 6]) + }; + FindFaces(bsp, pos, normal); + } catch (const std::exception&) { + printf("Error reading position/normal\n"); + } + return 0; + } else if (!strcmp(argv[i], "--settexinfo")) { + // (i + 1) facenum + // (i + 2) texinfonum + + if (i + 2 >= argc) { + Error("--settexinfo requires 2 arguments"); + } + + const int fnum = std::stoi(argv[i + 1]); + const int texinfonum = std::stoi(argv[i + 2]); + + bsp2_dface_t* face = BSP_GetFace(bsp, fnum); + face->texinfo = texinfonum; + + ConvertBSPFormat(bspdata.loadversion, &bspdata); + StripExtension(source); + strcat(source, "-settexinfo.bsp"); + WriteBSPFile(source, &bspdata); + + return 0; } }