我正在使用OpenGL ES 2.0绘制文本,但结果看起来不太好(尤其是“t”的底部):
(它看起来有点像素化和模糊)
我不确切地知道为什么因为我以高分辨率(200px或400px)加载字体。
我在图像中绘制每个字符,组成纹理图集。所以所有的字符都可以随时显示而无需加载。我使用openGL(MVP矩阵)缩放纹理,但不管怎样它都不应该看起来那么差。
这是我用以下方法检索的char“t”的UIImage,在用openGL处理之前:
(没有OpenGL,你可以看到它看起来更好)
这是我用来从char创建图像的代码:
- (id)initWithString:(NSString *)string dimensions:(CGSize)dimensions alignment:(NSTextAlignment)alignment fontName:(NSString *)name fontSize:(CGFloat)size {
NSUInteger width, height, i;
CGContextRef context;
void *data;
CGColorSpaceRef colorSpace;
UIFont *font;
font = [UIFont fontWithName:name size:size];
width = (NSUInteger)dimensions.width;
height = (NSUInteger)dimensions.height;
while(dimensions.width > kMaxTextureSize && size > 0) {
font = [UIFont fontWithName:name size: --size];
dimensions = [string sizeWithAttributes:@{NSFontAttributeName: font}];
width = (GLuint)NextPowerOfTwo((int)dimensions.width);
height = (GLuint)NextPowerOfTwo((int)dimensions.height);
}
colorSpace = CGColorSpaceCreateDeviceRGB();
data = calloc(1, width * height * 4);
context = CGBitmapContextCreate(data,
width, height,
8,
4 * width,
colorSpace,
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetRGBFillColor(context, 0.f, 0.f, 0.f, 1);
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
UIGraphicsPushContext(context);
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
UIColor *whiteColor = [UIColor whiteColor];
NSDictionary *attrsDictionary = @{
NSFontAttributeName: font,
NSBaselineOffsetAttributeName: @1.0F,
NSParagraphStyleAttributeName: style,
NSForegroundColorAttributeName: whiteColor
};
CGRect destRect = CGRectMake(0, 0, dimensions.width, dimensions.height);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetShouldAntialias(context, YES);
CGContextSetShouldSmoothFonts(context, YES);
[string drawInRect:destRect
withAttributes:attrsDictionary];
CGImageRef cgImageFinal = CGBitmapContextCreateImage(context);
UIGraphicsPopContext();
self = [self initWithData:data pixelFormat:GL_RGBA
pixelsWide:width pixelsHigh:height];
CGContextRelease(context);
free(data);
return self;
}
这是从中检索openGL纹理的代码:
- (id)initWithData:(const void *)data
pixelFormat:(GLenum)pixelFormat
pixelsWide:(NSUInteger)width
pixelsHigh:(NSUInteger)height {
if ((self = [super init])) {
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &_texture);
glBindTexture(GL_TEXTURE_2D, _texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
switch (pixelFormat) {
case GL_RGBA:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
break;
case GL_RGB:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
break;
case GL_ALPHA:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@""];
}
self.width = width;
self.height = height;
}
return self;
}
非常感谢任何帮助!