Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Filling a Polygon

Posted on 2003-11-17
3
Medium Priority
?
6,942 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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 …
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

722 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