Opengl不规则形状颜色填充
我正在用 GL_LINES 绘制一个 5 角星。当我使用 GL_POLYGON 时,它会扭曲星形并连接第一个和最后一个顶点。请帮忙:(?
I am drawing a 5 corner star with GL_LINES. When I use the GL_POLYGON, it distorts the shape of star and connects the first and last vertex. Help please :( ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
现代显卡无法绘制 3D 多边形;他们只画三角形。因此,当您使用
GL_POLYGON
时,驱动程序必须将其分解为三角形。在您的情况下,此过程可能会中断。我建议自己创建三角形。根据您的情况,添加中心点并使用 GL_TRIANGLE_FAN 来创建连续网格。Modern graphics cards can't draw 3D polygons; they draw only triangles. So when you use
GL_POLYGON
, the driver must break it up into triangles. It's possible that this process breaks in your case. I suggest to create the triangles yourself. In your case, add a center point and useGL_TRIANGLE_FAN
to create a continuous mesh.对于五点星的具体情况,Aaron 建议使用中心顶点为三角形的扇形,效果非常好!请注意,星形是凹多边形; GL_POLYGON 只能渲染凸多边形。
对于凹多边形存在许多通用解决方案。一种是在 CPU 上简单地将多边形细分为三角形;如果您使用的是 GLU,则可以使用 gluTessBeginPolygon 函数来实现此目的。如果您想避免在 GLU 库中进行链接,您可以在网络上找到其源代码并提取相关部分。
红皮书还描述了一个很酷的技巧,可以使用模板缓冲区和普通的旧三角形扇形来快速绘制凹多边形。模板技术确实很不错,但它可能比您真正需要的更精美。
For the specific case of a five-point star, Aaron's suggestion of using a triangle fan with a base vertex at the center works great! Note that a star is a concave polygon; GL_POLYGON can only render a convex polygon.
A number of general-purpose solutions exists for concave polygons. One is simply tessellating the poly into triangles on the CPU; you can use the gluTessBeginPolygon function for this if you're using GLU. If you'd like to avoid linking in the GLU library, you can find its source on the web and extract the relevant pieces.
The red book also describes a cool trick to quickly draw concave polygons using the stencil buffer and a plain old triangle fan. The stencil technique is really sweet, but it might be fancier than what you really need.
在 OpenGL 中使用 GL_POLYGON 进行渲染要求点共面,并且生成的多边形是凸的。您描述的恒星不会是凸的,因此行为未定义。
Rendering with GL_POLYGON in OpenGL requires the points to be coplanar, and the resulting polygon to be convex. The star you described would not be convex, hence undefined behavior.