如何在 OpenGL ES 2.0 Android 中绘制基本圆 [英] How to draw basic circle in OpenGL ES 2.0 Android

查看:32
本文介绍了如何在 OpenGL ES 2.0 Android 中绘制基本圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OpenGL ES 2 的新手,我已经阅读了很多关于如何在 Android 上的 OpenGL ES 2 中画一个圆圈.基于 绘制形状这个代码在gamedev.net上找到,我可以画三角形和正方形,但是我还是不知道怎么画圆.我现在有三种画圆的方法:

I'm new in OpenGL ES 2, and I have read many topics about how to draw a circle in OpenGL ES 2 on Android. Based on Drawing Shapes and this code found on gamedev.net, I can draw triangles and quares, but I still don't know how to draw a circle. I now have three ways to draw a circle:

  1. 在圆中生成顶点并使用 glDrawArray( GL_LINES, ... ).根据您生成的顶点数量,这将产生漂亮而清晰的结果.
  2. 使用预先生成的圆形纹理(具有 Alpha 透明度)并将其映射到四边形上.这将产生非常平滑的图形并允许一个厚"的圆圈,但它不会那么灵活:即使使用 mipmapping,您也希望您的纹理与渲染四边形的大小大致相同.
  3. 使用片段着色器.

但是我该如何实现它们呢?

But how do I implement them?

推荐答案

我绝对不推荐通过几何体渲染圆.它有两个主要缺点:

I definitely do not recommend rendering a circle through geometry. It has two major disadvantages:

  1. 速度很慢.如果你想获得可接受的精度,你需要很多顶点,并且这些顶点中的任何一个都需要在着色器中进行处理.对于一个真正的圆,您需要的顶点数与圆的像素数一样多.
  2. 它不是很灵活.拥有不同的圈子、样式和颜色很难掌握.

还有另一种方法,我个人在每个图形 API 中都使用它.至少渲染一个三角形或正方形/四边形,并使用片段着色器仅使不期望的(基于方程)像素可见.这很容易理解.它灵活且快速.它需要混合,但这并不难实现.

There is another method, which I personally use in every graphics API. Rendering at least a triangle or a sqare/quad and use the fragment-shader to only make the disired (based on a equation) pixel visible. It is very easy to understand. It is flexible and fast. It needs blending, but this is not really hard to get to work.

步骤:

使用数据初始化缓冲区.您需要一个顶点缓冲区,如果您使用方形几何图形,则需要一个索引缓冲区,以及纹理坐标的纹理坐标缓冲区.对于正方形,我建议使用 -1.0 作为最低纹理坐标,1.0 作为最高纹理坐标,因为这样你就可以使用单位圆方程了.

Initialize your buffers with data. You need a vertex-buffer for the vertices, an index-buffer for the indices if you're a using a square geometry, and a textureCoord-buffer for your texture coordinates. For a square I recommend using -1.0 as the lowest and 1.0 as the highest texture coordinate, because then you are able to use the unit circle equation.

在您的片段着色器中,使用如下内容:

In your fragment-shader, use something like this:

if ((textureCoord.x * textureCoord.x) + (textureCoord.y * textureCoord.y) <= 1.0)
{
    // Render colored and desired transparency
}
else
{
    // Render with 0.0 in alpha channel
}

虽然 (textureCoord.x * textureCoord.x) + (textureCoord.y * textureCoord.y) <= 1.0 是不等式,因为您需要一个圆,所以您必须渲染该范围内的每个像素,而不仅仅是边界.您可以更改它,以便它为您提供所需的输出.

While (textureCoord.x * textureCoord.x) + (textureCoord.y * textureCoord.y) <= 1.0 is the inequality, because you need a circle, you have to render every pixel within that range, not just the border. You can change this so that it gives you the desired output.

就是这样.实现起来不是很复杂,所以我在这里不提供任何基本的渲染代码.您需要的一切都发生在片段着色器中.

And that is it. Not very complex to implement, so I don't offer any basic rendering code here. All you need happens within the fragment-shader.

这篇关于如何在 OpenGL ES 2.0 Android 中绘制基本圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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