2009-11-09 1 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

전체 코드 세그먼트가 코드로 표시되도록 편집 할 수 있습니까? 모든 코드 행을 4 칸만큼 들여 쓰기하면됩니다. 또한, 어떤 언어를 사용하고 있습니까? – Pops

+0

C와 유사하지만 태그에는 C++이 표시됩니다. 그리고 그는 OpenGL을 사용하는 것 같습니다. –

+0

OGL에서 픽셀 ops 읽기/쓰기를 수행하는 것은 약간 무서운 반면 flood fill 자체는 소리가납니다 ... getPixel() 함수 또는 float 비교 결과가 의심 스럽습니다 ... – Aaron

답변

1

귀하의 setPixel() 루틴이 끊어집니다. 창을 흰색으로 지우므로 이전에 그린 모든 픽셀이 지워집니다. 이 줄을 제거하고 대신 Circle() 루틴에 넣어해야합니다 때문에 그 라인의

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

, getPixel() 항상 (일 제외) 흰색 픽셀을 읽어 오른쪽으로 재귀. 운이 좋다면 glReadPixels()은 프레임 버퍼 외부에 도달 할 때 임의의 색상을 반환합니다. 그렇지 않으면 스택 오버플로가 생성됩니다. 따라서 픽셀 위치도 Boundary_fill()에서 확인해야합니다.