Skip to content

Commit 8604085

Browse files
authored
[examples] Fixed shaders_mandelbrot_set for WebGL (#5286)
1 parent 5aeedb4 commit 8604085

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

examples/shaders/resources/shaders/glsl100/mandelbrot_set.fs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ uniform int maxIterations; // Max iterations per pixel
1717
const float max = 4.0; // We consider infinite as 4.0: if a point reaches a distance of 4.0 it will escape to infinity
1818
const float max2 = max*max; // Square of max to avoid computing square root
1919

20+
// WebGL shaders for loop iteration limit only const
21+
const int maxIterationsLimit = 20000;
22+
2023
void main()
2124
{
2225
// The pixel coordinates are scaled so they are on the mandelbrot scale
@@ -31,31 +34,29 @@ void main()
3134
// Fc(z) = z^2 + c on the complex numbers c from the plane does not diverge to infinity starting at z = 0
3235
// Here: z = a + bi. Iterations: z -> z^2 + c = (a + bi)^2 + (c.x + c.yi) = (a^2 - b^2 + c.x) + (2ab + c.y)i
3336

34-
int iter = 0;
35-
while (iter < maxIterations)
37+
for (int iter = 0; iter < maxIterationsLimit; ++iter)
3638
{
3739
float aa = a*a;
3840
float bb = b*b;
41+
if (iter >= maxIterations)
42+
{
43+
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
44+
return;
45+
}
3946
if (aa + bb > max2)
40-
break;
47+
{
48+
float normR = float(iter - (iter/55)*55)/55.0;
49+
float normG = float(iter - (iter/69)*69)/69.0;
50+
float normB = float(iter - (iter/40)*40)/40.0;
51+
52+
gl_FragColor = vec4(sin(normR*PI), sin(normG*PI), sin(normB*PI), 1.0);
53+
return;
54+
}
4155

4256
float twoab = 2.0*a*b;
4357
a = aa - bb + c.x;
4458
b = twoab + c.y;
45-
46-
++iter;
4759
}
4860

49-
if (iter >= maxIterations)
50-
{
51-
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
52-
}
53-
else
54-
{
55-
float normR = float(iter - (iter/55)*55)/55.0;
56-
float normG = float(iter - (iter/69)*69)/69.0;
57-
float normB = float(iter - (iter/40)*40)/40.0;
58-
59-
gl_FragColor = vec4(sin(normR*PI), sin(normG*PI), sin(normB*PI), 1.0);
60-
}
61+
gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
6162
}

examples/shaders/shaders_mandelbrot_set.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,13 @@ int main(void)
7070
float zoom = startingZoom;
7171
// Depending on the zoom the mximum number of iterations must be adapted to get more detail as we zzoom in
7272
// The solution is not perfect, so a control has been added to increase/decrease the number of iterations with UP/DOWN keys
73+
#if defined(PLATFORM_DESKTOP)
7374
int maxIterations = 333;
7475
float maxIterationsMultiplier = 166.5f;
76+
#else
77+
int maxIterations = 43;
78+
float maxIterationsMultiplier = 22.0f;
79+
#endif
7580

7681
// Get variable (uniform) locations on the shader to connect with the program
7782
// NOTE: If uniform variable could not be found in the shader, function returns -1

0 commit comments

Comments
 (0)