简体   繁体   English

openGL怪异的bug?

[英]openGL weird bug?

I have the following code: 我有以下代码:

glNormal3f(0, 0, 1);
glColor3f(1, 0, 0);
glBegin(GL_POINTS);
    glVertex3f(-45, 75, -5);
    glVertex3f(-45, 90, -5);
    glVertex3f(-30, 90, -5);
    glVertex3f(-30, 80, -5);
    glVertex3f(-35, 80, -5);
    glVertex3f(-35, 75, -5);
    glVertex3f(-45, 75, -5);

glEnd();
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);
    glVertex3f(-45, 75, -5);
    glVertex3f(-45, 90, -5);
    glVertex3f(-30, 90, -5);
    glVertex3f(-30, 80, -5);
    glVertex3f(-35, 80, -5);
    glVertex3f(-35, 75, -5);
    glVertex3f(-45, 75, -5);

glEnd();

Notice how the code between glBegin and glEnd in each instance is identical. 请注意,每个实例中glBegin和glEnd之间的代码是相同的。

But the vertices of the GL_POLYGON (yellow) don't match up with the GL_POINTS (red). 但是GL_POLYGON(黄色)的顶点与GL_POINTS(红色)不匹配。 Here is a screenshot: 这是屏幕截图:

openGL错误

The more I use openGL the more I'm hating it. 我使用openGL越多,就越讨厌它。 But I guess it's probably something I'm doing wrong... What is up? 但是我想这可能是我做错了...怎么了?

That's because your polygon is not convex - the top right corner is concave. 那是因为您的多边形不是凸的-右上角是凹的。 GL_POLYGON only works for convex polygons. GL_POLYGON仅适用于凸多边形。

Try using GL_TRIANGLE_FAN instead and start from the lower-left corner: Your polygon is star-shaped, so you can draw it with a single GL_TRIANGLE_FAN if you start from a point in its kernel (of your vertices, the lower-left one is the only one that satisfies this condition). 尝试改用GL_TRIANGLE_FAN并从左下角开始:您的多边形是星形的,因此,如果从其核中的某个点(顶点的左下角是顶点)开始,则可以用一个GL_TRIANGLE_FAN绘制多边形。仅满足此条件的一个)。

If you expect more complicated polygons, you need to break them up into convex bits (triangles would be best) to render them. 如果期望更复杂的多边形,则需要将其分解为凸位(最好是三角形)以进行渲染。

The specification says for GL_POLYGON: 该规范对GL_POLYGON表示:

Only convex polygons are guaranteed to be drawn correctly by the GL. GL保证只能正确绘制凸多边形。 If a specied polygon is nonconvex when projected onto the window, then the rendered polygon need only lie within the convex hull of the projected vertices dening its boundary. 如果指定的多边形在投影到窗口时是非凸的,则渲染的多边形只需要位于投影顶点的凸包中即可界定其边界。

Since you are defining a concave polygon, this is a valid behaviour. 由于定义的是凹面多边形,因此这是有效的行为。 Try using a triangle strip instead of a polygon. 尝试使用三角带代替多边形。 It would be much faster because a polygon needs to be triangulated by the GL. 因为多边形需要由GL三角剖分,所以速度会更快。

BTW: I haven't used GL_POLYGON so far. 顺便说一句:到目前为止,我还没有使用过GL_POLYGON。 But I think you do not need to specify the last vertex (which equals the first one). 但我认为您无需指定最后一个顶点(等于第一个顶点)。 As far as I know, it will be connected automatically. 据我所知,它将自动连接。

The GLU Tesselator is a handy way to automatically convert concave (and other complex) polygons into proper opengl friendly ones. GLU Tesselator是将凹形(和其他复杂)多边形自动转换为适合opengl友好的多边形的便捷方法。 Check it out: 看看这个:

http://glprogramming.com/red/chapter11.html http://glprogramming.com/red/chapter11.html

http://www.songho.ca/opengl/gl_tessellation.html http://www.songho.ca/opengl/gl_tessellation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM