Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Filling a Polygon

Posted on 2003-11-17
Medium Priority
6,944 Views
I'm drawing an oddly shaped polygon and I'm having problems filling in the inside.  Basically, another program is sending me a 2d array of points (x and y values).  I'm taking these points, maping them to my own co-ordinate system, and storing them in a vertex. Once all the points have been put into a vetex, I close the polygon.  Here is an example...

glBegin(GL_POLYGON);
for (int i=0;i<TotalPoints;i++){
glVertex2f(ArrayOfPoints[i][0],ArrayOfPoints[i][1]);
}
glEnd();

Once all the points are plotted, I can connect them to form an enclosed area.  I know it forms an enclosed area, because if I change the GL_POLYGON to GL_LINE_STRIP, it will draw the polygon correctly.  Also, if I drew it as GL_POINTS I can see all the points plotted on the screen correctly.  If I set my polygon mode to GL_LINE, it will also draw it as a non-filled polygon corrected

ex:          glPolygonMode(GL_BACK, GL_LINE);
glPolygonMode(GL_FRONT, GL_LINE);

If I try to fill either the front or back, or both, I start getting weird shapes.  Now I know that normally you should be plotting the points in a logical order (ex counter clockwise), unfortunately the array that's being given to me is not sorted like that.  It's drawing the dots in a strange order.

Now I know I can go through the array, do a sort to arrange the points into a sorted order to draw them counter clockwise, etc.  I know I could also go through the array, try and find the edges by looking at the max and min x and y values, then going up line by line and drawing dots to fill in the polygon.  What I'm wondering is if there is a handy OpenGl tool/function that could detect the edges and fill in the object for me, or something similar.  If it can connect all the dots properly, I'm hoping there's an easy way to fill in the enclosed area no matter what the order is that you plotted those dots.

Anyone know an easy way to do this?

0
Question by:nexisvi
• 2

Author Comment

ID: 9771639
I've done some more research, and it seems like part of the problem is the fact that I'm trying to draw a non-convex (concave) polygon.  Apparently I ned to tesselate my polygon, and then maybe it will fill correctly.  I've got the following code to accomplish this, but so far nothing seems to be showing up on the screen.  I'm using the same vertices as I was before, but for this to work it seems like I need to pass glTessVertex two vertex at a time, instead of glVertex where I was only creating one at a time.  Here's how I'm trying to tesselate a polygon...

GLUtesselator* mytess = gluNewTess();
double vertex1[2],vertex2[2], vertex3[2],vertex4[2];

vertex1[0] = -9; vertex1[1] = 3;
vertex2[0] = -9; vertex2[1] = 5;
vertex3[0] = -4; vertex3[1] = 3;
vertex4[0] = -4; vertex4[1] = 5;

gluTessBeginPolygon(mytess,NULL);
gluTessBeginContour(mytess);
gluTessVertex(mytess,vertex1,vertex2);
gluTessVertex(mytess,vertex1,vertex4);
gluTessVertex(mytess,vertex3,vertex2);
gluTessVertex(mytess,vertex3,vertex4);
gluTessEndContour(mytess);
gluTessEndPolygon(mytess);

Note, this isn't the much more complicated polygon that I'm actually trying to draw above.  Just a simple concave polygon.  It should look like a sideways hourglass  !><!

Using these vertices will draw correctly if I'm doing it as a GL_LINE_LOOP.  The above code isn't showing anything on the screen, which leads me to believe that I'm not understanding the tesselation function properly, or missing some step.

0

Accepted Solution

nexisvi earned 0 total points
ID: 9780436
I finally figured it out.  To shade a non-simple polygon, you need to first tesselate the polygon (break it up into simple polygons).  I was having problems figuring out how to do it, but now I've got it.

The important thing to remember is that you need to define your callback functions.  At the very least, you should have them for GLU_TESS_VERTEX, GLU_TESS_BEGIN, and GLU_TESS_END.  Store all your vertices in a data structure, then pass it to gluTessVertex, which goes to your GLU_TESS_VERTEX callback function.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
###### Suggested Courses
Course of the Month8 days, 4 hours left to enroll