From da66c30a110233f7c8b71b5e6aa8dd804879c1b6 Mon Sep 17 00:00:00 2001 From: LaG1924 Date: Sun, 5 Dec 2021 05:42:15 +0500 Subject: Added blending --- src/Gal.hpp | 7 +++++++ src/GalOgl.cpp | 21 ++++++++++++++++++++- src/Rml.cpp | 2 ++ 3 files changed, 29 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Gal.hpp b/src/Gal.hpp index 76db560..fe99dc7 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -86,6 +86,11 @@ namespace Gal { TriangleFan, }; + enum class Blending { + Opaque, + Additive, + }; + struct VertexAttribute { std::string name; Type type; @@ -186,6 +191,8 @@ namespace Gal { virtual void SetPrimitive(Primitive primitive) = 0; + virtual void SetBlending(Blending blendingMode) = 0; + virtual std::shared_ptr BindVertexBuffer(std::vector &&bufferLayout) = 0; virtual std::shared_ptr BindIndexBuffer() = 0; diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index c7344fe..4ac6c84 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -89,6 +89,7 @@ class OglState { GLuint activeTextureUnit = 0; GLint vpX = 0, vpY = 0; GLsizei vpW = 0, vpH = 0; + bool blending = false; public: @@ -162,6 +163,16 @@ public: glCheckError(); } + void EnableBlending(bool enable) { + if (enable != blending) { + blending = enable; + if (blending) + glEnable(GL_BLEND); + else + glDisable(GL_BLEND); + } + } + } oglState; std::unique_ptr impl; @@ -791,6 +802,7 @@ struct PipelineConfigOgl : public PipelineConfig { std::shared_ptr targetFb; std::vector> vertexBuffers; Primitive vertexPrimitive = Primitive::Triangle; + Blending blending = Blending::Opaque; virtual void SetVertexShader(std::shared_ptr shader) override { vertexShader = std::static_pointer_cast(shader); @@ -818,6 +830,10 @@ struct PipelineConfigOgl : public PipelineConfig { vertexPrimitive = primitive; } + virtual void SetBlending(Blending blendingMode) override { + blending = blendingMode; + } + virtual std::shared_ptr BindVertexBuffer(std::vector &&bufferLayout) override { auto binding = std::make_shared(vertexBuffers.size()); vertexBuffers.push_back(bufferLayout); @@ -901,12 +917,14 @@ struct PipelineOgl : public Pipeline { }; std::vector vertexBindCmds; Primitive primitive; + Blending blending; std::shared_ptr target; virtual void Activate() override { oglState.UseProgram(program); oglState.BindFbo(target->fbo); oglState.SetViewport(target->vpX, target->vpY, target->vpW, target->vpH); + oglState.EnableBlending(blending == Blending::Additive); if (target->fbo) glDrawBuffers(target->attachments.size(), target->attachments.data()); @@ -1076,7 +1094,7 @@ struct ImplOgl : public Impl { glCullFace(GL_BACK); glFrontFace(GL_CCW); - glEnable(GL_BLEND); + oglState.EnableBlending(true); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCheckError(); if (glActiveTexture == nullptr) { @@ -1201,6 +1219,7 @@ struct ImplOgl : public Impl { auto config = std::static_pointer_cast(pipelineConfig); pipeline->primitive = config->vertexPrimitive; + pipeline->blending = config->blending; pipeline->target = config->targetFb; if (!pipeline->target) diff --git a/src/Rml.cpp b/src/Rml.cpp index 746f6a4..f6fff44 100644 --- a/src/Rml.cpp +++ b/src/Rml.cpp @@ -68,6 +68,7 @@ RmlRenderInterface::RmlRenderInterface() { pipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); pipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); pipelineConfig->SetPixelShader(gal->LoadPixelShader(pixelSource)); + pipelineConfig->SetBlending(Gal::Blending::Additive); auto vertBuffBind = pipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, @@ -92,6 +93,7 @@ RmlRenderInterface::RmlRenderInterface() { texPipelineConfig->SetTarget(gal->GetDefaultFramebuffer()); texPipelineConfig->SetVertexShader(gal->LoadVertexShader(vertexSource)); texPipelineConfig->SetPixelShader(gal->LoadPixelShader(texPixelSource)); + texPipelineConfig->SetBlending(Gal::Blending::Additive); auto texVertBuffBind = texPipelineConfig->BindVertexBuffer({ {"pos", Gal::Type::Vec2}, -- cgit v1.2.3