简体   繁体   English

OpenGL减法混合

[英]OpenGL subtractive blending

In a paint app I am developing, I want my user to be able to draw with a transparent brush, for example black paint over white background should result in grey colour. 在我正在开发的绘画应用程序中,我希望我的用户能够使用透明画笔绘制,例如在白色背景上的黑色涂料应该导致灰色。 When more paint is applied, the resulting colour will be closer to black. 当涂抹更多涂料时,产生的颜色将更接近黑色。

在此输入图像描述

However no matter how many times I draw over the place, the resulting colour never turnts black; 然而,无论我在这个地方画多少次,最终的颜色都不会变黑; in fact it stops changing after a few lines. 实际上它在几行后就停止了变化。 Photoshop says that the alpha of the blob drawn on the left in OpenGL is max 0.8, where I expect it to be 1. Photoshop说OpenGL中左边绘制的blob的alpha值最大为0.8,我希望它为1。

My app works by drawing series of stamps as in Apple's GLPaint sample to form a line. 我的应用程序通过绘制系列邮票,如Apple的GLPaint样本中形成一条线。 The stamps are blended with the following function: 邮票与以下功能混合:

glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA, GL_ONE);
glBlendEquation(GL_FUNC_ADD);

My fragment shader: 我的片段着色器:

uniform lowp sampler2D u_texture;
varying highp vec4 f_color;

void main(){
    gl_FragColor = texture2D(u_texture, gl_PointCoord).aaaa*f_color*vec4(f_color.aaa, 1);
}

How should I configure the blending in order to get full colour when drawing repeatedly? 我应该如何配置混合以便在重复绘制时获得全彩色?

Update 07/11/2013 更新07/11/2013

Perhaps I should also note that I first draw to a texture, and then draw the texture onscreen. 也许我还应该注意,我首先绘制一个纹理,然后在屏幕上绘制纹理。 The texture is generated using the following code: 使用以下代码生成纹理:

    glGenFramebuffers(1, &textureFramebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, textureFramebuffer);

    glGenTextures(1, &drawingTexture);
    glBindTexture(GL_TEXTURE_2D, drawingTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,  pixelWidth, pixelHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

Update 02/12/2013 I tried modifying Apple's GLPaint program to and it turned out that this behaviour is observable only on iOS7. 更新02/12/2013我尝试修改Apple的GLPaint程序,结果证明这种行为只能在iOS7上观察到。 As it can be seen on the screenshots bellow, the colours on iOS 7 are a bit pale and don't blend nicely. 正如在下面的屏幕截图中可以看到的那样,iOS 7上的颜色有点苍白,并且不能很好地融合。 The

GL_ONE, GL_ONE_MINUS_SRC_ALPHA

blend function does well on iOS6. 混合功能在iOS6上运行良好。 Can this behaviour be caused by iOS7's implementation of CALAyer or something else and how do I solve it? 这种行为可能是由iOS7的CALAyer或其他东西的实现引起的,我该如何解决?

iOS6的IOS 7

Update 10/07/2014 2014年10月7日更新

Apple recently updated their GLPaint sample for iOS7 and the issue is observable there, too. Apple最近为iOS7更新了他们的GLPaint样本,并且在那里也可以观察到这个问题。 I made a separate thread based on their code: Unmodified iOS7 Apple GLPaint example blending issue 我基于他们的代码创建了一个单独的线程: 未修改的iOS7 Apple GLPaint示例混合问题

Just because your brush does "darken" the image doesn't mean, that this was subtractive blending. 仅仅因为你的画笔“变暗”图像并不意味着,这是减法混合。 This is in face regular additive blending, where the black brush merely overdraws the picture. 这是面对常规的添加剂混合,其中黑色画笔仅仅是过度绘制图片。 You want a (GL_ONE, GL_ONE_MINUS_SRC_ALPHA) blending function (nonseparated). 您需要(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)混合功能(非分离)。 The brush is contained only within the alpha channel of the texture, there's no color channels in the texture, the brush color is determined by glColor or an equivalent uniform. 画笔仅包含在纹理的alpha通道内,纹理中没有颜色通道,画笔颜色由glColor或等效的均匀度决定。

Using the destination alpha value is not required in most cases. 在大多数情况下,不需要使用目标alpha值。

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

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