2012-10-30 38 views
2

我一直在做Mandelbrot設置和試圖放大,但放大patter變得非常麻煩。當我縮放它完美的縮放,但圖像的大小減少到原來的一半。下一次,我再次縮小圖片尺寸並嘗試跳過查看窗口。代碼在C++/opengl中。我試圖在發佈之前將我的代碼清理乾淨。Mandelbrot放大,但圖像轉變,並得到收縮

#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)enter image description here

2)enter image description here

3)enter image description here

+0

請提供更乾淨的代碼和一個小例子。可能縮放不取決於Mandelbrot計算itsels。 –

+0

我已經編輯了代碼的一部分..我通過選擇一個點並添加小距離來縮放「SizeReal =(pow(2.0,(-zoomlevel)))* baseSize;」在左上角的底部..但我不知道爲什麼圖像只是試圖擺脫普通視圖:( – solti

+1

[渲染你的brot到一個紋理,並使用OpenGL來顯示*,*。](http:// rosettacode.org/wiki/Mandelbrot_set#PPM_Interactive)你有什麼*效率非常低* – genpfault

回答

3

的問題是固定通過將

glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 

略高於gluOrtho2D(左,右,下,上);在顯示功能 這是爲了初始化。這在opengl超級聖經書的第2章中有很好的解釋。

1)enter image description here

2)enter image description here

3)enter image description here

1

我想你不應該改變使用gluOrtho2D您的視角變換(左,右,底部,頂部)。只要給他們(最初的)固定值。視圖將保持不變,如圖1)所示。您應該將您傳遞的座標轉換爲Mandelbrot函數。

而@genpfault提到:考慮使用紋理。這樣更有效率。

+0

固定gluOrtho2D的值時,圖像變得越來越小,而窗口保持不變,圖像變慢成爲一個點,並且應該爲右下角頂部留出gluOrtho2D查看窗口的大小。我不知道我爲什麼總是這樣想(我認爲那是我的問題),那就是我在筆記中讀到的東西。我仍然困惑於我自己的代碼:(..我堅持這個爲期2天..儘管它放大完美..我一定會看看紋理雖然:) – solti

+0

然後,我可能不明白你到底想要什麼,當你放大。 –