From c5de5ae479df3956c5d6f8a9eda2e5d6790e3e98 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 26 Jun 2014 10:45:20 -0400 Subject: [PATCH] rework text shader, add basic and rect shaders --- runtime/shaders/basic.f.glsl | 6 ++++++ runtime/shaders/basic.v.glsl | 21 +++++++++++++++++++++ runtime/shaders/rect.v.glsl | 23 +++++++++++++++++++++++ runtime/shaders/text.f.glsl | 2 ++ runtime/shaders/text.v.glsl | 23 ++++++++++++++++------- 5 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 runtime/shaders/basic.f.glsl create mode 100644 runtime/shaders/basic.v.glsl create mode 100644 runtime/shaders/rect.v.glsl diff --git a/runtime/shaders/basic.f.glsl b/runtime/shaders/basic.f.glsl new file mode 100644 index 0000000..b51cfff --- /dev/null +++ b/runtime/shaders/basic.f.glsl @@ -0,0 +1,6 @@ +uniform vec4 color; + +void main(void) +{ + gl_FragColor = color; +} diff --git a/runtime/shaders/basic.v.glsl b/runtime/shaders/basic.v.glsl new file mode 100644 index 0000000..a281dd3 --- /dev/null +++ b/runtime/shaders/basic.v.glsl @@ -0,0 +1,21 @@ +/* Viewport width and height */ +uniform ivec2 viewport_size; +/* Position of lower left corner of glyph */ +uniform ivec2 position; + +/* Vertex coordinates: x, y */ +attribute vec2 coords; + +/** + * Map coordinates such that: + * (0 .. viewport_size.[xy]) => (-1.0 .. 1.0) + */ +vec2 map_to_screen(vec2 position) +{ + return 2.0 * position / viewport_size - 1.0; +} + +void main(void) +{ + gl_Position = vec4(map_to_screen((vec2)position + coords), 0, 1); +} diff --git a/runtime/shaders/rect.v.glsl b/runtime/shaders/rect.v.glsl new file mode 100644 index 0000000..2c551b1 --- /dev/null +++ b/runtime/shaders/rect.v.glsl @@ -0,0 +1,23 @@ +/* Viewport width and height */ +uniform ivec2 viewport_size; +/* Position of lower left corner of rectangle */ +uniform ivec2 position; +/* Size of the rectangle */ +uniform ivec2 size; + +/* Vertex coordinates: x, y (0 or 1) */ +attribute ivec2 coords; + +/** + * Map coordinates such that: + * (0 .. viewport_size.[xy]) => (-1.0 .. 1.0) + */ +vec2 map_to_screen(vec2 position) +{ + return 2.0 * position / viewport_size - 1.0; +} + +void main(void) +{ + gl_Position = vec4(map_to_screen((vec2)(position + coords * size)), 0, 1); +} diff --git a/runtime/shaders/text.f.glsl b/runtime/shaders/text.f.glsl index 3cdfc99..0494621 100644 --- a/runtime/shaders/text.f.glsl +++ b/runtime/shaders/text.f.glsl @@ -1,5 +1,7 @@ +/* Texture coordinate: s, t */ varying vec2 texture_coord_v; +/* Texture unit */ uniform sampler2D texture; uniform vec4 color; diff --git a/runtime/shaders/text.v.glsl b/runtime/shaders/text.v.glsl index c0501dc..5851094 100644 --- a/runtime/shaders/text.v.glsl +++ b/runtime/shaders/text.v.glsl @@ -1,16 +1,25 @@ -uniform vec2 screen_size; -uniform vec2 position; +/* Viewport width and height */ +uniform ivec2 viewport_size; +/* Position of lower left corner of glyph */ +uniform ivec2 position; +/* Vertex coordinates: x, y, s, t */ attribute vec4 coords; +/* Output texture coordinate: s, t */ varying vec2 texture_coord_v; +/** + * Map coordinates such that: + * (0 .. viewport_size.[xy]) => (-1.0 .. 1.0) + */ +vec2 map_to_screen(vec2 position) +{ + return 2.0 * position / viewport_size - 1.0; +} + void main(void) { - // Map coordinates such that: - // (0 .. screen_size.x) => (-1.0 .. 1.0) - gl_Position = vec4(2.0 * (position.x + coords.x) / screen_size.x - 1.0, - 2.0 * (position.y + coords.y) / screen_size.y - 1.0, - 0, 1); + gl_Position = vec4(map_to_screen((vec2)position + coords.xy), 0, 1); texture_coord_v = coords.zw; }