Mandelbrot set not displaying properly

Posted by brainydexter on Game Development See other posts from Game Development or by brainydexter
Published on 2011-02-17T00:21:16Z Indexed on 2011/02/17 7:34 UTC
Read the original article Hit count: 285

Filed under:
|
|
|
|

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);
}    

© Game Development or respective owner

Related posts about opengl

Related posts about math