I wrote the following piece of code for flood-fill as part of a paint program I'm developing:
void setPixel(int x, int y)
{
glColor4f(red, green, blue, alpha);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glFlush();
}
void floodFill(int x, int y)
{
unsigned char pick_col[3];
float R, G, B;
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pick_col);
R = (float) pick_col[0]/255.0;
G = (float) pick_col[1]/255.0;
B = (float) pick_col[2]/255.0;
//std::cout<<R<<" "<<G<<" "<<B<<"\n";
if(R!=0.0 || G!=0.0 || B!=0.0) //true for any non-black colour (BTW, my canvas is black)
return;
setPixel(x,y);
floodFill(x+1,y);
floodFill(x,y+1);
floodFill(x,y-1);
floodFill(x-1,y);
}
Unfortunately, this code is not working. For example, I first drew a rectangle and tried to fill the rectangle. What actually happened was that from the point where I clicked to fill, pixels to its right started getting filled in a line and eventually the pixels near the right-most edge started getting drawn. But that's all that happened. I don't understand why.
float
? Why not just check for zero and be done with it? – Captain Obvlious