在CCRenderTexture中绘制抗锯齿圆

时间:2012-09-15 23:07:33

标签: ios cocos2d-iphone opengl-es-2.0 antialiasing

我是cocos2d / OpenGLES的新手,我遇到了一个无法找到解决方案的问题。基本上,我想在CCRenderTexture中绘制一个抗锯齿的圆,然后在多个精灵上使用该纹理。除了抗锯齿部分之外的所有东西都很简单,但是我被困住了,无法弄清楚下一步该去哪里。

我现在的代码是:

int textureSize = 64;
CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];
[rt beginWithClear:spriteColor.r g:spriteColor.g b:spriteColor.b a:0.0f];

ccDrawColor4F(spriteColor.r, spriteColor.g, spriteColor.b, spriteColor.a);
ccDrawCircle(CGPointMake(textureSize / 2.0f, textureSize / 2.0f), textureSize / 2.0f, 0.0f, 360, false);

[rt end];
然而,这导致了锯齿状的混乱,我无法弄清楚从哪里开始。我在网上看过使用点绘制光滑圆圈的例子,但这似乎在OpenGLES 2.0中不起作用。

性能不是什么问题,因为我一次性绘制纹理并反复重复使用纹理。

1 个答案:

答案 0 :(得分:9)

在Core Graphics中创建圆形纹理,并将其作为CGImage添加到纹理缓存中。 Core Graphics自动使用抗锯齿功能。圆圈看起来像这样。

Circle

示例代码:

//Setup Core Graphics
CGSize circleSize = CGSizeMake(100, 100);
CGPoint circlePosition = ccp(50, 50);
UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
CGContextRef context = UIGraphicsGetCurrentContext();

//Add the circle to the context and draw it.
CGContextAddArc(context, circlePosition.x, circlePosition.y , circleSize.width/2, 0,2*M_PI,1);
CGContextDrawPath(context,kCGPathStroke);

//Get an image so we can store it in the Texture Cache
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//Add the image to the texture cache
[[CCTextureCache sharedTextureCache] addCGImage:[img CGImage] forKey:@"circleKey"];

然后你可以使用

制作一个精灵
CCSprite *circle = [CCSprite spriteWithTexture:[[CCTextureCache sharedTextureCache] textureForKey:@"circleKey"]];