fractorb

annotate 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
rev   line source
nuclear@0 1 // vi:ft=glsl:
nuclear@3 2 #define ITER 128
nuclear@0 3
nuclear@0 4 uniform float view_scale;
nuclear@0 5 uniform vec2 view_center;
nuclear@1 6 uniform vec2 seed;
nuclear@0 7
nuclear@0 8 float mbrot_dist_point(in vec2 c, in vec2 p)
nuclear@0 9 {
nuclear@3 10 vec2 pixpos = c;
nuclear@0 11 vec2 z = c;
nuclear@0 12 float mindist_sq = 8192.0;
nuclear@0 13
nuclear@0 14 for(int i=0; i<ITER; i++) {
nuclear@0 15 float x = (z.x * z.x - z.y * z.y) + c.x;
nuclear@0 16 float y = (z.x * z.y + z.x * z.y) + c.y;
nuclear@0 17
nuclear@0 18 z = vec2(x, y);
nuclear@3 19 vec2 dir = z - p;
nuclear@1 20 mindist_sq = min(mindist_sq, dot(dir, dir));
nuclear@0 21 }
nuclear@0 22
nuclear@3 23 return mindist_sq;
nuclear@3 24 }
nuclear@3 25
nuclear@3 26 float line_dist_sq(in vec2 a, in vec2 b, in vec2 p)
nuclear@3 27 {
nuclear@3 28 vec2 dir = b - a;
nuclear@3 29 vec2 pvec = p - a;
nuclear@3 30 float t = dot(dir, pvec);
nuclear@3 31
nuclear@3 32 if(t < 0.0) return dot(pvec, pvec);
nuclear@3 33 if(t > 1.0) return dot(p - b, p - b);
nuclear@3 34
nuclear@3 35 vec2 ppt = a + dir * t;
nuclear@3 36 vec2 lpvec = p - ppt;
nuclear@3 37 return dot(lpvec, lpvec);
nuclear@3 38 }
nuclear@3 39
nuclear@3 40 float mbrot_orbit_dist(in vec2 c, in vec2 p)
nuclear@3 41 {
nuclear@3 42 vec2 pixpos = c;
nuclear@3 43 vec2 z = c;
nuclear@3 44 float mindist_sq = 8192.0;
nuclear@3 45
nuclear@3 46 for(int i=0; i<ITER; i++) {
nuclear@3 47 float x = (z.x * z.x - z.y * z.y) + c.x;
nuclear@3 48 float y = (z.x * z.y + z.x * z.y) + c.y;
nuclear@3 49 vec2 next = vec2(x, y);
nuclear@3 50
nuclear@3 51 float dist_sq = line_dist_sq(z, next, p);
nuclear@3 52 mindist_sq = min(mindist_sq, dist_sq);
nuclear@3 53
nuclear@3 54 z = next;
nuclear@3 55 }
nuclear@3 56
nuclear@3 57 return mindist_sq;
nuclear@0 58 }
nuclear@0 59
nuclear@0 60 void main()
nuclear@0 61 {
nuclear@1 62 vec2 c = gl_TexCoord[0].xy;
nuclear@3 63 float pdist_sq;
nuclear@3 64 float m = 1.0 - min(sqrt(mbrot_dist_point(c, seed)), 1.0);
nuclear@0 65
nuclear@3 66 #ifdef PLOT_ORBIT
nuclear@3 67 float orb_dist = sqrt(mbrot_orbit_dist(seed, c));
nuclear@3 68 vec3 orbcol = vec3(0.001 / orb_dist, 0.0, 0.0);
nuclear@3 69 gl_FragColor.rgb = vec3(m, m, m) / 2.0 + orbcol;
nuclear@3 70 #else
nuclear@0 71 gl_FragColor.rgb = vec3(m, m, m);
nuclear@3 72 #endif
nuclear@0 73 gl_FragColor.a = 1.0;
nuclear@0 74 }