From 70bf7ac330545f14ab9babddfdf0cb5df9e9ee69 Mon Sep 17 00:00:00 2001 From: Charles Date: Tue, 19 May 2020 22:45:04 +0200 Subject: Simple (but insanly fast compared to CPU) fragment shader to draw mandelbrot set --- shader/fragment.glsl | 39 +++++++++++++++++++++++++++++++++++++-- shader/vertex.glsl | 3 +++ 2 files changed, 40 insertions(+), 2 deletions(-) (limited to 'shader') diff --git a/shader/fragment.glsl b/shader/fragment.glsl index 3fbeac5..19b020e 100644 --- a/shader/fragment.glsl +++ b/shader/fragment.glsl @@ -1,8 +1,43 @@ #version 400 core -out vec4 out_color; +out vec4 out_color; + +uniform int u_width; +uniform int u_height; + +uniform float u_real_start; +uniform float u_real_end; +uniform float u_imag_start; +uniform float u_imag_end; + +uniform int u_iterations; void main() { - out_color = vec4(1.0, 1.0, 1.0, 1.0); + 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 n; + + for (n = 0; n < u_iterations; n++) + { + zi_square = zi * zi; + zr_square = zr * zr; + if (zr_square + zi_square > 4.0) + break; + zi = 2.0 * zr * zi; + zr = zr_square - zi_square; + zi += cb; + zr += ca; + } + + out_color = vec4( + float(n) / float(u_iterations), + float(n) / float(u_iterations), + 0.0, + 1.0 + ); } diff --git a/shader/vertex.glsl b/shader/vertex.glsl index 9ec6439..714e089 100644 --- a/shader/vertex.glsl +++ b/shader/vertex.glsl @@ -1,5 +1,8 @@ #version 400 core +layout (location = 0) in vec2 in_position; + void main() { + gl_Position = vec4(in_position, 0.0, 1.0); } -- cgit