2014-03-19 123 views
1

算法進入遞歸。它在多邊形上打印出藍色線條,但是在特定位置(X = 397,Y = 98)。顏色不會在像素處打印,然後進入相同像素及其鄰居的遞歸。Opengl中的邊界填充算法

#include<GL/gl.h> 
#include<GL/glu.h> 
#include<GL/glut.h> 

typedef struct pixel{ GLubyte red, green, blue; } pixel; 



void boundaryfill(float x,float y, pixel fill, pixel boundary) 
{ 
    pixel c; 
    glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &c); 
// printf("%d,%d,%d",(int)c.red,(int)c.green,(int)c.blue); 
    if ((c.red!=boundary.red)&&(c.red!=boundary.blue)&&(c.green!=boundary.green)&&(c.green!=fill.green)&&(c.blue!=fill.blue)&&(c.red!=fill.red)&&\ 
(x<=400))//&&(y<=100)&&(y>=50)&&(x>=200)) 
    { 

     glBegin(GL_POINTS); 
     glColor3ub(fill.red,fill.green,fill.blue); 
     glVertex2f(x,y); 
     glEnd(); 
     glFlush(); 
     glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &c); 
     printf("\nCOlOR %d,%d,%d",(int)c.red,(int)c.green,(int)c.blue); 
     printf("\nX=%f,Y=%f",x,y) 

      //                                
     boundaryfill(x+1,y,fill,boundary); 
     boundaryfill(x-1,y,fill,boundary); 
     boundaryfill(x,y+1,fill,boundary); 
     boundaryfill(x,y-1,fill,boundary); 

    } 

} 

void mydisplay() 
{ 
     glBegin(GL_POLYGON); 
     glColor3ub(10,10,10); 
     glVertex2f(200,50); 
     glVertex2f(200,100); 
     glVertex2f(400,100); 
     glVertex2f(400,50); 
     glEnd(); 
     glFlush(); 
     pixel fill,boundary; 
     fill.red=0; 
     fill.green=0; 
     fill.blue=255; 
     boundary.red=255; 
     boundary.green=255; 
     boundary.blue=255; 
     boundaryfill(300,75,fill,boundary); 
     glEnd(); 
     glFlush(); 
} 

void main(int argc,char **argv) 
{ 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
    glutInitWindowSize(400,400); 
    glutInitWindowPosition(540,320); 
    glutCreateWindow("my first attempt"); 
    glClearColor(1.0f,1.0f,1.0,0.0f); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glutDisplayFunc(mydisplay); 

    gluOrtho2D(0.0,400.0,0.0,400.0); 
    glutMainLoop(); 


} 
+0

即使你可以得到這個工作,它將是非常低效的。你並不是打算連續幾百萬次調用glReadPixels! – StilesCrisis

+0

我面臨的問題是它在延伸到屏幕之外的多邊形上繪製一條線。然後,當我添加x <= 400時,則該線在邊界處向上延伸400並進入白色背景(用COLOR打印註釋行)。邊界填充算法是這樣定義的。有什麼缺失嗎? –

回答

0

我懷疑問題是由於無限遞歸導致堆溢出。什麼阻止程序永遠只需調用boundaryfill

此外,你錯過 boundaryfill內的電話,但我懷疑這不是墜機的原因。