light: add debug visualization for octree lightgrid

This commit is contained in:
Eric Wasylishen 2023-02-12 16:35:46 -07:00
parent 48fe527955
commit f5fad41402
5 changed files with 93 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include <common/bspfile.hh>
#include <fstream>
#include <fmt/ostream.h>
constexpr const char *PORTALFILE = "PRT1";
constexpr const char *PORTALFILE2 = "PRT2";
@ -171,3 +172,28 @@ prtfile_t LoadPrtFile(const fs::path &name, const bspversion_t *loadversion)
return result;
}
static void WriteDebugPortal(const polylib::winding_t &w, std::ofstream &portalFile)
{
fmt::print(portalFile, "{} {} {} ", w.size(), 0, 0);
for (int i = 0; i < w.size(); i++) {
fmt::print(portalFile, "({} {} {}) ", w.at(i)[0], w.at(i)[1], w.at(i)[2]);
}
fmt::print(portalFile, "\n");
}
void WriteDebugPortals(const std::vector<polylib::winding_t> &portals, fs::path name)
{
size_t portal_count = portals.size();
std::ofstream portal_file(name, std::ios_base::binary | std::ios_base::out);
if (!portal_file)
FError("Failed to open {}: {}", name, strerror(errno));
fmt::print(portal_file, "PRT1\n");
fmt::print(portal_file, "{}\n", 0);
fmt::print(portal_file, "{}\n", portal_count);
for (auto &p : portals) {
WriteDebugPortal(p, portal_file);
}
}

View File

@ -228,6 +228,21 @@ public:
auto stream_data() { return std::tie(m_corners); }
};
template<class V>
inline std::array<qplane3<V>, 6> aabb_planes(const aabb<V, 3> &bbox)
{
return {
qplane3<V>{qvec<V,3>( 1,0,0), bbox.maxs()[0]}, // +X
qplane3<V>{qvec<V,3>(-1,0,0), -bbox.mins()[0]}, // -X
qplane3<V>{qvec<V,3>(0, 1,0), bbox.maxs()[1]}, // +Y
qplane3<V>{qvec<V,3>(0,-1,0), -bbox.mins()[1]}, // -Y
qplane3<V>{qvec<V,3>(0,0, 1), bbox.maxs()[2]}, // +Z
qplane3<V>{qvec<V,3>(0,0,-1), -bbox.mins()[2]}, // -Z
};
}
// Fmt support
template<class T, size_t Dim>
struct fmt::formatter<aabb<T, Dim>> : formatter<qvec<T, Dim>>

View File

@ -1321,6 +1321,33 @@ public:
{
return directional_equal(w, equal_epsilon) || directional_equal(w.flip(), equal_epsilon);
}
static std::array<winding_base_t, 6> aabb_windings(const aabb3d &bbox) {
double worldextent = 0;
for (int i = 0; i < 3; ++i) {
worldextent = max(worldextent, abs(bbox.maxs()[i]));
worldextent = max(worldextent, abs(bbox.mins()[i]));
}
worldextent += 1;
std::array<winding_base_t, 6> result;
auto planes = aabb_planes(bbox);
for (int i = 0; i < 6; ++i) {
result[i] = winding_base_t::from_plane(planes[i], worldextent);
}
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 6; ++j) {
if (i == j)
continue;
result[i] = *result[i].clip_back(planes[j]);
}
}
return result;
}
};
// the default amount of points to keep on stack

View File

@ -50,3 +50,4 @@ struct prtfile_t
struct bspversion_t;
prtfile_t LoadPrtFile(const fs::path &name, const bspversion_t *loadversion);
void WriteDebugPortals(const std::vector<polylib::winding_t> &portals, fs::path name);

View File

@ -32,6 +32,7 @@
#include <common/log.hh>
#include <common/bsputils.hh>
#include <common/fs.hh>
#include <common/prtfile.hh>
#include <common/imglib.hh>
#include <common/parallel.hh>
@ -96,6 +97,10 @@ void LightGrid(bspdata_t *bspdata)
ceil(world_size[2] / light_options.lightgrid_dist.value()[2])
};
auto grid_index_to_world = [&](const qvec3i &index) -> qvec3f {
return grid_mins + (index * light_options.lightgrid_dist.value());
};
std::vector<lightgrid_samples_t> grid_result;
grid_result.resize(grid_size[0] * grid_size[1] * grid_size[2]);
@ -414,6 +419,25 @@ void LightGrid(bspdata_t *bspdata)
// build the root node
const uint32_t root_node = build_octree(qvec3i{0,0,0}, grid_size, 0);
// visualize the leafs
{
std::vector<polylib::winding_t> windings;
for (auto &leaf : octree_leafs) {
auto leaf_world_mins = grid_index_to_world(leaf.mins);
auto leaf_world_maxs = grid_index_to_world(leaf.mins + leaf.size - qvec3i(1,1,1));
aabb3d bounds(leaf_world_mins, leaf_world_maxs);
auto bounds_windings = polylib::winding_t::aabb_windings(bounds);
for (auto &w : bounds_windings) {
windings.push_back(std::move(w));
}
}
WriteDebugPortals(windings, fs::path(light_options.sourceMap).replace_extension(".octree.prt"));
}
// stats
int stored_cells = 0;
for (auto &leaf : octree_leafs) {