iPhone OpenGLES 2.0文本纹理带有奇怪的边框(不是笔触)问题 [英] iPhone OpenGLES 2.0 Text Texture w/ strange border (not stroke) issue

查看:95
本文介绍了iPhone OpenGLES 2.0文本纹理带有奇怪的边框(不是笔触)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CoreGraphcis创建文本纹理.不幸的是,文本的呈现方式是这样的(文本颜色与背景相同,以显示奇怪的边框).

I'm using the CoreGraphcis to create a text texture. Unfortunately the text renders like this (Text color is same as background to demonstrate the strange border).

我尝试过使用笔触颜色和边框,我认为这是对OpenGLES 2.0而非CoreGraphics的影响.

I've tried playing with stroke colors and borders to I think it is do to OpenGLES 2.0 and not CoreGraphics.

// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
    glGenFramebuffers(1, &defaultFramebuffer);
    glGenRenderbuffers(1, &colorRenderbuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer);

    glActiveTexture(GL_TEXTURE0);
    glUniform1i(uniforms[UNIFORM_SAMPLER], 0);

    // Set up the texture state.
    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);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    texture = [[FW2Texture alloc] initWithString:@"Text"];
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture.width, texture.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture.imageData);

核心图形位:

-(id)initWithString:(NSString*)str {
if((self = [super init])) {
    UIFont *font = [UIFont systemFontOfSize:17];
    CGSize size = [str sizeWithFont:font];

    NSInteger i;

    width = size.width;
    if((width != 1) && (((int)width) & (((int)width) - 1))) {
        i = 1;
        while(i < width)
            i *= 2;
        width = i;
    }

    height = size.height;
    if((height != 1) && (((int)height) & (((int)height) - 1))) {
        i = 1;
        while(i < height)
            i *= 2;
        height = i;
    }

    NSInteger BitsPerComponent = 8;

    int bpp = BitsPerComponent / 2;
    int byteCount = width * height * bpp;
    uint8_t *data = (uint8_t*) calloc(byteCount, 1);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
    CGContextRef context = CGBitmapContextCreate(data,
                                                 width,
                                                 height,
                                                 BitsPerComponent,
                                                 bpp * width,
                                                 colorSpace,
                                                 bitmapInfo);
    CGColorSpaceRelease(colorSpace);

    CGContextSetGrayFillColor(context, 0.5f, 1.0f);
    CGContextTranslateCTM(context, 0.0f, height);
    CGContextScaleCTM(context, 1.0f, -1.0f);
    UIGraphicsPushContext(context);

    [str drawInRect:CGRectMake(0,
                               0,
                               size.width,
                               size.height)
           withFont:font
      lineBreakMode:UILineBreakModeWordWrap
          alignment:UITextAlignmentCenter];
    UIGraphicsPopContext();
    CGContextRelease(context);

    imageData = (uint8_t*)[[NSData dataWithBytesNoCopy:data length:byteCount freeWhenDone:YES] bytes];
}
return self;

}

推荐答案

您的glBlendFunc是什么?您正在从CoreGraphics获取预乘Alpha,例如而不是(r,g,b,0.5)的边界像素,而是(0.5 * r,0.5 * g,0.5 * b,0.5).这意味着您应该使用glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)启用混合功能以进行合成,以便获得srcColour +(1-srcColour的alpha)* dstColour.

What's your glBlendFunc? You're taking premultiplied alpha from CoreGraphics, so e.g. instead of a border pixel being (r, g, b, 0.5) it'll be (0.5*r, 0.5*g, 0.5*b, 0.5). That means you should composite with blending enabled, using glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) so that you get srcColour + (1 - alpha of srcColour)*dstColour.

这篇关于iPhone OpenGLES 2.0文本纹理带有奇怪的边框(不是笔触)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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