1
0
Fork 0
Univerxel/src/contouring/FlatSurroundingBox.cpp

127 lines
5.6 KiB
C++
Raw Normal View History

2020-07-18 15:42:45 +00:00
#include "FlatSurroundingBox.hpp"
#include "boxing.hpp"
#include "../world/Chunk.hpp"
2020-07-22 20:55:13 +00:00
#include <Remotery.h>
#include <imgui.h>
2020-07-18 15:42:45 +00:00
2020-07-25 16:45:03 +00:00
using namespace geometry;
2020-07-18 15:42:45 +00:00
namespace contouring {
2020-07-22 20:55:13 +00:00
FlatSurroundingBox::FlatSurroundingBox(const std::string &opt) : AbstractFlat(opt) {
for (size_t i = 1; i <= 4; i++) {
workers.push_back(std::thread([&] {
while (running) {
2020-07-24 19:42:47 +00:00
std::pair<chunk_pos, surrounding::faces> ctx;
2020-07-22 20:55:13 +00:00
loadQueue.wait();
if (loadQueue.pop(ctx)) {
rmt_ScopedCPUSample(ProcessContouring, 0);
2020-07-25 16:45:03 +00:00
std::vector<buffer::VertexData> vertices;
2020-07-22 20:55:13 +00:00
render(ctx.second, vertices);
{
rmt_ScopedCPUSample(Index, 0);
2020-07-25 16:45:03 +00:00
loadedQueue.push({ctx.first, buffer::ShortIndexed::Data(vertices)});
2020-07-22 20:55:13 +00:00
}
}
}
}));
}
}
FlatSurroundingBox::~FlatSurroundingBox() {
running = false;
loadQueue.notify();
2020-07-18 15:42:45 +00:00
2020-07-22 20:55:13 +00:00
for(auto& worker: workers) {
if (worker.joinable())
worker.join();
}
}
2020-07-25 16:45:03 +00:00
void FlatSurroundingBox::enqueue(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<world::Chunk>> &data) {
2020-07-22 20:55:13 +00:00
rmt_ScopedCPUSample(EnqueueContouring, RMTSF_Aggregate);
const auto dist2 = glm::length2(pos - center);
if (dist2 <= loadDistance * loadDistance) {
2020-07-24 19:42:47 +00:00
surrounding::faces surrounding;
2020-07-22 20:55:13 +00:00
if(surrounding::load(surrounding, pos, data)) {
loadQueue.push(pos, surrounding, -dist2);
2020-07-18 15:42:45 +00:00
}
}
2020-07-22 20:55:13 +00:00
}
2020-07-25 16:45:03 +00:00
void FlatSurroundingBox::onUpdate(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<world::Chunk>> &data, Faces neighbors) {
2020-07-22 20:55:13 +00:00
enqueue(pos, data);
if (neighbors && Faces::Right)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Right)], data);
if (neighbors && Faces::Left)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Left)], data);
if (neighbors && Faces::Up)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Up)], data);
if (neighbors && Faces::Down)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Down)], data);
if (neighbors && Faces::Forward)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Forward)], data);
if (neighbors && Faces::Backward)
enqueue(pos + g_face_offsets[static_cast<int>(Face::Backward)], data);
2020-07-18 15:42:45 +00:00
}
2020-07-25 16:45:03 +00:00
void FlatSurroundingBox::onNotify(const chunk_pos &pos, const robin_hood::unordered_map<chunk_pos, std::shared_ptr<world::Chunk>> &data) {
2020-07-22 20:55:13 +00:00
if (buffers.find(pos) == buffers.end()) {
enqueue(pos, data);
}
}
2020-07-18 15:42:45 +00:00
2020-07-22 20:55:13 +00:00
void FlatSurroundingBox::update(const camera_pos& pos) {
AbstractFlat::update(pos);
2020-07-25 16:45:03 +00:00
std::pair<chunk_pos, buffer::ShortIndexed::Data> out;
2020-07-22 20:55:13 +00:00
reports.load.push(loadQueue.size());
//MAYBE: clear out of range loadQueue.trim(keepDistance * keepDistance)
reports.loaded.push(loadedQueue.size());
while(loadedQueue.pop(out)) {
2020-07-25 16:45:03 +00:00
const auto buffer = new buffer::ShortIndexed(GL_TRIANGLES, out.second);
2020-07-22 20:55:13 +00:00
const auto it = buffers.find(out.first);
2020-07-18 15:42:45 +00:00
if (it != buffers.end()) {
if(it->second != NULL)
delete it->second;
it->second = buffer;
} else {
2020-07-22 20:55:13 +00:00
buffers.emplace(out.first, buffer);
2020-07-18 15:42:45 +00:00
}
}
2020-07-22 20:55:13 +00:00
reports.count.push(buffers.size());
}
void FlatSurroundingBox::onGui() {
ImGui::PlotHistogram("Count", reports.count.buffer.get(), reports.count.size, 0, std::to_string(reports.count.current()).c_str(), 0);
ImGui::PlotHistogram("Loading", reports.load.buffer.get(), reports.load.size, 0, std::to_string(reports.load.current()).c_str(), 0);
ImGui::PlotHistogram("Waiting", reports.loaded.buffer.get(), reports.loaded.size, 0, std::to_string(reports.loaded.current()).c_str(), 0);
ImGui::Separator();
AbstractFlat::onGui();
2020-07-18 15:42:45 +00:00
}
2020-07-24 19:42:47 +00:00
bool FlatSurroundingBox::isTransparent(const surrounding::faces &surrounding, const std::pair<ushort, ushort> &idx) {
2020-07-25 16:45:03 +00:00
return surrounding[idx.first]->get(idx.second).Density < UCHAR_MAX; // MAYBE: materials::transparent
2020-07-18 15:42:45 +00:00
}
2020-07-25 16:45:03 +00:00
void FlatSurroundingBox::render(const surrounding::faces &surrounding, std::vector<buffer::VertexData> &vertices) {
const auto center = surrounding[surrounding::CENTER];
2020-07-18 15:42:45 +00:00
vertices.clear();
for (ushort i = 0; i < CHUNK_SIZE; i++) {
2020-07-25 16:45:03 +00:00
if (center->get(i).Density > 0) {
Faces faces = center->get(i).Density < UCHAR_MAX ? Faces::All :
2020-07-18 15:42:45 +00:00
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Right)) & Faces::Right) |
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Left)) & Faces::Left) |
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Up)) & Faces::Up) |
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Down)) & Faces::Down) |
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Forward)) & Faces::Forward) |
(isTransparent(surrounding, surrounding::getNeighborIdx(i, Face::Backward)) & Faces::Backward);
2020-07-25 16:45:03 +00:00
box::addCube(vertices, world::Chunk::getPosition(i), center->get(i).Material, faces, glm::vec3(center->get(i).Density * 1.f / UCHAR_MAX));
2020-07-18 15:42:45 +00:00
}
}
}
}