From 8ec4c2c30b0a6d313ca5df5ff83210fd4f553908 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Mon, 13 May 2024 22:24:06 -0400 Subject: [PATCH] working outlining --- CMakeLists.txt | 2 +- include/blt/gfx/renderer/batch_2d_renderer.h | 6 +- .../blt/gfx/renderer/shaders/2d_textured.frag | 8 +-- .../renderer/shaders/2d_textured_circle.frag | 8 +-- src/blt/gfx/renderer/batch_2d_renderer.cpp | 64 ++++++++++--------- 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index abc8a6d..6936495 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -set(BLT_GRAPHICS_VERSION 0.13.16) +set(BLT_GRAPHICS_VERSION 0.13.17) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) diff --git a/include/blt/gfx/renderer/batch_2d_renderer.h b/include/blt/gfx/renderer/batch_2d_renderer.h index 86c97a2..57560be 100644 --- a/include/blt/gfx/renderer/batch_2d_renderer.h +++ b/include/blt/gfx/renderer/batch_2d_renderer.h @@ -206,9 +206,9 @@ namespace blt::gfx // called after draw_objects() void post_reset(); - void draw_points(bool outlined, const f32 denominator); - void draw_lines(bool outlined, const f32 denominator); - void draw_rectangles(bool outlined, const f32 denominator); + void draw_points(bool outlined, bool clear, const f32 denominator); + void draw_lines(bool outlined, bool clear, const f32 denominator); + void draw_rectangles(bool outlined, bool clear, const f32 denominator); void draw_objects(); diff --git a/include/blt/gfx/renderer/shaders/2d_textured.frag b/include/blt/gfx/renderer/shaders/2d_textured.frag index 61f7cdc..66084cb 100644 --- a/include/blt/gfx/renderer/shaders/2d_textured.frag +++ b/include/blt/gfx/renderer/shaders/2d_textured.frag @@ -5,14 +5,14 @@ const std::string shader_2d_textured_frag = R"(" precision mediump float; layout (location = 0) out vec4 FragColor; -layout (location = 1) out vec4 OutMask; +//layout (location = 1) out vec4 OutMask; in vec2 uv; in vec2 pos; uniform sampler2D tex; uniform vec4 color; uniform vec4 use_texture; -uniform vec4 outline_color; +//uniform vec4 outline_color; vec4 linear_iter(vec4 i, vec4 p, float factor){ return (i + p) * factor; @@ -20,8 +20,8 @@ vec4 linear_iter(vec4 i, vec4 p, float factor){ void main() { FragColor = (texture(tex, uv) * use_texture) + color; - OutMask = outline_color; - OutMask.a = FragColor.a; + //OutMask = outline_color; + //OutMask.a = FragColor.a; } ")"; diff --git a/include/blt/gfx/renderer/shaders/2d_textured_circle.frag b/include/blt/gfx/renderer/shaders/2d_textured_circle.frag index 3bb7d71..9598f8c 100644 --- a/include/blt/gfx/renderer/shaders/2d_textured_circle.frag +++ b/include/blt/gfx/renderer/shaders/2d_textured_circle.frag @@ -5,14 +5,14 @@ const std::string shader_2d_textured_cirlce_frag = R"(" precision mediump float; layout (location = 0) out vec4 FragColor; -layout (location = 1) out vec4 OutMask; +//layout (location = 1) out vec4 OutMask; in vec2 uv; in vec2 pos; uniform sampler2D tex; uniform vec4 color; uniform vec4 use_texture; -uniform vec4 outline_color; +//uniform vec4 outline_color; const float offset = 1.0 / 32.0; @@ -28,8 +28,8 @@ void main() { if (ts >= sq) discard; FragColor = (texture(tex, uv) * use_texture) + color; - OutMask = outline_color; - OutMask.a = FragColor.a; + //OutMask = outline_color; + //OutMask.a = FragColor.a; } ")"; diff --git a/src/blt/gfx/renderer/batch_2d_renderer.cpp b/src/blt/gfx/renderer/batch_2d_renderer.cpp index 6a050a4..0326ada 100644 --- a/src/blt/gfx/renderer/batch_2d_renderer.cpp +++ b/src/blt/gfx/renderer/batch_2d_renderer.cpp @@ -188,35 +188,35 @@ namespace blt::gfx const f32 denominator = 1.0f / (draw.z_max - draw.z_min); glEnable(GL_STENCIL_TEST); + glClear(GL_STENCIL_BUFFER_BIT); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); - - glClear(GL_STENCIL_BUFFER_BIT); glStencilMask(0xFF); + glStencilFunc(GL_ALWAYS, 1, 0xFF); - draw_points(false, denominator); + draw_points(true, false, denominator); - glStencilFunc(GL_NOTEQUAL, 1, 0xFF); - glStencilMask(0x00); - draw_points(true, denominator); + glDisable(GL_DEPTH_TEST); + glStencilFunc(GL_EQUAL, 1, 0xFF); + draw_points(false, true, denominator); + glEnable(GL_DEPTH_TEST); - glClear(GL_STENCIL_BUFFER_BIT); - glStencilMask(0xFF); glStencilFunc(GL_ALWAYS, 2, 0xFF); - draw_rectangles(false, denominator); + draw_rectangles(true, false, denominator); - glStencilFunc(GL_NOTEQUAL, 2, 0xFF); - glStencilMask(0x00); - draw_rectangles(true, denominator); + glDisable(GL_DEPTH_TEST); + glStencilFunc(GL_EQUAL, 2, 0xFF); + draw_rectangles(false, true, denominator); + glEnable(GL_DEPTH_TEST); - glClear(GL_STENCIL_BUFFER_BIT); - glStencilMask(0xFF); glStencilFunc(GL_ALWAYS, 4, 0xFF); - draw_lines(false, denominator); + draw_lines(true, false, denominator); + + glDisable(GL_DEPTH_TEST); + glStencilFunc(GL_EQUAL, 4, 0xFF); + draw_lines(false, true, denominator); + glEnable(GL_DEPTH_TEST); - glStencilFunc(GL_NOTEQUAL, 4, 0xFF); glStencilMask(0x00); - draw_lines(true, denominator); - glDisable(GL_STENCIL_TEST); post_reset(); } @@ -237,7 +237,7 @@ namespace blt::gfx } - void batch_renderer_2d::draw_points(const bool outlined, const f32 denominator) + void batch_renderer_2d::draw_points(const bool outlined, bool clear, const f32 denominator) { point_shader->bind(); square_vao->bind(); @@ -266,19 +266,19 @@ namespace blt::gfx point_shader->setVec4("use_texture", render_info.blend); } - point_shader->setVec4("outline_color", render_info.outline_color); + //point_shader->setVec4("outline_color", render_info.outline_color); point_shader->setFloat("z_index", (z_index - draw.z_min) * denominator); point_shader->setMatrix("model", model); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); draw.draw_count++; } - if (outlined) + if (clear) objects.clear(); } } - void batch_renderer_2d::draw_lines(bool outlined, const f32 denominator) + void batch_renderer_2d::draw_lines(bool outlined, bool clear, const f32 denominator) { mat4x4 empty_model; square_shader->setMatrix("model", empty_model); @@ -294,17 +294,19 @@ namespace blt::gfx { auto& [z_index, line] = object; + float thickness = line.thickness; if (outlined){ if (!render_info.outline) continue; square_shader->setVec4("color", render_info.outline_color); square_shader->setVec4("use_texture", {0,0,0,0}); + thickness *= render_info.outline_thickness; } else { square_shader->setVec4("color", render_info.color); square_shader->setVec4("use_texture", render_info.blend); } - square_shader->setVec4("outline_color", render_info.outline_color); + //square_shader->setVec4("outline_color", render_info.outline_color); square_shader->setFloat("z_index", (z_index - draw.z_min) * denominator); // 0, 1 (top right) @@ -315,11 +317,11 @@ namespace blt::gfx vec2 right = {dir.y(), -dir.x()}; vec2 left = {-dir.y(), dir.x()}; - auto bottom_left = line.p1 + left * line.thickness; - auto bottom_right = line.p1 + right * line.thickness; + auto bottom_left = line.p1 + left * thickness; + auto bottom_right = line.p1 + right * thickness; - auto top_left = line.p2 + left * line.thickness; - auto top_right = line.p2 + right * line.thickness; + auto top_left = line.p2 + left * thickness; + auto top_right = line.p2 + right * thickness; line_vertices[0] = top_right.x(); line_vertices[1] = top_right.y(); @@ -338,12 +340,12 @@ namespace blt::gfx glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); draw.draw_count++; } - if (outlined) + if (clear) objects.clear(); } } - void batch_renderer_2d::draw_rectangles(bool outlined, const f32 denominator) + void batch_renderer_2d::draw_rectangles(bool outlined, bool clear, const f32 denominator) { square_shader->bind(); square_vao->bind(); @@ -377,13 +379,13 @@ namespace blt::gfx if (rect.rotation != 0) model.rotateZ(toRadians(rect.rotation)); - square_shader->setVec4("outline_color", render_info.outline_color); + //square_shader->setVec4("outline_color", render_info.outline_color); square_shader->setFloat("z_index", (z_index - draw.z_min) * denominator); square_shader->setMatrix("model", model); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); draw.draw_count++; } - if (outlined) + if (clear) objects.clear(); } }