From 8315b3255047083ff2ea5c4479de5f6b71a68952 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Fri, 18 Jan 2019 11:11:00 +0100 Subject: [PATCH] implement brush outline --- src/app_shaders.cpp | 10 +++++++++- src/canvas.cpp | 3 ++- src/texture.cpp | 6 ++++++ src/texture.h | 1 + 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/app_shaders.cpp b/src/app_shaders.cpp index 47b7275..cd70fb3 100644 --- a/src/app_shaders.cpp +++ b/src/app_shaders.cpp @@ -161,7 +161,15 @@ void App::initShaders() "out mediump vec4 frag;\n" "void main() {\n" " mediump float stroke = 1.0 - texture(tex, uv).r;\n" - " frag = vec4(col.rgb, stroke * alpha);\n" + " int zero_count = 0;\n" + " for (int x = -1; x <= 1; x++){\n" + " for (int y = -1; y <= 1; y++){\n" + " if (textureOffset(tex, uv, ivec2(x, y)).r == 1.0) \n" + " zero_count++;\n" + " }\n" + " }\n" + " float edge = (zero_count > 1 && zero_count < 9) ? 0.75 : 0.0;\n" + " frag = vec4(col.rgb, edge);\n" "}\n"; // TEXTURE COMP ERASE static const char* shader_comp_erase_f = diff --git a/src/canvas.cpp b/src/canvas.cpp index 28cf3b5..b55441c 100644 --- a/src/canvas.cpp +++ b/src/canvas.cpp @@ -1090,8 +1090,9 @@ bool Canvas::create(int width, int height) } m_sampler.create(GL_NEAREST); m_sampler.create(GL_LINEAR); - m_sampler_brush.create(); + m_sampler_brush.create(GL_LINEAR, GL_CLAMP_TO_BORDER); m_sampler_brush.set_filter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR); + m_sampler_brush.set_border({ 1, 1, 1, 1 }); m_sampler_bg.create(GL_NEAREST); m_sampler_mask.create(GL_LINEAR); m_sampler_stencil.create(GL_LINEAR, GL_REPEAT); diff --git a/src/texture.cpp b/src/texture.cpp index a93de34..54a0d98 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -134,6 +134,12 @@ void Sampler::set_filter(GLint filter_min, GLint filter_mag) glSamplerParameteri(id, GL_TEXTURE_MAG_FILTER, filter_mag); #endif // USE_SAMPLER } +void Sampler::set_border(glm::vec4 rgba) +{ +#if USE_SAMPLER + glSamplerParameterfv(id, GL_TEXTURE_BORDER_COLOR, glm::value_ptr(rgba)); +#endif // USE_SAMPLER +} void Sampler::bind(int unit) const { current_unit = unit; diff --git a/src/texture.h b/src/texture.h index bae25b1..13cf103 100644 --- a/src/texture.h +++ b/src/texture.h @@ -32,6 +32,7 @@ public: bool create(GLint filter = GL_LINEAR, GLint wrap = GL_CLAMP_TO_EDGE); void set(GLint filter = GL_LINEAR, GLint wrap = GL_CLAMP_TO_EDGE); void set_filter(GLint filter_min, GLint filter_mag); + void set_border(glm::vec4 rgba); void bind(int unit) const; void unbind(); bool ready() const { return id != 0; }