aboutsummaryrefslogtreecommitdiff
path: root/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'shaders')
-rw-r--r--shaders/rect.frag36
-rw-r--r--shaders/rect.vert18
2 files changed, 54 insertions, 0 deletions
diff --git a/shaders/rect.frag b/shaders/rect.frag
new file mode 100644
index 0000000..d128f37
--- /dev/null
+++ b/shaders/rect.frag
@@ -0,0 +1,36 @@
+#version 330
+
+/* The fragment shader uses the UV coordinates to calculate whether it is in
+ the T, R, B, or L border. These are then mixed with the border color, and
+ their inverse is mixed with the fill color, to calculate the fragment color.
+ For example, if we are in the top border, then T=1, so the border mix factor
+ TRBL=1, and the fill mix factor (1-TRBL) is 0.
+
+ The use of pixel units here is handy because the border width can be
+ specified precisely in pixels to draw sharp lines. The border width is just
+ hardcoded, but could be made a uniform or vertex attribute easily enough. */
+
+uniform vec2 u_size;
+uniform vec4 u_borderColor;
+uniform vec4 u_fillColor;
+
+noperspective in vec2 f_uv;
+
+layout(location = 0) out vec4 FragColor;
+
+void
+main()
+{
+ const float border_width = 2.0;
+
+ float t = step(border_width, f_uv[1]);
+ float r = step(border_width, u_size.x - f_uv[0]);
+ float b = step(border_width, u_size.y - f_uv[1]);
+ float l = step(border_width, f_uv[0]);
+ float fill_mix = t * r * b * l;
+ float border_mix = 1.0 - fill_mix;
+ vec4 fill = fill_mix * u_fillColor;
+ vec4 border = border_mix * u_borderColor;
+
+ FragColor = fill + border;
+}
diff --git a/shaders/rect.vert b/shaders/rect.vert
new file mode 100644
index 0000000..34fe0b3
--- /dev/null
+++ b/shaders/rect.vert
@@ -0,0 +1,18 @@
+#version 330
+
+/* The vertex shader is trivial, but forwards scaled UV coordinates (in pixels)
+ to the fragment shader for drawing the border. */
+
+uniform mat4 MVP;
+uniform vec2 u_size;
+
+in vec2 v_position;
+
+noperspective out vec2 f_uv;
+
+void
+main()
+{
+ f_uv = v_position * u_size;
+ gl_Position = MVP * vec4(v_position, 0.0, 1.0);
+}