From 3590bd14cd47c0d0c9a2abb4b3690ec30736c5c1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 21 Jul 2024 16:06:40 +0300 Subject: [PATCH] update DrawContext --- src/graphics/core/Batch2D.hpp | 4 +-- src/graphics/core/Batch3D.hpp | 5 +-- src/graphics/core/DrawContext.cpp | 23 +++++++++----- src/graphics/core/DrawContext.hpp | 8 +++-- src/graphics/core/LineBatch.cpp | 4 +-- src/graphics/core/LineBatch.hpp | 6 ++-- src/graphics/core/commons.hpp | 7 +++++ src/graphics/render/WorldRenderer.cpp | 11 +++---- src/objects/Entities.cpp | 44 +++++++++++++-------------- 9 files changed, 67 insertions(+), 45 deletions(-) diff --git a/src/graphics/core/Batch2D.hpp b/src/graphics/core/Batch2D.hpp index a1a1b308..ed8f9f70 100644 --- a/src/graphics/core/Batch2D.hpp +++ b/src/graphics/core/Batch2D.hpp @@ -11,7 +11,7 @@ class Mesh; class Texture; -class Batch2D { +class Batch2D : public Flushable { std::unique_ptr buffer; size_t capacity; std::unique_ptr mesh; @@ -89,7 +89,7 @@ public: float r4, float g4, float b4, int sh ); - void flush(); + void flush() override; void lineWidth(float width); }; diff --git a/src/graphics/core/Batch3D.hpp b/src/graphics/core/Batch3D.hpp index 61ef2eb0..79b17697 100644 --- a/src/graphics/core/Batch3D.hpp +++ b/src/graphics/core/Batch3D.hpp @@ -2,6 +2,7 @@ #define GRAPHICS_CORE_BATCH3D_HPP_ #include "../../typedefs.hpp" +#include "commons.hpp" #include #include @@ -11,7 +12,7 @@ class Mesh; class Texture; struct UVRegion; -class Batch3D { +class Batch3D : public Flushable { std::unique_ptr buffer; size_t capacity; std::unique_ptr mesh; @@ -54,7 +55,7 @@ public: void blockCube(const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); void point(glm::vec3 pos, glm::vec2 uv, glm::vec4 tint); void point(glm::vec3 pos, glm::vec4 tint); - void flush(); + void flush() override; void flushPoints(); }; diff --git a/src/graphics/core/DrawContext.cpp b/src/graphics/core/DrawContext.cpp index 9f10ca26..68b31125 100644 --- a/src/graphics/core/DrawContext.cpp +++ b/src/graphics/core/DrawContext.cpp @@ -29,12 +29,13 @@ DrawContext::DrawContext( Batch2D* g2d ) : parent(parent), viewport(std::move(viewport)), - g2d(g2d) + g2d(g2d), + flushable(g2d) {} DrawContext::~DrawContext() { - if (g2d) { - g2d->flush(); + if (flushable) { + flushable->flush(); } while (scissorsCount--) { @@ -73,6 +74,9 @@ DrawContext::~DrawContext() { if (blendMode != parent->blendMode) { set_blend_mode(parent->blendMode); } + if (lineWidth != parent->lineWidth) { + glLineWidth(parent->lineWidth); + } } const Viewport& DrawContext::getViewport() const { @@ -83,10 +87,10 @@ Batch2D* DrawContext::getBatch2D() const { return g2d; } -DrawContext DrawContext::sub() const { - auto ctx = DrawContext(this, viewport, g2d); - ctx.depthTest = depthTest; - ctx.cullFace = cullFace; +DrawContext DrawContext::sub(Flushable* flushable) const { + auto ctx = DrawContext(*this); + ctx.parent = this; + ctx.flushable = flushable; return ctx; } @@ -148,3 +152,8 @@ void DrawContext::setScissors(glm::vec4 area) { Window::pushScissor(area); scissorsCount++; } + +void DrawContext::setLineWidth(float width) { + lineWidth = width; + glLineWidth(width); +} diff --git a/src/graphics/core/DrawContext.hpp b/src/graphics/core/DrawContext.hpp index 6bf741d0..ea152f20 100644 --- a/src/graphics/core/DrawContext.hpp +++ b/src/graphics/core/DrawContext.hpp @@ -12,19 +12,22 @@ class DrawContext { const DrawContext* parent; Viewport viewport; Batch2D* const g2d; + Flushable* flushable = nullptr; Framebuffer* fbo = nullptr; bool depthMask = true; bool depthTest = false; bool cullFace = false; BlendMode blendMode = BlendMode::normal; int scissorsCount = 0; + float lineWidth = 1.0f; public: - DrawContext(const DrawContext* parent, Viewport viewport, Batch2D* g2d); + DrawContext(const DrawContext* parent, Viewport viewport, Batch2D* g2d); ~DrawContext(); Batch2D* getBatch2D() const; + const Viewport& getViewport() const; - DrawContext sub() const; + DrawContext sub(Flushable* flushable=nullptr) const; void setViewport(const Viewport& viewport); void setFramebuffer(Framebuffer* fbo); @@ -33,6 +36,7 @@ public: void setCullFace(bool flag); void setBlendMode(BlendMode mode); void setScissors(glm::vec4 area); + void setLineWidth(float width); }; #endif // GRAPHICS_CORE_GFX_CONTEXT_HPP_ diff --git a/src/graphics/core/LineBatch.cpp b/src/graphics/core/LineBatch.cpp index 254a25a6..dd3f8067 100644 --- a/src/graphics/core/LineBatch.cpp +++ b/src/graphics/core/LineBatch.cpp @@ -22,7 +22,7 @@ void LineBatch::line( float r, float g, float b, float a ) { if (index + LB_VERTEX_SIZE * 2 >= capacity) { - render(); + flush(); } buffer[index] = x1; buffer[index+1] = y1; @@ -65,7 +65,7 @@ void LineBatch::box(float x, float y, float z, float w, float h, float d, line(x+w, y+h, z-d, x+w, y+h, z+d, r,g,b,a); } -void LineBatch::render(){ +void LineBatch::flush(){ if (index == 0) return; mesh->reload(buffer.get(), index / LB_VERTEX_SIZE); diff --git a/src/graphics/core/LineBatch.hpp b/src/graphics/core/LineBatch.hpp index 58303dc9..a31b827d 100644 --- a/src/graphics/core/LineBatch.hpp +++ b/src/graphics/core/LineBatch.hpp @@ -5,9 +5,11 @@ #include #include +#include "commons.hpp" + class Mesh; -class LineBatch { +class LineBatch : public Flushable { std::unique_ptr mesh; std::unique_ptr buffer; size_t index; @@ -29,7 +31,7 @@ public: rgba.r, rgba.g, rgba.b, rgba.a); } - void render(); + void flush() override; void lineWidth(float width); }; diff --git a/src/graphics/core/commons.hpp b/src/graphics/core/commons.hpp index 6a8d8bb2..e6ccc57f 100644 --- a/src/graphics/core/commons.hpp +++ b/src/graphics/core/commons.hpp @@ -11,4 +11,11 @@ enum class BlendMode { normal, addition, inversion }; +class Flushable { +public: + virtual ~Flushable() = default; + + virtual void flush() = 0; +}; + #endif // GRAPHICS_CORE_COMMONS_HPP_ diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 42d5f354..b0b08714 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -243,15 +243,15 @@ void WorldRenderer::renderBlockSelection() { void WorldRenderer::renderLines( Camera* camera, Shader* linesShader, const DrawContext& pctx ) { + auto ctx = pctx.sub(lineBatch.get()); linesShader->use(); linesShader->uniformMatrix("u_projview", camera->getProjView()); if (player->selection.vox.id != BLOCK_VOID) { renderBlockSelection(); } if (player->debug && showEntitiesDebug) { - level->entities->renderDebug(*lineBatch, *frustumCulling, pctx); + level->entities->renderDebug(*lineBatch, *frustumCulling, ctx); } - lineBatch->render(); } void WorldRenderer::renderDebugLines( @@ -259,7 +259,7 @@ void WorldRenderer::renderDebugLines( Camera* camera, Shader* linesShader ) { - DrawContext ctx = pctx.sub(); + DrawContext ctx = pctx.sub(lineBatch.get()); const auto& viewport = ctx.getViewport(); uint displayWidth = viewport.getWidth(); uint displayHeight = viewport.getHeight(); @@ -296,14 +296,13 @@ void WorldRenderer::renderDebugLines( lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 1.f); - lineBatch->render(); + lineBatch->flush(); ctx.setDepthTest(true); lineBatch->lineWidth(2.0f); lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f); lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f); - lineBatch->render(); } void WorldRenderer::draw( @@ -395,5 +394,5 @@ void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) lineBatch->line(ex, i, sz, sx, i, sz, 0, 0.8f, 0, 1); } - lineBatch->render(); + lineBatch->flush(); } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index adc979df..7f2d1347 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -462,33 +462,35 @@ static void debug_render_skeleton( void Entities::renderDebug( LineBatch& batch, const Frustum& frustum, const DrawContext& pctx ) { - batch.lineWidth(1.0f); - auto view = registry.view(); - for (auto [entity, transform, rigidbody] : view.each()) { - const auto& hitbox = rigidbody.hitbox; - const auto& pos = transform.pos; - const auto& size = transform.size; - if (!frustum.isBoxVisible(pos-size, pos+size)) { - continue; - } - batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f)); - - for (auto& sensor : rigidbody.sensors) { - if (sensor.type != SensorType::AABB) + { + auto ctx = pctx.sub(&batch); + ctx.setLineWidth(1); + auto view = registry.view(); + for (auto [entity, transform, rigidbody] : view.each()) { + const auto& hitbox = rigidbody.hitbox; + const auto& pos = transform.pos; + const auto& size = transform.size; + if (!frustum.isBoxVisible(pos-size, pos+size)) { continue; - batch.box( - sensor.calculated.aabb.center(), - sensor.calculated.aabb.size(), - glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)); + } + batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f)); + + for (auto& sensor : rigidbody.sensors) { + if (sensor.type != SensorType::AABB) + continue; + batch.box( + sensor.calculated.aabb.center(), + sensor.calculated.aabb.size(), + glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)); + } } } - batch.render(); { auto view = registry.view(); - auto ctx = pctx.sub(); + auto ctx = pctx.sub(&batch); ctx.setDepthTest(false); ctx.setDepthMask(false); - batch.lineWidth(2); + ctx.setLineWidth(2); for (auto [entity, transform, skeleton] : view.each()) { auto config = skeleton.config; const auto& pos = transform.pos; @@ -499,8 +501,6 @@ void Entities::renderDebug( auto bone = config->getRoot(); debug_render_skeleton(batch, bone, skeleton); } - batch.render(); - batch.lineWidth(1); } }