common: LoadLitFile: return hdr variant as well

This commit is contained in:
Eric Wasylishen 2024-05-20 13:37:31 -06:00
parent 920355fcfb
commit 8bf2b6e93d
4 changed files with 46 additions and 15 deletions

View File

@ -126,7 +126,7 @@ qvec3f HDR_UnpackE5BRG9(uint32_t packed)
return qvec3f(red_int, green_int, blue_int) * multiplier;
}
std::vector<uint8_t> LoadLitFile(const fs::path &path)
std::variant<lit1_t, lit_hdr> LoadLitFile(const fs::path &path)
{
std::ifstream stream(path, std::ios_base::in | std::ios_base::binary);
stream >> endianness<std::endian::little>;
@ -139,16 +139,23 @@ std::vector<uint8_t> LoadLitFile(const fs::path &path)
int version;
stream >= version;
if (version != 1) {
throw std::runtime_error("invalid lit version");
if (version == LIT_VERSION) {
std::vector<uint8_t> litdata;
while (stream.good()) {
uint8_t b;
stream >= b;
litdata.push_back(b);
}
return {lit1_t{.rgbdata = std::move(litdata)}};
} else if (version == LIT_VERSION_E5BGR9) {
std::vector<uint32_t> litdata;
while (stream.good()) {
uint32_t sample;
stream >= sample;
litdata.push_back(sample);
}
return {lit_hdr{.samples = std::move(litdata)}};
}
std::vector<uint8_t> litdata;
while (stream.good()) {
uint8_t b;
stream >= b;
litdata.push_back(b);
}
return litdata;
throw std::runtime_error("invalid lit version");
}

View File

@ -23,6 +23,7 @@
#include <array>
#include <iostream>
#include <vector>
#include <variant>
constexpr int32_t LIT_VERSION = 1;
constexpr int32_t LIT_VERSION_E5BGR9 = (0x00010000 | LIT_VERSION);
@ -55,4 +56,14 @@ struct litheader_t
uint32_t HDR_PackE5BRG9(qvec3f rgb);
qvec3f HDR_UnpackE5BRG9(uint32_t packed);
std::vector<uint8_t> LoadLitFile(const fs::path &path);
struct lit1_t {
// 3 bytes (r,g,b) per sample
std::vector<uint8_t> rgbdata;
};
struct lit_hdr {
// 1 packed e5bgr9 uint32_t per sample
std::vector<uint32_t> samples;
};
std::variant<lit1_t, lit_hdr> LoadLitFile(const fs::path &path);

View File

@ -933,7 +933,12 @@ int MainWindow::compileMap(const QString &file, bool is_reload)
lit_path.replace_extension(".lit");
try {
m_litdata = LoadLitFile(lit_path);
auto lit_variant = LoadLitFile(lit_path);
if (auto* lit1_ptr = std::get_if<lit1_t>(&lit_variant)) {
m_litdata = std::move(lit1_ptr->rgbdata);
}
// FIXME: handle hdr variant
} catch (const std::runtime_error &error) {
logging::print("error loading lit: {}", error.what());
m_litdata = {};

View File

@ -118,9 +118,17 @@ testresults_lit_t QbspVisLight_Q1(
auto lit_path = fs::path(test_quake_maps_dir) / name.filename();
lit_path.replace_extension(".lit");
std::vector<uint8_t> litdata = LoadLitFile(lit_path);
auto lit_variant = LoadLitFile(lit_path);
std::vector<uint8_t> lit_bytes;
return testresults_lit_t{.bsp = res.bsp, .bspx = res.bspx, .lit = litdata};
if (auto* lit1_ptr = std::get_if<lit1_t>(&lit_variant)) {
lit_bytes = std::move(lit1_ptr->rgbdata);
}
// FIXME: handle hdr variant
return testresults_lit_t{.bsp = std::move(res.bsp),
.bspx = std::move(res.bspx),
.lit = std::move(lit_bytes)};
}
testresults_t QbspVisLight_Q2(