I am drawing a 5 corner star with GL_LINES. When I use the GL_POLYGON, it distorts the shape of star and connects the first and last vertex. Help please :( ?
Modern graphics cards can't draw 3D polygons; they draw only triangles. So when you use GL_POLYGON
, the driver must break it up into triangles. It's possible that this process breaks in your case. I suggest to create the triangles yourself. In your case, add a center point and use GL_TRIANGLE_FAN
to create a continuous mesh.
For the specific case of a five-point star, Aaron's suggestion of using a triangle fan with a base vertex at the center works great! Note that a star is a concave polygon; GL_POLYGON can only render a convex polygon.
A number of general-purpose solutions exists for concave polygons. One is simply tessellating the poly into triangles on the CPU; you can use the gluTessBeginPolygon function for this if you're using GLU. If you'd like to avoid linking in the GLU library, you can find its source on the web and extract the relevant pieces.
The red book also describes a cool trick to quickly draw concave polygons using the stencil buffer and a plain old triangle fan. The stencil technique is really sweet, but it might be fancier than what you really need.
Rendering with GL_POLYGON in OpenGL requires the points to be coplanar, and the resulting polygon to be convex. The star you described would not be convex, hence undefined behavior.