Reputation: 2082
I have the following Mandelbrot set code in C++ for use with opengl but the colours are not correct, these are the desired colors:
but I get this:
int vala = 600;
int valb = 600;
//render one frame
void Application::render(void)
{
// Clear the window and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//render grid
Image img( vala, valb);
double MinRe = -2.0;
double MaxRe = 1.0;
double MinIm = -1.2;
double MaxIm = MinIm+(MaxRe-MinRe)*vala/valb;
double Re_factor = (MaxRe-MinRe)/(vala-1);
double Im_factor = (MaxIm-MinIm)/(valb-1);
unsigned MaxIterations = 250;
for(unsigned int y = 0; y < img.height; y++){
double c_im = MaxIm - y*Im_factor;
for(unsigned x=0; x< img.width; ++x){
double c_re = MinRe + x*Re_factor;
double Z_re = c_re, Z_im = c_im;
bool isInside = true;
for(unsigned n=0; n<MaxIterations; ++n){
double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
if(Z_re2 + Z_im2 > 4)
{
isInside = false;
break;
}
Z_im = 2*Z_re*Z_im + c_im;
Z_re = Z_re2 - Z_im2 + c_re;
double z = sqrt(Z_re*Z_re + Z_im*Z_im);
int brightness = 256. *log(1.75 + n - log(log(z)));
img.setPixel(x,y, Color(sin(time)*brightness, sin(time/2)*brightness, brightness));
}
}
}
img.scale( this->window_width, this->window_height );
renderImage( &img );
//swap between front buffer and back buffer
SDL_GL_SwapWindow(this->window);
}
Anyone know why this happens and how to fix it?
Thank you very much.
Upvotes: 1
Views: 897
Reputation: 54602
You're setting the color inside the loop, which is different from any Mandelbrot code I have seen. It also sets the color for the same pixel many times, which is very inefficient. Another aspect of the logic problem is that you set isInside
, but then never use it.
The way this is typically done is that you set the color after the loop terminated. If isInside
is true, you set the color to black (or leave it untouched if the image was cleared to black). Otherwise you set the color. If you look at the origin of the code you use, they suggest to determine the color in the "outside" case based on the number of iterations, which would be n
in your case. The code you posted is using the value of Z_re/Z_im
to determine the color.
Edit: Ok, your formula uses both n
and Z_re/Z_im
. In any case, unless you know exactly what forumula were used for the reference image, and you use the same, you can't expect exactly the same coloring. If you fix what I pointed out above, you should at least get black for the inside.
Upvotes: 0