From 5d3fab75b6450fe0de13fa73b676f6218c153099 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 20 Nov 2022 23:01:05 -0700 Subject: [PATCH] light: allow .wal_json metadata to contain "light": [ r, g, b ] to override emissive color Use this to fix the "emissive lights" test case so it can run on CI without any texture data --- common/imglib.cc | 12 +++++++++ include/common/imglib.hh | 4 +++ light/light.cc | 26 ++++++++++++++----- .../textures/e1u1/baselt_5.wal_json | 6 +++++ tests/test_ltface.cc | 10 ++++--- 5 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 testmaps/q2_wal_metadata/textures/e1u1/baselt_5.wal_json diff --git a/common/imglib.cc b/common/imglib.cc index 797f91a9..589e970a 100644 --- a/common/imglib.cc +++ b/common/imglib.cc @@ -565,6 +565,18 @@ std::optional load_wal_json_meta( meta.animation = json["animation"].get(); } + if (json.contains("color")) { + auto &color = json["color"]; + + qvec3b color_vec = { + color.at(0).get(), + color.at(1).get(), + color.at(2).get() + }; + + meta.color_override = {color_vec}; + } + return meta; } catch (json::exception e) { logging::funcprint("{}, invalid JSON: {}\n", name, e.what()); diff --git a/include/common/imglib.hh b/include/common/imglib.hh index 7fecfeea..5500c735 100644 --- a/include/common/imglib.hh +++ b/include/common/imglib.hh @@ -47,6 +47,10 @@ struct texture_meta // extension that we pulled the pixels in from. std::optional extension; + // so .json metadata can set an emissive color when we don't have + // texture data. Also useful to override the emissive color + std::optional color_override; + // Q2/WAL only surfflags_t flags{}; contentflags_t contents{}; diff --git a/light/light.cc b/light/light.cc index b4cca107..e014144d 100644 --- a/light/light.cc +++ b/light/light.cc @@ -1395,9 +1395,16 @@ static void AddTextureName(const std::string_view &textureName, const mbsp_t *bs tex.meta = std::move(texture_meta.value()); } - tex.averageColor = img::calculate_average(tex.pixels); - tex.width_scale = (float)tex.width / (float)tex.meta.width; - tex.height_scale = (float)tex.height / (float)tex.meta.height; + if (tex.meta.color_override) { + tex.averageColor = *tex.meta.color_override; + } else { + tex.averageColor = img::calculate_average(tex.pixels); + } + + if (tex.meta.width && tex.meta.height) { + tex.width_scale = (float)tex.width / (float)tex.meta.width; + tex.height_scale = (float)tex.height / (float)tex.meta.height; + } } // Load all of the referenced textures from the BSP texinfos into @@ -1460,9 +1467,16 @@ static void ConvertTextures(const mbsp_t *bsp) continue; } - tex.averageColor = img::calculate_average(tex.pixels); - tex.width_scale = (float)tex.width / (float)tex.meta.width; - tex.height_scale = (float)tex.height / (float)tex.meta.height; + if (tex.meta.color_override) { + tex.averageColor = *tex.meta.color_override; + } else { + tex.averageColor = img::calculate_average(tex.pixels); + } + + if (tex.meta.width && tex.meta.height) { + tex.width_scale = (float)tex.width / (float)tex.meta.width; + tex.height_scale = (float)tex.height / (float)tex.meta.height; + } } } diff --git a/testmaps/q2_wal_metadata/textures/e1u1/baselt_5.wal_json b/testmaps/q2_wal_metadata/textures/e1u1/baselt_5.wal_json new file mode 100644 index 00000000..2a970d35 --- /dev/null +++ b/testmaps/q2_wal_metadata/textures/e1u1/baselt_5.wal_json @@ -0,0 +1,6 @@ +{ + "contents": ["SOLID"], + "flags": ["LIGHT"], + "value": 1000, + "color": [255, 128, 64] +} diff --git a/tests/test_ltface.cc b/tests/test_ltface.cc index bf3bc73b..e32b3b83 100644 --- a/tests/test_ltface.cc +++ b/tests/test_ltface.cc @@ -38,9 +38,11 @@ static testresults_t LoadTestmap(const std::filesystem::path &name, std::vector< { std::vector light_args{ "", // the exe path, which we're ignoring in this case - "-nodefaultpaths" // in case test_quake2_maps_dir is pointing at a real Q2 install, don't - // read texture data etc. from there - we want the tests to behave the same - // during development as they do on CI (which doesn't have a Q2 install). + "-nodefaultpaths", // in case test_quake2_maps_dir is pointing at a real Q2 install, don't + // read texture data etc. from there - we want the tests to behave the same + // during development as they do on CI (which doesn't have a Q2 install). + "-path", + wal_metadata_path.string() }; for (auto &arg : extra_args) { light_args.push_back(arg); @@ -163,7 +165,7 @@ TEST_CASE("emissive lights") { for (int y = 0; y < extents.height(); ++y) { auto sample = LM_Sample(&bsp, extents, face->lightofs, {x, y}); INFO("sample ", x, ", ", y); - //CHECK(sample[0] > 0); + CHECK(sample[0] > 0); } } }