Boundary Fill function assistance needed [modified]
-
Hello, i am trying to implement boudary function but i am trying to run it .. it just crashes on me with no reason...what am i doing wrong? i think there is a problem with getPixel part... please help
void CtestopenglView::setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}void CtestopenglView::getPixel(GLint x ,GLint y, GLint &color)
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color);
}void CtestopenglView::boundFill(int x, int y, int fillColor, int borderColor)
{
int interiorColor;
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor!= fillColor))
{
setPixel(x,y);
boundFill(x + 1, y, fillColor, borderColor);
boundFill(x - 1, y, fillColor, borderColor);
boundFill(x, y + 1, fillColor, borderColor);
boundFill(x, y - 1, fillColor, borderColor);
}
glFlush();
}thank you
modified on Thursday, March 5, 2009 11:41 PM
-
Hello, i am trying to implement boudary function but i am trying to run it .. it just crashes on me with no reason...what am i doing wrong? i think there is a problem with getPixel part... please help
void CtestopenglView::setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}void CtestopenglView::getPixel(GLint x ,GLint y, GLint &color)
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color);
}void CtestopenglView::boundFill(int x, int y, int fillColor, int borderColor)
{
int interiorColor;
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor!= fillColor))
{
setPixel(x,y);
boundFill(x + 1, y, fillColor, borderColor);
boundFill(x - 1, y, fillColor, borderColor);
boundFill(x, y + 1, fillColor, borderColor);
boundFill(x, y - 1, fillColor, borderColor);
}
glFlush();
}thank you
modified on Thursday, March 5, 2009 11:41 PM
Are you sure boundFill actually terminates properly. If it doesn't, you'll wind up the stack and crash or eventually hit the edge of the image and crash.
"Republicans are the party that says government doesn't work and then they get elected and prove it." -- P.J. O'Rourke
-
Are you sure boundFill actually terminates properly. If it doesn't, you'll wind up the stack and crash or eventually hit the edge of the image and crash.
"Republicans are the party that says government doesn't work and then they get elected and prove it." -- P.J. O'Rourke
when i tried running it ... it did crash i know there is a way to fill in the polygon using something like poligonmode... but i am not sure how to use that correctly.. what i am doing is using GL_LINES to draw a polygon and then need to fill it in. how can i do that if not based on that function or what changes should i make to the function itself.
-
when i tried running it ... it did crash i know there is a way to fill in the polygon using something like poligonmode... but i am not sure how to use that correctly.. what i am doing is using GL_LINES to draw a polygon and then need to fill it in. how can i do that if not based on that function or what changes should i make to the function itself.
If you want a filled polygon, why not just let OpenGL draw it as filled, which is the default. Start with glBegin(GL_POLYGON)...glColor..(), bunch of glVertex..() calls...glEnd(). Just be advised that the polygon has to be convex, it it's not, you need to break it until into as many convex polygons as needed.
"Republicans are the party that says government doesn't work and then they get elected and prove it." -- P.J. O'Rourke
-
Hello, i am trying to implement boudary function but i am trying to run it .. it just crashes on me with no reason...what am i doing wrong? i think there is a problem with getPixel part... please help
void CtestopenglView::setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}void CtestopenglView::getPixel(GLint x ,GLint y, GLint &color)
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color);
}void CtestopenglView::boundFill(int x, int y, int fillColor, int borderColor)
{
int interiorColor;
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor!= fillColor))
{
setPixel(x,y);
boundFill(x + 1, y, fillColor, borderColor);
boundFill(x - 1, y, fillColor, borderColor);
boundFill(x, y + 1, fillColor, borderColor);
boundFill(x, y - 1, fillColor, borderColor);
}
glFlush();
}thank you
modified on Thursday, March 5, 2009 11:41 PM
Hey, There doesn't seem to be any exit condition if you reach the edge of the image so you will be accessing memory you aren't meant to. Either that or you are overflowing the stack and boom!
At university studying Software Engineering - if i say this line to girls i find they won't talk to me Dan
-
Hello, i am trying to implement boudary function but i am trying to run it .. it just crashes on me with no reason...what am i doing wrong? i think there is a problem with getPixel part... please help
void CtestopenglView::setPixel(GLint x, GLint y)
{
glBegin(GL_POINTS);
glVertex2d(x, y);
glEnd();
}void CtestopenglView::getPixel(GLint x ,GLint y, GLint &color)
{
glReadPixels(x, y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, &color);
}void CtestopenglView::boundFill(int x, int y, int fillColor, int borderColor)
{
int interiorColor;
getPixel (x, y, interiorColor);
if ((interiorColor != borderColor) && (interiorColor!= fillColor))
{
setPixel(x,y);
boundFill(x + 1, y, fillColor, borderColor);
boundFill(x - 1, y, fillColor, borderColor);
boundFill(x, y + 1, fillColor, borderColor);
boundFill(x, y - 1, fillColor, borderColor);
}
glFlush();
}thank you
modified on Thursday, March 5, 2009 11:41 PM
If you are using recursion as shown, even if you're drawing a bounded shape, the maximum stack depth will be roughly equal to the number of pixels in the area to be filled. For shapes of non-trivial size, that's not going to work. Much better would be to add points to be drawn into a queue and then have a loop which takes a point from the queue, fills it, and queues up any neighbors that need filling. If you use that approach, the number of items in the queue will be limited to approximately twice the larger dimension of the shape you're filling. Performance with that approach won't be great, but it will work. Things may be improved considerably if each queue item is a horizontal line rather than just a point. The code will be more complicated, but if written properly it will run much faster. One caveat is that there won't be any nicely-defined limit on the required queue size. On the other hand, one could design the algorithm so that when the queue is nearly full, queue items that would generate too many more queue items simply get re-queued instead of being processed. Some care would be necessary to prevent 'deadlock' conditions, but I would expect that in practice things shouldn't be too bad.