Use a single program for drawing background images

This commit is contained in:
Kovid Goyal
2020-01-31 15:09:47 +05:30
parent 9034b50b3a
commit 5263babc9a
7 changed files with 54 additions and 51 deletions

View File

@@ -3,6 +3,13 @@
#define top 1.0f
#define right 1.0f
#define bottom -1.0f
#define tex_left 0
#define tex_top 0
#define tex_right 1
#define tex_bottom 1
uniform float tiled;
uniform vec4 sizes; // [ window_width, window_height, image_width, image_height ]
out vec2 texcoord;
@@ -12,10 +19,24 @@ const vec2 pos_map[] = vec2[4](
vec2(right, bottom),
vec2(right, top)
);
const vec2 tex_map[] = vec2[4](
vec2(tex_left, tex_top),
vec2(tex_left, tex_bottom),
vec2(tex_right, tex_bottom),
vec2(tex_right, tex_top)
);
float scale_factor(float window, float image) {
return max(1, window / image);
}
float tiling_factor(int i) {
return tiled * scale_factor(sizes[i], sizes[i + 2]) + (1 - tiled);
}
void main() {
vec2 vertex = pos_map[gl_VertexID];
texcoord = clamp(vec2(vertex[0], vertex[1]*-1), 0, 1);
gl_Position = vec4(vertex, 0, 1);
vec2 tex_coords = tex_map[gl_VertexID];
texcoord = vec2(tex_coords[0] * tiling_factor(0), tex_coords[1] * tiling_factor(1));
gl_Position = vec4(pos_map[gl_VertexID], 0, 1);
}