caleb.breckon
caleb.breckon

Reputation: 1364

OpenGL GL_POLYGON not printing correctly

I am drawing a basic polygon with 5 sides that looks like a C shape almost.

(50,60) _____(70,60)
       |    /
       |   /
       |  /(60,40)
       |  \
       |   \
(50,20)|____\(70,20)

Pretty straightforward.

When I use

wcPt2D verts[5] = {{50,20}, {70,20}, {**60**,40}, {70,60}, {50,60}};

enter image description here

I get this. Expected. But when I change that center piece to

wcPt2D verts[5] = {{50,20}, {70,20}, {**51**,40}, {70,60}, {50,60}};

I get

enter image description here

Which is clearly not 1 unit from the left wall/9 units to the left of image 1, and it clearly drops the y value by approximately 10 units. Why is this?

void polygon (wcPt2D *verts)
{
GLint k;

glBegin(GL_POLYGON);
    for (k = 0; k < 5; k++)
    {
        glVertex2f(verts [k].x,verts [k].y);
    }
glEnd();
}

Upvotes: 2

Views: 774

Answers (1)

user149341
user149341

Reputation:

The behavior of GL_POLYGON is only defined for convex polygons. Your polygon is concave, so the behavior of this function is undefined (and, as you've observed, it doesn't always work).

You will need to break your polygon up into one or more convex polygons for this to work correctly. In your case, one easy way of doing this would be to "cut" horizontally across the polygon:

(50,60) _____(70,60)
       |    /
       |   /
(50,40)|__/(60,40)
       |  \
       |   \
(50,20)|____\(70,20)

Upvotes: 5

Related Questions