summaryrefslogtreecommitdiffstats
path: root/examples/polygon.frag
blob: a0188654f6e071581c4028fedd4a6c33321b523b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#version 330

out vec4 fragColor;

uniform vec2 res;
uniform float time;

const float sharpness = 300;

const float PI = 3.14159265358979323846;


mat2 rot(float a) {
	return mat2(
		cos(a), -sin(a),
		sin(a), cos(a)
	);
}

float poly(float n, vec2 m, float r, float rt, vec2 p) {
	vec2 diff = (m - p) * rot(rt);
	float d = length(diff);
	float alpha = PI / n;
	float gamma = mod(abs(atan(diff.x, diff.y)), alpha*2);

	if (gamma > alpha)
		gamma -= 2*alpha;

	float r2 = r * cos(alpha) / cos(gamma);

	return clamp((r2 - d)*sharpness, 0.0, 1.0);
}

float poly_d(float n, vec2 m, float r, float rt, vec2 p) {
	return poly(n, m, r, rt, p) - poly(n, m, r - 1/sharpness, rt, p);
}

void main(void) {
	vec2 m = res/2;
	float s = min(m.x, m.y);
	vec2 p = (gl_FragCoord.xy - m) / s;

	float scene = 0;

	float c;

	c = poly_d(5 - 3*cos(time/1000), vec2(0, 0), 0.5, PI * time/1000, p);
	scene = abs(scene - c);

	c = poly_d(5 - 3*cos(time/1000), vec2(0, 0), 0.5, -PI * time/1000, p);
	scene = abs(scene - c);

	fragColor = vec4(vec3(1, 1, 1) * scene, 1);
}