Solved

Filling a Polygon

Posted on 2003-11-17
3
6,936 Views
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...

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?

Thanks in advance
0
Comment
Question by:nexisvi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:nexisvi
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

by:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to get safe trainer for Dead Space 3 Electrical Engineering Interface 4 1,046
Python RTS must use TKinter? 5 480
Sudoku - eraseable white board 2 298
Spell-checking 1 101
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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