Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 14, 2024
1 parent e9163f4 commit f0b6521
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 169 deletions.
3 changes: 2 additions & 1 deletion src/frontend/debug_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "graphics/ui/elements/InputBindBox.hpp"
#include "graphics/render/WorldRenderer.hpp"
#include "graphics/render/ParticlesRenderer.hpp"
#include "graphics/render/ChunksRenderer.hpp"
#include "logic/scripting/scripting.hpp"
#include "objects/Player.hpp"
#include "objects/Entities.hpp"
Expand Down Expand Up @@ -95,7 +96,7 @@ std::shared_ptr<UINode> create_debug_panel(
}));
panel->add(create_label([=]() {
return L"chunks: "+std::to_wstring(level->chunks->getChunksCount())+
L" visible: "+std::to_wstring(level->chunks->visible);
L" visible: "+std::to_wstring(ChunksRenderer::visibleChunks);
}));
panel->add(create_label([=]() {
return L"entities: "+std::to_wstring(level->entities->size())+L" next: "+
Expand Down
99 changes: 92 additions & 7 deletions src/graphics/render/ChunksRenderer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
#include "ChunksRenderer.hpp"
#include "BlocksRenderer.hpp"
#include "debug/Logger.hpp"
#include "assets/Assets.hpp"
#include "graphics/core/Mesh.hpp"
#include "graphics/core/Shader.hpp"
#include "graphics/core/Texture.hpp"
#include "graphics/core/Atlas.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp"
#include "world/Level.hpp"
#include "window/Camera.hpp"
#include "maths/FrustumCulling.hpp"
#include "util/listutil.hpp"
#include "settings.hpp"

#include <iostream>
Expand All @@ -12,6 +20,8 @@

static debug::Logger logger("chunks-render");

size_t ChunksRenderer::visibleChunks = 0;

class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResult> {
Level* level;
BlocksRenderer renderer;
Expand Down Expand Up @@ -39,9 +49,14 @@ class RendererWorker : public util::Worker<std::shared_ptr<Chunk>, RendererResul

ChunksRenderer::ChunksRenderer(
Level* level,
const Assets& assets,
const Frustum& frustum,
const ContentGfxCache* cache,
const EngineSettings* settings
) : level(level),
assets(assets),
frustum(frustum),
settings(settings),
threadPool(
"chunks-render-pool",
[=](){return std::make_shared<RendererWorker>(level, cache, settings);},
Expand Down Expand Up @@ -103,14 +118,84 @@ std::shared_ptr<Mesh> ChunksRenderer::getOrRender(const std::shared_ptr<Chunk>&
return found->second;
}

std::shared_ptr<Mesh> ChunksRenderer::get(Chunk* chunk) {
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
if (found != meshes.end()) {
return found->second;
void ChunksRenderer::update() {
threadPool.update();
}

bool ChunksRenderer::drawChunk(
size_t index, const Camera& camera, Shader& shader, bool culling
) {
auto chunk = level->chunks->getChunks()[index];
if (chunk == nullptr || !chunk->flags.lighted) {
return false;
}
return nullptr;
float distance = glm::distance(
camera.position,
glm::vec3(
(chunk->x + 0.5f) * CHUNK_W,
camera.position.y,
(chunk->z + 0.5f) * CHUNK_D
)
);
auto mesh = getOrRender(chunk, distance < CHUNK_W * 1.5f);
if (mesh == nullptr) {
return false;
}
if (culling) {
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
glm::vec3 max(
chunk->x * CHUNK_W + CHUNK_W,
chunk->top,
chunk->z * CHUNK_D + CHUNK_D
);

if (!frustum.isBoxVisible(min, max)) return false;
}
glm::vec3 coord(chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f);
glm::mat4 model = glm::translate(glm::mat4(1.0f), coord);
shader.uniformMatrix("u_model", model);
mesh->draw();
return true;
}

void ChunksRenderer::update() {
threadPool.update();
void ChunksRenderer::drawChunks(
const Camera& camera, Shader& shader
) {
const auto& chunks = *level->chunks;
const auto& atlas = assets.require<Atlas>("blocks");

atlas.getTexture()->bind();
update();

// [warning] this whole method is not thread-safe for chunks

int chunksWidth = chunks.getWidth();
int chunksOffsetX = chunks.getOffsetX();
int chunksOffsetY = chunks.getOffsetY();

if (indices.size() != chunks.getVolume()) {
indices.clear();
for (int i = 0; i < chunks.getVolume(); i++) {
indices.push_back(ChunksSortEntry {i, 0});
}
}
float px = camera.position.x / static_cast<float>(CHUNK_W) - 0.5f;
float pz = camera.position.z / static_cast<float>(CHUNK_D) - 0.5f;
for (auto& index : indices) {
float x = index.index % chunksWidth + chunksOffsetX - px;
float z = index.index / chunksWidth + chunksOffsetY - pz;
index.d = (x * x + z * z) * 1024;
}
util::insertion_sort(indices.begin(), indices.end());

bool culling = settings->graphics.frustumCulling.get();

visibleChunks = 0;
//if (GLEW_ARB_multi_draw_indirect && false) {
// TODO: implement Multi Draw Indirect chunks draw
//} else {
for (size_t i = 0; i < indices.size(); i++) {
visibleChunks += drawChunk(indices[i].index, camera, shader, culling);
}
//}
}
38 changes: 34 additions & 4 deletions src/graphics/render/ChunksRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,24 @@
class Mesh;
class Chunk;
class Level;
class Camera;
class Shader;
class Chunks;
class Assets;
class Frustum;
class BlocksRenderer;
class ContentGfxCache;
struct EngineSettings;

struct ChunksSortEntry {
int index;
int d;

inline bool operator<(const ChunksSortEntry& o) const noexcept {
return d > o.d;
}
};

struct RendererResult {
glm::ivec2 key;
bool cancelled;
Expand All @@ -26,25 +40,41 @@ struct RendererResult {

class ChunksRenderer {
Level* level;
const Assets& assets;
const Frustum& frustum;
const EngineSettings* settings;
std::unique_ptr<BlocksRenderer> renderer;
std::unordered_map<glm::ivec2, std::shared_ptr<Mesh>> meshes;
std::unordered_map<glm::ivec2, bool> inwork;
std::vector<ChunksSortEntry> indices;

util::ThreadPool<std::shared_ptr<Chunk>, RendererResult> threadPool;

bool drawChunk(
size_t index, const Camera& camera, Shader& shader, bool culling
);
public:
ChunksRenderer(
Level* level,
Level* level,
const Assets& assets,
const Frustum& frustum,
const ContentGfxCache* cache,
const EngineSettings* settings
);
virtual ~ChunksRenderer();

std::shared_ptr<Mesh> render(const std::shared_ptr<Chunk>& chunk, bool important);
std::shared_ptr<Mesh> render(
const std::shared_ptr<Chunk>& chunk, bool important
);
void unload(const Chunk* chunk);
void clear();

std::shared_ptr<Mesh> getOrRender(const std::shared_ptr<Chunk>& chunk, bool important);
std::shared_ptr<Mesh> get(Chunk* chunk);
std::shared_ptr<Mesh> getOrRender(
const std::shared_ptr<Chunk>& chunk, bool important
);
void drawChunks(const Camera& camera, Shader& shader);

void update();

static size_t visibleChunks;
};
12 changes: 6 additions & 6 deletions src/graphics/render/ModelBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ static glm::mat4 extract_rotation(glm::mat4 matrix) {

ModelBatch::ModelBatch(
size_t capacity,
Assets* assets,
Chunks* chunks,
const EngineSettings* settings
const Assets& assets,
const Chunks& chunks,
const EngineSettings& settings
)
: batch(std::make_unique<MainBatch>(capacity)),
assets(assets),
Expand All @@ -73,7 +73,7 @@ void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix,
if (mesh.lighting) {
glm::vec3 gpos = matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
gpos += lightsOffset;
lights = MainBatch::sampleLight(gpos, *chunks, backlight);
lights = MainBatch::sampleLight(gpos, chunks, backlight);
}
for (size_t i = 0; i < vcount / 3; i++) {
batch->prepare(3);
Expand Down Expand Up @@ -107,7 +107,7 @@ void ModelBatch::render() {
return a.mesh->texture < b.mesh->texture;
}
);
bool backlight = settings->graphics.backlight.get();
bool backlight = settings.graphics.backlight.get();
for (auto& entry : entries) {
draw(
*entry.mesh,
Expand Down Expand Up @@ -136,6 +136,6 @@ void ModelBatch::setTexture(const std::string& name,
return setTexture(found->second, varTextures);
}
}
auto region = util::get_texture_region(*assets, name, "blocks:notfound");
auto region = util::get_texture_region(assets, name, "blocks:notfound");
batch->setTexture(region.texture, region.region);
}
13 changes: 7 additions & 6 deletions src/graphics/render/ModelBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ namespace model {
using texture_names_map = std::unordered_map<std::string, std::string>;

class ModelBatch {
Assets* assets;
Chunks* chunks;
const Assets& assets;
const Chunks& chunks;

const EngineSettings* settings;
const EngineSettings& settings;
glm::vec3 lightsOffset {};

static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f};
Expand All @@ -39,6 +39,7 @@ class ModelBatch {
glm::vec3 tint,
const texture_names_map* varTextures,
bool backlight);

void setTexture(const std::string& name,
const texture_names_map* varTextures);

Expand All @@ -53,9 +54,9 @@ class ModelBatch {
public:
ModelBatch(
size_t capacity,
Assets* assets,
Chunks* chunks,
const EngineSettings* settings
const Assets& assets,
const Chunks& chunks,
const EngineSettings& settings
);
~ModelBatch();

Expand Down
16 changes: 7 additions & 9 deletions src/graphics/render/TextsRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#include "graphics/core/Shader.hpp"
#include "presets/NotePreset.hpp"

TextsRenderer::TextsRenderer(const Frustum* frustum) : frustum(frustum) {
TextsRenderer::TextsRenderer(
Batch3D& batch, const Assets& assets, const Frustum& frustum
)
: batch(batch), assets(assets), frustum(frustum) {
}

void TextsRenderer::renderText(
Batch3D& batch,
const TextNote& note,
const DrawContext& context,
const Assets& assets,
const Camera& camera,
const EngineSettings& settings,
bool hudVisible
Expand Down Expand Up @@ -54,8 +55,8 @@ void TextsRenderer::renderText(
}

if (preset.displayMode != NoteDisplayMode::PROJECTED) {
if (!frustum->isBoxVisible(pos - xvec * (width * 0.5f),
pos + xvec * (width * 0.5f))) {
if (!frustum.isBoxVisible(pos - xvec * (width * 0.5f),
pos + xvec * (width * 0.5f))) {
return;
}
}
Expand All @@ -71,9 +72,7 @@ void TextsRenderer::renderText(
}

void TextsRenderer::renderTexts(
Batch3D& batch,
const DrawContext& context,
const Assets& assets,
const Camera& camera,
const EngineSettings& settings,
bool hudVisible,
Expand All @@ -96,9 +95,8 @@ void TextsRenderer::renderTexts(
shader.uniformMatrix("u_projview", camera.getProjView());
shader.uniformMatrix("u_apply", glm::mat4(1.0f));
batch.begin();

for (const auto& note : notes) {
renderText(batch, note, context, assets, camera, settings, hudVisible);
renderText(note, context, camera, settings, hudVisible);
}
batch.flush();
}
10 changes: 4 additions & 6 deletions src/graphics/render/TextsRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,22 @@ class TextNote;
struct EngineSettings;

class TextsRenderer {
const Frustum* frustum;
Batch3D& batch;
const Assets& assets;
const Frustum& frustum;

void renderText(
Batch3D& batch,
const TextNote& note,
const DrawContext& context,
const Assets& assets,
const Camera& camera,
const EngineSettings& settings,
bool hudVisible
);
public:
TextsRenderer(const Frustum* frustum);
TextsRenderer(Batch3D& batch, const Assets& assets, const Frustum& frustum);

void renderTexts(
Batch3D& batch,
const DrawContext& context,
const Assets& assets,
const Camera& camera,
const EngineSettings& settings,
bool hudVisible,
Expand Down
Loading

0 comments on commit f0b6521

Please sign in to comment.