First attempt for mandelbrot implementation

This commit is contained in:
hodasemi 2020-11-23 19:08:34 +01:00
parent cf6eaa2f10
commit 6abc793708
2 changed files with 89 additions and 1 deletions

View file

@ -2,6 +2,94 @@
layout (location = 0) out vec4 color;
void main() {
color = vec4(1.0, 0.0, 0.0, 1.0);
const int MAX_ITER = 1000;
const float SIZE = 0.01;
float hue_to_rgb(float p, float q, float t) {
if (t < 0.0) {
t += 1.0;
}
if (t > 1.0) {
t -= 1.0;
}
if (t < (1.0 / 6.0)) {
return p + (q - p) * 6.0 * t;
} else if (t < (1.0 / 2.0)) {
return q;
} else if (t < (2.0 / 3.0)) {
return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
}
return p;
}
vec3 hsl_to_rgb(float h, float s, float l) {
float r, g, b;
if (s == 0.0) {
r = l;
g = l;
b = l;
} else {
float q, p;
if (l < 0.5) {
q = l * (1. + s);
} else {
q = l + s - l * s;
}
p = 2. * l - q;
r = hue_to_rgb(p, q, h + 1.0 / 3.0);
g = hue_to_rgb(p, q, h);
b = hue_to_rgb(p, q, h - 1.0 / 3.0);
}
return vec3(r, g, b);
}
void mandelbrot_iter(float px, float py, out float ox, out int iter) {
float x = 0.0;
float y = 0.0;
float xx = 0.0;
float yy = 0.0;
float xy = 0.0;
for (int i = 0; i < MAX_ITER; i++) {
xx = x * x;
yy = y * y;
xy = x * y;
if ((xx + yy) > 4.0) {
ox = xx + yy;
iter = i;
return;
}
x = xx - yy + px;
y = 2.0 * xy + py;
}
ox = xx + yy;
iter = MAX_ITER;
}
vec3 paint(float nx, float ny) {
float r;
int n;
mandelbrot_iter(SIZE * nx, SIZE * ny, r, n);
if (r > 4.0) {
return hsl_to_rgb(float(n) / 800.0 * r, 1.0, 0.5);
} else {
return vec3(1.0, 1.0, 1.0);
}
}
void main() {
color = vec4(paint(gl_FragCoord.x, gl_FragCoord.y), 1.0);
}

Binary file not shown.