我如何获得无限小的数字(对于分形)

Spa*_*Kek 4 c++ opengl glsl fractals mandelbrot

我正在使用 OpenGL 使用 C++ 对 Mandelbrotset 进行编程,但遇到了一个问题:我发送到着色器并在着色器中计算的浮点数只能容纳一定数量的小数位。因此,如果我放大得太远,它就会变得像素化。

我想过创建自定义数组函数,但我无法真正弄清楚。除了使用数组还有其他方法吗?如果不是,我如何使用数组来计算东西,就好像它们是单个数字一样?(例如 arr[1,2] x arr[0,2] 应该给出与仅计算 1.2 x 0.2 相同的输出)

in vec4 gl_FragCoord;
 
out vec4 frag_color;
uniform float zoom;
uniform float x;
uniform float y;
#define maximum_iterations 1000

int mandelbrot_iterations()
{
    float real = ((gl_FragCoord.x / 1440.0f - 0.5f) * zoom + x) * 4.0f;
    float imag = ((gl_FragCoord.y / 1440.0f - 0.5f) * zoom + y) * 4.0f;
 
    int iterations = 0;
    float const_real = real;
    float const_imag = imag;
 
    
    while (iterations < maximum_iterations)
    {
        float tmp_real = real;
        real = (real * real - imag * imag) + const_real;
        imag = (2.0f * tmp_real * imag) + const_imag;
         
        float dist = real * real + imag * imag;
 
        if (dist > 4.0f)
            break;
 
        ++iterations;
    }
    return iterations;
}
Run Code Online (Sandbox Code Playgroud)

^我的fragmentshader中的Mandelbrot函数

Spe*_*tre 7

正如其他人建议的那样,使用double而不是float那样会给你带来更高的可能缩放。最重要的是使用分数转义,这将允许更高的细节和更少的迭代,因此更快的速度和更好的细节,请参见我的:

其中有我的 Mandelbrot 的浮点代码以及 32 位和 64 位浮点的 Win32 演示的链接。然而double版本着色器不适合回答,所以它们在这里(对于分形,第二遍重新着色着色器并不重要,但您可以从演示中提取它们):

// Fragment
#version 450 core
uniform dvec2 p0=vec2(0.0,0.0);     // mouse position <-1,+1>
uniform double zoom=1.000;          // zoom [-]
uniform int  n=100;                 // iterations [-]
uniform int  sh=7;                  // fixed point accuracy [bits]
uniform int  multipass=0;           // multi pass?
uniform int  inverted=0;            // inverted/reciprocal position?
in smooth vec2 p32;
out vec4 col;

const int n0=1;                     // forced iterations after escape to improve precision

vec3 spectral_color(float l)        // RGB <0,1> <- lambda l <400,700> [nm]
    {
    float t;  vec3 c=vec3(0.0,0.0,0.0);
         if ((l>=400.0)&&(l<410.0)) { t=(l-400.0)/(410.0-400.0); c.r=    +(0.33*t)-(0.20*t*t); }
    else if ((l>=410.0)&&(l<475.0)) { t=(l-410.0)/(475.0-410.0); c.r=0.14         -(0.13*t*t); }
    else if ((l>=545.0)&&(l<595.0)) { t=(l-545.0)/(595.0-545.0); c.r=    +(1.98*t)-(     t*t); }
    else if ((l>=595.0)&&(l<650.0)) { t=(l-595.0)/(650.0-595.0); c.r=0.98+(0.06*t)-(0.40*t*t); }
    else if ((l>=650.0)&&(l<700.0)) { t=(l-650.0)/(700.0-650.0); c.r=0.65-(0.84*t)+(0.20*t*t); }
         if ((l>=415.0)&&(l<475.0)) { t=(l-415.0)/(475.0-415.0); c.g=             +(0.80*t*t); }
    else if ((l>=475.0)&&(l<590.0)) { t=(l-475.0)/(590.0-475.0); c.g=0.8 +(0.76*t)-(0.80*t*t); }
    else if ((l>=585.0)&&(l<639.0)) { t=(l-585.0)/(639.0-585.0); c.g=0.84-(0.84*t)           ; }
         if ((l>=400.0)&&(l<475.0)) { t=(l-400.0)/(475.0-400.0); c.b=    +(2.20*t)-(1.50*t*t); }
    else if ((l>=475.0)&&(l<560.0)) { t=(l-475.0)/(560.0-475.0); c.b=0.7 -(     t)+(0.30*t*t); }
    return c;
    }

