update DrawContext

This commit is contained in:
MihailRis 2024-07-21 16:06:40 +03:00
parent cf12338a32
commit 3590bd14cd
9 changed files with 67 additions and 45 deletions

View File

@ -11,7 +11,7 @@
class Mesh; class Mesh;
class Texture; class Texture;
class Batch2D { class Batch2D : public Flushable {
std::unique_ptr<float[]> buffer; std::unique_ptr<float[]> buffer;
size_t capacity; size_t capacity;
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
@ -89,7 +89,7 @@ public:
float r4, float g4, float b4, int sh float r4, float g4, float b4, int sh
); );
void flush(); void flush() override;
void lineWidth(float width); void lineWidth(float width);
}; };

View File

@ -2,6 +2,7 @@
#define GRAPHICS_CORE_BATCH3D_HPP_ #define GRAPHICS_CORE_BATCH3D_HPP_
#include "../../typedefs.hpp" #include "../../typedefs.hpp"
#include "commons.hpp"
#include <memory> #include <memory>
#include <stdlib.h> #include <stdlib.h>
@ -11,7 +12,7 @@ class Mesh;
class Texture; class Texture;
struct UVRegion; struct UVRegion;
class Batch3D { class Batch3D : public Flushable {
std::unique_ptr<float[]> buffer; std::unique_ptr<float[]> buffer;
size_t capacity; size_t capacity;
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
@ -54,7 +55,7 @@ public:
void blockCube(const glm::vec3 size, const UVRegion(&texfaces)[6], const glm::vec4 tint, bool shading=true); 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::vec2 uv, glm::vec4 tint);
void point(glm::vec3 pos, glm::vec4 tint); void point(glm::vec3 pos, glm::vec4 tint);
void flush(); void flush() override;
void flushPoints(); void flushPoints();
}; };

View File

@ -29,12 +29,13 @@ DrawContext::DrawContext(
Batch2D* g2d Batch2D* g2d
) : parent(parent), ) : parent(parent),
viewport(std::move(viewport)), viewport(std::move(viewport)),
g2d(g2d) g2d(g2d),
flushable(g2d)
{} {}
DrawContext::~DrawContext() { DrawContext::~DrawContext() {
if (g2d) { if (flushable) {
g2d->flush(); flushable->flush();
} }
while (scissorsCount--) { while (scissorsCount--) {
@ -73,6 +74,9 @@ DrawContext::~DrawContext() {
if (blendMode != parent->blendMode) { if (blendMode != parent->blendMode) {
set_blend_mode(parent->blendMode); set_blend_mode(parent->blendMode);
} }
if (lineWidth != parent->lineWidth) {
glLineWidth(parent->lineWidth);
}
} }
const Viewport& DrawContext::getViewport() const { const Viewport& DrawContext::getViewport() const {
@ -83,10 +87,10 @@ Batch2D* DrawContext::getBatch2D() const {
return g2d; return g2d;
} }
DrawContext DrawContext::sub() const { DrawContext DrawContext::sub(Flushable* flushable) const {
auto ctx = DrawContext(this, viewport, g2d); auto ctx = DrawContext(*this);
ctx.depthTest = depthTest; ctx.parent = this;
ctx.cullFace = cullFace; ctx.flushable = flushable;
return ctx; return ctx;
} }
@ -148,3 +152,8 @@ void DrawContext::setScissors(glm::vec4 area) {
Window::pushScissor(area); Window::pushScissor(area);
scissorsCount++; scissorsCount++;
} }
void DrawContext::setLineWidth(float width) {
lineWidth = width;
glLineWidth(width);
}

View File

@ -12,19 +12,22 @@ class DrawContext {
const DrawContext* parent; const DrawContext* parent;
Viewport viewport; Viewport viewport;
Batch2D* const g2d; Batch2D* const g2d;
Flushable* flushable = nullptr;
Framebuffer* fbo = nullptr; Framebuffer* fbo = nullptr;
bool depthMask = true; bool depthMask = true;
bool depthTest = false; bool depthTest = false;
bool cullFace = false; bool cullFace = false;
BlendMode blendMode = BlendMode::normal; BlendMode blendMode = BlendMode::normal;
int scissorsCount = 0; int scissorsCount = 0;
float lineWidth = 1.0f;
public: public:
DrawContext(const DrawContext* parent, Viewport viewport, Batch2D* g2d); DrawContext(const DrawContext* parent, Viewport viewport, Batch2D* g2d);
~DrawContext(); ~DrawContext();
Batch2D* getBatch2D() const; Batch2D* getBatch2D() const;
const Viewport& getViewport() const; const Viewport& getViewport() const;
DrawContext sub() const; DrawContext sub(Flushable* flushable=nullptr) const;
void setViewport(const Viewport& viewport); void setViewport(const Viewport& viewport);
void setFramebuffer(Framebuffer* fbo); void setFramebuffer(Framebuffer* fbo);
@ -33,6 +36,7 @@ public:
void setCullFace(bool flag); void setCullFace(bool flag);
void setBlendMode(BlendMode mode); void setBlendMode(BlendMode mode);
void setScissors(glm::vec4 area); void setScissors(glm::vec4 area);
void setLineWidth(float width);
}; };
#endif // GRAPHICS_CORE_GFX_CONTEXT_HPP_ #endif // GRAPHICS_CORE_GFX_CONTEXT_HPP_

View File

@ -22,7 +22,7 @@ void LineBatch::line(
float r, float g, float b, float a float r, float g, float b, float a
) { ) {
if (index + LB_VERTEX_SIZE * 2 >= capacity) { if (index + LB_VERTEX_SIZE * 2 >= capacity) {
render(); flush();
} }
buffer[index] = x1; buffer[index] = x1;
buffer[index+1] = y1; 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); 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) if (index == 0)
return; return;
mesh->reload(buffer.get(), index / LB_VERTEX_SIZE); mesh->reload(buffer.get(), index / LB_VERTEX_SIZE);

View File

@ -5,9 +5,11 @@
#include <stdlib.h> #include <stdlib.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "commons.hpp"
class Mesh; class Mesh;
class LineBatch { class LineBatch : public Flushable {
std::unique_ptr<Mesh> mesh; std::unique_ptr<Mesh> mesh;
std::unique_ptr<float[]> buffer; std::unique_ptr<float[]> buffer;
size_t index; size_t index;
@ -29,7 +31,7 @@ public:
rgba.r, rgba.g, rgba.b, rgba.a); rgba.r, rgba.g, rgba.b, rgba.a);
} }
void render(); void flush() override;
void lineWidth(float width); void lineWidth(float width);
}; };

