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

View File

@ -2,6 +2,7 @@
#define GRAPHICS_CORE_BATCH3D_HPP_
#include "../../typedefs.hpp"
#include "commons.hpp"
#include <memory>
#include <stdlib.h>
@ -11,7 +12,7 @@ class Mesh;
class Texture;
struct UVRegion;
class Batch3D {
class Batch3D : public Flushable {
std::unique_ptr<float[]> buffer;
size_t capacity;
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 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();
};

View File

@ -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);
}

View File

@ -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_

View File

@ -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);

View File

@ -5,9 +5,11 @@
#include <stdlib.h>
#include <glm/glm.hpp>
#include "commons.hpp"
class Mesh;
class LineBatch {
class LineBatch : public Flushable {
std::unique_ptr<Mesh> mesh;
std::unique_ptr<float[]> 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);
};

View File

@ -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_

View File

@ -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();
}

View File

@ -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<Transform, Rigidbody>();
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<Transform, Rigidbody>();
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<Transform, rigging::Skeleton>();
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);
}
}