gpt4 book ai didi

c++ - 使用 C++ 和 OpenGL 设置的 Mandelbrot 中的错误着色

转载 作者:太空宇宙 更新时间:2023-11-04 13:51:55 25 4
gpt4 key购买 nike

我在 C++ 中有以下 Mandelbrot 集代码用于 opengl,但颜色不正确,这些是所需的颜色: enter image description here

但我明白了: enter image description here

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

有人知道为什么会发生这种情况以及如何解决它吗?

非常感谢。

最佳答案

您在循环内设置颜色,这与我见过的任何 Mandelbrot 代码都不同。它还多次为同一个像素设置颜色,这是非常低效的。逻辑问题的另一个方面是您设置了 isInside,但从未使用它。

通常的做法是在循环终止后设置颜色。如果 isInside 为真,则将颜色设置为黑色(如果图像已清除为黑色,则保持不变)。否则你设置颜色。如果您查看 origin of the code you use ,他们建议根据迭代次数确定“外部”情况下的颜色,在您的情况下为 n 。您发布的代码使用 Z_re/Z_im 的值来确定颜色。

编辑:好的,您的公式同时使用了 nZ_re/Z_im。在任何情况下,除非您确切知道引用图像使用的是什么论坛,并且您使用相同的论坛,否则您不能期望完全相同的着色。如果你解决了我上面指出的问题,你至少应该把里面弄黑。

关于c++ - 使用 C++ 和 OpenGL 设置的 Mandelbrot 中的错误着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23165040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com