From f3cc0907c8e345882b0d98fda74f4ed47fe96326 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Wed, 2 Nov 2022 00:56:23 -0600 Subject: [PATCH] faceextents_t: refactor to remove lightmapshift member --- common/bsputils.cc | 17 ++++++++++++++--- include/common/bsputils.hh | 3 ++- tests/test_ltface.cc | 13 +++++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/common/bsputils.cc b/common/bsputils.cc index 63afe36a..2c195d24 100644 --- a/common/bsputils.cc +++ b/common/bsputils.cc @@ -682,7 +682,7 @@ qmat4x4f TexSpaceToWorld(const mbsp_t *bsp, const mface_t *f) // faceextents_t -faceextents_t::faceextents_t(const mface_t &face, const mbsp_t &bsp, float lmshift) : lightmapshift(lmshift) +faceextents_t::faceextents_t(const mface_t &face, const mbsp_t &bsp, float lightmapshift) { worldToTexCoordMatrix = WorldToTexSpace(&bsp, &face); texCoordToWorldMatrix = TexSpaceToWorld(&bsp, &face); @@ -731,6 +731,13 @@ faceextents_t::faceextents_t(const mface_t &face, const mbsp_t &bsp, float lmshi origin = bounds.mins() + radius; this->radius = qv::length(radius); + + LMToTexCoordMatrix = qmat3x3f::row_major({ + lightmapshift, 0, texmins[0] * lightmapshift, + 0 , lightmapshift, texmins[1] * lightmapshift, + 0 , 0 , 1 + }); + TexCoordToLMMatrix = qv::inverse(LMToTexCoordMatrix); } int faceextents_t::width() const @@ -755,12 +762,16 @@ qvec2i faceextents_t::texsize() const qvec2f faceextents_t::lightmapCoordToTexCoord(const qvec2f &LMCoord) const { - return {lightmapshift * (texmins[0] + LMCoord[0]), lightmapshift * (texmins[1] + LMCoord[1])}; + qvec3f LMCoordPadded = {LMCoord[0], LMCoord[1], 1.0f}; + qvec2f tex = LMToTexCoordMatrix * LMCoordPadded; + return tex; } qvec2f faceextents_t::texCoordToLightmapCoord(const qvec2f &tc) const { - return {(tc[0] / lightmapshift) - texmins[0], (tc[1] / lightmapshift) - texmins[1]}; + qvec3f tcPadded = {tc[0], tc[1], 1.0f}; + qvec2f lm = TexCoordToLMMatrix * tcPadded; + return lm; } qvec2f faceextents_t::worldToTexCoord(qvec3f world) const diff --git a/include/common/bsputils.hh b/include/common/bsputils.hh index 1444fcb2..3b600abe 100644 --- a/include/common/bsputils.hh +++ b/include/common/bsputils.hh @@ -125,9 +125,10 @@ class faceextents_t public: qvec2i texmins; qvec2i texextents; - float lightmapshift; qmat4x4f worldToTexCoordMatrix; qmat4x4f texCoordToWorldMatrix; + qmat3x3f LMToTexCoordMatrix; + qmat3x3f TexCoordToLMMatrix; qvec3d origin; vec_t radius; diff --git a/tests/test_ltface.cc b/tests/test_ltface.cc index e8b5f77a..14593d05 100644 --- a/tests/test_ltface.cc +++ b/tests/test_ltface.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include @@ -39,6 +40,18 @@ static void LoadTestmap(const std::filesystem::path &name, std::vector(bspdata.bsp), + fs::path(qbsp_options.bsp_path).replace_extension(".bsp.json")); + } } TEST_CASE("TestLight") {