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
+2 -2
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);
};
+3 -2
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();
};
+16 -7
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);
}
+6 -2
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_
+2 -2
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);
+4 -2
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);
};
+7
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_