#version 330 core uniform float time; uniform vec2 resolution; uniform vec2 mouse; out vec4 color; float quadratic_bezier (float x, vec2 a) { float epsilon = 0.00001; a.x = clamp (a.x, 0, 1); a.y = clamp (a.y, 0, 1); if (a.x == .5) a += epsilon; // solve t from x (an inverse operation) float om2a = 1 - 2*a.x; float t = (sqrt (a.x*a.x + om2a*x) - a.x)/om2a; float y = (1 - 2*a.y)*(t*t) + 2*a.y*t; return y; } float line_segment (vec2 p, vec2 a, vec2 b) { vec2 pa = p - a; vec2 ba = b - a; float h = clamp (dot (pa, ba)/dot (ba, ba), 0, 1); return smoothstep (0, 1/resolution.x, length (pa - ba*h)); } void main () { vec2 st = gl_FragCoord.xy/resolution; float px = 1/resolution.y; // control point // vec2 cp = mouse/resolution; vec2 cp = vec2 (cos (time), sin (time))*0.45 + 0.5; float l = quadratic_bezier(st.x, cp); vec3 c = vec3 (smoothstep (l, l + px, st.y)); // draw control point c = mix (vec3 (.5), c, line_segment (st, vec2 (0), cp)); c = mix (vec3 (.5), c, line_segment (st, vec2 (1), cp)); float d = distance (cp, st); c = mix (vec3 (1, 0, 0), c, smoothstep (.01, .01 + px, d)); color = vec4 (c, 1); }