Xenoprimate
Xenoprimate

Reputation: 7963

openGL weird bug?

I have the following code:

glNormal3f(0, 0, 1);
glColor3f(1, 0, 0);
glBegin(GL_POINTS);
    glVertex3f(-45, 75, -5);
    glVertex3f(-45, 90, -5);
    glVertex3f(-30, 90, -5);
    glVertex3f(-30, 80, -5);
    glVertex3f(-35, 80, -5);
    glVertex3f(-35, 75, -5);
    glVertex3f(-45, 75, -5);

glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);
    glVertex3f(-45, 75, -5);
    glVertex3f(-45, 90, -5);
    glVertex3f(-30, 90, -5);
    glVertex3f(-30, 80, -5);
    glVertex3f(-35, 80, -5);
    glVertex3f(-35, 75, -5);
    glVertex3f(-45, 75, -5);

glEnd();

Notice how the code between glBegin and glEnd in each instance is identical.

But the vertices of the GL_POLYGON (yellow) don't match up with the GL_POINTS (red). Here is a screenshot:

openGL bug

The more I use openGL the more I'm hating it. But I guess it's probably something I'm doing wrong... What is up?

Upvotes: 1

Views: 827

Answers (3)

sidewinderguy
sidewinderguy

Reputation: 2404

The GLU Tesselator is a handy way to automatically convert concave (and other complex) polygons into proper opengl friendly ones. Check it out:

http://glprogramming.com/red/chapter11.html

http://www.songho.ca/opengl/gl_tessellation.html

Upvotes: 1

Matthias
Matthias

Reputation: 7521

The specification says for GL_POLYGON:

Only convex polygons are guaranteed to be drawn correctly by the GL. If a specied polygon is nonconvex when projected onto the window, then the rendered polygon need only lie within the convex hull of the projected vertices dening its boundary.

Since you are defining a concave polygon, this is a valid behaviour. Try using a triangle strip instead of a polygon. It would be much faster because a polygon needs to be triangulated by the GL.

BTW: I haven't used GL_POLYGON so far. But I think you do not need to specify the last vertex (which equals the first one). As far as I know, it will be connected automatically.

Upvotes: 3

ltjax
ltjax

Reputation: 15997

That's because your polygon is not convex - the top right corner is concave. GL_POLYGON only works for convex polygons.

Try using GL_TRIANGLE_FAN instead and start from the lower-left corner: Your polygon is star-shaped, so you can draw it with a single GL_TRIANGLE_FAN if you start from a point in its kernel (of your vertices, the lower-left one is the only one that satisfies this condition).

If you expect more complicated polygons, you need to break them up into convex bits (triangles would be best) to render them.

Upvotes: 5

Related Questions