0

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?

  • `SwapBuffers` will swap out the buffer you're drawing to with another one, IIRC with undefined contents. Even if you removed the swap, the overhead of reading pixels from the GPU is probably not worth it. Your best bet is to keep a CPU-side pixel array, flood fill using that, then upload it to a texture and render that. – Colonel Thirty Two May 04 '15 at 13:56
  • @ColonelThirtyTwo, thanks for your answer! I removed glutSwapBuffers() from the recursive part of floodfill() function and added it at the end of my first floodfill() call. It colors up the polygon faster than before, but this whole algorithm is still pretty slow for big sized polygons. – Mandeep Beniwal May 04 '15 at 14:45
  • @ColonelThirtyTwo, can you please tell me how to read pixels directly from CPU side and then how to modify the values to color the polygon? – Mandeep Beniwal May 04 '15 at 16:13
  • What I meant was create a `new int[WIDTH][HEIGHT]`, do your image operations on that array, then upload it to a texture and draw that. – Colonel Thirty Two May 04 '15 at 18:47
  • @ColonelThirtyTwo thanks for that easy -1 to my answer it is probably you. OP stated in his question that he has stack overflowing problem. I was just trying to help OP by showing that there is a way to implement iterative version of algorithm. – Berke Cagkan Toptas May 04 '15 at 22:29

0 Answers0