OpenGL GL_POLYGON 凹多边形不着色

2021-12-19 00:00:00 opengl c polygon drawing c++

当我尝试在 OpenGL 中使用多边形时遇到问题.我不知道如何解释这一点,但我所有的顶点都与开始的顶点相连.当我尝试为对象着色时会出现问题.我想画一个简单的对象.

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();
}

输出:

推荐答案

GL_POLYGON 仅适用于 凸多边形一个>:

GL_POLYGON:绘制单个凸多边形.顶点 1 到 N 定义了这个多边形.

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

对于凹多边形,您至少有两个选择:

For concave polygons you have at least two options:

  1. 对多边形进行三角剖分并使用GL_TRIANGLES.

使用模板缓冲技巧.

相关文章