-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrotShader.frag.in
64 lines (57 loc) · 1.87 KB
/
mandelbrotShader.frag.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// mandelbrot -- interactive mandelbrot set explorer
// Copyright (C) 2022 sedsedus
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#ifdef GL_ES
precision highp float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_size;
uniform vec2 u_center;
// u_colors is a map: iterations -> color (vec4 from sf::Color) value
uniform vec4 u_colors[${ITERATION_LIMIT}];
uniform int u_maxIterations;
// complex number (x + yi) squared is: x^2 - y^2 + 2xyi
vec2 complexPow2(vec2 n)
{
return vec2(n.x * n.x - n.y * n.y, 2 * (n.x * n.y));
}
float parabola(float x, float k)
{
return pow(4.0 * x * (1.0 - x), k);
}
// the stop condition is either maxIterations or distance (all mandelbrot points fall in the 2.0 circle around center)
int getMandelbrotIterations(vec2 p)
{
vec2 m = vec2(0.0, 0.0);
int i = 0;
// |zn| <= 2.0
// |zn| = sqrt(x^2 + y^2)
for (i = 0; i < u_maxIterations && sqrt(m.x * m.x + m.y * m.y) <= 2.0; ++i) {
m = complexPow2(m) + p;
}
return i;
}
int getIterations()
{
vec2 sc = gl_FragCoord.xy / u_resolution;
vec2 p = sc * u_size + u_center - u_size / 2.0;
return getMandelbrotIterations(p);
}
void main()
{
int i = getIterations();
gl_FragColor = u_colors[i];
}