1
\$\begingroup\$

I am trying to render mandelbrot set using glsl. I'm not sure why its not rendering the correct shape. Does the mandelbrot calculation require values to be within a range for the (x,y) [ or (real, imag) ] ? Here is a screenshot:

enter image description here


I render a quad as follows:

float w2 = 6;
float h2 = 5;
glBegin(GL_QUADS);

glVertex3f(-w2, h2, 0.0);
glVertex3f(-w2, -h2, 0.0);

glVertex3f(w2, -h2, 0.0);
glVertex3f(w2, h2, 0.0);

glEnd();

My vertex shader:

varying vec3 Position;

void main(void)
{
    Position = gl_Vertex.xyz;

    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

My fragment shader (where all the meat is):

uniform float MAXITERATIONS;

varying vec3 Position;

void main (void)  
{     
   float zoom = 1.0;
   float centerX = 0.0;
   float centerY = 0.0;

   float real = Position.x * zoom + centerX;
   float imag = Position.y * zoom + centerY;

   float r2 = 0.0;
   float iter;

   for(iter = 0.0; iter < MAXITERATIONS && r2 < 4.0; ++iter)
   {
    float tempreal = real;

    real = (tempreal * tempreal) + (imag * imag);
    imag = 2.0 * real * imag;

    r2 = (real * real) + (imag * imag);
   }

   vec3 color;

   if(r2 < 4.0)
    color = vec3(1.0);
   else
    color = vec3( iter / MAXITERATIONS );

gl_FragColor = vec4(color, 1.0);
}    
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It was my fragment shader. Here is the correct version:

uniform float MAXITERATIONS;

varying vec3 Position;

void main (void)  
{     
   float zoom = 1.0;
   float centerX = 0.0;
   float centerY = 0.0;

   float real = Position.x * zoom + centerX;
   float imag = Position.y * zoom + centerY;
   float Creal = real;
   float Cimag = imag;

   float r2 = 0.0;
   float iter;

   for(iter = 0.0; iter < MAXITERATIONS && r2 < 4.0; ++iter)
   {
    float tempreal = real;

    real = (tempreal * tempreal) - (imag * imag) + Creal;
    imag = (2.0 * tempreal * imag) + Cimag;

    r2 = (real * real) + (imag * imag);
   }

   vec3 color;

   if(r2 < 4.0)
    color = vec3(1.0);
    else
    color = vec3( iter / MAXITERATIONS );

    gl_FragColor = vec4(color, 1.0);
}  
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The best answers sometimes are the ones you come up with on your own, preceded by slapping your forehead with your hand :) \$\endgroup\$ Commented Feb 17, 2011 at 16:06
  • \$\begingroup\$ @espais: ha ha. I couldn't agree more. \$\endgroup\$ Commented Feb 17, 2011 at 16:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.