2009-11-09 108 views
0

林停留在這串代碼的......我不能讓像素,填補了一圈?? ...任何幫助邊界填寫問題

#include<iostream> 
#include<glut.h> 

struct Color{ 
    float red, green, blue; 
}; 

Color getPixel(int x, int y){   // gets the color of the pixel at (x,y) 
    Color c; 
    float color[4]; 
    glReadPixels(x,y,1,1,GL_RGBA, GL_FLOAT, color); 
    c.red = color[0]; 
    c.green = color[1];  
    c.blue = color[2]; 
    return c; 
} 

void setPixel(int x, int y, Color c){ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glPushAttrib(GL_ALL_ATTRIB_BITS); 
    glColor3f(c.red, c.green, c.blue); 
    glBegin(GL_POINTS); 
    glVertex2i(x,y); 
    glEnd(); 
    glPopAttrib(); 
    glFlush(); 
} 

void init() 
    { 
     glClearColor(1.0,1.0,1.0,0.0); 
     gluOrtho2D(0.0,300.0,0.0,300.0); 
    } 

void drawPixel(int x,int y) 
{ 
    glBegin(GL_POINTS); 
    glVertex2i(x,y); 
    glEnd(); 
    glFlush(); 
} 

void Boundary_fill(int x,int y,Color thisColor){ 
    Color boundary_color; 
    boundary_color.red=0.0; 
    boundary_color.green=1.0; 
    boundary_color.blue=0.0; 
    Color nextpixel=getPixel(x,y); 
    if((nextpixel.red!=boundary_color.red)&&(nextpixel.blue!=boundary_color.blue)&&(nextpixel.green!=boundary_color.green) && (nextpixel.red!=thisColor.red)&& (nextpixel.blue!=thisColor.blue)&& (nextpixel.green!=thisColor.green)){ 
     setPixel(x,y,thisColor); 
     Boundary_fill((x+1),y,thisColor); 
     Boundary_fill((x-1),y,thisColor); 
     Boundary_fill(x,(y+1),thisColor); 
     Boundary_fill(x,(y-1),thisColor); 
    } 

} 

void draw(int x1,int y1, int x, int y){ 
    drawPixel(x1+x,y1+y);//quadrant1 
    drawPixel(x1+x,y1-y);//quadrant2 
    drawPixel(x1-x,y1+y);//quadrant3 
    drawPixel(x1-x,y1-y);//quadrant4 
    drawPixel(x1+y,y1+x);//quadrant5 
    drawPixel(x1+y,y1-x);//quadrant6 
    drawPixel(x1-y,y1+x);//quadrant7 
    drawPixel(x1-y,y1-x);//quadrant8 
} 

void circle(int px,int py,int r){ 
    int a,b; 
    float p; 
    a=0; 
    b=r; 
    p=(5/4)-r; 

    while(a<=b){ 
     draw(px,py,a,b); 
     if(p<0){ 
      p=p+(2*a)+1; 
     } 
     else{ 
      b=b-1; 
      p=p+(2*a)+1-(2*b); 
     } 
     a=a+1; 
    } 
} 


void Circle(void) 
{ 
    Color thisColor; 
    thisColor.red=1.0; 
    thisColor.blue=0.0; 
    thisColor.green=0.0; 

    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(0.0,1.0,0.0); 
    glPointSize(2.0); 
    int x0 = 100; 
    int y0 = 150; 
    circle(x0,y0,50); 
    glColor3f(thisColor.red,thisColor.blue,thisColor.green); 
    Boundary_fill(x0,y0,thisColor); 
} 

void main(int argc, char**argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(400,400); 
    glutInitWindowPosition(1,1); 
    glutCreateWindow("Boundary fill in a circle:Taaseen And Abhinav"); 
    init(); 
    glutDisplayFunc(Circle); 
    glutMainLoop(); 
} 
+1

你能編輯這個,以便整個代碼段顯示爲代碼嗎?您只需要將所有代碼行縮進四個空格。另外,你使用什麼語言? – Pops 2009-11-09 18:36:06

+0

看起來像C,儘管標籤上寫着C++。他似乎在使用OpenGL。 – 2009-11-09 18:48:30

+0

雖然在OGL中進行讀/寫像素操作是有點可怕的,但是填充本身看起來很合理...我會懷疑getPixel()函數或結果上的浮點數比較... – Aaron 2009-11-09 18:53:53

回答

1

setPixel()常規被打破了。它將窗口清除爲白色,從而擦除所有先前繪製的像素。您應該刪除此行,並把它放在你的Circle()例程,而不是:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

因爲這條線的,getPixel()總會念白像素(除了一個),並改乘權。如果幸運的話glReadPixels()在到達幀緩衝區外部時會返回一些隨機的顏色。否則會產生堆棧溢出。因此,您應該檢查Boundary_fill()中的像素位置。