如何在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;
}
将在您绘制到屏幕的方块内绘制一个平滑的红色圆圈。您需要为您的方块提供顶点到位置
属性,并在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屋!