I'm creating a program in OpenGL using glut in which the user can draw lines or circles on the screen by selecting the appropriate option and dragging the left mouse button on the screen. Also, I want the user to be able to fill color in the polygon (which could be drawn by combination of several lines) when he clicks the right mouse button.
I tried implementing flood fill algorithm, by using glReadPixels() in order to determine the color at each and every pixel recursively, but it's too slow and results in stack overflow for larger size pixels.
(Initially I'm executing this instruction at the point where the right mouse button is clicked to store the RGB values in backColor array of 3 float-elements)
glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,backColor);
The code for flood fill that I implemented is -
void floodfill(int x, int y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glReadPixels(x-1,y,1,1,GL_RGB,GL_FLOAT,curColor);
if (checkColor(backColor,curColor))
{
floodfill(x-1,y);
}
glReadPixels(x,y-1,1,1,GL_RGB,GL_FLOAT,curColor);
if (checkColor(backColor,curColor))
{
floodfill(x,y-1);
}
glReadPixels(x+1,y,1,1,GL_RGB,GL_FLOAT,curColor);
if (checkColor(backColor,curColor))
{
floodfill(x+1,y);
}
glReadPixels(x,y+1,1,1,GL_RGB,GL_FLOAT,curColor);
if (checkColor(backColor,curColor))
{
floodfill(x,y+1);
}
}
int checkColor(float a[], float b[])
{
if ((a[0] == b[0]) && (a[1] == b[1]) && (a[2] == b[2]))
return 1;
return 0;
}
So, what's the best way to color a polygon (possibly concave polygon) on right mouse click? Also, what are the various ways to make the program more efficient? For eg. storing pixel value as GL_FLOAT and comparing 3 floating point numbers for every pixel may not be the most efficient way I feel, but I don't know what are the other possible ways in which we can compare pixel values.
But most importantly, what's the best algorithm for this?