2009-11-09 97 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()中的像素位置。