简体   繁体   English

OpenGL ES 2.0-文本混合问题(iOS)

[英]OpenGL ES 2.0 - Blending issue with text (iOS)

I have grey shadows around texts when I blend them with OpenGL. 当我将它们与OpenGL混合时,文本周围会有灰色阴影。

在此处输入图片说明

Currently, this is my blending function : 目前,这是我的混合功能:

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

If I change it to : 如果我将其更改为:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

It works, and I obtain the same result with Gimp : 它有效,并且我用Gimp获得了相同的结果:

在此处输入图片说明

But, however with GL_ONE, the text can't be faded into the background anymore. 但是,无论如何使用GL_ONE,文本都无法淡入背景。 It's like it's either transparency 0 or 1. 就像透明度0或1。

Also with GL_ONE, a crossfade between 2 pictures will somehow make the result image super bright : 同样使用GL_ONE时,两张图片之间的淡入淡出将以某种方式使结果图像超亮:

在此处输入图片说明

Whereas with GL_SRC_ALPHA it looks normal : GL_SRC_ALPHA看起来很正常:

在此处输入图片说明

So both solutions have pros and cons. 因此,这两种解决方案都各有利弊。 I don't want the grey shadows, but I want to keep the crossfade effect.. Any suggestions would be very appreciated. 我不需要灰色阴影,但我想保持交叉淡入淡出效果。任何建议,我们将不胜感激。

This is my fragment shader : 这是我的片段着色器:

gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));

And here's how the textures (text and images) are loaded (premultiplied last): 这是纹理(文本和图像)的加载方式(最后预乘):

+ (GLuint)getGLTextureFromCGIImage:(CGImageRef)cgiImage {
    size_t width = CGImageGetWidth(cgiImage);
    size_t height = CGImageGetHeight(cgiImage);

    GLubyte *spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef spriteContext = CGBitmapContextCreate(spriteData,
                                                       width,
                                                       height,
                                                       bitsPerComponent,
                                                       bytesPerRow,
                                                       colorSpace,
                                                       kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), cgiImage);
    CGContextRelease(spriteContext);

    return [GLMediaUtils getGLTextureFromPixelsInFormat:GL_RGBA
                                                  width:(int)width
                                                 height:(int)height
                                                 pixels:spriteData];
}

+ (GLuint)getGLTextureFromPixelsInFormat:(GLenum)format
                                   width:(int)width
                                  height:(int)height
                                  pixels:(void *)pixels {

    glActiveTexture(GL_TEXTURE0);

    GLuint texture;
    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);


    GLenum type = GL_UNSIGNED_BYTE;
    if(format == GL_RGB) // RGB565
        type = GL_UNSIGNED_SHORT_5_6_5;

    glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, pixels);

    free(pixels);
    glFlush();

    return texture;
} 

You're almost there. 你快到了。 You need to use pre-multiplied alpha if you want to avoid ghosting from the texture (because GL blending is post-multiplied alpha normally, which causes color bleed across the channels). 如果要避免从纹理重影,则需要使用预乘Alpha(因为GL混合通常是后乘Alpha,这会导致通道中的颜色泛滥)。

Normally GL_ONE works because the correct alpha has been pre-baked into the texture RGB color channels prior to texture upload. 通常, GL_ONE之所以有效,是因为在上传纹理之前,已将正确的alpha预先烘焙到纹理RGB颜色通道中。 If you start adding custom fading on top of that in the shader then you end up with a desync between the alpha you use for blending in the shader and the alpha that was used for pre-multiplication. 如果您开始在着色器中添加自定义淡入淡出,则最终会导致在着色器中用于混合的Alpha与用于预乘的Alpha之间不同步。

... so you need to add an adjustment to your fragment shader based on the new alpha value to get back to a stable premultiplication value. ...因此您需要根据新的alpha值对片段着色器进行调整,以返回到稳定的预乘值。 Something like: 就像是:

vec4 texColor = texture2D(u_Texture, v_TexCoordinate);
// Scale the texture RGB by the vertex color
texColor.rgb *= v_color.rgb;
// Scale the texture RGBA by the vertex alpha to reinstate premultiplication
gl_FragColor = texColor * v_color.a;

The other approach is to store the font as only a luminance texture. 另一种方法是将字体仅存储为亮度纹理。 You get grey shadows because the color channels are black outside of the font and texture filtering is mixing white and black. 您会得到灰色阴影,因为颜色通道在字体之外为黑色,并且纹理过滤将白色和黑色混合在一起。 If you know the font color is white you don't need to store RGB values in a texture at all ... (or if you are lazy just keep the existing texture and flood fill the entire texture RGB channels with white). 如果您知道字体颜色是白色,则根本不需要将RGB值存储在纹理中……(或者,如果您很懒,只需保留现有纹理并用白色填充整个纹理RGB通道即可)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM