简体   繁体   中英

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}};

在此处输入图片说明

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

在此处输入图片说明

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();
}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM