当我尝试在OpenGL中使用Polygon时遇到问题.我不知道如何解释这个,但我的所有顶点都与开头的顶点相关联.当我尝试着色对象时会发生问题.我想绘制一个简单的对象.
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+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仅适用于
convex polygons.
对于concave polygons,您至少有两个选择:
> Triangulate the polygon并使用GL_TRIANGLES.
> Use the stencil buffer trick.