OpenGL—ES 1.0 2d圆角矩形 [英] OpenGL—ES 1.0 2d rounded rectangle

查看:160
本文介绍了OpenGL—ES 1.0 2d圆角矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在OpenGL中或任何带有圆角的多边形中制作圆角矩形?

How to make rounded rectangle in OpenGL, or any polygon with rounded corners?

推荐答案

使用多边形

如果绝对需要使用多边形,例如,如果需要大量缩放或缩放带有舍入的对象,或者需要控制舍入的数量,则可以将矩形分成多个子对象.

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.

 float anglerad = PI * angle / 180.0f;
 float x = sinf(anglerad) * radius; 
 float y = cosf(anglerad) * radius;

它仍将具有尖锐的边缘,但是更多的点会使拐角变得更圆.小物体比大物体需要更少的点.

It will still have sharp edges, but more points make corners more round. Small objects need less points than big objects.

简单的方法是在拐角处使用 GL_TRIANGLE_FAN .但是,对于OpenGL ES,明智的做法是尽量减少OpenGL调用的数量,并仅使用更多的顶点,因为可以将整个对象构建为GL_TRIANGLE_STRIP.

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.

这种方法可以用于任何形状.对于矩形,拐角角度始终为90度,而对于其他形状,则需要从边缘计算角度.

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.

另一种方法称为

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.

这篇关于OpenGL—ES 1.0 2d圆角矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