From 5d6b59778a7346317ddfc549c350c0960a7a54a7 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 20 May 2020 12:42:39 +0200 Subject: Added Smoothness and super sampling --- shader/fragment.glsl | 115 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 98 insertions(+), 17 deletions(-) (limited to 'shader') diff --git a/shader/fragment.glsl b/shader/fragment.glsl index 6720236..6bc028c 100644 --- a/shader/fragment.glsl +++ b/shader/fragment.glsl @@ -11,36 +11,117 @@ uniform float u_imag_start; uniform float u_imag_end; uniform int u_iterations; +uniform bool u_smooth; +uniform float u_samples; uniform sampler1D u_texture; #define ESCAPE_RADIUS 4.0 +#define LOG_2 0.69314718056 -void main() -{ - float ca = u_real_start + float(gl_FragCoord.x) / float(u_width) * (u_real_end - u_real_start); - float cb = u_imag_start + float(gl_FragCoord.y) / float(u_height) * (u_imag_end - u_imag_start); - float zr = ca; - float zi = cb; - float zr_square; - float zi_square; +int mandelbrot_func(vec2 c) +{ + vec2 z; + vec2 z_square; int n; + z = c; for (n = 0; n < u_iterations; n++) { - zi_square = zi * zi; - zr_square = zr * zr; - if (zr_square + zi_square > ESCAPE_RADIUS) + z_square = z * z; + if (z_square.x + z_square.y > ESCAPE_RADIUS) break; - zi = 2.0 * zr * zi; - zr = zr_square - zi_square; - zi += cb; - zr += ca; + z.y = 2.0 * z.x * z.y; + z.x = z_square.x - z_square.y; + z += c; } + return n; +} +float mandelbrot_smooth(vec2 c) +{ + vec2 z; + vec2 z_square; + int n; + + z = c; + for (n = 0; n < u_iterations; n++) + { + z_square = z * z; + if (z_square.x + z_square.y > ESCAPE_RADIUS) + break; + z.y = 2.0 * z.x * z.y; + z.x = z_square.x - z_square.y; + z += c; + } if (n == u_iterations) - out_color = vec4(0.0, 0.0, 0.0, 1.0); + return float(n); + // http://linas.org/art-gallery/escape/escape.html + z_square = z * z; + z.y = 2.0 * z.x * z.y; + z.x = z_square.x - z_square.y; + z += c; + z_square = z * z; + z.y = 2.0 * z.x * z.y; + z.x = z_square.x - z_square.y; + z += c; + float modulus = sqrt(z.x * z.x + z.y * z.y); + return float(n) - log(log(modulus)) / LOG_2; +} + +vec4 mandelbrot_color(vec2 c) +{ + float n; + + if (u_smooth) + n = mandelbrot_smooth(c); + else + n = float(mandelbrot_func(c)); + + if (n == float(u_iterations)) + return vec4(0.0, 0.0, 0.0, 1.0); + else + return texture(u_texture, n / float(u_iterations)); + +} + +vec4 supersample_grid(vec2 c) +{ + vec2 epsilon; + vec2 _sample; + vec2 _step; + vec4 color; + + color = vec4(0.0, 0.0, 0.0, 0.0); + _step.x = (u_real_end - u_real_start) / float(u_width); + _step.y = (u_imag_end - u_imag_start) / float(u_height); + epsilon.y = 0.0; + while (epsilon.y < u_samples) + { + epsilon.x = 0.0; + while (epsilon.x < u_samples) + { + _sample = c + _step * (epsilon / u_samples); + color += mandelbrot_color(_sample); + epsilon.x += 1.0; + } + epsilon.y += 1.0; + } + return color / (u_samples * u_samples); +} + +void main() +{ + vec2 c; + float n; + + c.x = u_real_start + float(gl_FragCoord.x) / float(u_width) * (u_real_end - u_real_start); + c.y = u_imag_start + float(gl_FragCoord.y) / float(u_height) * (u_imag_end - u_imag_start); + + + if (u_samples == 1.0) + out_color = mandelbrot_color(c); else - out_color = texture1D(u_texture, float(n) / float(u_iterations)); + out_color = supersample_grid(c); } -- cgit