simplify bspx code

remove old obj preview for wrnormals
This commit is contained in:
Jonathan 2022-06-20 20:42:34 -04:00
parent e422da15c3
commit c79e263753
9 changed files with 53 additions and 97 deletions

View File

@ -1955,9 +1955,7 @@ void LoadBSPFile(fs::path &filename, bspdata_t *bspdata)
while (xlumps-- > 0) {
uint32_t ofs = LittleLong(xlump[xlumps].fileofs);
uint32_t len = LittleLong(xlump[xlumps].filelen);
uint8_t *lumpdata = new uint8_t[len];
memcpy(lumpdata, (const uint8_t *)file_data->data() + ofs, len);
bspdata->bspx.transfer(xlump[xlumps].lumpname.data(), lumpdata, len);
bspdata->bspx.transfer(xlump[xlumps].lumpname.data(), std::vector<uint8_t>(file_data->begin() + ofs, file_data->begin() + ofs + len - 1));
}
} else {
if (memcmp(&bspx->id, "BSPX", 4))
@ -2154,14 +2152,14 @@ public:
static constexpr char pad[4]{};
bspx_lump_t &lump = xlumps.emplace_back();
lump.filelen = x.second.lumpsize;
lump.filelen = x.second.size();
lump.fileofs = stream.tellp();
memcpy(lump.lumpname.data(), x.first.c_str(), std::min(x.first.size(), lump.lumpname.size() - 1));
stream.write(reinterpret_cast<const char *>(x.second.lumpdata.get()), x.second.lumpsize);
stream.write(reinterpret_cast<const char *>(x.second.data()), x.second.size());
if (x.second.lumpsize % 4)
stream.write(pad, 4 - (x.second.lumpsize % 4));
if (x.second.size() % 4)
stream.write(pad, 4 - (x.second.size() % 4));
}
stream.seekp(bspxheader);
@ -2305,6 +2303,6 @@ void PrintBSPFileSizes(const bspdata_t *bspdata)
}
for (auto &x : bspdata->bspx.entries) {
logging::print("{:7} {:<12} {:10}\n", "BSPX", x.first, x.second.lumpsize);
logging::print("{:7} {:<12} {:10}\n", "BSPX", x.first, x.second.size());
}
}

View File

@ -42,11 +42,11 @@ static std::string hex_string(const uint8_t *bytes, const size_t count)
/**
* returns a JSON array of models
*/
static json serialize_bspxbrushlist(const bspxentry_t &lump)
static json serialize_bspxbrushlist(const std::vector<uint8_t> &lump)
{
json j = json::array();
memstream p(lump.lumpdata.get(), lump.lumpsize, std::ios_base::in | std::ios_base::binary);
memstream p(lump.data(), lump.size(), std::ios_base::in | std::ios_base::binary);
p >> endianness<std::endian::little>;
@ -468,7 +468,7 @@ void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &
} else {
// unhandled BSPX lump, just write the raw data
entry["lumpdata"] =
hex_string(reinterpret_cast<uint8_t *>(lump.second.lumpdata.get()), lump.second.lumpsize);
hex_string(lump.second.data(), lump.second.size());
}
}
}

View File

@ -407,7 +407,7 @@ using texvecf = texvec<float>;
#include "bspfile_q2.hh"
#include "bspxfile.hh"
using bspxentries_t = std::unordered_map<std::string, bspxentry_t>;
using bspxentries_t = std::unordered_map<std::string, std::vector<uint8_t>>;
struct bspdata_t
{
@ -420,21 +420,17 @@ struct bspdata_t
struct
{
bspxentries_t entries;
// convenience function to transfer a generic pointer into
// the entries list
inline void transfer(const char *xname, uint8_t *&xdata, size_t xsize)
// transfer ownership of the vector into a BSPX lump
inline void transfer(const char *xname, std::vector<uint8_t> &xdata)
{
entries.insert_or_assign(xname, bspxentry_t{xdata, xsize});
xdata = nullptr;
entries.insert_or_assign(xname, std::move(xdata));
}
// copies the data over to the BSP
void copy(const char *xname, const uint8_t *xdata, size_t xsize)
// transfer ownership of the vector into a BSPX lump
inline void transfer(const char *xname, std::vector<uint8_t> &&xdata)
{
uint8_t *copy = new uint8_t[xsize];
memcpy(copy, xdata, xsize);
transfer(xname, copy, xsize);
entries.insert_or_assign(xname, xdata);
}
} bspx;
};

