OpenGL GL_POLYGON concave polygon doesn't color in

12,363

GL_POLYGON is only for convex polygons:

GL_POLYGON: Draws a single, convex polygon. Vertices 1 through N define this polygon.

For concave polygons you have at least two options:

  1. Triangulate the polygon and use GL_TRIANGLES.

  2. Use the stencil buffer trick.

Share:
12,363
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a problem when I try to use Polygon in OpenGL. I don't know how to explain this but all my vertices are connected with the beginning one. The problem happens when I try to color the object. I want to draw a simple object.

    void TOP (float x1, float y1, float x2, float h,float n)
    {
      float r = x2-x1;
      if(n==1){glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);} // FOR FILL OR NO FILL OBJECT
      glBegin(GL_POLYGON);
         glVertex2f(x1,y1);
         glVertex2f(x2,y1);
         glVertex2f(x2,y1+h/7);
         y1=y1+h/7;
         glVertex2f(x2-r/5,y1+h/7);
         x2=x2-r/5; y1=y1+h/7;
         glVertex2f(x2,y1+2*h/7);
         y1=y1+2*h/7;
          glVertex2f(x2+r/5,y1+h/7);
          y1=y1+h/7; x2=x2+r/5;
          glVertex2f(x2,y1+2*h/7);
    cout<<y1<<endl;
          y1=y1+2*h/7;
           glVertex2f(x2-r/5,y1); x2=x2-r/5;
          glVertex2f(x2,y1-h/7); y1=y1-h/7;
          glVertex2f(x2-r/5,y1);x2=x2-r/5;
          glVertex2f(x2,y1+h/7); y1=y1+h/7;
          glVertex2f(x2-r/5,y1);x2=x2-r/5;
    
           glVertex2f(x2,y1-h/7); y1=y1-h/7;
           glVertex2f(x2-r/5,y1);x2=x2-r/5;
            glVertex2f(x2,y1+h/7); y1=y1+h/7;
             glVertex2f(x2-r/5,y1);x2=x2-r/5;
             glVertex2f(x2,y1-2*h/7);y1=y1-2*h/7;
             glVertex2f(x2+r/5,y1-h/7);y1=y1-h/7; x2=x2+r/5;
             glVertex2f(x2,y1-2*h/7); y1=y1-2*h/7;
             glVertex2f(x2-r/5,y1-h/7); y1=y1-h/7;x2=x2-r/5;
              glVertex2f(x2,y1-h/7);
    
      glEnd();
    }
    

    Output:

    http://i.stack.imgur.com/BLMSJ.png