gpt4 book ai didi

c++ - 凹面 GL_POLYGON 不着色?

转载 作者:太空宇宙 更新时间:2023-11-04 14:19:11 24 4
gpt4 key购买 nike

当我尝试在 OpenGL 中使用 GL_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);
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();
}

输出:

/image/BLMSJ.png

最佳答案

GL_POLYGON仅适用于convex polygons :

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

对于concave polygons您至少有两个选择:

  1. Triangulate the polygon并使用GL_TRIANGLES

  2. Use the stencil buffer trick .

关于c++ - 凹面 GL_POLYGON 不着色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27393304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com