如何在 iPhone 上使用 OpenGL ES 绘制实心圆? [英] How do I draw a filled circle with OpenGL ES on iPhone?
问题描述
如何在 iPhone 上用 openGl 画一个实心圆?
How do I draw a filled circle with openGl on iPhone ?
我找到了许多解决方案,但没有一个有效.可能是因为有很多方法可以做到这一点.但是代码最短的方法是什么?
I've found many solutions but none of them work. Probably because there are many ways to do it. But what's the method with shortest code ?
推荐答案
要获得真正平滑的圆,您将需要一个自定义片段着色器.例如下面的顶点着色器:
For a truly smooth circle, you're going to want a custom fragment shader. For example, the following vertex shader:
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
varying vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}
和片段着色器:
varying highp vec2 textureCoordinate;
const highp vec2 center = vec2(0.5, 0.5);
const highp float radius = 0.5;
void main()
{
highp float distanceFromCenter = distance(center, textureCoordinate);
lowp float checkForPresenceWithinCircle = step(distanceFromCenter, radius);
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0) * checkForPresenceWithinCircle;
}
将在您绘制到屏幕的正方形内绘制一个平滑的红色圆圈.您需要将正方形的顶点提供给 position
属性,并将 X 和 Y 的坐标范围从 0.0 到 1.0 提供给 inputTextureCoordinate
属性,但这会绘制一个圆在视口分辨率允许的范围内锐利,而且速度非常快.
will draw a smooth red circle within a square that you draw to the screen. You'll need to supply vertices for your square to the position
attribute and coordinates that range from 0.0 to 1.0 in X and Y to the inputTextureCoordinate
attribute, but this will draw a circle that's as sharp as your viewport's resolution allows and do so very quickly.
这篇关于如何在 iPhone 上使用 OpenGL ES 绘制实心圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!