fractorb

diff 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 diff
     1.1 --- a/mbrot.glsl	Mon Nov 20 03:53:02 2017 +0200
     1.2 +++ b/mbrot.glsl	Sun Nov 26 14:49:34 2017 +0200
     1.3 @@ -1,5 +1,5 @@
     1.4  // vi:ft=glsl:
     1.5 -#define ITER	96
     1.6 +#define ITER	128
     1.7  
     1.8  uniform float view_scale;
     1.9  uniform vec2 view_center;
    1.10 @@ -7,6 +7,7 @@
    1.11  
    1.12  float mbrot_dist_point(in vec2 c, in vec2 p)
    1.13  {
    1.14 +	vec2 pixpos = c;
    1.15  	vec2 z = c;
    1.16  	float mindist_sq = 8192.0;
    1.17  
    1.18 @@ -15,18 +16,59 @@
    1.19  		float y = (z.x * z.y + z.x * z.y) + c.y;
    1.20  
    1.21  		z = vec2(x, y);
    1.22 -		vec2 dir = z - seed;
    1.23 +		vec2 dir = z - p;
    1.24  		mindist_sq = min(mindist_sq, dot(dir, dir)); 
    1.25  	}
    1.26  
    1.27 -	return sqrt(mindist_sq);
    1.28 +	return mindist_sq;
    1.29 +}
    1.30 +
    1.31 +float line_dist_sq(in vec2 a, in vec2 b, in vec2 p)
    1.32 +{
    1.33 +	vec2 dir = b - a;
    1.34 +	vec2 pvec = p - a;
    1.35 +	float t = dot(dir, pvec);
    1.36 +
    1.37 +	if(t < 0.0) return dot(pvec, pvec);
    1.38 +	if(t > 1.0) return dot(p - b, p - b);
    1.39 +
    1.40 +	vec2 ppt = a + dir * t;
    1.41 +	vec2 lpvec = p - ppt;
    1.42 +	return dot(lpvec, lpvec);
    1.43 +}
    1.44 +
    1.45 +float mbrot_orbit_dist(in vec2 c, in vec2 p)
    1.46 +{
    1.47 +	vec2 pixpos = c;
    1.48 +	vec2 z = c;
    1.49 +	float mindist_sq = 8192.0;
    1.50 +
    1.51 +	for(int i=0; i<ITER; i++) {
    1.52 +		float x = (z.x * z.x - z.y * z.y) + c.x;
    1.53 +		float y = (z.x * z.y + z.x * z.y) + c.y;
    1.54 +		vec2 next = vec2(x, y);
    1.55 +
    1.56 +		float dist_sq = line_dist_sq(z, next, p);
    1.57 +		mindist_sq = min(mindist_sq, dist_sq); 
    1.58 +
    1.59 +		z = next;
    1.60 +	}
    1.61 +
    1.62 +	return mindist_sq;
    1.63  }
    1.64  
    1.65  void main()
    1.66  {
    1.67  	vec2 c = gl_TexCoord[0].xy;
    1.68 -	float m = 1.0 - mbrot_dist_point(c, vec2(0.0, 0.0));
    1.69 +	float pdist_sq;
    1.70 +	float m = 1.0 - min(sqrt(mbrot_dist_point(c, seed)), 1.0);
    1.71  
    1.72 +#ifdef PLOT_ORBIT
    1.73 +	float orb_dist = sqrt(mbrot_orbit_dist(seed, c));
    1.74 +	vec3 orbcol = vec3(0.001 / orb_dist, 0.0, 0.0);
    1.75 +	gl_FragColor.rgb = vec3(m, m, m) / 2.0 + orbcol;
    1.76 +#else
    1.77  	gl_FragColor.rgb = vec3(m, m, m);
    1.78 +#endif
    1.79  	gl_FragColor.a = 1.0;
    1.80  }