aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/RoundedRectangle.hpp1
-rw-r--r--shaders/rounded_rectangle.glsl13
-rw-r--r--shaders/rounded_rectangle_mask.glsl6
-rw-r--r--src/RoundedRectangle.cpp87
4 files changed, 19 insertions, 88 deletions
diff --git a/include/RoundedRectangle.hpp b/include/RoundedRectangle.hpp
index 2ebd915..42ffbad 100644
--- a/include/RoundedRectangle.hpp
+++ b/include/RoundedRectangle.hpp
@@ -27,7 +27,6 @@ namespace QuickMedia {
sf::Vector2f pos;
sf::Vector2f size;
sf::Vertex vertices[4];
- sf::Vertex drop_shadow_vertices[32];
sf::Shader *rounded_rectangle_shader;
sf::Vector2f band_pos;
sf::Vector2f band_size;
diff --git a/shaders/rounded_rectangle.glsl b/shaders/rounded_rectangle.glsl
index 956e28e..8c379b7 100644
--- a/shaders/rounded_rectangle.glsl
+++ b/shaders/rounded_rectangle.glsl
@@ -5,18 +5,21 @@ uniform vec4 band_color;
uniform vec2 resolution;
float rounded_rect(vec2 coord, vec2 size, float r) {
- return smoothstep(0.0, 2.0, length(max(abs(coord) - size, 0.0)) - r);
+ return length(max(abs(coord) - size+r, 0.0)) - r;
}
void main() {
+ vec2 shadow_offset = vec2(20.0, 20.0);
vec2 uv = gl_TexCoord[0].xy * resolution;
vec2 center = resolution * 0.5;
- vec2 size = resolution * 0.5;
+ vec2 size = (resolution - shadow_offset * 2.0) * 0.5;
- float a = clamp(rounded_rect(uv - center, size - radius, radius), 0.0, 1.0);
+ float rect_dist = rounded_rect(uv - center, size, radius);
+ float a = 1.0 - smoothstep(0.0, 2.0, rect_dist);
// same as: if(uv.x >= band_pos.x && uv.x <= band_pos.x + band_size.x && uv.y >= band_pos.y && uv.y <= band_pos.y + band_size.y)
vec2 band_blend = step(band_pos, uv) - step(band_pos + band_size, uv);
vec4 front_color = mix(gl_Color, band_color, band_blend.x*band_blend.y);
- front_color.a *= (1.0 - a);
- gl_FragColor = front_color;
+ float shadow_a = 1.0 - smoothstep(0.0, shadow_offset.x, rect_dist);
+ front_color.a *= a;
+ gl_FragColor = mix(front_color, vec4(0.0, 0.0, 0.0, 0.135), shadow_a - a);
} \ No newline at end of file
diff --git a/shaders/rounded_rectangle_mask.glsl b/shaders/rounded_rectangle_mask.glsl
index a4546e3..8cbe0fc 100644
--- a/shaders/rounded_rectangle_mask.glsl
+++ b/shaders/rounded_rectangle_mask.glsl
@@ -3,7 +3,7 @@ uniform float radius;
uniform vec2 resolution;
float rounded_rect(vec2 coord, vec2 size, float r) {
- return smoothstep(0.0, 2.0, length(max(abs(coord) - size, 0.0)) - r);
+ return length(max(abs(coord) - size+r, 0.0)) - r;
}
void main() {
@@ -12,7 +12,7 @@ void main() {
vec2 size = resolution * 0.5;
vec4 texture_color = texture2D(texture, gl_TexCoord[0].xy);
- float a = clamp(rounded_rect(uv - center, size - radius, radius), 0.0, 1.0);
- texture_color.a *= (1.0 - a);
+ float a = 1.0 - smoothstep(0.0, 2.0, rounded_rect(uv - center, size, radius));
+ texture_color.a *= a;
gl_FragColor = texture_color;
} \ No newline at end of file
diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp
index f9b79c4..20abe14 100644
--- a/src/RoundedRectangle.cpp
+++ b/src/RoundedRectangle.cpp
@@ -4,6 +4,8 @@
#include <assert.h>
namespace QuickMedia {
+ static const float shadow_radius = 20.0f; // Has to match the shadow offset in rounded_rectangle.glsl
+
RoundedRectangle::RoundedRectangle(sf::Vector2f size, float radius, sf::Color color, sf::Shader *rounded_rectangle_shader) :
radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader), band_color(sf::Color::Transparent)
{
@@ -13,84 +15,14 @@ namespace QuickMedia {
vertices[1].texCoords = sf::Vector2f(1.0f, 0.0f);
vertices[2].texCoords = sf::Vector2f(1.0f, 1.0f);
vertices[3].texCoords = sf::Vector2f(0.0f, 1.0f);
-
- const sf::Color shadow_color(0, 0, 0, 50);
-
- // Sides
- for(size_t i = 0; i < 16; i += 4) {
- drop_shadow_vertices[i + 0].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 1].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 2].color = shadow_color;
- drop_shadow_vertices[i + 3].color = shadow_color;
- }
-
- // Corners
- for(size_t i = 16; i < 32; i += 4) {
- drop_shadow_vertices[i + 0].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 1].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 2].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 3].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 100);
- }
}
void RoundedRectangle::set_position(sf::Vector2f pos) {
this->pos = pos;
- vertices[0].position = pos;
- vertices[1].position = pos + sf::Vector2f(size.x, 0.0f);
- vertices[2].position = pos + sf::Vector2f(size.x, size.y);
- vertices[3].position = pos + sf::Vector2f(0.0f, size.y);
-
- const float shadow_radius = 5.0f;
-
- // Top
- drop_shadow_vertices[0].position = pos + sf::Vector2f(radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
- drop_shadow_vertices[1].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
- drop_shadow_vertices[2].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f);
- drop_shadow_vertices[3].position = pos + sf::Vector2f(radius*0.5f, 0.0f);
-
- // Bottom
- drop_shadow_vertices[4].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[5].position = pos + sf::Vector2f(radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[6].position = pos + sf::Vector2f(radius*0.5f, size.y);
- drop_shadow_vertices[7].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y);
-
- // Left
- drop_shadow_vertices[8].position = pos + sf::Vector2f(-shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[9].position = pos + sf::Vector2f(-shadow_radius, radius*0.5f);
- drop_shadow_vertices[10].position = pos + sf::Vector2f(0.0f, radius*0.5f);
- drop_shadow_vertices[11].position = pos + sf::Vector2f(0.0f, size.y - radius*0.5f);
-
- // Right
- drop_shadow_vertices[12].position = pos + sf::Vector2f(size.x + shadow_radius, radius*0.5f);
- drop_shadow_vertices[13].position = pos + sf::Vector2f(size.x + shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[14].position = pos + sf::Vector2f(size.x, size.y - radius*0.5f);
- drop_shadow_vertices[15].position = pos + sf::Vector2f(size.x, radius*0.5f);
-
- const float overshoot = 1.7f;
-
- // Top left
- drop_shadow_vertices[16].position = pos + sf::Vector2f(-shadow_radius, radius*0.5f);
- drop_shadow_vertices[17].position = pos + sf::Vector2f(-shadow_radius*overshoot, -shadow_radius*overshoot);
- drop_shadow_vertices[18].position = pos + sf::Vector2f(radius*0.5f, -shadow_radius);
- drop_shadow_vertices[19].position = pos + sf::Vector2f(radius*0.5f, radius*0.5f);
-
- // Top right
- drop_shadow_vertices[20].position = pos + sf::Vector2f(size.x - radius*0.5f, -shadow_radius);
- drop_shadow_vertices[21].position = pos + sf::Vector2f(size.x + shadow_radius*overshoot, -shadow_radius*overshoot);
- drop_shadow_vertices[22].position = pos + sf::Vector2f(size.x + shadow_radius, radius*0.5f);
- drop_shadow_vertices[23].position = pos + sf::Vector2f(size.x - radius*0.5f, radius*0.5f);
-
- // Bottom right
- drop_shadow_vertices[24].position = pos + sf::Vector2f(size.x + shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[25].position = pos + sf::Vector2f(size.x + shadow_radius*overshoot, size.y + shadow_radius*overshoot);
- drop_shadow_vertices[26].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[27].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y - radius*0.5f);
-
- // Bottom left
- drop_shadow_vertices[28].position = pos + sf::Vector2f(radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[29].position = pos + sf::Vector2f(-shadow_radius*overshoot, size.y + shadow_radius*overshoot);
- drop_shadow_vertices[30].position = pos + sf::Vector2f(-shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[31].position = pos + sf::Vector2f(radius*0.5f, size.y - radius*0.5f);
+ vertices[0].position = pos + sf::Vector2f(-shadow_radius, -shadow_radius);
+ vertices[1].position = pos + sf::Vector2f(shadow_radius, -shadow_radius) + sf::Vector2f(size.x, 0.0f);
+ vertices[2].position = pos + sf::Vector2f(shadow_radius, shadow_radius) + sf::Vector2f(size.x, size.y);
+ vertices[3].position = pos + sf::Vector2f(-shadow_radius, shadow_radius) + sf::Vector2f(0.0f, size.y);
}
void RoundedRectangle::set_size(sf::Vector2f size) {
@@ -121,15 +53,12 @@ namespace QuickMedia {
}
void RoundedRectangle::draw(sf::RenderTarget &target) {
- if(drop_shadow_enabled)
- target.draw(drop_shadow_vertices, 32, sf::Quads);
-
// TODO: Remove these for optimizations. Also do the same in other places where setUniform is called
rounded_rectangle_shader->setUniform("radius", radius);
- rounded_rectangle_shader->setUniform("band_pos", band_pos);
+ rounded_rectangle_shader->setUniform("band_pos", band_pos + sf::Vector2f(shadow_radius, shadow_radius));
rounded_rectangle_shader->setUniform("band_size", band_size);
rounded_rectangle_shader->setUniform("band_color", sf::Glsl::Vec4(band_color.r/255.0f, band_color.g/255.0f, band_color.b/255.0f, band_color.a/255.0f));
- rounded_rectangle_shader->setUniform("resolution", size);
+ rounded_rectangle_shader->setUniform("resolution", size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f));
target.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
}
} \ No newline at end of file