user335593
user335593

Reputation:

flood fill algorithm

i want to implement the flood fill algorthm...so that when i get the x and y co-od of a point...it should start flooding from that point and fill till it finds a boundary but it is not filling the entire region...say a pentagon this is the code i am using

void setpixel(struct fill fillcolor,int x,int y)
{
     glColor3f(fillcolor.r,fillcolor.g,fillcolor.b);
     glBegin(GL_POINTS);
     glVertex2i(x,y);
     glEnd();
     glFlush();
}

struct fill getpixcol(int x,int y)
{
    struct fill gotpixel;
    glReadPixels(x,y,1,1,GL_RGB,GL_UNSIGNED_BYTE,pick_col);
    gotpixel.r =(float) pick_col[0]/255.0;
    gotpixel.g =(float) pick_col[1]/255.0;
    gotpixel.b =(float) pick_col[2]/255.0;
    return(gotpixel);
}

void floodFill(int x, int y,struct fill fillcolor,struct fill boundarycolor)
{
    struct fill tmp;
    //    if ((x < 0) || (x >= 500)) return;
    // if ((y < 0) || (y >= 500)) return;
    tmp=getpixcol(x,y);
    while (tmp.r!=boundarycolor.r && tmp.g!=boundarycolor.g && tmp.b!=boundarycolor.b)
    {
           setpixel(fillcolor,x,y);
           setpixel(fillcolor,x+1,y);
           setpixel(fillcolor,x,y+1);
           setpixel(fillcolor,x,y-1);
           setpixel(fillcolor,x-1,y);
           floodFill(x-1,y+1,fillcolor,boundarycolor);
           floodFill(x-1,y,fillcolor,boundarycolor);
           floodFill(x-1,y-1,fillcolor,boundarycolor);
           floodFill(x,y+1,fillcolor,boundarycolor);
           floodFill(x,y-1,fillcolor,boundarycolor);
           floodFill(x+1,y+1,fillcolor,boundarycolor);
           floodFill(x+1,y,fillcolor,boundarycolor);
           floodFill(x+1,y-1,fillcolor,boundarycolor);
     }
}

Upvotes: 1

Views: 4619

Answers (2)

Danvil
Danvil

Reputation: 22991

You should not issue a read pixel and write pixel for every single pixel. This is dead slow!

Instead read all pixels into host memory, operate the floodfill in host memory and write all pixels back to the framebuffer (or texture).

Upvotes: 1

voodoogiant
voodoogiant

Reputation: 2148

I can't say if this is the problem or not, but you're comparing floats, which is a floating-point no no. When you draw a color to OpenGL and read it back into the 0-1 range, it might not be the same number. You might want to try your comparisons using just integers.

Upvotes: 2

Related Questions