View File

@ -89,15 +89,3 @@ struct bspxfacenormals_header
};
// BSPX data
struct bspxentry_t
{
std::unique_ptr<uint8_t[]> lumpdata;
size_t lumpsize;
// bspxentry_t takes ownership over the pointer and will
// free it automatically.
inline bspxentry_t(void *lumpdata, size_t lumpsize) : lumpdata(reinterpret_cast<uint8_t *>(lumpdata)), lumpsize(lumpsize)
{
}
};

View File

@ -528,8 +528,8 @@ public:
extern settings::light_settings options;
extern uint8_t *filebase;
extern uint8_t *lit_filebase;
extern uint8_t *lux_filebase;
extern std::vector<uint8_t> lit_filebase;
extern std::vector<uint8_t> lux_filebase;
extern std::vector<surfflags_t> extended_texinfo_flags;

View File

@ -69,14 +69,14 @@ static int file_p;
static int file_end;
/// start of litfile data
uint8_t *lit_filebase;
std::vector<uint8_t> lit_filebase;
/// offset of start of free space after litfile data (should be kept a multiple of 12)
static int lit_file_p;
/// offset of end of space for litfile data
static int lit_file_end;
/// start of luxfile data
uint8_t *lux_filebase;
std::vector<uint8_t> lux_filebase;
/// offset of start of free space after luxfile data (should be kept a multiple of 12)
static int lux_file_p;
/// offset of end of space for luxfile data
@ -219,8 +219,8 @@ void GetFileSpace(uint8_t **lightdata, uint8_t **colordata, uint8_t **deluxdata,
light_mutex.lock();
*lightdata = filebase + file_p;
*colordata = lit_filebase + lit_file_p;
*deluxdata = lux_filebase + lux_file_p;
*colordata = lit_filebase.data() + lit_file_p;
*deluxdata = lux_filebase.data() + lux_file_p;
// if size isn't a multiple of 4, round up to the next multiple of 4
if ((size % 4) != 0) {
@ -253,11 +253,11 @@ void GetFileSpace_PreserveOffsetInBsp(uint8_t **lightdata, uint8_t **colordata,
*lightdata = filebase + lightofs;
if (colordata) {
*colordata = lit_filebase + (lightofs * 3);
*colordata = lit_filebase.data() + (lightofs * 3);
}
if (deluxdata) {
*deluxdata = lux_filebase + (lightofs * 3);
*deluxdata = lux_filebase.data() + (lightofs * 3);
}
// NOTE: file_p et. al. are not updated, since we're not dynamically allocating the lightmaps
@ -424,8 +424,8 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
mbsp_t &bsp = std::get<mbsp_t>(bspdata->bsp);
delete[] filebase;
delete[] lit_filebase;
delete[] lux_filebase;
lit_filebase.clear();
lux_filebase.clear();
/* greyscale data stored in a separate buffer */
filebase = new uint8_t[MAX_MAP_LIGHTING]{};
@ -435,16 +435,12 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
file_end = MAX_MAP_LIGHTING;
/* litfile data stored in a separate buffer */
lit_filebase = new uint8_t[MAX_MAP_LIGHTING * 3]{};
if (!lit_filebase)
FError("allocation of {} bytes failed.", MAX_MAP_LIGHTING * 3);
lit_filebase.resize(MAX_MAP_LIGHTING * 3);
lit_file_p = 0;
lit_file_end = (MAX_MAP_LIGHTING * 3);
/* lux data stored in a separate buffer */
lux_filebase = new uint8_t[MAX_MAP_LIGHTING * 3]{};
if (!lux_filebase)
FError("allocation of {} bytes failed.", MAX_MAP_LIGHTING * 3);
lux_filebase.resize(MAX_MAP_LIGHTING * 3);
lux_file_p = 0;
lux_file_end = (MAX_MAP_LIGHTING * 3);
@ -460,7 +456,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
if (lmshift_lump != bspdata->bspx.entries.end()) {
for (int i = 0; i < bsp.dfaces.size(); i++)
faces_sup[i].lmscale = nth_bit(reinterpret_cast<const char *>(lmshift_lump->second.lumpdata.get())[i]);
faces_sup[i].lmscale = nth_bit(reinterpret_cast<const char *>(lmshift_lump->second.data())[i]);
} else {
for (int i = 0; i < bsp.dfaces.size(); i++)
faces_sup[i].lmscale = modelinfo.at(0)->lightmapscale;
@ -505,7 +501,7 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
if (!options.litonly.value()) {
if (bsp.loadversion->game->has_rgb_lightmap) {
bsp.dlightdata.resize(lit_file_p);
memcpy(bsp.dlightdata.data(), lit_filebase, bsp.dlightdata.size());
memcpy(bsp.dlightdata.data(), lit_filebase.data(), bsp.dlightdata.size());
} else {
bsp.dlightdata.resize(file_p);
memcpy(bsp.dlightdata.data(), filebase, bsp.dlightdata.size());
@ -520,15 +516,17 @@ static void LightWorld(bspdata_t *bspdata, bool forcedscale)
bspdata->bspx.entries.erase("LMOFFSET");
if (faces_sup) {
uint8_t *styles = new uint8_t[4 * bsp.dfaces.size()];
int32_t *offsets = new int32_t[bsp.dfaces.size()];
std::vector<uint8_t> styles(4 * bsp.dfaces.size());
std::vector<uint8_t> offsets_mem(bsp.dfaces.size() * sizeof(int32_t));
memstream offsets(offsets_mem.data(), std::ios_base::out | std::ios_base::binary);
offsets << endianness<std::endian::little>;
for (int i = 0; i < bsp.dfaces.size(); i++) {
offsets[i] = faces_sup[i].lightofs;
offsets <= faces_sup[i].lightofs;
for (int j = 0; j < MAXLIGHTMAPS; j++)
styles[i * 4 + j] = faces_sup[i].styles[j];
}
bspdata->bspx.transfer("LMSTYLE", styles, sizeof(*styles) * 4 * bsp.dfaces.size());
bspdata->bspx.transfer("LMOFFSET", (uint8_t *&)offsets, sizeof(*offsets) * bsp.dfaces.size());
bspdata->bspx.transfer("LMSTYLE", styles);
bspdata->bspx.transfer("LMOFFSET", offsets_mem);
}
}
@ -860,8 +858,8 @@ static inline void WriteNormals(const mbsp_t &bsp, bspdata_t &bspdata)
}
size_t data_size = sizeof(uint32_t) + (sizeof(qvec3f) * unique_normals.size()) + (sizeof(uint32_t) * num_normals);
uint8_t *data = new uint8_t[data_size];
memstream stream(data, data_size);
std::vector<uint8_t> data(data_size);
memstream stream(data.data(), data_size);
stream << endianness<std::endian::little>;
stream <= numeric_cast<uint32_t>(unique_normals.size());
@ -887,33 +885,7 @@ static inline void WriteNormals(const mbsp_t &bsp, bspdata_t &bspdata)
logging::print(logging::flag::VERBOSE, "Compressed {} normals down to {}\n", num_normals, unique_normals.size());
bspdata.bspx.transfer("FACENORMALS", data, data_size);
ofstream obj("test.obj");
size_t index_id = 1;
for (auto &face : bsp.dfaces) {
auto &cache = FaceCacheForFNum(&face - bsp.dfaces.data());
for (size_t i = 0; i < cache.points().size(); i++) {
auto &pt = cache.points()[i];
auto &n = cache.normals()[i];
fmt::print(obj, "v {}\n", pt);
fmt::print(obj, "vn {}\n", n.normal);
}
for (size_t i = 1; i < cache.points().size() - 1; i++) {
size_t n1 = 0;
size_t n2 = i;
size_t n3 = (i + 1) % cache.points().size();
fmt::print(obj, "f {0}//{0} {1}//{1} {2}//{2}\n", index_id + n1, index_id + n2, index_id + n3);
}
index_id += cache.points().size();
}
bspdata.bspx.transfer("FACENORMALS", data);
}
/*
@ -1035,13 +1007,15 @@ int light_main(int argc, const char **argv)
WriteLitFile(&bsp, faces_sup, source, LIT_VERSION);
}
if (options.write_litfile & lightfile::bspx) {
bspdata.bspx.transfer("RGBLIGHTING", lit_filebase, bsp.dlightdata.size() * 3);
lit_filebase.resize(bsp.dlightdata.size() * 3);
bspdata.bspx.transfer("RGBLIGHTING", lit_filebase);
}
if (options.write_luxfile & lightfile::external) {
WriteLuxFile(&bsp, source, LIT_VERSION);
}
if (options.write_luxfile & lightfile::bspx) {
bspdata.bspx.transfer("LIGHTINGDIR", lux_filebase, bsp.dlightdata.size() * 3);
lux_filebase.resize(bsp.dlightdata.size() * 3);
bspdata.bspx.transfer("LIGHTINGDIR", lux_filebase);
}
}

View File

@ -56,11 +56,11 @@ void WriteLitFile(const mbsp_t *bsp, facesup_t *facesup, const fs::path &filenam
j++;
litfile <= (uint8_t) j;
}
litfile.write((const char *) lit_filebase, bsp->dlightdata.size() * 3);
litfile.write((const char *) lux_filebase, bsp->dlightdata.size() * 3);
litfile.write((const char *) lit_filebase.data(), bsp->dlightdata.size() * 3);
litfile.write((const char *) lux_filebase.data(), bsp->dlightdata.size() * 3);
}
else
litfile.write((const char *) lit_filebase, bsp->dlightdata.size() * 3);
litfile.write((const char *) lit_filebase.data(), bsp->dlightdata.size() * 3);
}
#include <fstream>
@ -76,5 +76,5 @@ void WriteLuxFile(const mbsp_t *bsp, const fs::path &filename, int version)
std::ofstream luxfile(luxname, std::ios_base::out | std::ios_base::binary);
luxfile <= header.v1;
luxfile.write((const char *) lux_filebase, bsp->dlightdata.size() * 3);
luxfile.write((const char *) lux_filebase.data(), bsp->dlightdata.size() * 3);
}

View File

@ -3226,7 +3226,7 @@ static void WriteLightmaps(
// Q2/HL native colored lightmaps
if (bsp->loadversion->game->has_rgb_lightmap) {
lightofs = lit - lit_filebase;
lightofs = lit - lit_filebase.data();
} else {
lightofs = out - filebase;
}

View File

@ -390,10 +390,10 @@ static void WriteBSPFile()
bspdata.version = &bspver_generic;
if (map.needslmshifts) {
bspdata.bspx.copy("LMSHIFT", map.exported_lmshifts.data(), map.exported_lmshifts.size());
bspdata.bspx.transfer("LMSHIFT", map.exported_lmshifts);
}
if (!map.exported_bspxbrushes.empty()) {
bspdata.bspx.copy("BRUSHLIST", map.exported_bspxbrushes.data(), map.exported_bspxbrushes.size());
bspdata.bspx.transfer("BRUSHLIST", map.exported_bspxbrushes);
}
if (!ConvertBSPFormat(&bspdata, options.target_version)) {