Re: Polygon missing vertex



Andy Kaye wrote:
Anyone know how to draw a polygon in opengl ?

The following code should display a flat T shape, but openGL decides to
ignore the second vertex and renders a diagonal line from point one to point
three. Change the mode to line and it all works OK.

glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBegin(GL_POLYGON);
glVertex3f(0,4,0);
glVertex3f(4,4,0); <------- This vertex ignored
glVertex3f(4,2,0);
glVertex3f(6,2,0);
glVertex3f(6,4,0);
glVertex3f(10,4,0);
glVertex3f(10,6,0);
glVertex3f(0,6,0);
glVertex3f(0,4,0); <----- makes no difference closing the polygon
or not.
glEnd();


Tried this on 2 machines with differing graphics cards, same result.



It is look like everything is said. But just a hint: When you make OGL draw a Polygon, it handles it, like it is a Triangle-Fan. That's why you (at least sometimes) cannot draw convex Polygons.

If you want to draw a "T" with just one GL_POLYGON you perhaps could try to split the above line of T into to edges, and start with the middle vertex. Like:

*--*--*
| |
*-* *-*
| |
| |
*-*

In your case it would be:

glBegin(GL_POLYGON);
glVertex3f(5,6,0);
glVertex3f(0,6,0);
glVertex3f(0,4,0);
glVertex3f(4,4,0);
glVertex3f(4,2,0);
glVertex3f(6,2,0);
glVertex3f(6,4,0);
glVertex3f(10,4,0);
glVertex3f(10,6,0);
glEnd();

When using this with Filled Mode it makes no difference if you use GL_POLYGON or GL_TRIANGLE_FAN. Try it and you will see.
Just Remember OGL has to handle everything as Triangles (more or less, its at least something good to imagine), even if you wont see first.

(An arrow for this case is another classic example)
So as fungus said, OGL can't handle non-convex polygons, but sometimes you can get it handle this right. (In your case with one more Vertex)
.