Filling a Polygon

Posted on 2003-11-17
Last Modified: 2013-12-06
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...

    for (int i=0;i<TotalPoints;i++){

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?

Thanks in advance
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;


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.


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.

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can anybody center this unfinished Death Star for me? 6 153
Any good game free GIF sites? 2 129
Is this *free* Mac Adobe Photoshop link for real? 4 163
triangle challenge 4 158
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 …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question