Solved

Filling a Polygon

Posted on 2003-11-17
3
6,928 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now