fractorb

view mbrot.glsl @ 3:f440ecffc45a

trying to draw the orbit in the shader
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 26 Nov 2017 14:49:34 +0200
parents 436f82447c44
children
line source
1 // vi:ft=glsl:
2 #define ITER 128
4 uniform float view_scale;
5 uniform vec2 view_center;
6 uniform vec2 seed;
8 float mbrot_dist_point(in vec2 c, in vec2 p)
9 {
10 vec2 pixpos = c;
11 vec2 z = c;
12 float mindist_sq = 8192.0;
14 for(int i=0; i<ITER; i++) {
15 float x = (z.x * z.x - z.y * z.y) + c.x;
16 float y = (z.x * z.y + z.x * z.y) + c.y;
18 z = vec2(x, y);
19 vec2 dir = z - p;
20 mindist_sq = min(mindist_sq, dot(dir, dir));
21 }
23 return mindist_sq;
24 }
26 float line_dist_sq(in vec2 a, in vec2 b, in vec2 p)
27 {
28 vec2 dir = b - a;
29 vec2 pvec = p - a;
30 float t = dot(dir, pvec);
32 if(t < 0.0) return dot(pvec, pvec);
33 if(t > 1.0) return dot(p - b, p - b);
35 vec2 ppt = a + dir * t;
36 vec2 lpvec = p - ppt;
37 return dot(lpvec, lpvec);
38 }
40 float mbrot_orbit_dist(in vec2 c, in vec2 p)
41 {
42 vec2 pixpos = c;
43 vec2 z = c;
44 float mindist_sq = 8192.0;
46 for(int i=0; i<ITER; i++) {
47 float x = (z.x * z.x - z.y * z.y) + c.x;
48 float y = (z.x * z.y + z.x * z.y) + c.y;
49 vec2 next = vec2(x, y);
51 float dist_sq = line_dist_sq(z, next, p);
52 mindist_sq = min(mindist_sq, dist_sq);
54 z = next;
55 }
57 return mindist_sq;
58 }
60 void main()
61 {
62 vec2 c = gl_TexCoord[0].xy;
63 float pdist_sq;
64 float m = 1.0 - min(sqrt(mbrot_dist_point(c, seed)), 1.0);
66 #ifdef PLOT_ORBIT
67 float orb_dist = sqrt(mbrot_orbit_dist(seed, c));
68 vec3 orbcol = vec3(0.001 / orb_dist, 0.0, 0.0);
69 gl_FragColor.rgb = vec3(m, m, m) / 2.0 + orbcol;
70 #else
71 gl_FragColor.rgb = vec3(m, m, m);
72 #endif
73 gl_FragColor.a = 1.0;
74 }