简体   繁体   English

在OpenGL中平滑混色

[英]Smooth color blending in OpenGL

I'm trying to achieve the following blending when the texture at one vertex merges with another: 当一个顶点的纹理与另一个顶点合并时,我正在尝试实现以下混合:

由FiftyThree在Paper iPad App中绘制的图像

Here's what I currently have: 这是我现在拥有的:

在此输入图像描述

I've enabled blending and am specifying the blending function as: 我已启用混合,并将混合功能指定为:

    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

I can see that the image drawn in the paper app is made up of a small circle that merges with the same texture before and after and has some blending effect on the color and the alpha. 我可以看到在纸质应用程序中绘制的图像由一个小圆圈组成,该圆圈在前后合并相同的纹理,并对颜色和alpha有一些混合效果。

How do I achieve the desired effect? 我如何达到预期的效果?

UPDATE: 更新:

What I think is happening is that the intersected region of the two textures is getting the alpha channel to be modified (either additive or some other custom function) while the texture is not being drawn in the intersected region. 我认为发生的是两个纹理的相交区域正在修改alpha通道(添加或其他自定义函数),而纹理没有在相交区域中绘制。 The rest of the region has the rest of the texture drawn. 该区域的其余部分绘制了纹理的其余部分。 Like so: 像这样:

在此输入图像描述

I'm not entirely sure of how to achieve this result, though. 不过,我不完全确定如何实现这个结果。

You shouldn't need blending for this (and it won't work the way you want). 你不应该为此混合(它不会按照你想要的方式工作)。

I think as long as you define your texture coordinate in the screen space, it should be seamless between two separate circles. 我认为只要您在屏幕空间中定义纹理坐标,它就应该在两个独立的圆圈之间无缝连接。

To do this, instead of using a texture coordinate passed through the vertex shader, just use the position of the fragment to sample the texture, plus or minus some scaling: 要做到这一点,不要使用通过顶点着色器传递的纹理坐标,只需使用片段的位置对纹理进行采样,加上或减去一些缩放:

float texcoord = gl_FragCoord / vec2(xresolution_in_pixels, yresolution_in_pixels);`
gl_FragColor = glTexture2D(papertexture, texcoord);

If you don't have access to GLSL, you could do something instead with the stencil buffer. 如果您无法访问GLSL,则可以使用模板缓冲区来执行某些操作。 Just draw all your circles into the stencil buffer, use the combined region as a stencil mask, and then draw a fullscreen quad of your texture. 只需将所有圆圈绘制到模板缓冲区中,将组合区域用作模板蒙版,然后绘制纹理的全屏四边形。 The color will be seamlessly deposited at the union of all the circles. 颜色将无缝地存放在所有圆圈的组合处。

You can achieve this effect with max blending for alpha. 您可以使用alpha的最大混合来实现此效果。 Or manual (blending off) with shader (OpenGL ES 2.0): 或者使用着色器(OpenGL ES 2.0)手动(混合):

#extension GL_EXT_shader_framebuffer_fetch : require
precision highp float;

uniform sampler2D texture;
uniform vec4      color;

varying vec2 texCoords;

void main() {
    float res_a =gl_LastFragData[0].a;
    float a = texture2D(texture, texCoords).a;
    res_a = max(a, res_a);
    gl_FragColor = vec4(color.rgb * res_a, res_a);
}

Result: 结果:

在此输入图像描述

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

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