View File

@ -11,4 +11,11 @@ enum class BlendMode {
normal, addition, inversion normal, addition, inversion
}; };
class Flushable {
public:
virtual ~Flushable() = default;
virtual void flush() = 0;
};
#endif // GRAPHICS_CORE_COMMONS_HPP_ #endif // GRAPHICS_CORE_COMMONS_HPP_

View File

@ -243,15 +243,15 @@ void WorldRenderer::renderBlockSelection() {
void WorldRenderer::renderLines( void WorldRenderer::renderLines(
Camera* camera, Shader* linesShader, const DrawContext& pctx Camera* camera, Shader* linesShader, const DrawContext& pctx
) { ) {
auto ctx = pctx.sub(lineBatch.get());
linesShader->use(); linesShader->use();
linesShader->uniformMatrix("u_projview", camera->getProjView()); linesShader->uniformMatrix("u_projview", camera->getProjView());
if (player->selection.vox.id != BLOCK_VOID) { if (player->selection.vox.id != BLOCK_VOID) {
renderBlockSelection(); renderBlockSelection();
} }
if (player->debug && showEntitiesDebug) { if (player->debug && showEntitiesDebug) {
level->entities->renderDebug(*lineBatch, *frustumCulling, pctx); level->entities->renderDebug(*lineBatch, *frustumCulling, ctx);
} }
lineBatch->render();
} }
void WorldRenderer::renderDebugLines( void WorldRenderer::renderDebugLines(
@ -259,7 +259,7 @@ void WorldRenderer::renderDebugLines(
Camera* camera, Camera* camera,
Shader* linesShader Shader* linesShader
) { ) {
DrawContext ctx = pctx.sub(); DrawContext ctx = pctx.sub(lineBatch.get());
const auto& viewport = ctx.getViewport(); const auto& viewport = ctx.getViewport();
uint displayWidth = viewport.getWidth(); uint displayWidth = viewport.getWidth();
uint displayHeight = viewport.getHeight(); 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, 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, 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->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); ctx.setDepthTest(true);
lineBatch->lineWidth(2.0f); 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, 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, 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->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( 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, lineBatch->line(ex, i, sz,
sx, i, sz, 0, 0.8f, 0, 1); sx, i, sz, 0, 0.8f, 0, 1);
} }
lineBatch->render(); lineBatch->flush();
} }

View File

@ -462,33 +462,35 @@ static void debug_render_skeleton(
void Entities::renderDebug( void Entities::renderDebug(
LineBatch& batch, const Frustum& frustum, const DrawContext& pctx LineBatch& batch, const Frustum& frustum, const DrawContext& pctx
) { ) {
batch.lineWidth(1.0f); {
auto view = registry.view<Transform, Rigidbody>(); auto ctx = pctx.sub(&batch);
for (auto [entity, transform, rigidbody] : view.each()) { ctx.setLineWidth(1);
const auto& hitbox = rigidbody.hitbox; auto view = registry.view<Transform, Rigidbody>();
const auto& pos = transform.pos; for (auto [entity, transform, rigidbody] : view.each()) {
const auto& size = transform.size; const auto& hitbox = rigidbody.hitbox;
if (!frustum.isBoxVisible(pos-size, pos+size)) { const auto& pos = transform.pos;
continue; const auto& size = transform.size;
} if (!frustum.isBoxVisible(pos-size, pos+size)) {
batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
for (auto& sensor : rigidbody.sensors) {
if (sensor.type != SensorType::AABB)
continue; continue;
batch.box( }
sensor.calculated.aabb.center(), batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
sensor.calculated.aabb.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 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<Transform, rigging::Skeleton>(); auto view = registry.view<Transform, rigging::Skeleton>();
auto ctx = pctx.sub(); auto ctx = pctx.sub(&batch);
ctx.setDepthTest(false); ctx.setDepthTest(false);
ctx.setDepthMask(false); ctx.setDepthMask(false);
batch.lineWidth(2); ctx.setLineWidth(2);
for (auto [entity, transform, skeleton] : view.each()) { for (auto [entity, transform, skeleton] : view.each()) {
auto config = skeleton.config; auto config = skeleton.config;
const auto& pos = transform.pos; const auto& pos = transform.pos;
@ -499,8 +501,6 @@ void Entities::renderDebug(
auto bone = config->getRoot(); auto bone = config->getRoot();
debug_render_skeleton(batch, bone, skeleton); debug_render_skeleton(batch, bone, skeleton);
} }
batch.render();
batch.lineWidth(1);
} }
} }