mandelbrot

Mandelbrot viewer written in C and GLSL
git clone anongit@rnpnr.xyz:mandelbrot.git
Log | Files | Refs | Feed | LICENSE

Commit: 630121998c5f85b2bb1b4e5d8058ad618a5047e7
Parent: 0cbf3890a22657b5bb305ac0fa513eae16613c05
Author: Randy Palamar
Date:   Fri, 24 May 2024 21:15:14 -0600

try to use escape count as a wavelength for colours

Diffstat:
Mfrag.glsl | 82++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 69 insertions(+), 13 deletions(-)

diff --git a/frag.glsl b/frag.glsl @@ -5,30 +5,86 @@ out vec4 colour; uniform uvec2 u_screen_dim; -vec2 map_mandelbrot(vec2 v) +vec2 map_mandelbrot(vec2 v, float aspect) { - float x = v.x * 2.47 - 2; - float y = v.y * 2.14 - 1.12; - return vec2(x, y); + /* midpoint is (-0.765, 0) */ + /* x is in [-2.00, 0.47] */ + /* x new [-2.5, 1] -> 3.5 */ + /* y is in [-1.12, 1.12] */ + /* y new [-1.5, 1.5] -> 3.0 */ + vec2 delta = vec2(0); + + float desired_aspect = 3.5 / 3; + delta.x = -0.765 - 3.5 * desired_aspect / 2; + delta.y = -1.5; + + float x = v.x * 3.5 * desired_aspect; + float y = v.y * 3; + return vec2(x, y) + delta; +} + +vec3 wavelength2rgb(float lambda) +{ + vec3 rgb = vec3(0); + float t; + /* red */ + if (lambda >= 400 && lambda < 410) { + t = (lambda - 400) / 10; + rgb.x = 0.33 * t - (0.20 * t * t); + } else if (lambda >= 410 && lambda < 475) { + t = (lambda - 410) / 65; + rgb.x = 0.14 - (0.13 * t * t); + } else if (lambda >= 545 && lambda < 595) { + t = (lambda - 545) / 50; + rgb.x = 1.98 * t - ( t * t); + } else if (lambda >= 595 && lambda < 650) { + t = (lambda - 595) / 65; + rgb.x = 0.98 + 0.06 * t - (0.40 * t * t); + } else if (lambda >= 650 && lambda < 700) { + t = (lambda - 650) / 50; + rgb.x = 0.65 - 0.84 * t + (0.20 * t * t); + } + + /* green */ + if (lambda >= 415 && lambda < 475) { + t = (lambda - 415) / 60; + rgb.y = + (0.80 * t * t); + } else if (lambda >= 475 && lambda < 585) { + t = (lambda - 475) / 115; + rgb.y = 0.80 + 0.76 * t - (0.80 * t * t); + } else if (lambda >= 585 && lambda < 639) { + t = (lambda - 585) / 54; + rgb.y = 0.84 - 0.84 * t ; + } + + /* blue */ + if (lambda >= 400 && lambda < 475) { + t = (lambda - 400) / 75; + rgb.z = 2.20 * t - (1.50 * t * t); + } else if (lambda >= 475 && lambda < 560) { + t = (lambda - 475) / 85; + rgb.z = 0.70 + t + (0.30 * t * t); + } + return rgb; } void main() { - vec2 xy0 = map_mandelbrot(gl_FragCoord.xy / u_screen_dim.xy); - colour = vec4(0, 0, 0.1, 1); + float aspect = u_screen_dim.x / u_screen_dim.y; + vec2 xy0 = map_mandelbrot(gl_FragCoord.xy / u_screen_dim.xy, aspect); int i; float xx = 0, yy = 0; vec2 xy = xy0; - for (i = 0; i < 256 && xx + yy < 4.0; i++) { + for (i = 0; i < 300 && xx + yy < 15.0; i++) { xx = xy.x * xy.x; yy = xy.y * xy.y; xy = vec2(xx - yy + xy0.x, 2 * xy.x * xy.y + xy0.y); } - if (i < 256) { - float r = (i >> 0) / 255.0; - float g = (i >> 2) / 255.0; - float b = (i >> 3) / 255.0; - colour = vec4(r, g, b, 1.0); - } + + float mu = i - log(log(sqrt(xx + yy))) / log(2.0); + mu = clamp(mu, 0, 300); + //float q = pow(mu / 300, 0.2); + float q = mu / 300; + colour = vec4(wavelength2rgb(400.0 + 300 * q).xyz, 1.0); }