void main()
    {
    int i,j,N;
    dvec2 pp,p;
    double x,y,q,xx,yy,mu,cx,cy;
    p=dvec2(p32);

    pp=(p/zoom)-p0;         // y (-1.0, 1.0)
    pp.x-=0.5;              // x (-1.5, 0.5)
    if (inverted!=0)
        {
        cx=pp.x/((pp.x*pp.x)+(pp.y*pp.y));  // inverted
        cy=pp.y/((pp.x*pp.x)+(pp.y*pp.y));
        }
    else{
        cx=pp.x;                // normal
        cy=pp.y;
        }
    for (x=0.0,y=0.0,xx=0.0,yy=0.0,i=0;(i<n-n0)&&(xx+yy<4.0);i++)
        {
        q=xx-yy+cx;
        y=(2.0*x*y)+cy;
        x=q;
        xx=x*x;
        yy=y*y;     
        }
    for (j=0;j<n0;j++,i++)  // 2 more iterations to diminish fraction escape error
        {
        q=xx-yy+cx;
        y=(2.0*x*y)+cy;
        x=q;
        xx=x*x;
        yy=y*y;
        }
    mu=double(i)-double(log2(log(float(sqrt(xx+yy)))));
    mu*=double(1<<sh); i=int(mu);
    N=n<<sh;
    if (i>N) i=N;
    if (i<0) i=0;

    if (multipass!=0)
        {
        // i
        float r,g,b;
        r= i     &255; r/=255.0;
        g=(i>> 8)&255; g/=255.0;
        b=(i>>16)&255; b/=255.0;
        col=vec4(r,g,b,255);
        }
    else{
        // RGB
        float q=float(i)/float(N);
        q=pow(q,0.2);
        col=vec4(spectral_color(400.0+(300.0*q)),1.0);
        }
    }
Run Code Online (Sandbox Code Playgroud)

和:

// Vertex
#version 450 core
layout(location=0) in vec2 pos;         // glVertex2f <-1,+1>
out smooth vec2 p32;                    // texture end point <0,1>
void main()
    {
    p32=pos;
    gl_Position=vec4(pos,0.0,1.0);
    }
Run Code Online (Sandbox Code Playgroud)

这可能会导致zoom = 1e+14像素化开始发生:

像素化

在 GPU 上使用任意精度浮点会非常慢且有问题(正如其他人已经建议的那样)。但是,有更简单的解决方法可以提高 float 或 double 的精度。

例如,您可以将您的价值保存为更多doubles...

代替

double x;
Run Code Online (Sandbox Code Playgroud)

您可以使用:

double x0,x1,x2,....,xn;
Run Code Online (Sandbox Code Playgroud)

其中x = x0+x1+x2+...+xnwherex0保存较小的值,x1较大的值,...xn最大的值。您只需要基本+,-,*操作即可

  1. x += y

    x0+=y0; x1+=y1; ... xn+=yn;
    
    Run Code Online (Sandbox Code Playgroud)
  2. x -= y

    x0-=y0; x1-=y1; ... xn-=yn;
    
    Run Code Online (Sandbox Code Playgroud)
  3. x *= y

    x0*=(y0+y1+...yn);
    x1*=(y0+y1+...yn);
    ...
    xn*=(y0+y1+...yn);
    
    Run Code Online (Sandbox Code Playgroud)

在每次操作之后,您都会标准化每个变量的范围:

   if (fabs(x0)>1e-10){ x1+=x0; x0=0; }
   if (fabs(x1)>1e-9) { x2+=x1; x1=0; }
   ...
   if (fabs(x(n-1))>1e+9){ xn+=x(n-1); x(n-1)=0; }
Run Code Online (Sandbox Code Playgroud)

您需要选择范围,这样您就不会在不会使用的数字上浪费变量......

这个精度仍然有限​​,但精度损失要小得多......

然而,仍然有一个限制(不容易跨越)。现在,您正在根据片段坐标计算位置,zoom并且平移x,y将仅限于浮动,因为我们仍然没有 64 位双插值器。如果你想打破这个障碍,你需要以与其余计算相同的方式在CPU端或顶点上计算缩放位置(更多变量的总和,但这次是浮动的)并将结果作为变化传递到片段中