0

I've written a Mandelbrot viewer with zoom using SharpGL (C# implementation of OpenGL). It works fine but after 17 zooms (double scale each zoom) it starts to pixelate pretty badly. I have almost identical code written in c# directly, which doesn't have this problem.

I have tried setting the floats to highp in the fragment shader in case it was a precision problem, but that hasn't made any different. Here is my fragment shader:

#version 400 core

out vec4 out_Color;
in vec4 gl_PointCoord;
in vec4 pixCoord;
uniform float max;
uniform float scale;
uniform float[2] center;

vec3 hsv2rgb(vec3 c)
{
    vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
    vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
    return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void main(void) {
    highp vec2 z,c,p;
    vec3 tcolor;
    float hue,norm,colorfix,hue2;
    highp float x,y;
    p.x = pixCoord.x;
    p.y = pixCoord.y;


    c.x = (p.x) / scale+center[0];
    c.y = (p.y) / scale-center[1];

    int i;
    z = c;

    for(i=0; i<max; i++) {
        x = (z.x * z.x - z.y * z.y) + c.x;
        y = (z.y * z.x + z.x * z.y) + c.y;

        if((x * x + y * y) > 4.0) break;
        z.x = x;
        z.y = y;
    }

    if (i >= max)
        out_Color = vec4(0.0,0.0,0.0, 1.0);
    else {
        norm = sqrt(x*x+y*y);
        colorfix = max / (log(2*(scale))+1);
        hue = i + 1 - log(log(norm)/log(2))/log(2);
        hue2 = mod((0.8 * colorfix + hue) , colorfix) / colorfix;
        tcolor = hsv2rgb(vec3(hue2,0.6,1.0));
        out_Color = vec4(tcolor, 1.0);
    }
}

I have tried just using black and red in case the smooth color calculations were causing some trouble but that didn't change anything.

Scale is set in the c# sharp program and is just a power of 2 for the zoom, so around 2^17 is where it starts to be noticeable.

center is the real center of image in the complex plane.

  • 1
    I would expect this behaviour, at least for `float`s. `float` has 6-9 significant (and exact) digits, and at 1 / 2^17 = 0,0000076 this starts to become your limit. There are still some non-`highp` `float`s, so perhaps this is your problem. – schnaader Mar 04 '15 at 08:21
  • Okay. I thought maybe highp would help with that. I just rewrote it using double instead of vectors and floats - but I am running into the problem that the math functions don't work with double for my smooth coloring. I can get around the square root issue, but not the log issue. Maybe someone knows another good smooth coloring idea? – David Taub Mar 04 '15 at 08:34
  • _I can get around the square root issue, but not the log issue_ Can you explain that a little; I don't see what you can't do with double.. – TaW Mar 04 '15 at 12:04
  • the glsl log function requires floats, doesn't it? Mine crashes if I try and use doubles with it, though I am just learning glsl and openGL (this is my first time using them). I actually wrote a function that used a taylor series to find the logs with doubles, but it's not very fast and it's not quite working right. – David Taub Mar 04 '15 at 17:55
  • @DavidTaub It might be just wrong order of computations see [Can't find a way to color the Mandelbrot-set the way i'm aiming for](https://stackoverflow.com/a/56197067/2521214) the float version is good in high zooms ... the 64bit demo can handle much much more see the fragment shader in it (64bit demo) how I managed to overcome the `double` functions missing in GLSL ... – Spektre Jun 27 '19 at 21:00

0 Answers0