Ferenc Dajka
Ferenc Dajka

Reputation: 1051

Julia set in GLSL

I'm trying to display the Julia set with Newton iteration, but I get a result shown below. What could be the problem? Here's my EDIT: FIXED, WORKING code:

#version 130

in vec3 vs_out_col;
in vec3 vs_out_pos;

out vec4 fs_out_col;

vec2 cplx_mul(vec2 z1, vec2 z2)
{
    return vec2(z1.x * z2.x - z1.y * z2.y, z1.y * z2.x + z1.x * z2.y); 
}

vec2 cplx_div(vec2 z1, vec2 z2)
{
    float denom = z2.x * z2.x + z2.y * z2.y;
    return vec2(
                (z1.x * z2.x + z1.y * z2.y) / denom,
                (z1.y * z2.x - z1.x * z2.y) / denom
            );
}

vec2 f(vec2 z)
{
    vec2 res = cplx_mul(cplx_mul(z, z), z);
    return vec2(res.x - 1.0f, res.y);
}

vec2 f_der(vec2 z)
{
    return 3 * cplx_mul(z, z);
}

void main()
{
    vec2 z = vs_out_pos.xy;

    for(int i = 0; i < 30; ++i){
        z = z - cplx_div(f(z), f_der(z));
    }

    vec2 root1 = vec2( 1.0f     ,  0.0f);
    vec2 root2 = vec2(-1.0f/2.0f,  1.0f/2.0f * sqrt(3.0f));
    vec2 root3 = vec2(-1.0f/2.0f, -1.0f/2.0f * sqrt(3.0f));

    if(abs(length(z - root1)) < 0.5f){
        fs_out_col = vec4 (1, 0, 0, 1);
    }
    else if(abs(length(z - root2)) < 0.01f){
        fs_out_col = vec4 (0, 1, 0, 1);
    }
    else if(abs(length(z - root3)) < 0.01f){
        fs_out_col = vec4 (0, 0, 1, 1);
    }
    else{
        fs_out_col = vec4 (0, 0, 0, 1);
    }
}

And here's the result:enter image description here And the Fixed result:enter image description here

Upvotes: 2

Views: 1063

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

You should compute the derivative correctly, of f(z)=z^3-1 the derivative is f'(z)=3*z^2, the factor 3 is missing.

And does the subtraction of the constant 1.0f really work that simply?

Upvotes: 2

Related Questions