【问题标题】:Mandelbrot zooms but image shifts and gets shrunkMandelbrot 放大但图像移动并缩小
【发布时间】:2012-10-30 18:24:27
【问题描述】:

我一直在做 Mandelbrot 设置并尝试缩放,但缩放模式变得非常麻烦。当我缩放时,它会完美缩放,但图像尺寸会缩小到原来的一半。下次我再次缩放时,图片尺寸会增加并尝试跳过查看窗口。代码在 c++/opengl 中。在发布到这里之前,我试图让我的代码稍微干净一些。

#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

double dividecubesby  = 700;
double left = -2.0;
double right = 2.0;
double bottom = -2.0;
double top = 2.0;
int maxiteration = 128;
int zoomlevel = 3;
double baseSize = 4.0;
double SizeReal;
double SizeImage;
double xco=0.0;
double yco=0.0;

void whatcoordinate(int x,int y)
{
    int xp=x;
    int yp=y;
    double delta1 = (right-left);
    double tempx;   
    double tempy;
    double delta=0.0;   
    double l = dividecubesby/2;

    delta = delta1/dividecubesby;

    if((xp > l) && (yp < l))
    {
        tempx = xp*delta;
        tempy = yp*delta;

        xco = right - tempx;
        yco = top - tempy;

        if(xco <0)xco=xco*-1;
        if(yco <0)yco=yco*-1;
    }
    else if( (x < l) && (y < l))
    {
        tempx = xp*delta;
        tempy = yp*delta;

        xco = right - tempx;
        yco = top - tempy;

        if(xco >0)xco=xco*-1;
        if(yco <0) yco =yco*-1;
    }
    else if((x < l) && (y > l))
    {
        tempx = xp*delta;
        tempy = yp*delta;

        xco = right - tempx;
        yco = top - tempy;

        if(xco >0)xco=xco*-1;
        if(yco >0)yco=yco*-1;
    }
    else if((x > l) && (y > l))
    {
        tempx = xp*delta;
        tempy = yp*delta;

        xco = right - tempx;
        yco = right - tempy;

        if(xco <0)xco=xco*-1;
        if(yco >0)yco=yco*-1;
    }
}

void keyPressed(unsigned char key, int x, int y)
{
    switch(key)
    {
    case 'z':
        printf("z pressed  x= %d, y= %d \n",x,y);

        whatcoordinate(x,y);

        SizeReal = (pow(2.0, (-zoomlevel)))*baseSize;
        SizeImage = (pow(2.0, (-zoomlevel)))*baseSize;

        baseSize = right-left;

        left   =  xco- (SizeReal/2);
        right  =  xco + (SizeReal/2);
        bottom = yco - (SizeReal/2);
        top    = yco + (SizeReal/2);    
        dividecubesby = dividecubesby+500;      
        maxiteration  = maxiteration+500; 
        zoomlevel=zoomlevel+1;

        glutPostRedisplay();

        break;
    }
}

int mandtest(double Cr, double Ci)
{
    double Zr = 0.0;
    double Zi = 0.0;
    int times = 0;
    double temp;
    Zr = Zr+Cr;
    Zi = Zi+Ci;

    while ((((Zr*Zr)+(Zi*Zi))<=4) && (times < maxiteration))
    {
        temp = (Zr*Zr)-(Zi*Zi);
        Zi = 2*Zr*Zi;

        Zr = temp+Cr;
        Zi = Zi+Ci;                

        times = times+1;  

    }

    return times;
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f,1.0f,1.0f);
    double deltax = ((right - left)/(dividecubesby-1));//this means length/700 
    double deltay = ((top- bottom)/(dividecubesby-1));// this means length/700

    gluOrtho2D(left,right,bottom,top);
    glBegin(GL_POINTS);

    for(double x= left;x<=right;x += deltax )
    {
        for(double y= bottom; y<=top;y +=  deltay )
        {
            if((mandtest(x,y))==maxiteration)
            {
                glColor3f(1.0f,1.0f,1.0f); 
                glVertex2f(x,y);
            }
            else 
            {
                glColor3f((float)mandtest(x,y)/10,0.0f,(float)mandtest(x,y)/30);
                glVertex2f(x,y);
            }
        }
    }
    glEnd();

    glFlush();
}

void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
}

int main(int argc, char ** argv)
{   
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(dividecubesby,dividecubesby);
    glutCreateWindow("A Simple OpenGL Windows Application with GLUT");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyPressed);
    glutMainLoop();

    return 0;
}

执行时

1)

2)

3)

【问题讨论】:

  • 请提供更简洁的代码和最小的示例。可能缩放不依赖于 Mandelbrot 计算本身。
  • 我已经编辑了部分代码..我通过选择一个点并添加小距离“SizeReal = (pow(2.0, (-zoomlevel)))*baseSize; 来进行缩放在左上右下.. 但我不知道为什么图像只是试图逃离正常视图:(
  • Render your 'brot to a texture and use OpenGL to display that. 你所拥有的是非常效率低下。
  • 您似乎正在放大“Mandelbrot 函数坐标”,同时您正在更改投影矩阵。我会尝试忘记 Mandelbrot,并首先尝试缩放具有恒定颜色的填充矩形。
  • 感谢 Corijin 的回复。我按照你说的使用了不变的颜色,结果是一样的。整个图像像图像 2 那样缩小成一个正方形(按下拳头 z).. 并再次像 3 那样扩展(按下第二个 z),但这次覆盖了整个窗口。一旦我看到一些异常情况,我可能不得不仔细查看帖子......这同时让我大吃一惊

标签: c++ algorithm opengl mandelbrot


【解决方案1】:

我认为您不应该在每次重新渲染时使用 gluOrtho2D(left,right,bottom,top) 更改视图转换。只需给他们(初始)固定值。视图将保持不变,就像您在图 1) 中看到的一样。您应该只转换您传递给 Mandelbrot 函数的坐标。

正如@genpfault 提到的:考虑使用纹理。这样更有效率。

【讨论】:

  • 在固定 gluOrtho2D 的值时,图像变得越来越小,而窗口保持不变并且变慢图像只是变成一个点,并且对于 gluOrtho2D 的查看窗口的大小也应该左下角。我不知道为什么我总是这样想(我认为这是我的问题),这就是我在笔记中读到的。我仍然对自己的代码感到困惑 :( .. 我被这个卡住了 2 天 .. 虽然它可以完美缩放 .. 不过我肯定会研究纹理 :)
  • 那我可能不明白你到底想要什么,当你放大的时候。
【解决方案2】:

问题已通过放置解决

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

就在 gluOrtho2D(left,right,bottom,top);在显示功能中 这是为了初始化。这在opengl超级圣经书的第2章中有很好的解释。

1)

2)

3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2016-05-24
    • 1970-01-01
    • 2015-04-28
    • 2013-05-23
    • 2021-10-13
    • 1970-01-01
    相关资源
    最近更新 更多