Click to See Complete Forum and Search --> : floodfill algorithm using openGL


tisa
November 9th, 2006, 07:13 AM
Im getting segmentation fault when im running the code given below:
Can anyone tell me why it happens and how can i solve it?


void floodfill(float pointx,float pointy,float f[3],float o[3])
{
float intensity,*pixels;
glReadPixels(pointx,pointy,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
if(*pixels==o[0] && *(pixels+1)==o[1] && *(pixels+2)==o[2])
{
glFlush();
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx,pointy);
glEnd();
glFlush();
floodfill(pointx+1,pointy,f,o);
floodfill(pointx,pointy+1,f,o);
floodfill(pointx-1,pointy,f,o);
floodfill(pointx,pointy-1,f,o);
}
}

MrViggy
November 9th, 2006, 12:51 PM
Is the value for 'pixels' valid?

Viggy

Andrea_Rossini
November 10th, 2006, 02:24 AM
Sure it's not valid, he didn't allocate memory for pixels.
since he's just reading a single pixel, pixels should not be declared as a pointer.

then:

void floodfill(float pointx,float pointy,float f[3],float o[3])
{
float intensity,pixels[3];
glReadPixels(pointx,pointy,1.0,1.0,GL_RGB,GL_FLOAT,pixels);
if(pixels[0]==o[0] && (pixels[1])==o[1] && (pixels[2])==o[2])
{
glFlush();
glBegin(GL_POINTS);
glColor3fv(f);
glVertex2i(pointx,pointy);
glEnd();
glFlush();
floodfill(pointx+1,pointy,f,o);
floodfill(pointx,pointy+1,f,o);
floodfill(pointx-1,pointy,f,o);
floodfill(pointx,pointy-1,f,o);
}
}


By the way you are not making good software rendering.
You would better call glReadPixels (If Needed) for the entire screen at the very beginning of the rendering process, work on a pixel buffer in your RAM, and then glDrawPixels the entire buffer.