OpenGL—ES 1.0 2d 圆角矩形
如何在OpenGL中制作圆角矩形,或任何带圆角的多边形?
How to make rounded rectangle in OpenGL, or any polygon with rounded corners?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在OpenGL中制作圆角矩形,或任何带圆角的多边形?
How to make rounded rectangle in OpenGL, or any polygon with rounded corners?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
使用多边形
如果绝对需要使用多边形,例如,如果需要大量缩放或缩放具有舍入的对象,或者需要控制舍入量,则可以将矩形分解为多个子对象。
至少有三个矩形部分和四个角。计算角坐标很容易。只需从圆中找到一点并构建三角形,如上图所示。
它仍然具有锋利的边缘,但更多的点使角更圆。小物体比大物体需要更少的点。
简单的路线是使用 GL_TRIANGLE_FAN 进行拐角。然而,对于 OpenGL ES,明智的做法是最大程度地减少 OpenGL 调用量并仅使用更多顶点,因为可以将整个对象构建为 GL_TRIANGLE_STRIP。
这种方法可以用于任何形状。对于矩形,角的角度始终为 90 度,但对于其他形状,需要从边缘计算角度。
使用纹理
另一种方法称为9 切片缩放。矩形和纹理被分成 9 个切片。实际的圆角位于纹理的角落。想法是角不缩放,但保持其原始大小。这种方法是 UI 设计中广泛使用的模式,允许可变大小的 UI 元素(如按钮)。它的优点是一个矩形只需要这9个四边形即可渲染。但如果角落也需要缩放,尤其是纹理分辨率较低时,看起来会很糟糕。
Using polygons
If using polygons is absolutely required, for example if objects with rounding need to be scaled or zoomed a lot or if amount of rounding needs to be controlled, it is possible to break rectangle into several sub-objects.
There are at least three rectangular parts and four corners. Calculating corner coordinates is easy. Just find a point from circle and build triangles like in picture above.
It will still have sharp edges, but more points make corners more round. Small objects need less points than big objects.
Easy route is to use GL_TRIANGLE_FAN for corners. However with OpenGL ES it might be wise to minimize amount of OpenGL calls and just use more vertices as it is possible to build whole object as GL_TRIANGLE_STRIP.
This approached can be used with any shape. With a rectangle, angle of corner is always 90 degrees, but with other shapes angle needs to be calculated from the edges.
Using textures
Another approach is called 9-slice scaling. Rectangle and texture are broken into 9 slices. Actual rounding is in corner of a texture. Idea is that corners are not scaled, but maintain their original size. This approach is widely used pattern in UI-design allowing variable size UI-elements like buttons. Its advantage is that one rectangle needs only these 9 quads to render. But it will look bad if also corners need to be scaled and especially if texture is low resolution.
有点颠簸,但今天我遇到了同样的问题,这就是我输出的内容,它是用 Desktop GL 创建的,但应该很容易转换为 GLES,一切都是条带。它可能没有达到应有的优化程度,但如果有人想尝试一下,请成为我的客人;)
要绘制圆角矩形,只需在正交视图内调用类似的命令:
Bit of a bump but I was stuck on the same problem today, this is what I output, its created with Desktop GL, but should be very easy to convert to GLES, everything is a strip. It is probably not as optimized as it should be but if someone want to have a stab at it please be my guest ;)
To draw the rounded rectangle simply call something like inside an orthographic view:
我需要绘制类似的矩形,但透明 - 上面的代码绘制了一些三角形重叠。修复了这个问题,还删除了 malloc,只是为了简化解决方案。这是我的版本:
I needed to draw similar rectangle, but transparent - and code above draws some of triangles overlap. Fixed that, also removed malloc, just to simplify solution. Here is my version:
以下代码来自我自己的项目,我在代码中添加了一些注释来解释。
如果会画一个没有边框的渐变圆角矩形。
如果你想绘制边框,这里是代码。
The following code is coping from my own project, I have added some comments to explain in the code.
If will draw a gradient rounded rectangle without border.
If you want draw the border, here is the code.
我在修复某些开源软件中的崩溃时遇到了这个问题 - 非 GL 版本工作正常,但基本上目的是实现圆角矩形,但开发人员太懒了,决定强制崩溃:-(
虽然我认为@vime的答案是简洁和完整的,我见过很多类似的例子,但没有一个给我任何信心,而且我认为这些例子并不明显,所以这是我的记录......调用函数
实现 4 角(代码片段)……
和圆弧截面函数 DrawGLRoundedCorner()。请注意,这假设 glBegin() 已被调用并绘制弧的起点和终点 - 这就是为什么您不需要在边的末尾显式添加顶点。
通过使用不同的 glBegin 例如 GL_LINE_LOOP 我认为你最终会得到一个未填充的圆角矩形。对于较大的角半径,可能需要使用各种抗锯齿提示等以使其看起来更漂亮,但还有其他相关帖子。
希望对某人有帮助。
I came across this fixing a crash in some open-source software - the non-GL version worked fine but basically the intention was to implement a rounded rectangle but the developer was too lazy for that and decided to force a crash instead :-(
Although I think @vime's answer is succinct and complete, I have seen lots of similar examples, none of which gave me any confidence and that I thought were non-obvious, so here's mine for the record... the calling function
implements the 4-corners ( code snippet )...
... and the arc-section function DrawGLRoundedCorner(). Note that this assumes that glBegin() has already been called and plots both the start and the end of the arc - which is why you don't need to explicitly add the vertices at the end of the sides.
By using a different glBegin such as with GL_LINE_LOOP I think you would end up with a non-filled rounded rectangle. For larger corner radii there might be a need to use various anti-aliasing hints or the like to make it look prettier, but there are other posts regarding that.
Hope that helps someone.
您还可以制作三角形而不是矩形来使边缘倾斜。
You can also make triangles instead of rectangles to bevel the edges.