1

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?

6
  • your example image has lots of other things in it too... have you tried only with the above code? Commented Apr 15, 2011 at 11:23
  • No, but eventually I need those things in there too anyway... I've checked and I'm ending every glBegin... I don't see what it could be. I should add that I'm a GL noob, though. Commented Apr 15, 2011 at 11:25
  • It is funny how people always blame the tool. You didn't mention it might be a compiler bug :P Commented Apr 15, 2011 at 14:17
  • possible duplicate of OpenGL filled polygon color "bleeding" Commented Apr 15, 2011 at 15:23
  • Funny, the more I use OpenGL the more I LOVE it. But I understand your frustration, Motig. Just take a deep breath and hang in there! :-) Commented Apr 15, 2011 at 20:44

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.